Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📎 Implement noMagicNumbers - eslint/no-magic-numbers, typescript-eslint/no-magic-numbers #4333

Open
Conaclos opened this issue Oct 18, 2024 · 3 comments
Assignees
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Help-wanted Status: you're familiar with the code base and want to help the project

Comments

@Conaclos
Copy link
Member

Description

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?

Options

We could introduce an ignoreIntegers option (turned off by default).
When the option is set to true, all integers values are ignored:

1795;
1795n;
@Conaclos Conaclos added S-Help-wanted Status: you're familiar with the code base and want to help the project A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Oct 18, 2024
@Conaclos
Copy link
Member Author

@fellipeutaka Could you add a comment on this task. I am not able to assign you to the task if you don't participate here.

@fellipeutaka
Copy link

@fellipeutaka Could you add a comment on this task. I am not able to assign you to the task if you don't participate here.

Oh, sorry. I'm ready to contribute to this task :)

@Conaclos
Copy link
Member Author

Conaclos commented Oct 18, 2024

Oh, sorry. I'm ready to contribute to this task :)

No problem. I was not aware of this limitation on GitHub ^^
I guess this is to avoid assigning a wrong person.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Help-wanted Status: you're familiar with the code base and want to help the project
Projects
None yet
Development

No branches or pull requests

2 participants