useNumericLiterals
诊断类别:lint/style/useNumericLiterals
¥Diagnostic Category: lint/style/useNumericLiterals
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
-
与以下相同:
prefer-numeric-literals
¥Same as:
prefer-numeric-literals
禁止 parseInt()
和 Number.parseInt()
,转而使用二进制、八进制和十六进制文字
¥Disallow parseInt()
and Number.parseInt()
in favor of binary, octal, and hexadecimal literals
JavaScript 为二进制、八进制和十六进制数字提供文字形式。例如:0b11
、0o77
和 0xff
。使用文字形式可启用静态代码分析并避免不必要的计算。
¥JavaScript provides literal forms for binary, octal, and hexadecimal numbers.
For example: 0b11
, 0o77
, and 0xff
.
Using the literal forms enable static code analysis and avoid unnecessary computations.
¥Examples
¥Invalid
code-block.js:1:1 lint/style/useNumericLiterals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This call to parseInt() can be replaced by a binary literal.
> 1 │ parseInt(“111110111”, 2);
│ ^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Using a literal avoids unnecessary computations.
ℹ Unsafe fix: Use the computed binary literal instead.
1 │ - parseInt(“111110111”,·2);
1 │ + 0b111110111;
2 2 │
code-block.js:1:1 lint/style/useNumericLiterals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This call to Number.parseInt() can be replaced by an octal literal.
> 1 │ Number.parseInt(“767”, 8);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Using a literal avoids unnecessary computations.
ℹ Unsafe fix: Use the computed octal literal instead.
1 │ - Number.parseInt(“767”,·8);
1 │ + 0o767;
2 2 │
code-block.js:1:1 lint/style/useNumericLiterals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This call to Number.parseInt() can be replaced by a hexadecimal literal.
> 1 │ Number.parseInt(“-1f7”, 16);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Using a literal avoids unnecessary computations.
ℹ Unsafe fix: Use the computed hexadecimal literal instead.
1 │ - Number.parseInt(“-1f7”,·16);
1 │ + -0x1f7;
2 2 │
¥Valid
¥Related links