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]; // 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
型: boolean
デフォルト: false
true の場合、オブジェクトのプロパティで使用される数値リテラルはマジックナンバーと見なされます。
enforceConst
型: boolean
デフォルト: false
true の場合、数値定数は let や var ではなく const で宣言されることを強制します。
ignore
型: array
デフォルト: []
マジックナンバーとして使用された場合に無視する数値の配列。小数や BigInt 文字列も含めることができます。
ignore[n]
ignoreArrayIndexes
型: boolean
デフォルト: false
true の場合、配列インデックスとして使用される数値リテラルは無視されます。
ignoreClassFieldInitialValues
型: boolean
デフォルト: false
true の場合、クラスフィールドの初期値として使用される数値リテラルは無視されます。
ignoreDefaultValues
型: boolean
デフォルト: false
true の場合、関数パラメータやデストラクチャリングでのデフォルト値として使用される数値リテラルは無視されます。
ignoreEnums
型: boolean
デフォルト: false
true の場合、TypeScript の列挙型内の数値リテラルは無視されます。
ignoreNumericLiteralTypes
型: boolean
デフォルト: false
true の場合、TypeScript の数値リテラル型として使用される数値リテラルは無視されます。
ignoreReadonlyClassProperties
型: boolean
デフォルト: false
true の場合、読み取り専用クラスプロパティ内の数値リテラルは無視されます。
ignoreTypeIndexes
型: boolean
デフォルト: false
true の場合、TypeScript の型インデックスとして使用される数値リテラルは無視されます。
使用方法
このルールを有効化するには、設定ファイルまたは CLI で次のように使用できます:
{
"rules": {
"no-magic-numbers": "error"
}
}oxlint --deny no-magic-numbers