Implement eslint/no-magic-numbers and typescript-eslint/no-magic-numbers.
Want to contribute? Lets you know you are interested! We will assign you to the issue to prevent several people to work on the same issue. Don't worry, we can unassign you later if you are no longer interested in the issue! Read our contributing guide and analyzer contributing guide.
See the related discussion for context.
The ESLint rule has many options.
I suggest implementing a single option ignoreIntegers
(See the Options
section) for the time being.
Here are the exceptions we should bake in:
ignore assignment to constant-like variables, i.e. const
, var
, and static class properties
const A = 0.25;
const B = 0.25;
class Foo {
static C = 0.5;
}
Ignore common numbers
We should always ignore 0
/0n
/0.
, and maybe 2
/2n
, 1
/1n
, and -1
/-1n
.
-1; 0; 1; -1n; 0n; 1n;
Should we ignore more integers?
Ignore integer indexing (corresponds setting ESLints' ignoreArrayIndexes
and ignoreTypeIndexes
to true
)
arr[10];
type Foo = Bar[10];
type Baz = Parameters<Foo>[10];
Ignore integer Enum values (corresponds setting ESLint's ignoreEnums
to true
)
enum E {
A = 3;
B = 4;
}
ignore numeric types (corresponds setting ESLint's ignoreNumericLiteralTypes
to true
)
type SmallPrimes = 2 | 3 | 5 | 7 | 11;
function f(x: 100): 100 { return x }
Ignore integer binary operations
const x = 1 << 4;
const x = a & 0xff;
const x = a | 0x0f;
Others?
We could introduce an ignoreIntegers
option (turned off by default).
When the option is set to true, all integers values are ignored:
1795;
1795n;
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too