question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

one-var reports error for a variable declared in a for-loop after an uninitialized variable declaration

See original GitHub issue

What 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 treat for loop variables differently.
  • Add an one-var option similar to init-declaration's ignoreForLoopInit that will tell one-var to treat variables defined in for-loops differently.

Thoughts?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ilyavolodincommented, Mar 31, 2016

@bajtos That looks like the right place. I verified that the rule doesn’t warn for regular for loop statements, only for for...in and for...of, so it sounds like you just need to add those as special case along with ForStatement. And yes, tests would go into the file you linked.

1reaction
mysticateacommented, May 2, 2016

I don’t understand this thread completely, but I think variable declarations at ForInStatement.left and ForOfStatement.left should be treated as initialized. Those variables will be initialized with iteration items.

Read more comments on GitHub >

github_iconTop Results From Across the Web

one-var - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
Error on Declaring variable in for statement
In C89/ANSI C you have to declare variables in the beginning of the scope block. Pay attention to that because you will most...
Read more >
SyntaxError: a declaration in the head of a for-of loop can't ...
The head of a for...of loop contains an initializer expression. That is, a variable is declared and assigned a value for (const i...
Read more >
A tour of the Dart language
Dart tools can report two kinds of problems: warnings and errors. ... Declaring a non-nullable variable that's initialized after its declaration.
Read more >
one-var - Rules
"uninitialized": "never" requires multiple variable declarations for uninitialized variables per scope. always. Examples of incorrect code for this ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found