New rule proposal: no-unassigned-vars
See original GitHub issuePlease describe what the rule should do:
Reports var
and let
variables that are never assigned a value. In other words, variables that have zero write references.
What category of rule is this? (place an “X” next to just one item)
[X] Warns about a potential error (problem)
Provide 2-3 code examples that this rule will warn about:
var obj;
obj.x = 1;
obj.y = 2;
let a, b, c;
if (something) {
a = 0;
b = false;
} else {
a = 1;
b = true;
}
f(a, b, c);
Why should this rule be included in ESLint (instead of a plugin)?
This rule looks like a nice addition to the Variables
category of rules, a complement to no-unused-vars
to detect possible errors in the code.
An unassigned variable is a possible error because it always has undefined
value. Even if that is the intention, it’s more clear to simply inline undefined
instead of reading from the variable, or to make the intention explicit with const foo = undefined
instead of just let foo
which looks more like an error in the code where it’s never assigned a value.
no-undef-init
does not disallow const
.
prefer-const
does not report these cases.
To avoid overlapping with no-unused-vars
, this rule would report only variables that have at least 1 read reference.
Are you willing to submit a pull request to implement this rule?
Yes
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:11 (11 by maintainers)
Top GitHub Comments
In that case, I think that explicit assignment (
c = undefined
) is a better choice for maintainability.I’m willing to work on this, for sure! On the other hand, I believe that this still needs a lot of additional analysis & design, and would end up being a fairly complex rule, so I definitely wouldn’t mind closing this in regard to the reasons for the new rules policy.