Skip to content

Commit

Permalink
Fix: no-var should fix ForStatement.init (#7993)
Browse files Browse the repository at this point in the history
* Fix: no-var should fix ForStatement.init

* Chore: add tests
  • Loading branch information
mysticatea authored and ilyavolodin committed Feb 3, 2017
1 parent 99d386d commit d6b6ba1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/rules/no-var.js
Expand Up @@ -273,6 +273,7 @@ module.exports = {

if (
!isLoopAssignee(node) &&
!(node.parent.type === "ForStatement" && node.parent.init === node) &&
node.parent.type !== "BlockStatement" &&
node.parent.type !== "Program" &&
node.parent.type !== "SwitchCase"
Expand Down
23 changes: 22 additions & 1 deletion tests/lib/rules/no-var.js
Expand Up @@ -85,7 +85,28 @@ ruleTester.run("no-var", rule, {
}
]
},

{
code: "for (var i = 0; i < list.length; ++i) { foo(i) }",
output: "for (let i = 0; i < list.length; ++i) { foo(i) }",
errors: [
{ message: "Unexpected var, use let or const instead.", type: "VariableDeclaration" }
]
},
{
code: "for (var i = 0, i = 0; false;);",
output: "for (var i = 0, i = 0; false;);",
errors: [
{ message: "Unexpected var, use let or const instead.", type: "VariableDeclaration" }
]
},
{
code: "var i = 0; for (var i = 1; false;); console.log(i);",
output: "var i = 0; for (var i = 1; false;); console.log(i);",
errors: [
{ message: "Unexpected var, use let or const instead.", type: "VariableDeclaration" },
{ message: "Unexpected var, use let or const instead.", type: "VariableDeclaration" }
]
},

// Not fix if it's redeclared or it's used from outside of the scope or it's declared on a case chunk.
{
Expand Down

0 comments on commit d6b6ba1

Please sign in to comment.