Skip to content

Commit

Permalink
Fix: no-undef-init autofix removes comments (#12299)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and platinumazure committed Sep 29, 2019
1 parent d89390b commit acec201
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rules/no-undef-init.js
Expand Up @@ -37,7 +37,8 @@ module.exports = {
init = node.init && node.init.name,
scope = context.getScope(),
undefinedVar = astUtils.getVariableByName(scope, "undefined"),
shadowed = undefinedVar && undefinedVar.defs.length > 0;
shadowed = undefinedVar && undefinedVar.defs.length > 0,
lastToken = sourceCode.getLastToken(node);

if (init === "undefined" && node.parent.kind !== "const" && !shadowed) {
context.report({
Expand All @@ -54,6 +55,11 @@ module.exports = {
// Don't fix destructuring assignment to `undefined`.
return null;
}

if (sourceCode.commentsExistBetween(node.id, lastToken)) {
return null;
}

return fixer.removeRange([node.id.range[1], node.range[1]]);
}
});
Expand Down
56 changes: 56 additions & 0 deletions tests/lib/rules/no-undef-init.js
Expand Up @@ -92,6 +92,62 @@ ruleTester.run("no-undef-init", rule, {
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" }]
},

// Should not autofix if it would remove comments
{
code: "let /* comment */a = undefined;",
output: "let /* comment */a;",
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: "let a /**/ = undefined;",
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
},
{
code: "let a//\n= 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: "let a = //\nundefined;",
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
},
{
code: "let a = undefined/* comment */;",
output: "let a/* comment */;",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
},
{
code: "let a = undefined/* comment */, b;",
output: "let a/* comment */, b;",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
},
{
code: "let a = undefined//comment\n, b;",
output: "let a//comment\n, b;",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
}
]
});

0 comments on commit acec201

Please sign in to comment.