From 67f93b19f2e481a4e441635d72e81de9d5d7ad44 Mon Sep 17 00:00:00 2001 From: auvred <61150013+auvred@users.noreply.github.com> Date: Fri, 28 Jul 2023 06:33:16 +0300 Subject: [PATCH] fix(eslint-plugin): [no-inferrable-types] apply also for parameter properties (#7288) --- .../src/rules/no-inferrable-types.ts | 22 +++++++----- .../tests/rules/no-inferrable-types.test.ts | 35 +++++++++++++++++++ 2 files changed, 48 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 54823f7be39..f63beda7440 100644 --- a/packages/eslint-plugin/src/rules/no-inferrable-types.ts +++ b/packages/eslint-plugin/src/rules/no-inferrable-types.ts @@ -250,15 +250,19 @@ export default util.createRule({ if (ignoreParameters || !node.params) { return; } - ( - node.params.filter( - param => - param.type === AST_NODE_TYPES.AssignmentPattern && - param.left && - param.right, - ) as TSESTree.AssignmentPattern[] - ).forEach(param => { - reportInferrableType(param, param.left.typeAnnotation, param.right); + + node.params.forEach(param => { + if (param.type === AST_NODE_TYPES.TSParameterProperty) { + param = param.parameter; + } + + if ( + param.type === AST_NODE_TYPES.AssignmentPattern && + param.left && + param.right + ) { + reportInferrableType(param, param.left.typeAnnotation, param.right); + } }); } 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 ce1b5deb277..fe91490df3e 100644 --- a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts +++ b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts @@ -150,6 +150,13 @@ class Foo { a?: number = 5; b?: boolean = true; c?: string = 'foo'; +} + `, + }, + { + code: ` +class Foo { + constructor(public a = true) {} } `, }, @@ -289,5 +296,33 @@ class Foo { }, ], }, + { + code: ` +class Foo { + constructor(public a: boolean = true) {} +} + `, + output: ` +class Foo { + constructor(public a = true) {} +} + `, + options: [ + { + ignoreParameters: false, + ignoreProperties: false, + }, + ], + errors: [ + { + messageId: 'noInferrableType', + data: { + type: 'boolean', + }, + line: 3, + column: 22, + }, + ], + }, ], });