Skip to content

Commit

Permalink
Chore: Handle sequence expression in radix suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Apr 29, 2021
1 parent 4f9fd94 commit 933e735
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/rules/radix.js
Expand Up @@ -131,9 +131,12 @@ module.exports = {
messageId: "addRadixParameter10",
fix(fixer) {
const sourceCode = context.getSourceCode();
const argText = sourceCode.getText(node.arguments[0]);
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.replaceText(node.arguments[0], `${argText}, 10`);
return fixer.insertTextBefore(lastToken, hasTrailingComma ? " 10," : ", 10");
}
}
]
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/radix.js
Expand Up @@ -89,6 +89,23 @@ ruleTester.run("radix", rule, {
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,);" }]
}]
},
{
code: "parseInt(\"10\", null);",
errors: [{
Expand Down

0 comments on commit 933e735

Please sign in to comment.