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

Bug: [prefer-literal-enum-member] allowBitwiseExpressions with self-referential enums does not disable error #7763

Closed
4 tasks done
jbs-marcus opened this issue Oct 19, 2023 · 1 comment · Fixed by #9114
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@jbs-marcus
Copy link

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.2.2&fileType=.ts&code=PTAEDMCcHsFtQBYBckAcDOAuEB3PA6JAT1QFN0BjSAS1SQBsBDAOwHN9pJXgATaC9MAQseAI2jQA1sFLMArrHT5ksegChZC0ADFq9UgEEKFculABvNaFAA5aM1IAaK6ABKpRj1ABeUAEZQAB5A-2drAHUaJFIffyCQgCYwtw8eSOpo2PdPUAAfUHTo5wBfIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0Wlsfy8AhvHpNYAW1oTEEgEa90YANpRe0APbRIAGkxQR8DQHcAQpXzHKyRAFEAHhxTJKGpqgz44iMAF8wALrgfiC%2BQA&tsconfig=&tokens=false

Repro Code

// from https://www.typescriptlang.org/docs/handbook/enums.html
enum FileAccess {
  None,
  Read = 1 << 1,
  Write = 1 << 2,
  ReadWrite = Read | Write,
}

ESLint Config

{
  "rules": {
    "@typescript-eslint/prefer-literal-enum-member": [
      "error", { "allowBitwiseExpressions": true }
    ]
  }
}

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

There should be no reported error. This is a fairly normal use-case for enums and bitwise operators. The official ts docs even have it as an example.

I would like a way to only allow literal enums, but also allow bitwise in this use case. I can get the code to pass by changing it to, but I feel it makes things less clear:

enum FileAccess {
  None,
  Read = 1 << 1, // 0b0010
  Write = 1 << 2, // 0b0100
  ReadWrite = 0b0010 | 0b0100,
}

Actual Result

Recieve error:

@typescript-eslint/prefer-literal-enum-member:
Explicit enum value must only be a literal value (string, number, boolean, etc). 6:3 - 6:12

Additional Info

In the actual code I an writing, I am extending "plugin:@typescript-eslint/strict-type-checked"

@jbs-marcus jbs-marcus added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Oct 19, 2023
@JoshuaKGoldberg
Copy link
Member

Makes sense. I think as long as the values refer to a previous value in the enum, then they should be allowed.

Aside: I'm always curious about this 😄 when an issue is filed and within an hour there's >1 👍... was there a Twitch stream or meetup or something?

Screenshot of two 👍 reactions

(not complaining, honestly more happy than anything that folks are getting involved in the issue tracker! ❤️ )

@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Oct 19, 2023
jbs-marcus pushed a commit to jbs-marcus/typescript-eslint that referenced this issue Oct 20, 2023
@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Jun 9, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 9, 2024
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 bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants