diff --git a/lib/rules/no-undef-init.js b/lib/rules/no-undef-init.js index 1390c880fc8..1fdccb867b3 100644 --- a/lib/rules/no-undef-init.js +++ b/lib/rules/no-undef-init.js @@ -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({ @@ -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]]); } }); diff --git a/tests/lib/rules/no-undef-init.js b/tests/lib/rules/no-undef-init.js index efbbbe4c4c7..509de01100e 100644 --- a/tests/lib/rules/no-undef-init.js +++ b/tests/lib/rules/no-undef-init.js @@ -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" }] } ] });