Skip to content

Docs: [prefer-literal-enum-member] Ensure invalid examples are invalid and remove TS errors as examples #10034

Closed
@kirkwaiblinger

Description

@kirkwaiblinger

Before You File a Documentation Request Please Confirm You Have Done The Following...

Suggested Changes

Looking through the examples in the docs for prefer-literal-enum-member, it's not very clear what behavior is actually banned by the rule, since 2 of the 5 examples in the invalid tab are invalid TS anyway, and 1 is not actually reported by the rule.

Furthermore some of the Valid examples are not valid TS either.

We should remove examples that have invalid TS (unless to point out explicitly "already banned by TS"), and ensure that each invalid case actually does trigger the rule.

I've annotated the docs example (view in playground):

const str = 'Test';

enum Invalid {
  A = str, // Variable assignment

  // BAD - not allowed by TS
  B = {}, // Object assignment

  // BAD - intentionally allowed by the rule
  C = `A template literal string`, // Template literal

  // BAD - not allowed by TS
  D = new Set(1, 2, 3), // Constructor in assignment

  E = 2 + 2, // Expression assignment
}

enum Valid {
  A,
  B = 'TestStr', // A regular string
  C = 4, // A number
  
  // BAD - not allowed by TS
  D = null,

  // BAD - not allowed by TS
  E = /some_regex/,
}

Note when fixing the template literal example,

enum Miscellaneous {
  // Allowed by TS starting in 5.0. Should be used in the Invalid examples instead of the current Template Literal example.
  A = `A template string that interpolates ${str}`,
}

Affected URL(s)

https://typescript-eslint.io/rules/prefer-literal-enum-member/#examples

Additional Info

This issue is just to change the docs, but I think that it would also make sense to spin off another issue to check for and remove if appropriate any unnecessary complexity in the rule related to expressions that are already banned by TS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuedocumentationDocumentation ("docs") that needs adding/updatinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.triageWaiting for team members to take a look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions