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: extend prefer-const fixer range to whole declaration (fixes #13899) #14033

Merged
merged 2 commits into from Jan 30, 2021
Merged

Fix: extend prefer-const fixer range to whole declaration (fixes #13899) #14033

merged 2 commits into from Jan 30, 2021

Conversation

snitin315
Copy link
Contributor

Prerequisites checklist

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

[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

Fixes #13899

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 Jan 24, 2021
@snitin315
Copy link
Contributor Author

I'm not sure why the test case is failing although it is correctly converting
let someFunc = () => { let a = 1, b = 2; foo(a, b) } to const someFunc = () => { const a = 1, b = 2; foo(a, b) }
when I am testing manually.

eslint-prefer-const.mp4

@mdjermanovic
Copy link
Member

I'm not sure why the test case is failing

There are overlapping ranges now, so the fixes cannot be applied in the same pass.

RuleTester runs only one pass, which in this case fixes the outer let only:

const someFunc = () => { let a = 1, b = 2; foo(a, b) }

and compares the result of that single pass with the provided output.

Before this change, the ranges were not overlapping, so both lets could be fixed in the same pass.

although it is correctly converting
let someFunc = () => { let a = 1, b = 2; foo(a, b) } to const someFunc = () => { const a = 1, b = 2; foo(a, b) }
when I am testing manually.

--fix repeats linting up to 10 times if the code was auto-fixed, so the inner let gets fixed in the second pass.

@mdjermanovic
Copy link
Member

The solution is to change the output in the failing test case, to:

    output: "const someFunc = () => { let a = 1, b = 2; foo(a, b) }"

and it would be nice to also leave a comment that the inner let will be auto-fixed in the second pass.

@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion autofix This change is related to ESLint's autofixing capabilities 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 Jan 25, 2021
@snitin315
Copy link
Contributor Author

snitin315 commented Jan 25, 2021

There are overlapping ranges now, so the fixes cannot be applied in the same pass.

Thanks for the explanation @mdjermanovic.

The solution is to change the output in the failing test case, to:

I have updated the test case 🚀

Copy link
Member

@mdjermanovic mdjermanovic 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!

@mdjermanovic mdjermanovic changed the title Fix: extend fixer range of prefer const to whole declaration Fix: extend fixer range of prefer-const to whole declaration Jan 25, 2021
@mdjermanovic mdjermanovic changed the title Fix: extend fixer range of prefer-const to whole declaration Fix: extend prefer-const fixer range to whole declaration (fixes #13899) Jan 30, 2021
@mdjermanovic mdjermanovic merged commit fb27422 into eslint:master Jan 30, 2021
@mdjermanovic
Copy link
Member

Thanks for contributing!

@snitin315 snitin315 deleted the fix/prefer-const branch January 30, 2021 13:01
@snitin315
Copy link
Contributor Author

snitin315 commented Jan 30, 2021

@mdjermanovic Thanks for your review and help. Can you point me towards some more beginner-friendly issues where I can help?

@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jul 30, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jul 30, 2021
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 autofix This change is related to ESLint's autofixing capabilities 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.

Bug: --fix ignores defined order and creates a JS error
2 participants