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

Allow the option to disable magic number warnings within arrays #11383

Closed
mulholo opened this issue Feb 12, 2019 · 3 comments
Closed

Allow the option to disable magic number warnings within arrays #11383

mulholo opened this issue Feb 12, 2019 · 3 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion auto closed The bot closed this issue enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules

Comments

@mulholo
Copy link

mulholo commented Feb 12, 2019

What rule do you want to change?
no-magic-numbers (source, docs)

Does this change cause the rule to produce more or fewer warnings?
Fewer (if enabled as option). No changes by default.

How will the change be implemented? (New option, new default behavior, etc.)?
I imagine this as an extra boolean option, e.g. ignoreArrayElements, that defaults to true (the current behaviour).

You could also specify a max array length. e.g. short arrays (which are more explicit) are ignore by the rule, long arrays are not.

Please provide some example code that this change will affect:

This is the code that made the need for the new option apparent:

  it('returns a number for numerical column', () => {
    // It is clear what these numbers are, yet they are producing an error
    const indexesOfNumericalCells = [12, 13]; // warning on this line ❌ 

    expect(
      stringValuesToNumbers(items)[indexesOfNumericalCells[0]]
    ).toEqual(expect.any(Number));

    expect(
      stringValuesToNumbers(items)[indexesOfNumericalCells[1]]
    ).toEqual(expect.any(Number));
  });

This scenario could be avoided by destructuring the array, but that feels inelegant at the moment. If you had a longer array of indexes that you wanted to loop over this would get even worse:

  it('returns a number for numerical column', () => {
    const indexesOfNumericalCells = [12, 13, 14, 15]; 

    indexesOfNumericalCells.forEach(index => {
      expect(stringValuesToNumbers(items)[index]).toEqual(expect.any(Number))
    });
  });

What does the rule currently do for this code?
Disallows ambiguously specified (magic) numbers from being written without a labelled variable. More specifically for this issue, the rule also raises a warning/error for magic numbers within arrays.

What will the rule do after it's changed?
Disable warnings for magic numbers found within arrays.

Are you willing to submit a pull request to implement this change?
Not unless absolutely necessary

@mulholo mulholo added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules triage An ESLint team member will look at this issue soon labels Feb 12, 2019
@kaicataldo
Copy link
Member

I think this is a very good use case for using inline eslint-disable comments rather than creating a brand new option for this pattern, but let's see what others on the team say.

@nzakas nzakas added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Feb 15, 2019
@nzakas
Copy link
Member

nzakas commented Feb 15, 2019

I agree, @kaicataldo

@eslint-deprecated eslint-deprecated bot added the auto closed The bot closed this issue label Mar 18, 2019
@eslint-deprecated
Copy link

Unfortunately, it looks like there wasn't enough interest from the team
or community to implement this change. While we wish we'd be able to
accommodate everyone's requests, we do need to prioritize. We've found
that issues failing to reach accepted status after 21 days tend to
never be accepted, and as such, we close those issues.
This doesn't mean the idea isn't interesting or useful, just that it's
not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Sep 15, 2019
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Sep 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion auto closed The bot closed this issue enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

3 participants