no-magic-numbers
禁用魔術數字
「魔術數字」是指在程式碼中多次出現,但沒有明確含義的數字。它們最好用具名的常數取代。
var now = Date.now(),
inOneHour = now + (60 * 60 * 1000);
規則詳情
no-magic-numbers
規則旨在透過確保特殊數字被宣告為常數,使其含義明確,從而使程式碼更具可讀性,並更容易重構。
此規則的不正確程式碼範例
/*eslint no-magic-numbers: "error"*/
var dutyFreePrice = 100,
finalPrice = dutyFreePrice + (dutyFreePrice * );
/*eslint no-magic-numbers: "error"*/
var data = ['foo', 'bar', 'baz'];
var dataLast = data[];
/*eslint no-magic-numbers: "error"*/
var SECONDS;
SECONDS = ;
此規則的正確程式碼範例
/*eslint no-magic-numbers: "error"*/
var TAX = 0.25;
var dutyFreePrice = 100,
finalPrice = dutyFreePrice + (dutyFreePrice * TAX);
選項
ignore
要忽略的數字陣列。預設設為 []
。如果提供,則必須是 Array
。
陣列可以包含 number
和 string
類型的值。如果是字串,則必須將文字解析為 bigint
字面值 (例如,"100n"
)。
範例 { "ignore": [1] }
選項的正確程式碼範例
/*eslint no-magic-numbers: ["error", { "ignore": [1] }]*/
var data = ['foo', 'bar', 'baz'];
var dataLast = data.length && data[data.length - 1];
範例 { "ignore": ["1n"] }
選項的正確程式碼範例
/*eslint no-magic-numbers: ["error", { "ignore": ["1n"] }]*/
foo(1n);
ignoreArrayIndexes
一個布林值,用於指定在陣列索引 (例如,data[2]
) 中使用的數字是否被認為可以接受。預設為 false
。
此選項僅允許有效的陣列索引:將強制轉換為 "0"
、"1"
、"2"
… "4294967294"
其中之一的數字。
陣列是物件,因此它們可以具有諸如 "-1"
或 "2.5"
之類的屬性名稱。然而,這些只是不代表陣列元素的「一般」物件屬性。它們不會影響陣列的 length
,並且會被陣列方法 (例如 .map
或 .forEach
) 忽略。
此外,由於最大陣列長度為 232 - 1,因此所有高於 232 - 2 的值也僅代表一般的屬性名稱,因此不被視為陣列索引。
{ "ignoreArrayIndexes": true }
選項的正確程式碼範例
/*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 }
選項的不正確程式碼範例
/*eslint no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/
f(); // not used as array index
a = data[];
a = data[];
a = data[];
a = data[];
a = data[]; // above the max array index
a = data[]; // same as data["Infinity"]
ignoreDefaultValues
一個布林值,用於指定在預設值指派中使用的數字是否被認為可以接受。預設為 false
。
{ "ignoreDefaultValues": true }
選項的正確程式碼範例
/*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
。
{ "ignoreClassFieldInitialValues": true }
選項的正確程式碼範例
/*eslint no-magic-numbers: ["error", { "ignoreClassFieldInitialValues": true }]*/
class C {
foo = 2;
bar = -3;
#baz = 4;
static qux = 5;
}
{ "ignoreClassFieldInitialValues": true }
選項的不正確程式碼範例
/*eslint no-magic-numbers: ["error", { "ignoreClassFieldInitialValues": true }]*/
class C {
foo = + ;
}
class D {
;
}
enforceConst
一個布林值,用於指定是否應檢查數字變數宣告中的 const 關鍵字。預設為 false
。
{ "enforceConst": true }
選項的不正確程式碼範例
/*eslint no-magic-numbers: ["error", { "enforceConst": true }]*/
var TAX = ;
var dutyFreePrice = ,
finalPrice = dutyFreePrice + (dutyFreePrice * TAX);
detectObjects
一個布林值,用於指定是否應在設定物件屬性時偵測數字。預設為 false
。
{ "detectObjects": true }
選項的不正確程式碼範例
/*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/
var magic = {
tax:
};
var dutyFreePrice = 100,
finalPrice = dutyFreePrice + (dutyFreePrice * magic.tax);
{ "detectObjects": true }
選項的正確程式碼範例
/*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 中引入。