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: check template literal in prefer-numeric-literals (fixes #13045) #13046

Merged
merged 2 commits into from Mar 20, 2020
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
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