diff --git a/lib/rules/radix.js b/lib/rules/radix.js index 84ca40b2817f..d1b67e5e280b 100644 --- a/lib/rules/radix.js +++ b/lib/rules/radix.js @@ -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"); } } ] diff --git a/tests/lib/rules/radix.js b/tests/lib/rules/radix.js index 315f7bac70da..ddb8363c1d9d 100644 --- a/tests/lib/rules/radix.js +++ b/tests/lib/rules/radix.js @@ -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: [{