版本

no-magic-numbers

禁止魔術數字

❄️ 已凍結

此規則目前為凍結狀態,且不接受功能請求。

「魔術數字」是指在程式碼中多次出現但沒有明確含義的數字。最好將它們替換為命名的常數。

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

規則詳細資訊

no-magic-numbers 規則旨在透過確保特殊數字被宣告為常數以使其含義明確,來使程式碼更具可讀性並簡化重構。

此規則的不正確程式碼範例

在 Playground 中開啟
/*eslint no-magic-numbers: "error"*/

const dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * 0.25);
在 Playground 中開啟
/*eslint no-magic-numbers: "error"*/

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

const dataLast = data[2];
在 Playground 中開啟
/*eslint no-magic-numbers: "error"*/

let SECONDS;

SECONDS = 60;

此規則的正確程式碼範例

在 Playground 中開啟
/*eslint no-magic-numbers: "error"*/

const TAX = 0.25;

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

選項

ignore

要忽略的數字陣列。預設設定為 []。如果提供,則必須是 Array

陣列可以包含 numberstring 類型的值。 如果它是字串,則文字必須解析為 bigint 字面值(例如,"100n")。

範例 { "ignore": [1] } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-magic-numbers: ["error", { "ignore": [1] }]*/

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

範例 { "ignore": ["1n"] } 選項的正確程式碼範例

在 Playground 中開啟
/*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 } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/

const 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 } 選項的不正確程式碼範例

在 Playground 中開啟
/*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

{ "ignoreDefaultValues": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-magic-numbers: ["error", { "ignoreDefaultValues": true }]*/

const { tax = 0.25 } = accountancy;

function mapParallel(concurrency = 3) { /***/ }
在 Playground 中開啟
/*eslint no-magic-numbers: ["error", { "ignoreDefaultValues": true }]*/

let head;
[head = 100] = []

ignoreClassFieldInitialValues

一個布林值,用於指定在類別欄位的初始值中使用的數字是否被認為可以接受。預設為 false

{ "ignoreClassFieldInitialValues": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-magic-numbers: ["error", { "ignoreClassFieldInitialValues": true }]*/

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

{ "ignoreClassFieldInitialValues": true } 選項的不正確程式碼範例

在 Playground 中開啟
/*eslint no-magic-numbers: ["error", { "ignoreClassFieldInitialValues": true }]*/

class C {
    foo = 2 + 3;
}

class D {
    2;
}

enforceConst

一個布林值,用於指定我們是否應該檢查數字變數宣告中的 const 關鍵字。預設為 false

{ "enforceConst": true } 選項的不正確程式碼範例

在 Playground 中開啟
/*eslint no-magic-numbers: ["error", { "enforceConst": true }]*/

let TAX = 0.25;

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

detectObjects

一個布林值,用於指定我們是否應該在設定物件屬性時偵測數字。預設為 false

{ "detectObjects": true } 選項的不正確程式碼範例

在 Playground 中開啟
/*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/

const magic = {
  tax: 0.25
};

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

{ "detectObjects": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/

const TAX = 0.25;

const magic = {
  tax: TAX
};

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

版本

此規則在 ESLint v1.7.0 中引入。

資源

變更語言