Skip to content

Commit 8a240a7

Browse files
authored
fix: avoid false positives in radix rule for spread arguments (#21044)
1 parent 0b4a73b commit 8a240a7

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎lib/rules/radix.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ module.exports = {
129129
*/
130130
function checkArguments(node) {
131131
const args = node.arguments;
132+
const spreadIndex = args.findIndex(
133+
arg => arg.type === "SpreadElement",
134+
);
135+
136+
if (spreadIndex !== -1 && spreadIndex < 2) {
137+
return;
138+
}
132139

133140
if (args.length === 0) {
134141
context.report({

‎tests/lib/rules/radix.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ ruleTester.run("radix", rule, {
4141
'Number.parseInt("10", ~1);',
4242
'Number.parseInt("10", +radix);',
4343
'Number.parseInt("10", -radix);',
44+
"parseInt(...args);",
45+
"parseInt(...args, 1);",
46+
'parseInt("10", ...args);',
47+
'parseInt("10", 10, ...args);',
48+
"Number.parseInt(...args);",
49+
"Number.parseInt(...args, 1);",
50+
'Number.parseInt("10", ...args);',
51+
'Number.parseInt("10", 10, ...args);',
4452
"parseInt",
4553
"Number.foo();",
4654
"Number[parseInt]();",
@@ -256,6 +264,14 @@ ruleTester.run("radix", rule, {
256264
},
257265
],
258266
},
267+
{
268+
code: 'parseInt("10", 1, ...args);',
269+
errors: [
270+
{
271+
messageId: "invalidRadix",
272+
},
273+
],
274+
},
259275
{
260276
code: "Number.parseInt();",
261277
errors: [
@@ -310,6 +326,14 @@ ruleTester.run("radix", rule, {
310326
},
311327
],
312328
},
329+
{
330+
code: 'Number.parseInt("10", 1, ...args);',
331+
errors: [
332+
{
333+
messageId: "invalidRadix",
334+
},
335+
],
336+
},
313337

314338
// Optional chaining
315339
{

0 commit comments

Comments
 (0)