Skip to content

Commit

Permalink
fix(eslint-plugin): [no-inferrable-types] apply also for parameter pr…
Browse files Browse the repository at this point in the history
…operties (#7288)
  • Loading branch information
auvred committed Jul 28, 2023
1 parent 7af040b commit 67f93b1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/eslint-plugin/src/rules/no-inferrable-types.ts
Expand Up @@ -250,15 +250,19 @@ export default util.createRule<Options, MessageIds>({
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);
}
});
}

Expand Down
35 changes: 35 additions & 0 deletions packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts
Expand Up @@ -150,6 +150,13 @@ class Foo {
a?: number = 5;
b?: boolean = true;
c?: string = 'foo';
}
`,
},
{
code: `
class Foo {
constructor(public a = true) {}
}
`,
},
Expand Down Expand Up @@ -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,
},
],
},
],
});

0 comments on commit 67f93b1

Please sign in to comment.