Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: no-undef-init autofix removes comments #12299

Merged
merged 1 commit into from Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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" }]
}
]
});