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: prefer-const produces invalid autofix (fixes #11699) #11827

Merged
merged 3 commits into from Jul 17, 2019
Merged

Fix: prefer-const produces invalid autofix (fixes #11699) #11827

merged 3 commits into from Jul 17, 2019

Conversation

mdjermanovic
Copy link
Member

What is the purpose of this pull request? (put an "X" next to item)

[X] Bug fix

issue #11699

What changes did you make? (Give an overview)

Fixed the autofix for the following cases:

let {a, b} = c, d;
let {a, b, c} = {}, e, f;
let {a, b} = {}, c = 0;
c = 1;

by moving and modifying logic from checkGroup() to VariableDeclaration()

Is there anything you'd like reviewers to focus on?

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Jun 11, 2019
@kaicataldo kaicataldo added accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Jun 11, 2019
Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

LGTM except for one suggestion to avoid needlessly breaking Typescript. Thanks!

(node.parent.type === "ForInStatement" || node.parent.type === "ForOfStatement" ||
node.declarations.every(declaration => declaration.init));

const declarationKindToken = sourceCode.getFirstToken(node);
Copy link
Member

Choose a reason for hiding this comment

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

It would probably be better to use a predicate here, in case of Typescript declare let or other potential token prefixes:

Suggested change
const declarationKindToken = sourceCode.getFirstToken(node);
const declarationKindToken = sourceCode.getFirstToken(node, t => t.value === node.kind);

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll fix this, it was inherited from checkGroup, as well as the autofix for each variable which isn't necessary now when we have the whole declaration.

Speaking of TypeScript, it seems that @typescript-eslint/parser allows let let = 1, so one fix without the other might produce changing several tokens.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's fixed now within the second commit (multiple autofixes are also removed).

While it is most likely good to support potential prefixes, this particular example shouldn't produce a problem (like #11441), because only initialized variables are auto-fixed and the following is not a valid TypeScript:

declare let x = 1; // tsc error `Initializers are not allowed in ambient contexts`

Nevertheless, @typescript-eslint/parser treats this as a valid code, so the autofix without this correction would indeed replace declare.

@platinumazure platinumazure self-requested a review June 12, 2019 12:26
Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you for nice refactoring.

As a non-blocking suggestion, for-of instead of forEach() is nice.

Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! (Sorry for letting this one sit.)

@mdjermanovic
Copy link
Member Author

Actually, I believe the refactoring is ** not good **, please do not merge this fix yet.

Although the tests are passing, after rereading the original code I realized I didn't fully understand intended functionality, in particular cases when destructuring happens outside of the declaration.

For instance, with the option {"destructuring": "all"}, this code was correct (no warnings):

let a; 
let b;
({a, b} = {});
b = 1;

After this refactoring, this code would be incorrect.

@platinumazure platinumazure changed the title Fix: prefer-const produces invalid autofix (fixes #11699) WIP: Fix: prefer-const produces invalid autofix (fixes #11699) Jun 22, 2019
@platinumazure
Copy link
Member

Hi @mdjermanovic, based on your last comment I've edited "WIP:" into the PR title. This helps our PR checks to show that there's more to do before we can merge. When you're ready for us to review and merge, simply edit the PR title to remove the WIP prefix.

Thanks for your diligence, we really appreciate it!

@mdjermanovic
Copy link
Member Author

I rolled back the original code and made a small fix just to address the original issue + the TypeScript fix.

As a summary, with this PR the following is done:

  • Don't fix (let -> const) a declaration if there is at least one uninitialized variable in the declaration.
  • Don't break TypeScript, i.e. don't replace the wrong token.

If you agree, this could close the original issue and I can open a new one:

  • It would be good to clarify the intended behavior with more test cases. The first version of this fix was wrong, but the tests didn't catch it.
  • There are still other bugs (e.g. let {a, b} = {}, c = 0; c = 1;). The code probably needs to be refactored, I'm not sure but it seems that the logic to fix or not to fix is in a place/time where there is not enough information.

@mdjermanovic mdjermanovic changed the title WIP: Fix: prefer-const produces invalid autofix (fixes #11699) Fix: prefer-const produces invalid autofix (fixes #11699) Jun 25, 2019
@mdjermanovic
Copy link
Member Author

Just a note that this is ready for a new review, and that the existing 2 approvals are no longer valid since it's a completely different code now.

Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Jan 14, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jan 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants