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

no-magic-numbers false negatives on invalid array indexes #12845

Closed
mdjermanovic opened this issue Jan 29, 2020 · 4 comments · Fixed by #12851
Closed

no-magic-numbers false negatives on invalid array indexes #12845

mdjermanovic opened this issue Jan 29, 2020 · 4 comments · Fixed by #12851
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@mdjermanovic
Copy link
Member

Tell us about your environment

  • ESLint Version: 7.0.0-alpha.0
  • Node Version: v12.14.0
  • npm Version: v6.13.4

What parser (default, Babel-ESLint, etc.) are you using?

default

Please show your full configuration:

Configuration
module.exports = {
    parserOptions: {
        ecmaVersion: 2020
    },
};

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

/* eslint no-magic-numbers: ["error", { ignoreArrayIndexes: true }] */

a[2.5];

a[-3];

a[10n];
eslint index.js

What did you expect to happen?

When ignoreArrayIndexes is set to false this code has 3 errors, so I would expect the same when this option is set to true.

2.5 and -3 are not array indexes.

Less sure about the bigint since it will indeed get the element at index 10. Still, a bigint there looks more like member access than array index.

What actually happened? Please include the actual, raw output from ESLint.

no errors.

Are you willing to submit a pull request to fix this bug?

Yes.

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Jan 29, 2020
@kaicataldo
Copy link
Member

kaicataldo commented Jan 30, 2020

I think it makes sense to warn on the first two (and assume this is a computed property name on a), but since the BigInt is a valid index, I think that should be ignored.

@kaicataldo kaicataldo added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Jan 30, 2020
@kaicataldo
Copy link
Member

Forgot to mention that I'm not seeing the BigInt example in our demo.

@mdjermanovic
Copy link
Member Author

mdjermanovic commented Jan 30, 2020

Forgot to mention that I'm not seeing the BigInt example in our demo.

I didn't include a demo link because of that, but forgot to mention why.

Online demo is still v.6.8.0 and the rule wasn't checking bigint literals at that point (#12701 was merged later as a breaking change), so the BigInt part of the issue is at the moment observable only in v7.0.0-alpha.0.

I think it makes sense to warn on the first two (and assume this is a computed property name on a), but since the BigInt is a valid index, I think that should be ignored.

Agreed, wasn't really sure about that.

I'll fix the option to have the following behavior: treat all non-negative integer numbers and non-negative bigints as array indexes (assuming that unary minus before the literal is a part of the number/bigint).

@kaicataldo
Copy link
Member

Online demo is still v.6.8.0 and the rule wasn't checking bigint literals at that point (#12701 was merged later as a breaking change), so the BigInt part of the issue is at the moment observable only in v7.0.0-alpha.0.

Ah, right, that makes sense!

I'll fix the option to have the following behavior: treat all non-negative integer numbers and non-negative bigints as array indexes (assuming that unary minus before the literal is a part of the number/bigint).

Sounds good to me.

kaicataldo pushed a commit that referenced this issue Mar 28, 2020
…#12851)

* Update: stricter array index check in no-magic-numbers (fixes #12845)

* Improve docs and comments in the code
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Sep 25, 2020
@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 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants