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

Update: Add automated suggestion to radix rule for parsing decimal numbers #14291

Merged
merged 1 commit into from Apr 30, 2021
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
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);" }]
}
]
}
]
});