Skip to content

Commit acec201

Browse files
mdjermanovicplatinumazure
authored andcommitted
Fix: no-undef-init autofix removes comments (#12299)
1 parent d89390b commit acec201

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

lib/rules/no-undef-init.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ module.exports = {
3737
init = node.init && node.init.name,
3838
scope = context.getScope(),
3939
undefinedVar = astUtils.getVariableByName(scope, "undefined"),
40-
shadowed = undefinedVar && undefinedVar.defs.length > 0;
40+
shadowed = undefinedVar && undefinedVar.defs.length > 0,
41+
lastToken = sourceCode.getLastToken(node);
4142

4243
if (init === "undefined" && node.parent.kind !== "const" && !shadowed) {
4344
context.report({
@@ -54,6 +55,11 @@ module.exports = {
5455
// Don't fix destructuring assignment to `undefined`.
5556
return null;
5657
}
58+
59+
if (sourceCode.commentsExistBetween(node.id, lastToken)) {
60+
return null;
61+
}
62+
5763
return fixer.removeRange([node.id.range[1], node.range[1]]);
5864
}
5965
});

tests/lib/rules/no-undef-init.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,62 @@ ruleTester.run("no-undef-init", rule, {
9292
output: "for(var i in [1,2,3]){let a; for(var j in [1,2,3]){}}",
9393
parserOptions: { ecmaVersion: 6 },
9494
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
95+
},
96+
97+
// Should not autofix if it would remove comments
98+
{
99+
code: "let /* comment */a = undefined;",
100+
output: "let /* comment */a;",
101+
parserOptions: { ecmaVersion: 6 },
102+
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
103+
},
104+
{
105+
code: "let a/**/ = undefined;",
106+
output: null,
107+
parserOptions: { ecmaVersion: 6 },
108+
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
109+
},
110+
{
111+
code: "let a /**/ = undefined;",
112+
output: null,
113+
parserOptions: { ecmaVersion: 6 },
114+
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
115+
},
116+
{
117+
code: "let a//\n= undefined;",
118+
output: null,
119+
parserOptions: { ecmaVersion: 6 },
120+
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
121+
},
122+
{
123+
code: "let a = /**/undefined;",
124+
output: null,
125+
parserOptions: { ecmaVersion: 6 },
126+
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
127+
},
128+
{
129+
code: "let a = //\nundefined;",
130+
output: null,
131+
parserOptions: { ecmaVersion: 6 },
132+
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
133+
},
134+
{
135+
code: "let a = undefined/* comment */;",
136+
output: "let a/* comment */;",
137+
parserOptions: { ecmaVersion: 6 },
138+
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
139+
},
140+
{
141+
code: "let a = undefined/* comment */, b;",
142+
output: "let a/* comment */, b;",
143+
parserOptions: { ecmaVersion: 6 },
144+
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
145+
},
146+
{
147+
code: "let a = undefined//comment\n, b;",
148+
output: "let a//comment\n, b;",
149+
parserOptions: { ecmaVersion: 6 },
150+
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
95151
}
96152
]
97153
});

0 commit comments

Comments
 (0)