useParseIntRadix
Summary
Section titled Summary- Diagnostic Category:
lint/nursery/useParseIntRadix
- This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
radix
- Same as
Description
Section titled DescriptionEnforce the consistent use of the radix argument when using parseInt()
.
When using the parseInt()
function it is common to omit the second argument, the radix, and let the function try to determine from the first argument what type of number it is. By default, parseInt()
will autodetect decimal and hexadecimal (via 0x
prefix). Prior to ECMAScript 5, parseInt()
also autodetected octal literals, which caused problems because many developers assumed a leading 0
would be ignored.
This confusion led to the suggestion that you always use the radix parameter to parseInt()
to eliminate unintended consequences.
Examples
Section titled ExamplesInvalid
Section titled InvalidparseInt("071");
code-block.js:1:1 lint/nursery/useParseIntRadix FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Missing radix parameter
> 1 │ parseInt(“071”);
│ ^^^^^^^^^^^^^^^
2 │
ℹ Add a non-fractional number between 2 and 36
ℹ Unsafe fix: Add a radix of 10
1 │ parseInt(“071”,·10);
│ ++++
parseInt(someValue);
code-block.js:1:1 lint/nursery/useParseIntRadix FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Missing radix parameter
> 1 │ parseInt(someValue);
│ ^^^^^^^^^^^^^^^^^^^
2 │
ℹ Add a non-fractional number between 2 and 36
ℹ Unsafe fix: Add a radix of 10
1 │ parseInt(someValue,·10);
│ ++++
parseInt("071", "abc");
code-block.js:1:1 lint/nursery/useParseIntRadix ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Invalid radix parameter
> 1 │ parseInt(“071”, “abc”);
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Radix must be a non-fractional number between 2 and 36
parseInt("071", 37);
code-block.js:1:1 lint/nursery/useParseIntRadix ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Invalid radix parameter
> 1 │ parseInt(“071”, 37);
│ ^^^^^^^^^^^^^^^^^^^
2 │
ℹ Radix must be a non-fractional number between 2 and 36
parseInt();
code-block.js:1:1 lint/nursery/useParseIntRadix ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This call to parseInt has no arguments, it will always return NaN
> 1 │ parseInt();
│ ^^^^^^^^^^
2 │
ℹ Add arguments to this function call
Valid
Section titled ValidparseInt("071", 10);parseInt("071", 8);parseFloat(someValue);
How to configure
Section titled How to configure{ "linter": { "rules": { "nursery": { "useParseIntRadix": "error" } } }}