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

Conversation

@mdjermanovic
Copy link
Member

commented Jun 11, 2019

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 eslint bot added the triage label Jun 11, 2019

@kaicataldo kaicataldo added accepted bug rule and removed triage labels Jun 11, 2019

@platinumazure
Copy link
Member

left a comment

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);

This comment has been minimized.

Copy link
@platinumazure

platinumazure Jun 11, 2019

Member

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);

This comment has been minimized.

Copy link
@mdjermanovic

mdjermanovic Jun 11, 2019

Author Member

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.

This comment has been minimized.

Copy link
@mdjermanovic

mdjermanovic Jun 12, 2019

Author Member

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 Jun 12, 2019

@mysticatea
Copy link
Member

left a comment

LGTM. Thank you for nice refactoring.

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

@platinumazure
Copy link
Member

left a comment

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

@mdjermanovic

This comment has been minimized.

Copy link
Member Author

commented Jun 18, 2019

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

This comment has been minimized.

Copy link
Member

commented Jun 22, 2019

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

This comment has been minimized.

Copy link
Member Author

commented Jun 25, 2019

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

This comment has been minimized.

Copy link
Member Author

commented Jul 5, 2019

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.

@platinumazure platinumazure requested a review from mysticatea Jul 5, 2019

@platinumazure
Copy link
Member

left a comment

LGTM, thanks!

@mysticatea mysticatea merged commit 2dafe2d into eslint:master Jul 17, 2019

10 checks passed

commit-message PR title follows commit message guidelines
Details
continuous-integration Build #20190625.7 succeeded
Details
continuous-integration (Test on Node.js 10 (Linux)) Test on Node.js 10 (Linux) succeeded
Details
continuous-integration (Test on Node.js 12 (Linux)) Test on Node.js 12 (Linux) succeeded
Details
continuous-integration (Test on Node.js 12 (Windows)) Test on Node.js 12 (Windows) succeeded
Details
continuous-integration (Test on Node.js 12 (macOS)) Test on Node.js 12 (macOS) succeeded
Details
continuous-integration (Test on Node.js 8 (Linux)) Test on Node.js 8 (Linux) succeeded
Details
licence/cla Contributor License Agreement is signed.
Details
release-monitor No patch release is pending
Details
wip This PR is no longer a work in progress
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.