radix
Require the radix argument of parseInt()
has suggestions
Without a radix, parseInt picks the base from the string, so parseInt("0x10") is 16, while parseInt("0x10", 10) is 0. With input from a user or a file, that's rarely what you want. The second argument says which base you mean.
The rule checks parseInt and Number.parseInt. It also reports a call without arguments, and a radix that can't work: undefined, or a literal that isn't a whole number from 2 to 36.
For a missing radix, the report comes with a suggestion that adds 10.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"radix" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"radix" = ["error", "always"] # "always" or "as-needed"Examples#
example.js4 problems
const page = parseInt(query.page)Missing radix parameter.;
const size = Number.parseInt(query.size)Missing radix parameter.;
const level = parseInt(input, 1)Invalid radix parameter, must be an integer between 2 and 36.;
const depth = parseInt(input, "10")Invalid radix parameter, must be an integer between 2 and 36.;example.jsNo problems
const page = parseInt(query.page, 10);
const color = Number.parseInt(hex, 16);
const value = parseInt(input, base);Options#
| Option | What it does |
|---|---|
"always" (default) | Require the radix. |
"as-needed" | Has no effect, as in ESLint 10. |
example.jswith
"as-needed"1 problemconst page = parseInt(query.page)Missing radix parameter.;If your project has an ESLint config and ESLint 9 or older installed, "as-needed" works like it does there. You can leave the radix out, and a radix of 10 is reported.