no-magic-numbers

禁止幻数

‘神奇数字’ 是在代码中多次出现但没有明确含义的数字。最好用命名常量替换它们。

¥’Magic numbers’ are numbers that occur multiple times in code without an explicit meaning. They should preferably be replaced by named constants.

var now = Date.now(),
    inOneHour = now + (60 * 60 * 1000);

规则详情

¥Rule Details

no-magic-numbers 规则旨在通过确保将特殊数字声明为常量以使其含义明确,从而使代码更具可读性和重构更容易。

¥The no-magic-numbers rule aims to make code more readable and refactoring easier by ensuring that special numbers are declared as constants to make their meaning explicit.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-magic-numbers: "error"*/

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * 0.25);
在线运行
/*eslint no-magic-numbers: "error"*/

var data = ['foo', 'bar', 'baz'];

var dataLast = data[2];
在线运行
/*eslint no-magic-numbers: "error"*/

var SECONDS;

SECONDS = 60;

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-magic-numbers: "error"*/

var TAX = 0.25;

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * TAX);

选项

¥Options

ignore

要忽略的数字数组。默认设置为 []。如果提供,它必须是 Array

¥An array of numbers to ignore. It’s set to [] by default. If provided, it must be an Array.

该数组可以包含 numberstring 类型的值。如果是字符串,则必须将文本解析为 bigint 字面(例如 "100n")。

¥The array can contain values of number and string types. If it’s a string, the text must be parsed as bigint literal (e.g., "100n").

示例 { "ignore": [1] } 选项的正确代码示例:

¥Examples of correct code for the sample { "ignore": [1] } option:

在线运行
/*eslint no-magic-numbers: ["error", { "ignore": [1] }]*/

var data = ['foo', 'bar', 'baz'];
var dataLast = data.length && data[data.length - 1];

示例 { "ignore": ["1n"] } 选项的正确代码示例:

¥Examples of correct code for the sample { "ignore": ["1n"] } option:

在线运行
/*eslint no-magic-numbers: ["error", { "ignore": ["1n"] }]*/

foo(1n);

ignoreArrayIndexes

一个布尔值,用于指定在数组索引的上下文中使用的数字(例如 data[2])是否被认为是可以的。默认为 false

¥A boolean to specify if numbers used in the context of array indexes (e.g., data[2]) are considered okay. false by default.

此选项仅允许有效的数组索引:将被强制转换为 "0""1""2" 之一的数字…"4294967294"

¥This option allows only valid array indexes: numbers that will be coerced to one of "0", "1", "2""4294967294".

数组是对象,因此它们可以具有属性名称,例如 "-1""2.5"。但是,这些只是不代表数组元素的 “normal” 对象属性。它们不会影响数组的 length,并且会被 .map.forEach 等数组方法忽略。

¥Arrays are objects, so they can have property names such as "-1" or "2.5". However, those are just “normal” object properties that don’t represent array elements. They don’t influence the array’s length, and they are ignored by array methods like .map or .forEach.

此外,由于 数组长度 的最大值为 232 - 1、所有大于 232的值 - 2 也只代表普通的属性名称,因此不被视为数组索引。

¥Additionally, since the maximum array length is 232 - 1, all values above 232 - 2 also represent just normal property names and are thus not considered to be array indexes.

{ "ignoreArrayIndexes": true } 选项的正确代码示例:

¥Examples of correct code for the { "ignoreArrayIndexes": true } option:

在线运行
/*eslint no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/

var item = data[2];

data[100] = a;

f(data[0]);

a = data[-0]; // same as data[0], -0 will be coerced to "0"

a = data[0xAB];

a = data[5.6e1];

a = data[10n]; // same as data[10], 10n will be coerced to "10"

a = data[4294967294]; // max array index

{ "ignoreArrayIndexes": true } 选项的错误代码示例:

¥Examples of incorrect code for the { "ignoreArrayIndexes": true } option:

在线运行
/*eslint no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/

f(2); // not used as array index

a = data[-1];

a = data[2.5];

a = data[5.67e1];

a = data[-10n];

a = data[4294967295]; // above the max array index

a = data[1e500]; // same as data["Infinity"]

ignoreDefaultValues

一个布尔值,用于指定默认值分配中使用的数字是否被认为是可以的。默认为 false

¥A boolean to specify if numbers used in default value assignments are considered okay. false by default.

{ "ignoreDefaultValues": true } 选项的正确代码示例:

¥Examples of correct code for the { "ignoreDefaultValues": true } option:

在线运行
/*eslint no-magic-numbers: ["error", { "ignoreDefaultValues": true }]*/

const { tax = 0.25 } = accountancy;

function mapParallel(concurrency = 3) { /***/ }
在线运行
/*eslint no-magic-numbers: ["error", { "ignoreDefaultValues": true }]*/

let head;
[head = 100] = []

ignoreClassFieldInitialValues

一个布尔值,用于指定用作类字段初始值的数字是否被认为是正确的。默认为 false

¥A boolean to specify if numbers used as initial values of class fields are considered okay. false by default.

{ "ignoreClassFieldInitialValues": true } 选项的正确代码示例:

¥Examples of correct code for the { "ignoreClassFieldInitialValues": true } option:

在线运行
/*eslint no-magic-numbers: ["error", { "ignoreClassFieldInitialValues": true }]*/

class C {
    foo = 2;
    bar = -3;
    #baz = 4;
    static qux = 5;
}

{ "ignoreClassFieldInitialValues": true } 选项的错误代码示例:

¥Examples of incorrect code for the { "ignoreClassFieldInitialValues": true } option:

在线运行
/*eslint no-magic-numbers: ["error", { "ignoreClassFieldInitialValues": true }]*/

class C {
    foo = 2 + 3;
}

class D {
    2;
}

enforceConst

一个布尔值,用于指定我们是否应该在数字的变量声明中检查 const 关键字。默认为 false

¥A boolean to specify if we should check for the const keyword in variable declaration of numbers. false by default.

{ "enforceConst": true } 选项的错误代码示例:

¥Examples of incorrect code for the { "enforceConst": true } option:

在线运行
/*eslint no-magic-numbers: ["error", { "enforceConst": true }]*/

var TAX = 0.25;

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * TAX);

detectObjects

一个布尔值,用于指定我们是否应该在设置对象属性时检测数字。默认为 false

¥A boolean to specify if we should detect numbers when setting object properties for example. false by default.

{ "detectObjects": true } 选项的错误代码示例:

¥Examples of incorrect code for the { "detectObjects": true } option:

在线运行
/*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/

var magic = {
  tax: 0.25
};

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * magic.tax);

{ "detectObjects": true } 选项的正确代码示例:

¥Examples of correct code for the { "detectObjects": true } option:

在线运行
/*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/

var TAX = 0.25;

var magic = {
  tax: TAX
};

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * magic.tax);

版本

此规则是在 ESLint v1.7.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号