Skip to content

useParseIntRadix

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"correctness": {
"useParseIntRadix": "error"
}
}
}
}

¥Description

强制要求在使用 parseInt() 时始终使用基数参数。

¥Enforce the consistent use of the radix argument when using parseInt().

使用 parseInt() 函数时,通常会省略第二个参数(基数),让函数尝试根据第一个参数确定数字类型。默认情况下,parseInt() 会自动检测十进制和十六进制(通过 0x 前缀)。在 ECMAScript 5 之前,parseInt() 还会自动检测八进制字面量,这导致了一些问题,因为许多开发者认为前导的 0 会被忽略。

¥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.

这种冲突导致我们建议你始终使用 parseInt() 的基数参数,以避免意外后果。

¥This confusion led to the suggestion that you always use the radix parameter to parseInt() to eliminate unintended consequences.

¥Examples

¥Invalid

parseInt("071");
code-block.js:1:1 lint/correctness/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/correctness/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/correctness/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/correctness/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/correctness/useParseIntRadix ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This call to parseInt has no arguments, it will always return NaN

> 1 │ parseInt();
^^^^^^^^^^
2 │

Add arguments to this function call

¥Valid

parseInt("071", 10);
parseInt("071", 8);
parseFloat(someValue);

¥Related links