Skip to content

Replace computed field on TSEnumMember with more enum variants on TSEnumMemberName #10332

@overlookmotel

Description

@overlookmotel

#10092 introduced a computed field on TSEnumMember, in order to align with TS-ESLint.

pub struct TSEnumMember<'a> {
pub span: Span,
pub id: TSEnumMemberName<'a>,
pub computed: bool,
pub initializer: Option<Expression<'a>>,
}

I suggest that we remove this field and instead have more variants on TSEnumMemberName:

pub enum TSEnumMemberName<'a> {
    Identifier(Box<'a, IdentifierName<'a>>),
+   ComputedIdentifier(Box<'a, IdentifierName<'a>>),
    String(Box<'a, StringLiteral<'a>>),
+   ComputedString(Box<'a, StringLiteral<'a>>),
-   TemplateString(Box<'a, TemplateLiteral<'a>>),
+   ComputedTemplateString(Box<'a, TemplateLiteral<'a>>),
}

There are 2 reasons I feel this is preferable:

  1. Shrink TSEnumMember by 8 bytes.
  2. Make it impossible to represent an illegal syntax in AST:
enum Foo {
    // Illegal syntax - template strings must be computed
    `X` = 1,
}

computed: false is not valid when member name is a TemplateLiteral.


Note: I had thought that computed Identifiers are also illegal ([A] = 1), but it appears that's a typechecking error, not a parse error.

Both TS and @typescript-eslint/parser accept this syntax when parsing only (type-checking disabled):

TS Playground
TS-ESLint AST explorer

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-astArea - AST

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions