one-var reports error for a variable declared in a for-loop after an uninitialized variable declaration
See original GitHub issueWhat version of ESLint are you using? v2.5.3
What parser (default, Babel-ESLint, etc.) are you using? default
Please show your full configuration:
{
"rules": {
"one-var": ["error", {
"initialized": "never",
"uninitialized": "always"
}]
}
}
What did you do? Please include the actual source code causing the issue.
Here is a small code snippet reproducing my issue. Just save it to a file and run eslint
with the config shown above.
function reproduce(candidates) {
var err;
for (var ix in candidates) {
err = ix > 0;
}
console.log(err);
}
What did you expect to happen?
The code should pass linter checks.
What actually happened? Please include the actual, raw output from ESLint.
$ eslint bug.js
/private/tmp/bug/bug.js
3:8 error Combine this with the previous 'var' statement with uninitialized variables one-var
✖ 1 problem (1 error, 0 warnings)
As far as my understanding goes, eslint considers the ix
variable declared inside for
loop as unitialized and therefore complains that it should have been defined on the same line as the err
variable (before the for
loop). Indeed, the following code passes the checks:
function reproduce(candidates) {
var err, ix;
for (ix in candidates) {
err = ix > 0;
}
console.log(err);
}
From my point of view, I am proposing one of the following two changes:
- Fix the implementation of
one-var
to treatfor
loop variables differently. - Add an
one-var
option similar toinit-declaration'
signoreForLoopInit
that will tellone-var
to treat variables defined in for-loops differently.
Thoughts?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:11 (7 by maintainers)
Top GitHub Comments
@bajtos That looks like the right place. I verified that the rule doesn’t warn for regular
for
loop statements, only forfor...in
andfor...of
, so it sounds like you just need to add those as special case along withForStatement
. And yes, tests would go into the file you linked.I don’t understand this thread completely, but I think variable declarations at
ForInStatement.left
andForOfStatement.left
should be treated as initialized. Those variables will be initialized with iteration items.