Skip to content

Commit

Permalink
Fix: check template literal in prefer-numeric-literals (fixes #13045) (
Browse files Browse the repository at this point in the history
…#13046)

* Fix: check template literal in prefer-numeric-literals (fixes #13045)

* remove useless parserOptions
  • Loading branch information
yeonjuan committed Mar 20, 2020
1 parent 2260611 commit dbe357d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/rules/prefer-numeric-literals.md
Expand Up @@ -17,6 +17,7 @@ Examples of **incorrect** code for this rule:
/*eslint prefer-numeric-literals: "error"*/

parseInt("111110111", 2) === 503;
parseInt(`111110111`, 2) === 503;
parseInt("767", 8) === 503;
parseInt("1F7", 16) === 503;
Number.parseInt("111110111", 2) === 503;
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/prefer-numeric-literals.js
Expand Up @@ -79,13 +79,13 @@ module.exports = {

"CallExpression[arguments.length=2]"(node) {
const [strNode, radixNode] = node.arguments,
str = strNode.value,
str = astUtils.getStaticStringValue(strNode),
radix = radixNode.value;

if (
strNode.type === "Literal" &&
str !== null &&
astUtils.isStringLiteral(strNode) &&
radixNode.type === "Literal" &&
typeof str === "string" &&
typeof radix === "number" &&
radixMap.has(radix) &&
isParseInt(node.callee)
Expand Down
47 changes: 47 additions & 0 deletions tests/lib/rules/prefer-numeric-literals.js
Expand Up @@ -37,6 +37,8 @@ ruleTester.run("prefer-numeric-literals", rule, {
"parseInt(1e5, 16);",
"parseInt('11', '2');",
"Number.parseInt('11', '8');",
"parseInt(/foo/, 2);",
"parseInt(`11${foo}`, 2);",
{
code: "parseInt('11', 2n);",
parserOptions: { ecmaVersion: 2020 }
Expand All @@ -49,6 +51,10 @@ ruleTester.run("prefer-numeric-literals", rule, {
code: "parseInt('11', 16n);",
parserOptions: { ecmaVersion: 2020 }
},
{
code: "parseInt(`11`, 16n);",
parserOptions: { ecmaVersion: 2020 }
},
{
code: "parseInt(1n, 2);",
parserOptions: { ecmaVersion: 2020 }
Expand Down Expand Up @@ -112,6 +118,42 @@ ruleTester.run("prefer-numeric-literals", rule, {
output: null, // not fixed, javascript doesn't support emoji literals
errors: [{ message: "Use hexadecimal literals instead of Number.parseInt()." }]
},
{
code: "parseInt(`111110111`, 2) === 503;",
output: "0b111110111 === 503;",
errors: [{ message: "Use binary literals instead of parseInt()." }]
}, {
code: "parseInt(`767`, 8) === 503;",
output: "0o767 === 503;",
errors: [{ message: "Use octal literals instead of parseInt()." }]
}, {
code: "parseInt(`1F7`, 16) === 255;",
output: "0x1F7 === 255;",
errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
},
{
code: "parseInt('', 8);",
output: null, // not fixed, it's empty string
errors: [{ message: "Use octal literals instead of parseInt()." }]
},
{
code: "parseInt(``, 8);",
output: null, // not fixed, it's empty string
errors: [{ message: "Use octal literals instead of parseInt()." }]
},
{
code: "parseInt(`7999`, 8);",
output: null, // not fixed, unexpected 9 in parseInt string
errors: [{ message: "Use octal literals instead of parseInt()." }]
}, {
code: "parseInt(`1234`, 2);",
output: null, // not fixed, invalid binary string
errors: [{ message: "Use binary literals instead of parseInt()." }]
}, {
code: "parseInt(`1234.5`, 8);",
output: null, // not fixed, this isn't an integer
errors: [{ message: "Use octal literals instead of parseInt()." }]
},

// Adjacent tokens tests
{
Expand Down Expand Up @@ -262,6 +304,11 @@ ruleTester.run("prefer-numeric-literals", rule, {
output: null,
errors: 1
},
{
code: "parseInt(`11`/**/, 2);",
output: null,
errors: 1
},
{
code: "parseInt('11', 2 /**/);",
output: null,
Expand Down

0 comments on commit dbe357d

Please sign in to comment.