Skip to content

Commit

Permalink
Fix: no-var don't fix globals (fixes #9520) (#9525)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and gyandeeps committed Oct 27, 2017
1 parent b8aa071 commit d80b9d0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
11 changes: 11 additions & 0 deletions lib/rules/no-var.js
Expand Up @@ -15,6 +15,15 @@ const astUtils = require("../ast-utils");
// Helpers
//------------------------------------------------------------------------------

/**
* Check whether a given variable is a global variable or not.
* @param {eslint-scope.Variable} variable The variable to check.
* @returns {boolean} `true` if the variable is a global variable.
*/
function isGlobal(variable) {
return Boolean(variable.scope) && variable.scope.type === "global";
}

/**
* Finds the nearest function scope or global scope walking up the scope
* hierarchy.
Expand Down Expand Up @@ -203,6 +212,7 @@ module.exports = {
* Checks whether it can fix a given variable declaration or not.
* It cannot fix if the following cases:
*
* - A variable is a global variable.
* - A variable is declared on a SwitchCase node.
* - A variable is redeclared.
* - A variable is used from outside the scope.
Expand Down Expand Up @@ -256,6 +266,7 @@ module.exports = {

if (node.parent.type === "SwitchCase" ||
node.declarations.some(hasSelfReferenceInTDZ) ||
variables.some(isGlobal) ||
variables.some(isRedeclared) ||
variables.some(isUsedFromOutsideOf(scopeNode))
) {
Expand Down
65 changes: 58 additions & 7 deletions tests/lib/rules/no-var.js
Expand Up @@ -21,13 +21,22 @@ const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
ruleTester.run("no-var", rule, {
valid: [
"const JOE = 'schmoe';",
"let moo = 'car';"
"let moo = 'car';",
{
code: "const JOE = 'schmoe';",
parserOptions: { ecmaFeatures: { globalReturn: true } }
},
{
code: "let moo = 'car';",
parserOptions: { ecmaFeatures: { globalReturn: true } }
}
],

invalid: [
{
code: "var foo = bar;",
output: "let foo = bar;",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
{
message: "Unexpected var, use let or const instead.",
Expand All @@ -38,6 +47,7 @@ ruleTester.run("no-var", rule, {
{
code: "var foo = bar, toast = most;",
output: "let foo = bar, toast = most;",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
{
message: "Unexpected var, use let or const instead.",
Expand All @@ -48,6 +58,7 @@ ruleTester.run("no-var", rule, {
{
code: "var foo = bar; let toast = most;",
output: "let foo = bar; let toast = most;",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
{
message: "Unexpected var, use let or const instead.",
Expand All @@ -58,6 +69,7 @@ ruleTester.run("no-var", rule, {
{
code: "for (var a of b) { console.log(a); }",
output: "for (let a of b) { console.log(a); }",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
{
message: "Unexpected var, use let or const instead.",
Expand All @@ -68,6 +80,7 @@ ruleTester.run("no-var", rule, {
{
code: "for (var a in b) { console.log(a); }",
output: "for (let a in b) { console.log(a); }",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
{
message: "Unexpected var, use let or const instead.",
Expand All @@ -78,6 +91,7 @@ ruleTester.run("no-var", rule, {
{
code: "for (let a of b) { var c = 1; console.log(c); }",
output: "for (let a of b) { let c = 1; console.log(c); }",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
{
message: "Unexpected var, use let or const instead.",
Expand All @@ -88,20 +102,23 @@ 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) }",
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
{ message: "Unexpected var, use let or const instead.", type: "VariableDeclaration" }
]
},
{
code: "for (var i = 0, i = 0; false;);",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
{ message: "Unexpected var, use let or const instead.", type: "VariableDeclaration" }
]
},
{
code: "var i = 0; for (var i = 1; false;); console.log(i);",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
{ message: "Unexpected var, use let or const instead.", type: "VariableDeclaration" },
{ message: "Unexpected var, use let or const instead.", type: "VariableDeclaration" }
Expand All @@ -112,6 +129,7 @@ ruleTester.run("no-var", rule, {
{
code: "var a, b, c; var a;",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead.",
"Unexpected var, use let or const instead."
Expand All @@ -120,6 +138,7 @@ ruleTester.run("no-var", rule, {
{
code: "var a; if (b) { var a; }",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead.",
"Unexpected var, use let or const instead."
Expand All @@ -128,34 +147,39 @@ ruleTester.run("no-var", rule, {
{
code: "if (foo) { var a, b, c; } a;",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
},
{
code: "for (var i = 0; i < 10; ++i) {} i;",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
},
{
code: "for (var a in obj) {} a;",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
},
{
code: "for (var a of list) {} a;",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
},
{
code: "switch (a) { case 0: var b = 1 }",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
Expand All @@ -165,13 +189,15 @@ ruleTester.run("no-var", rule, {
{
code: "for (var a of b) { arr.push(() => a); }",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
},
{
code: "for (let a of b) { var c; console.log(c); c = 'hello'; }",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
Expand All @@ -181,46 +207,47 @@ ruleTester.run("no-var", rule, {
{
code: "var a = a",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
},
{
code: "var {a = a} = {}",
output: null,
parserOptions: { ecmaVersion: 2015 },
parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
},
{
code: "var {a = b, b} = {}",
output: null,
parserOptions: { ecmaVersion: 2015 },
parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
},
{
code: "var {a, b = a} = {}",
output: "let {a, b = a} = {}",
parserOptions: { ecmaVersion: 2015 },
parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
},
{
code: "var a = b, b = 1",
output: null,
parserOptions: { ecmaVersion: 2015 },
parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
},
{
code: "var a = b; var b = 1",
output: "let a = b; var b = 1",
parserOptions: { ecmaVersion: 2015 },
parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead.",
"Unexpected var, use let or const instead."
Expand All @@ -232,7 +259,7 @@ ruleTester.run("no-var", rule, {
{
code: "function foo() { a } var a = 1; foo()",
output: null,
parserOptions: { ecmaVersion: 2015 },
parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
errors: [
"Unexpected var, use let or const instead."
]
Expand All @@ -242,9 +269,33 @@ ruleTester.run("no-var", rule, {
{
code: "if (foo) var bar = 1;",
output: null,
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [
{ message: "Unexpected var, use let or const instead.", type: "VariableDeclaration" }
]
},

// https://github.com/eslint/eslint/issues/9520
{
code: "var foo = 1",
output: null,
errors: ["Unexpected var, use let or const instead."]
},
{
code: "{ var foo = 1 }",
output: null,
errors: ["Unexpected var, use let or const instead."]
},
{
code: "if (true) { var foo = 1 }",
output: null,
errors: ["Unexpected var, use let or const instead."]
},
{
code: "var foo = 1",
output: "let foo = 1",
parserOptions: { sourceType: "module" },
errors: ["Unexpected var, use let or const instead."]
}
]
});

0 comments on commit d80b9d0

Please sign in to comment.