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: prefer-numeric-literals autofix removes comments #12313

Merged
merged 1 commit into from Sep 25, 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
4 changes: 4 additions & 0 deletions lib/rules/prefer-numeric-literals.js
Expand Up @@ -96,6 +96,10 @@ module.exports = {
fix(fixer) {
const newPrefix = prefixMap[node.arguments[1].value];

if (sourceCode.getCommentsInside(node).length) {
return null;
}

if (+(newPrefix + node.arguments[0].value) !== parseInt(node.arguments[0].value, node.arguments[1].value)) {

/*
Expand Down
62 changes: 62 additions & 0 deletions tests/lib/rules/prefer-numeric-literals.js
Expand Up @@ -90,6 +90,68 @@ ruleTester.run("prefer-numeric-literals", rule, {
code: "Number.parseInt('1️⃣3️⃣3️⃣7️⃣', 16);",
output: null, // not fixed, javascript doesn't support emoji literals
errors: [{ message: "Use hexadecimal literals instead of Number.parseInt()." }]
},

// Should not autofix if it would remove comments
{
code: "/* comment */Number.parseInt('11', 2);",
output: "/* comment */0b11;",
errors: 1
},
{
code: "Number/**/.parseInt('11', 2);",
output: null,
errors: 1
},
{
code: "Number//\n.parseInt('11', 2);",
output: null,
errors: 1
},
{
code: "Number./**/parseInt('11', 2);",
output: null,
errors: 1
},
{
code: "Number.parseInt(/**/'11', 2);",
output: null,
errors: 1
},
{
code: "Number.parseInt('11', /**/2);",
output: null,
errors: 1
},
{
code: "Number.parseInt('11', 2)/* comment */;",
output: "0b11/* comment */;",
errors: 1
},
{
code: "parseInt/**/('11', 2);",
output: null,
errors: 1
},
{
code: "parseInt(//\n'11', 2);",
output: null,
errors: 1
},
{
code: "parseInt('11'/**/, 2);",
output: null,
errors: 1
},
{
code: "parseInt('11', 2 /**/);",
output: null,
errors: 1
},
{
code: "parseInt('11', 2)//comment\n;",
output: "0b11//comment\n;",
errors: 1
}
]
});