From dbe357de199620675446464f6fd0e35064c4d247 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Fri, 20 Mar 2020 13:05:41 +0900 Subject: [PATCH] Fix: check template literal in prefer-numeric-literals (fixes #13045) (#13046) * Fix: check template literal in prefer-numeric-literals (fixes #13045) * remove useless parserOptions --- docs/rules/prefer-numeric-literals.md | 1 + lib/rules/prefer-numeric-literals.js | 6 +-- tests/lib/rules/prefer-numeric-literals.js | 47 ++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/docs/rules/prefer-numeric-literals.md b/docs/rules/prefer-numeric-literals.md index 7f4dac1ce1b..bf81e5ec336 100644 --- a/docs/rules/prefer-numeric-literals.md +++ b/docs/rules/prefer-numeric-literals.md @@ -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; diff --git a/lib/rules/prefer-numeric-literals.js b/lib/rules/prefer-numeric-literals.js index c352d88dc07..2a4fb5d954a 100644 --- a/lib/rules/prefer-numeric-literals.js +++ b/lib/rules/prefer-numeric-literals.js @@ -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) diff --git a/tests/lib/rules/prefer-numeric-literals.js b/tests/lib/rules/prefer-numeric-literals.js index 4d3037f4469..d8ce4117140 100644 --- a/tests/lib/rules/prefer-numeric-literals.js +++ b/tests/lib/rules/prefer-numeric-literals.js @@ -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 } @@ -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 } @@ -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 { @@ -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,