From a59e6a7bd39eda2b4e5e0942f36abb53b3e04474 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Tue, 4 Feb 2025 20:32:50 +0900 Subject: [PATCH 1/2] fix(eslint-plugin): [no-inferrable-types] handle accessor --- .../src/rules/no-inferrable-types.ts | 9 +++++ .../tests/rules/no-inferrable-types.test.ts | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/packages/eslint-plugin/src/rules/no-inferrable-types.ts b/packages/eslint-plugin/src/rules/no-inferrable-types.ts index 8c18f786e143..a39fa5216060 100644 --- a/packages/eslint-plugin/src/rules/no-inferrable-types.ts +++ b/packages/eslint-plugin/src/rules/no-inferrable-types.ts @@ -193,6 +193,7 @@ export default createRule({ */ function reportInferrableType( node: + | TSESTree.AccessorProperty | TSESTree.Parameter | TSESTree.PropertyDefinition | TSESTree.VariableDeclarator, @@ -277,7 +278,15 @@ export default createRule({ reportInferrableType(node, node.typeAnnotation, node.value); } + function inferrableAccessorVisitor(node: TSESTree.AccessorProperty): void { + if (ignoreProperties) { + return; + } + reportInferrableType(node, node.typeAnnotation, node.value); + } + return { + AccessorProperty: inferrableAccessorVisitor, ArrowFunctionExpression: inferrableParameterVisitor, FunctionDeclaration: inferrableParameterVisitor, FunctionExpression: inferrableParameterVisitor, diff --git a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts index 13b71358e3e0..79304307ea3d 100644 --- a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts +++ b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts @@ -113,6 +113,11 @@ class Foo { readonly a: number = 5; } `, + ` +class Foo { + accessor a = 5; +} + `, 'const a: any = 5;', "const fn = function (a: any = 5, b: any = true, c: any = 'foo') {};", @@ -141,6 +146,14 @@ class Foo { }, { code: ` +class Foo { + accessor a: number = 5; +} + `, + options: [{ ignoreProperties: true }], + }, + { + code: ` class Foo { a?: number = 5; b?: boolean = true; @@ -316,6 +329,28 @@ class Foo { output: ` class Foo { constructor(public a = true) {} +} + `, + }, + { + code: ` +class Foo { + accessor a: number = 5; +} + `, + errors: [ + { + column: 3, + data: { + type: 'number', + }, + line: 3, + messageId: 'noInferrableType', + }, + ], + output: ` +class Foo { + accessor a = 5; } `, }, From 1e0affacb85eb59b703c2460b074aeeb20182659 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Sat, 8 Feb 2025 23:56:23 +0900 Subject: [PATCH 2/2] apply reviews --- .../eslint-plugin/src/rules/no-inferrable-types.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-inferrable-types.ts b/packages/eslint-plugin/src/rules/no-inferrable-types.ts index a39fa5216060..4d8496a7b288 100644 --- a/packages/eslint-plugin/src/rules/no-inferrable-types.ts +++ b/packages/eslint-plugin/src/rules/no-inferrable-types.ts @@ -266,7 +266,7 @@ export default createRule({ } function inferrablePropertyVisitor( - node: TSESTree.PropertyDefinition, + node: TSESTree.AccessorProperty | TSESTree.PropertyDefinition, ): void { // We ignore `readonly` because of Microsoft/TypeScript#14416 // Essentially a readonly property without a type @@ -278,15 +278,8 @@ export default createRule({ reportInferrableType(node, node.typeAnnotation, node.value); } - function inferrableAccessorVisitor(node: TSESTree.AccessorProperty): void { - if (ignoreProperties) { - return; - } - reportInferrableType(node, node.typeAnnotation, node.value); - } - return { - AccessorProperty: inferrableAccessorVisitor, + AccessorProperty: inferrablePropertyVisitor, ArrowFunctionExpression: inferrableParameterVisitor, FunctionDeclaration: inferrableParameterVisitor, FunctionExpression: inferrableParameterVisitor,