Skip to content

Commit

Permalink
Update: Add automated suggestion to radix rule for parsing decimals (
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Apr 30, 2021
1 parent 0b6a3f3 commit f071d1e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
22 changes: 19 additions & 3 deletions lib/rules/radix.js
Expand Up @@ -82,7 +82,8 @@ module.exports = {
description: "enforce the consistent use of the radix argument when using `parseInt()`",
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/radix"
url: "https://eslint.org/docs/rules/radix",
suggestion: true
},

schema: [
Expand All @@ -95,7 +96,8 @@ module.exports = {
missingParameters: "Missing parameters.",
redundantRadix: "Redundant radix parameter.",
missingRadix: "Missing radix parameter.",
invalidRadix: "Invalid radix parameter, must be an integer between 2 and 36."
invalidRadix: "Invalid radix parameter, must be an integer between 2 and 36.",
addRadixParameter10: "Add radix parameter `10` for parsing decimal numbers."
}
},

Expand Down Expand Up @@ -123,7 +125,21 @@ module.exports = {
if (mode === MODE_ALWAYS) {
context.report({
node,
messageId: "missingRadix"
messageId: "missingRadix",
suggest: [
{
messageId: "addRadixParameter10",
fix(fixer) {
const sourceCode = context.getSourceCode();
const tokens = sourceCode.getTokens(node);
const lastToken = tokens[tokens.length - 1]; // Parenthesis.
const secondToLastToken = tokens[tokens.length - 2]; // May or may not be a comma.
const hasTrailingComma = secondToLastToken.type === "Punctuator" && secondToLastToken.value === ",";

return fixer.insertTextBefore(lastToken, hasTrailingComma ? " 10," : ", 10");
}
}
]
});
}
break;
Expand Down
64 changes: 58 additions & 6 deletions tests/lib/rules/radix.js
Expand Up @@ -76,7 +76,34 @@ ruleTester.run("radix", rule, {
code: "parseInt(\"10\");",
errors: [{
messageId: "missingRadix",
type: "CallExpression"
type: "CallExpression",
suggestions: [{ messageId: "addRadixParameter10", output: "parseInt(\"10\", 10);" }]
}]
},
{
code: "parseInt(\"10\",);", // Function parameter with trailing comma
parserOptions: { ecmaVersion: 2017 },
errors: [{
messageId: "missingRadix",
type: "CallExpression",
suggestions: [{ messageId: "addRadixParameter10", output: "parseInt(\"10\", 10,);" }]
}]
},
{
code: "parseInt((0, \"10\"));", // Sequence expression (no trailing comma).
errors: [{
messageId: "missingRadix",
type: "CallExpression",
suggestions: [{ messageId: "addRadixParameter10", output: "parseInt((0, \"10\"), 10);" }]
}]
},
{
code: "parseInt((0, \"10\"),);", // Sequence expression (with trailing comma).
parserOptions: { ecmaVersion: 2017 },
errors: [{
messageId: "missingRadix",
type: "CallExpression",
suggestions: [{ messageId: "addRadixParameter10", output: "parseInt((0, \"10\"), 10,);" }]
}]
},
{
Expand Down Expand Up @@ -154,7 +181,8 @@ ruleTester.run("radix", rule, {
code: "Number.parseInt(\"10\");",
errors: [{
messageId: "missingRadix",
type: "CallExpression"
type: "CallExpression",
suggestions: [{ messageId: "addRadixParameter10", output: "Number.parseInt(\"10\", 10);" }]
}]
},
{
Expand Down Expand Up @@ -191,22 +219,46 @@ ruleTester.run("radix", rule, {
{
code: "parseInt?.(\"10\");",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "missingRadix" }]
errors: [
{
messageId: "missingRadix",
type: "CallExpression",
suggestions: [{ messageId: "addRadixParameter10", output: "parseInt?.(\"10\", 10);" }]
}
]
},
{
code: "Number.parseInt?.(\"10\");",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "missingRadix" }]
errors: [
{
messageId: "missingRadix",
type: "CallExpression",
suggestions: [{ messageId: "addRadixParameter10", output: "Number.parseInt?.(\"10\", 10);" }]
}
]
},
{
code: "Number?.parseInt(\"10\");",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "missingRadix" }]
errors: [
{
messageId: "missingRadix",
type: "CallExpression",
suggestions: [{ messageId: "addRadixParameter10", output: "Number?.parseInt(\"10\", 10);" }]
}
]
},
{
code: "(Number?.parseInt)(\"10\");",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "missingRadix" }]
errors: [
{
messageId: "missingRadix",
type: "CallExpression",
suggestions: [{ messageId: "addRadixParameter10", output: "(Number?.parseInt)(\"10\", 10);" }]
}
]
}
]
});

0 comments on commit f071d1e

Please sign in to comment.