From f8a642ac01fd153d5cde552a94d24371bae42e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Wed, 13 Oct 2021 20:41:43 +0800 Subject: [PATCH] Fix: False negative with TypeScript rules that don't have type parameters (fixes #202) (#209) --- lib/utils.js | 2 -- tests/lib/utils.js | 33 ++++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 5d757a10..face10b6 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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: node.arguments.length === 1 && node.arguments[0].type === 'ObjectExpression' && // Check various TypeScript rule helper formats. diff --git a/tests/lib/utils.js b/tests/lib/utils.js index 396fd9c9..7dee2dc9 100644 --- a/tests/lib/utils.js +++ b/tests/lib/utils.js @@ -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' }); @@ -75,13 +71,6 @@ describe('utils', () => { 'export default foo(123);', '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 createESLintRule<>({ create() {}, meta: {} });', - '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 = typescriptEslintParser.parse(noRuleCase, { ecmaVersion: 8, range: true, sourceType: 'module' }); @@ -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({ 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)({ 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 => {