Skip to content
Permalink
Browse files

Fix: no-extra-label autofix removes comments (#12298)

  • Loading branch information
mdjermanovic authored and platinumazure committed Sep 29, 2019
1 parent acec201 commit 6ad7e864303e56a39c89569d50c6caf80752ee21
Showing with 51 additions and 1 deletion.
  1. +9 −1 lib/rules/no-extra-label.js
  2. +42 −0 tests/lib/rules/no-extra-label.js
@@ -116,7 +116,15 @@ module.exports = {
node: labelNode,
messageId: "unexpected",
data: labelNode,
fix: fixer => fixer.removeRange([sourceCode.getFirstToken(node).range[1], labelNode.range[1]])
fix(fixer) {
const breakOrContinueToken = sourceCode.getFirstToken(node);

if (sourceCode.commentsExistBetween(breakOrContinueToken, labelNode)) {
return null;
}

return fixer.removeRange([breakOrContinueToken.range[1], labelNode.range[1]]);
}
});
}
return;
@@ -106,6 +106,48 @@ ruleTester.run("no-extra-label", rule, {
}
`,
errors: [{ messageId: "unexpected", data: { name: "A" }, type: "Identifier", line: 2 }]
},

// Should not autofix if it would remove comments
{
code: "A: while(true) { /*comment*/break A; }",
output: "A: while(true) { /*comment*/break; }",
errors: [{ messageId: "unexpected", data: { name: "A" } }]
},
{
code: "A: while(true) { break/**/ A; }",
output: null,
errors: [{ messageId: "unexpected", data: { name: "A" } }]
},
{
code: "A: while(true) { continue /**/ A; }",
output: null,
errors: [{ messageId: "unexpected", data: { name: "A" } }]
},
{
code: "A: while(true) { break /**/A; }",
output: null,
errors: [{ messageId: "unexpected", data: { name: "A" } }]
},
{
code: "A: while(true) { continue/**/A; }",
output: null,
errors: [{ messageId: "unexpected", data: { name: "A" } }]
},
{
code: "A: while(true) { continue A/*comment*/; }",
output: "A: while(true) { continue/*comment*/; }",
errors: [{ messageId: "unexpected", data: { name: "A" } }]
},
{
code: "A: while(true) { break A//comment\n }",
output: "A: while(true) { break//comment\n }",
errors: [{ messageId: "unexpected", data: { name: "A" } }]
},
{
code: "A: while(true) { break A/*comment*/\nfoo() }",
output: "A: while(true) { break/*comment*/\nfoo() }",
errors: [{ messageId: "unexpected", data: { name: "A" } }]
}
]
});

0 comments on commit 6ad7e86

Please sign in to comment.
You can’t perform that action at this time.