unicorn/prefer-number-properties Restriction
作用
禁止将 parseInt()、parseFloat()、isNan()、isFinite()、Nan、Infinity 和 -Infinity 作为全局变量使用。
为什么这不好?
ECMAScript 2015 为了保持一致性并稍微改进这些全局对象,将它们移到了 Number 构造函数上。此规则强制使用这些属性,以减少全局变量的使用:
- 使用
Number.parseInt()而不是parseInt() - 使用
Number.parseFloat()而不是parseFloat() - 使用
Number.isNaN()而不是isNaN()(它们的行为略有不同) - 使用
Number.isFinite()而不是isFinite()(它们的行为略有不同) - 使用
Number.NaN而不是NaN - 使用
Number.POSITIVE_INFINITY而不是Infinity - 使用
Number.NEGATIVE_INFINITY而不是-Infinity
示例
此规则的错误代码示例:
javascript
const foo = parseInt("10", 2);
const bar = parseFloat("10.5");此规则的正确代码示例:
javascript
const foo = Number.parseInt("10", 2);
const bar = Number.parseFloat("10.5");配置
此规则接受一个包含以下属性的配置对象:
checkInfinity
type: boolean
default: false
如果设置为 true,则检查是否将 Infinity 和 -Infinity 作为全局变量使用。
checkNaN
type: boolean
default: true
如果设置为 true,则检查是否将 NaN 作为全局变量使用。
如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"unicorn/prefer-number-properties": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"unicorn/prefer-number-properties": "error",
},
});bash
oxlint --deny unicorn/prefer-number-properties版本
此规则在 v0.0.19 中加入。
