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 positive with array/tuple type index access #4780

Closed
3 tasks done
davecardwell opened this issue Apr 3, 2022 · 4 comments · Fixed by #4789
Closed
3 tasks done

[no-magic-numbers] False positive with array/tuple type index access #4780

davecardwell opened this issue Apr 3, 2022 · 4 comments · Fixed by #4789
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@davecardwell
Copy link
Contributor

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "@typescript-eslint/no-magic-numbers": [
      "error",
      { "ignoreArrayIndexes": true }
    ],
  }
}
type Foo = ["bar", "baz"];
function foo(bar: string, baz: number) {}

// No magic number: 1. eslint(@typescript-eslint/no-magic-numbers)
type Oof1 = Foo[1];

// No magic number: 1. eslint(@typescript-eslint/no-magic-numbers)
type Oof2 = Parameters<typeof foo>[1];

Expected Result

I expect the array index access to be ignored.

Actual Result

No magic number: 1. eslint(@typescript-eslint/no-magic-numbers)

Additional Info

An alternative would be to add an additional option for this if you don’t think it should be covered under “ignoreArrayIndexes”.

Versions

package version
@typescript-eslint/eslint-plugin 5.17.0
@typescript-eslint/parser 5.17.0
TypeScript 4.6.3
ESLint 8.12.0
node 17.8.0
@davecardwell davecardwell added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Apr 3, 2022
@JoshuaKGoldberg
Copy link
Member

Yeah, I think this makes sense as an opt-out option: ignoring indexes on tuple types such as those from Parameters. Maybe,ignoreTypeIndexes?

@JoshuaKGoldberg JoshuaKGoldberg added enhancement: plugin rule option New rule option for an existing eslint-plugin rule accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Apr 3, 2022
@davecardwell
Copy link
Contributor Author

Thanks @JoshuaKGoldberg. I’ll take a look at creating a PR to add an ignoreTypeIndexes option.

@timdeschryver
Copy link
Contributor

👋 @davecardwell , if you want, I can create a PR for this.
I managed to get this working earlier today but I didn't see your comment until now.
Don't feel obligated though, if you want to contribute that's totally fine.

@davecardwell
Copy link
Contributor Author

👋 @timdeschryver Whoops! I had also done the work after leaving my comment. I finished up the docs change just now and submitted #4789, but I haven’t worked with estree before so would be happy to close that PR in favor of yours if you recommend your approach instead.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants