Navigation Menu

Skip to content

Commit

Permalink
Fix: prefer-numeric-literals autofix removes comments (#12313)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and kaicataldo committed Sep 25, 2019
1 parent 11ae6fc commit f5537b2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
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
}
]
});

0 comments on commit f5537b2

Please sign in to comment.