Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upDisallow variable declarations from shadowing variables declared in the outer scope (no-shadow) #711
Comments
dcousens
added
the
enhancement
label
Dec 6, 2016
This comment has been minimized.
This comment has been minimized.
|
Another annoying, commonly used pattern: foo((err) => {
if (err) return
bar((err) => {
if (err) return
})
})
|
This comment has been minimized.
This comment has been minimized.
|
Generally feel fine about this. Need to see how many repos in the test suite would be affected by the change, though. |
This comment has been minimized.
This comment has been minimized.
|
Having turned this on local for a few days now... it has caught quite a lot of potential issues I was not previously aware of, about a 5-10% hit rate for actual bugs though. |
This comment has been minimized.
This comment has been minimized.
|
Would the following case be an error? const result = ['a', 'b', 'b'].reduce((result, letter) => {
result[letter] = (result[letter] || 0) + 1
return result
}, {}) |
This comment has been minimized.
This comment has been minimized.
|
@cesarandreu yes, I'm not sure if that would be configurable |
feross
changed the title
No-shadow
Disallow variable declarations from shadowing variables declared in the outer scope (no-shadow)
Apr 4, 2017
feross
added this to the
standard v11 milestone
Apr 4, 2017
dcousens
closed this
Feb 1, 2018
dcousens
reopened this
Feb 1, 2018
feross
modified the milestones:
standard v12,
standard v13
Aug 28, 2018
feross
modified the milestones:
standard v13,
standard v14
Jul 5, 2019
This comment has been minimized.
This comment has been minimized.
ManuelFte
commented
Aug 5, 2019
|
I went a bit more strict and used Wouldn't it be preferable to turn that setting to |
This comment has been minimized.
This comment has been minimized.
|
While this rule seems appealing, it breaks way too many packages (33%!) even with me specifying as many exceptions as I could think of. And it's not automatically fixable. Most of these are harmless instances of shadowing. Config:
Results:
I think it's too noisy to ship. Note: we already have https://eslint.org/docs/rules/no-shadow-restricted-names enabled, which is related, but much more conservative. It only prevents shadowing important globals. |
feross
closed this
Aug 14, 2019
feross
removed this from the standard 14 milestone
Aug 14, 2019
This comment has been minimized.
This comment has been minimized.
|
I don't have a problem with shadowing. I wouldn't go as far as to rule it out. |
This comment has been minimized.
This comment has been minimized.
|
I personally don't have a problem with shadowing either, I think it's appropriate in some situations |
This comment has been minimized.
This comment has been minimized.
|
For me, the problem with shadowing is the temporal nature of bugs that appear. If Until that point, I think it is a risk kept alive only by our lack of creativity with our variable names. |
This comment has been minimized.
This comment has been minimized.
ManuelFte
commented
Aug 18, 2019
|
I think shadowing, if it wasn't deliberate, is in general a bad practice and a symptom of lazy programming, whether it causes problems or not. Yes, it's harmless in most cases, but that's also the case for most rules in this standard; which is not the point. The point is forcing some good habits of ideal programming, and a habit like shadowing that has the potential to cause bugs and that can be easily avoided by merely picking different names for variables doesn't fit in that goal. However, I acknowledge that it's a very extended practice, and if so many packages would break by implementing it in the core standard then I guess it can't be helped. For my part I'll continue disallowing it for my projects. |
This comment has been minimized.
This comment has been minimized.
|
I'd like to change my mind, please. I might have come across a shadowing bug in my short career. I wouldn't mind having this rule. I hardly ever shadow, anyway. |
This comment has been minimized.
This comment has been minimized.
|
@ManuelFte @mightyiam I don't want to prevent patterns like this: foo(err => {
if (err) return console.error(err)
bar(err => {
if (err) return console.error(err)
console.log('success!')
})
})If you try changing the rule and running the test suite, you'll find tons of harmless cases like this one. It's also not automatically fixable. While it's regrettable to miss out on catching some possible bugs, we can't enable rules that are too noisy or people will find |
This comment has been minimized.
This comment has been minimized.
|
@feross I think that is a reasonable position. Out of interest, were there any actual bugs found in your tests? |
This comment has been minimized.
This comment has been minimized.
|
@dcousens I only skimmed it, but I didn't see any. |
dcousens commentedDec 6, 2016
•
edited
Couldn't find an issue for this, so I just thought I'd bring it up.
http://eslint.org/docs/rules/no-shadow
Bad
OK
Where can I see this being an issue?
Personally, the above has bitten me when
callbackwas lost inside the inner scope, so, I run this rule locally now.