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

no-shadow: allow a declaration's body to reuse the declared variable's name #12687

Closed
philipahlberg opened this issue Dec 19, 2019 · 16 comments
Closed
Assignees
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

Comments

@philipahlberg
Copy link

philipahlberg commented Dec 19, 2019

What rule do you want to change?
no-shadow

Does this change cause the rule to produce more or fewer warnings?
Fewer warnings

How will the change be implemented? (New option, new default behavior, etc.)?
New default behavior

Please provide some example code that this change will affect:

const person = people.find(person => person.name === "John");

What does the rule currently do for this code?
It currently produces a warning, claiming that the right-hand side person shadows the left-hand side person.

What will the rule do after it's changed?
It will not produce warnings when the purportedly-shadowed variable has not been declared.

I believe the current behavior is incorrect, as the variable has not (semantically) been declared at the point where the warning is generated, and so it can not be shadowed.

Are you willing to submit a pull request to implement this change?
Yes, though I would need some pointers on how to get started.

@philipahlberg philipahlberg added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules triage An ESLint team member will look at this issue soon labels Dec 19, 2019
@kaicataldo kaicataldo added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Dec 19, 2019
@kaicataldo
Copy link
Member

This makes sense to me. As far as I understand it, there's no danger here because it hasn't been declared yet and so cannot be overwritten.

The following example leads to a ReferenceError:

let user = ['philipahlberg', 'kaicataldo'].find(p => user === 'philipahlberg');
ReferenceError: can't access lexical declaration `user' before initialization

@mysticatea
Copy link
Member

mysticatea commented Dec 20, 2019

There are similar patterns:

// variable declarations
const a = [].find(a => true)

// assignment patterns in variable declarations or parameters
const [b = [].find(b => true)] = dummy
const { c = [].find(c => true) } = dummy
function func(d = [].find(d => true)) {}

// for-in / for-of with variable declarations
for (const e in [].find(e => true)) {}
for (const f of [].find(f => true)) {}

Online demo

Those a to f should not be warned as same.

@philipahlberg
Copy link
Author

philipahlberg commented Dec 20, 2019

It seems like it's sort of checked already, but perhaps it fails because the initialized variable is inserted into the outer scope before evaluating the rule or something similar?

@mysticatea
Copy link
Member

@philipahlberg That check is to avoid reporting function expression names:

const a = function a() {}

@philipahlberg
Copy link
Author

Any updates on this?

@kaicataldo
Copy link
Member

I think this could be classified as a bug since the rule currently has false negatives. @mysticatea Thoughts?

@philipahlberg
Copy link
Author

It seems to me like you generally agree that it should be implemented/fixed. Is that correct?

In that case, I wouldn't mind looking into it myself, assuming you don't have time to do so. I'm not familiar with the ESLint codebase though, so any help with getting started would be appreciated. Perhaps you have a forum/discord/irc where I can ask, if anything comes up?

@kaicataldo kaicataldo added accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly and removed enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Jan 29, 2020
@kaicataldo
Copy link
Member

Sorry for the radio silence on our end, and thanks for being willing to work on this! It looks like both @mysticatea and myself agree that this is a false-positive bug and I'm therefore going to mark it as accepted. We would gladly accept a PR for this!

We have some documentation around contributing that can be found here. If you have any questions, feel free to comment here (or on a work in progress pull request, if that's easier), or stop by our Gitter chat.

We're happy to help :)

@kaicataldo kaicataldo added good first issue Good for people who haven't worked on ESLint before help wanted The team would welcome a contribution from the community for this issue labels Feb 7, 2020
simwipado pushed a commit to simwipado/eslint that referenced this issue Feb 8, 2020
simwipado pushed a commit to simwipado/eslint that referenced this issue Feb 8, 2020
simwipado added a commit to simwipado/eslint that referenced this issue Feb 8, 2020
@mdjermanovic mdjermanovic linked a pull request Feb 9, 2020 that will close this issue
2 tasks
@kaicataldo kaicataldo removed the good first issue Good for people who haven't worked on ESLint before label Feb 17, 2020
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 9, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Jan 29, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Feb 5, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Feb 8, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Feb 13, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Feb 14, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Feb 21, 2022
boutahlilsoufiane added a commit to boutahlilsoufiane/eslint that referenced this issue Feb 23, 2022
Triage automation moved this from Pull Request Opened to Complete Feb 25, 2022
srijan-deepsource pushed a commit to DeepSourceCorp/eslint that referenced this issue May 30, 2022
…4963)

* Fix: Remove warning  in initialized variables (fixes eslint#12687)

* Fix: Remove warning  in initialized variables (fixes eslint#12687)

* Fix: Remove warning  in initialized variables (fixes eslint#12687)

* Docs: add invalid ambiguous example in doc (fixes eslint#12687)

* Docs: add invalid ambiguous example in doc (fixes eslint#12687)

* Fix: Adding option for variables on intialization (fixes eslint#12687)

* Fix: Adding option for variables on intialization (fixes eslint#12687)

* Fix: Adding option for variables on intialization (fixes eslint#12687)

* Fix: Adding option for variables on intialization (fixes eslint#12687)

* Fix: Adding option for variables on intialization (fixes eslint#12687)

* Fix: Adding option for variables on intialization (fixes eslint#12687)

* Fix: Adding option for variables on intialization (fixes eslint#12687)

* Fix: Adding option for variables on intialization (fixes eslint#12687)

* Fix: Adding option for variables on intialization (fixes eslint#12687)

* Fix: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)

* feat: Adding option for variables on intialization (fixes eslint#12687)
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Aug 25, 2022
@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 Aug 25, 2022
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
Archived in project
Triage
Complete
Development

Successfully merging a pull request may close this issue.

7 participants