Skip to content

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

Closed
@jbs-marcus

Description

@jbs-marcus

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions