Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-inferrable] fix optional param to valid code #5342

Merged
merged 2 commits into from Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/eslint-plugin/src/rules/no-inferrable-types.ts
Expand Up @@ -46,6 +46,8 @@ export default util.createRule<Options, MessageIds>({
},
],
create(context, [{ ignoreParameters, ignoreProperties }]) {
const sourceCode = context.getSourceCode();

function isFunctionCall(
init: TSESTree.Expression,
callName: string,
Expand Down Expand Up @@ -215,7 +217,16 @@ export default util.createRule<Options, MessageIds>({
data: {
type,
},
fix: fixer => fixer.remove(typeNode),
*fix(fixer) {
if (
(node.type === AST_NODE_TYPES.AssignmentPattern &&
node.left.optional) ||
(node.type === AST_NODE_TYPES.PropertyDefinition && node.definite)
) {
yield fixer.remove(sourceCode.getTokenBefore(typeNode)!);
}
yield fixer.remove(typeNode);
},
});
}

Expand Down
49 changes: 48 additions & 1 deletion packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts
Expand Up @@ -156,7 +156,54 @@ class Foo {

invalid: [
...invalidTestCases,

{
// This is invalid TS semantic, but it's trivial to make valid anyway
code: 'const fn = (a?: number = 5) => {};',
Copy link
Member

@armano2 armano2 Jul 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if we want to handle this in here, but there is one more use case that is broken.

playground

class Test {
  x!: number = 5
  abstract y!: number = 5
}

maybe something like this could be better:

if (
  node.type === AST_NODE_TYPES.AssignmentPattern &&
  node.left.optional
) {
  yield fixer.remove(sourceCode.getTokenBefore(typeNode)!);
} else if (
  (node.type === AST_NODE_TYPES.TSAbstractPropertyDefinition ||
  node.type === AST_NODE_TYPES.PropertyDefinition) &&
  node.definite
) {
  yield fixer.remove(sourceCode.getTokenBefore(typeNode)!);
}

we can make separate ticket for this, if you like


there is also

class Test2 {
  constructor(private a: number = 5) { }
}

witch is currently not reported

Copy link
Member Author

@Josh-Cena Josh-Cena Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add definite assignment to this PR, but the private one seems tangential to this. Does this one also seem worth reporting?

class Test2 {
  x?: number = 5;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the abstract y!: number = 5 is too messed up to be autofixed. The initializer needs to be removed as well. It isn't even reported by the rule under the status quo, so I'll just ignore that.

output: 'const fn = (a = 5) => {};',
options: [
{
ignoreParameters: false,
},
],
errors: [
{
messageId: 'noInferrableType',
data: {
type: 'number',
},
line: 1,
column: 13,
},
],
},
{
// This is invalid TS semantic, but it's trivial to make valid anyway
code: `
class A {
a!: number = 1;
}
`,
output: `
class A {
a = 1;
}
`,
options: [
{
ignoreProperties: false,
},
],
errors: [
{
messageId: 'noInferrableType',
data: {
type: 'number',
},
line: 3,
column: 3,
},
],
},
{
code: "const fn = (a: number = 5, b: boolean = true, c: string = 'foo') => {};",
output: "const fn = (a = 5, b = true, c = 'foo') => {};",
Expand Down