From 13bc9afee942a8a7742ec0b9746d8520331c40b0 Mon Sep 17 00:00:00 2001 From: auvred Date: Fri, 21 Jul 2023 20:21:12 +0000 Subject: [PATCH 1/4] fix(eslint-plugin): [no-inferrable-types] apply also for parameter properties --- .../src/rules/no-inferrable-types.ts | 22 ++++---- .../tests/rules/no-inferrable-types.test.ts | 52 +++++++++++++++++++ 2 files changed, 65 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..af9326bf0ce 100644 --- a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts +++ b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts @@ -289,5 +289,57 @@ class Foo { }, ], }, + { + code: ` +class Foo { + constructor( + a: number = 5, + public b: boolean = true, + readonly c: string = 'foo', + ) {} +} + `, + output: ` +class Foo { + constructor( + a = 5, + public b = true, + readonly c = 'foo', + ) {} +} + `, + options: [ + { + ignoreParameters: false, + ignoreProperties: false, + }, + ], + errors: [ + { + messageId: 'noInferrableType', + data: { + type: 'number', + }, + line: 4, + column: 5, + }, + { + messageId: 'noInferrableType', + data: { + type: 'boolean', + }, + line: 5, + column: 12, + }, + { + messageId: 'noInferrableType', + data: { + type: 'string', + }, + line: 6, + column: 14, + }, + ], + }, ], }); From 4ab2057d6f179e845b2e3051413024743ec89639 Mon Sep 17 00:00:00 2001 From: auvred Date: Fri, 21 Jul 2023 20:35:40 +0000 Subject: [PATCH 2/4] chore: trigger pr checks (i hope without v8 internal errors this time) From 00adda3ec3371c1b4e8ac1ac532bfe115085581d Mon Sep 17 00:00:00 2001 From: auvred Date: Wed, 26 Jul 2023 15:22:12 +0000 Subject: [PATCH 3/4] test: simplify invalid case and add valid one --- .../tests/rules/no-inferrable-types.test.ts | 39 ++++++------------- 1 file changed, 11 insertions(+), 28 deletions(-) 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 af9326bf0ce..287ccb7f657 100644 --- a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts +++ b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts @@ -153,6 +153,13 @@ class Foo { } `, }, + { + code: ` +class Foo { + constructor(public a = true) {} +} + `, + }, ], invalid: [ @@ -292,20 +299,12 @@ class Foo { { code: ` class Foo { - constructor( - a: number = 5, - public b: boolean = true, - readonly c: string = 'foo', - ) {} + constructor(public a: boolean = true) {} } `, output: ` class Foo { - constructor( - a = 5, - public b = true, - readonly c = 'foo', - ) {} + constructor(public a = true) {} } `, options: [ @@ -315,29 +314,13 @@ class Foo { }, ], errors: [ - { - messageId: 'noInferrableType', - data: { - type: 'number', - }, - line: 4, - column: 5, - }, { messageId: 'noInferrableType', data: { type: 'boolean', }, - line: 5, - column: 12, - }, - { - messageId: 'noInferrableType', - data: { - type: 'string', - }, - line: 6, - column: 14, + line: 3, + column: 22, }, ], }, From 6bb4d5bf42bfee773f454ac31486ac8f87bc7194 Mon Sep 17 00:00:00 2001 From: auvred Date: Wed, 26 Jul 2023 15:35:10 +0000 Subject: [PATCH 4/4] chore: fix wrong indentation --- packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 287ccb7f657..fe91490df3e 100644 --- a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts +++ b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts @@ -158,7 +158,7 @@ class Foo { class Foo { constructor(public a = true) {} } - `, + `, }, ],