Skip to content

Commit

Permalink
Fix: remove autofix for var undef inits (fixes #9231) (#9288)
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHom authored and not-an-aardvark committed Sep 15, 2017
1 parent 002e199 commit e220687
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/rules/no-undef-init.js
Expand Up @@ -43,6 +43,10 @@ module.exports = {
message: "It's not necessary to initialize '{{name}}' to undefined.",
data: { name },
fix(fixer) {
if (node.parent.kind === "var") {
return null;
}

if (node.id.type === "ArrayPattern" || node.id.type === "ObjectPattern") {

// Don't fix destructuring assignment to `undefined`.
Expand Down
47 changes: 44 additions & 3 deletions tests/lib/rules/no-undef-init.js
Expand Up @@ -27,17 +27,17 @@ ruleTester.run("no-undef-init", rule, {
invalid: [
{
code: "var a = undefined;",
output: "var a;",
output: null,
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
},
{
code: "var a = undefined, b = 1;",
output: "var a, b = 1;",
output: null,
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
},
{
code: "var a = 1, b = undefined, c = 5;",
output: "var a = 1, b, c = 5;",
output: null,
errors: [{ message: "It's not necessary to initialize 'b' to undefined.", type: "VariableDeclarator" }]
},
{
Expand All @@ -51,6 +51,47 @@ ruleTester.run("no-undef-init", rule, {
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize '{a}' to undefined.", type: "VariableDeclarator" }]
},
{
code: "for(var i in [1,2,3]){var a = undefined; for(var j in [1,2,3]){}}",
output: null,
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
},
{
code: "let a = undefined;",
output: "let a;",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
},
{
code: "let a = undefined, b = 1;",
output: "let a, b = 1;",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
},
{
code: "let a = 1, b = undefined, c = 5;",
output: "let a = 1, b, c = 5;",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize 'b' to undefined.", type: "VariableDeclarator" }]
},
{
code: "let [a] = undefined;",
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize '[a]' to undefined.", type: "VariableDeclarator" }]
},
{
code: "let {a} = undefined;",
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize '{a}' to undefined.", type: "VariableDeclarator" }]
},
{
code: "for(var i in [1,2,3]){let a = undefined; for(var j in [1,2,3]){}}",
output: "for(var i in [1,2,3]){let a; for(var j in [1,2,3]){}}",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
}
]
});

0 comments on commit e220687

Please sign in to comment.