eslint/no-magic-numbers 风格
它的作用
此规则通过确保特殊数字被声明为常量,使代码更易读且便于重构,从而明确其含义。 当前实现不支持数组索引中的 BigInt 数字。
为什么这是问题?
“魔法数字”是指在代码中多次出现但没有明确含义的数字。它们最好替换为命名常量。
示例
以下为 错误 的代码示例:
var dutyFreePrice = 100;
var finalPrice = dutyFreePrice + dutyFreePrice * 0.25;以下为 正确 的代码示例(使用选项 "ignore"):
/*typescript no-magic-numbers: ["error", { "ignore": [1] }]*/
var data = ["foo", "bar", "baz"];
var dataLast = data.length && data[data.length - 1];以下为 正确 的代码示例(使用选项 "ignoreArrayIndexes"):
/*typescript no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/
var item = data[2];
data[100] = a;
f(data[0]);
a = data[-0]; // 与 data[0] 相同,-0 会强制转换为 "0"
a = data[0xab];
a = data[5.6e1];
a = data[4294967294]; // 最大数组索引以下为 正确 的代码示例(使用选项 "ignoreDefaultValues"):
/*typescript no-magic-numbers: ["error", { "ignoreDefaultValues": true }]*/
const { tax = 0.25 } = accountancy;
function mapParallel(concurrency = 3) {
/***/
}以下为 正确 的代码示例(使用选项 "ignoreClassFieldInitialValues"):
/*typescript no-magic-numbers: ["error", { "ignoreClassFieldInitialValues": true }]*/
class C {
foo = 2;
bar = -3;
#baz = 4;
static qux = 5;
}以下为 错误 的代码示例(使用选项 "enforceConst"):
/*typescript no-magic-numbers: ["error", { "enforceConst": true }]*/
var TAX = 0.25;以下为 错误 的代码示例(使用选项 "detectObjects"):
/*typescript no-magic-numbers: ["error", { "detectObjects": true }]*/
var magic = {
tax: 0.25,
};以下为 正确 的代码示例(使用选项 "detectObjects"):
/*typescript no-magic-numbers: ["error", { "detectObjects": true }]*/
var TAX = 0.25;
var magic = {
tax: TAX,
};以下为 正确 的代码示例(使用选项 "ignoreEnums"):
/*typescript no-magic-numbers: ["error", { "ignoreEnums": true }]*/
enum foo {
SECOND = 1000,
}以下为 正确 的代码示例(使用选项 "ignoreNumericLiteralTypes"):
/*typescript no-magic-numbers: ["error", { "ignoreNumericLiteralTypes": true }]*/
type SmallPrimes = 2 | 3 | 5 | 7 | 11;以下为 正确 的代码示例(使用选项 "ignoreReadonlyClassProperties"):
/*typescript no-magic-numbers: ["error", { "ignoreReadonlyClassProperties": true }]*/
class Foo {
readonly A = 1;
readonly B = 2;
public static readonly C = 1;
static readonly D = 1;
}以下为 正确 的代码示例(使用选项 "ignoreTypeIndexes"):
/*typescript no-magic-numbers: ["error", { "ignoreTypeIndexes": true }]*/
type Foo = Bar[0];
type Baz = Parameters<Foo>[2];配置
此规则接受一个包含以下属性的配置对象:
detectObjects
type: boolean
默认值: false
当为 true 时,对象属性中使用的数值字面量被视为魔法数字。
enforceConst
type: boolean
默认值: false
当为 true 时,强制要求数字常量必须使用 const 声明,而非 let 或 var。
ignore
type: array
默认值: []
要忽略的数字列表(如果用作魔法数字)。可包含浮点数或 BigInt 字符串。
ignore[n]
ignoreArrayIndexes
type: boolean
默认值: false
当为 true 时,用作数组索引的数值字面量将被忽略。
ignoreClassFieldInitialValues
type: boolean
默认值: false
当为 true 时,用作类字段初始值的数值字面量将被忽略。
ignoreDefaultValues
type: boolean
默认值: false
当为 true 时,用作函数参数和解构默认值的数值字面量将被忽略。
ignoreEnums
type: boolean
默认值: false
当为 true 时,TypeScript 枚举中的数值字面量将被忽略。
ignoreNumericLiteralTypes
type: boolean
默认值: false
当为 true 时,用作 TypeScript 数值字面量类型的数值字面量将被忽略。
ignoreReadonlyClassProperties
type: boolean
默认值: false
当为 true 时,只读类属性中的数值字面量将被忽略。
ignoreTypeIndexes
type: boolean
默认值: false
当为 true 时,用于索引 TypeScript 类型的数值字面量将被忽略。
如何使用
要通过配置文件或 CLI 启用 此规则,可以使用:
{
"rules": {
"no-magic-numbers": "error"
}
}oxlint --deny no-magic-numbers