Skip to content

Commit

Permalink
Fix: False negative with TypeScript rules that don't have type parame…
Browse files Browse the repository at this point in the history
…ters (fixes #202) (#209)
  • Loading branch information
aladdin-add authored Oct 13, 2021
1 parent 0ebc427 commit f8a642a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
2 changes: 0 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ function getRuleExportsESM (ast) {
return { create: node, meta: null, isNewStyle: false };
} else if (
node.type === 'CallExpression' &&
node.typeParameters &&
node.typeParameters.params.length === 2 && // Expecting: <Options, MessageIds>
node.arguments.length === 1 &&
node.arguments[0].type === 'ObjectExpression' &&
// Check various TypeScript rule helper formats.
Expand Down
33 changes: 22 additions & 11 deletions tests/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ describe('utils', () => {
'export default foo.bar(123);',
'export default foo.bar()(123);',

// Correct TypeScript helper structure but missing parameterized types:
'export default createESLintRule({ create() {}, meta: {} });',
'export default util.createRule({ create() {}, meta: {} });',
'export default ESLintUtils.RuleCreator(docsUrl)({ create() {}, meta: {} });',
].forEach(noRuleCase => {
it(`returns null for ${noRuleCase}`, () => {
const ast = espree.parse(noRuleCase, { ecmaVersion: 8, range: true, sourceType: 'module' });
Expand All @@ -75,13 +71,6 @@ describe('utils', () => {
'export default foo<Options, MessageIds>(123);',
'export default foo.bar<Options, MessageIds>(123);',
'export default foo.bar()<Options, MessageIds>(123);',

// Correct TypeScript helper structure but missing parameterized types:
'export default createESLintRule({ create() {}, meta: {} });',
'export default createESLintRule<>({ create() {}, meta: {} });',
'export default createESLintRule<OnlyOneType>({ create() {}, meta: {} });',
'export default util.createRule({ create() {}, meta: {} });',
'export default ESLintUtils.RuleCreator(docsUrl)({ create() {}, meta: {} });',
].forEach(noRuleCase => {
it(`returns null for ${noRuleCase}`, () => {
const ast = typescriptEslintParser.parse(noRuleCase, { ecmaVersion: 8, range: true, sourceType: 'module' });
Expand Down Expand Up @@ -112,18 +101,40 @@ describe('utils', () => {
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
'export default createESLintRule<>({ create() {}, meta: {} });': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
'export default createESLintRule({ create() {}, meta: {} });': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},

// Util function from util object
'export default util.createRule<Options, MessageIds>({ create() {}, meta: {} });': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
'export default util.createRule({ create() {}, meta: {} });': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},

// Util function from util object with additional doc URL argument
'export default ESLintUtils.RuleCreator(docsUrl)<Options, MessageIds>({ create() {}, meta: {} });': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
'export default ESLintUtils.RuleCreator(docsUrl)({ create() {}, meta: {} });': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
};

Object.keys(CASES).forEach(ruleSource => {
Expand Down

0 comments on commit f8a642a

Please sign in to comment.