useParseIntRadix
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
诊断类别:
lint/correctness/useParseIntRadix¥Diagnostic Category:
lint/correctness/useParseIntRadix -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
¥How to configure
{ "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
Biome v2.1 中文网 - 粤ICP备13048890号