Skip to content

Align AST for TSEnumDeclaration with TSInterfaceDeclaration, TSModuleDeclaration, etc #1305

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

Closed
bradzacher opened this issue Dec 4, 2019 · 3 comments · Fixed by #8920
Closed
Labels
accepting prs Go ahead, send a pull request that resolves this issue breaking change This change will require a new major version to be released enhancement New feature or request locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: typescript-estree Issues related to @typescript-eslint/typescript-estree
Milestone

Comments

@bradzacher
Copy link
Member

For each TSEnumDeclaration, we place its members directly on this AST node.

export interface TSEnumDeclaration extends BaseNode {
  type: AST_NODE_TYPES.TSEnumDeclaration;
  id: Identifier;
  members: TSEnumMember[];
  const?: boolean;
  declare?: boolean;
  modifiers?: Modifier[];
  decorators?: Decorator[];
}

For each TSInterfaceDeclaration, we have a separate TSInterfaceBody node, upon which we place the members.

export interface TSInterfaceDeclaration extends BaseNode {
  type: AST_NODE_TYPES.TSInterfaceDeclaration;
  body: TSInterfaceBody;
  id: Identifier;
  typeParameters?: TSTypeParameterDeclaration;
  extends?: ExpressionWithTypeArguments[];
  implements?: ExpressionWithTypeArguments[];
  decorators?: Decorator[];
  abstract?: boolean;
  declare?: boolean;
}

export interface TSInterfaceBody extends BaseNode {
  type: AST_NODE_TYPES.TSInterfaceBody;
  body: TypeElement[];
}

I'm not sure why there's this inconsistency between the two, but I think we should consider aligning the two.

I'm not sure why TSEnumDeclaration was created without a body, but I think that creating a TSEnumBody node seems like the correct thing to do.
Doing so would align it with not only TSInterfaceDeclaration, but also TSModuleDeclaration, ClassDeclaration and FunctionDeclaration.

@bradzacher bradzacher added enhancement New feature or request package: typescript-estree Issues related to @typescript-eslint/typescript-estree breaking change This change will require a new major version to be released labels Dec 4, 2019
@bradzacher bradzacher changed the title Align AST for TSEnumDeclaration and TSInterfaceDeclaration Align AST for TSEnumDeclaration with TSInterfaceDeclaration, TSModuleDeclaration, etc Dec 4, 2019
@armano2
Copy link
Collaborator

armano2 commented Dec 12, 2019

hi, @bradzacher reason behind not using *body node for enum was alignment between babel ast for typescript and typescript itself. Enum structure is created in similar way to Object rather than interface.

id: this.convertChild(node.name),
members: node.members.map(el => this.convertChild(el)),
});

note

Enum is not behaving in exactly same way as object.
http://www.typescriptlang.org/play/?ssl=10&ssc=1&pln=1&pc=1#code/MYewdgzgLgBAZjAvDA7Gg3AKAKZgK4C2MAogIwwDemMN8SMpANDAPQswDKAFgIYAmIAO4QYULthh4ADlOwAnGAAM4igHTVaAc3pxmbeqUwBfHPiLEATJQ0045ZExsxtyXa3YAVLgEsRv0eKSMvJKKqr0aCjGmEA

@bradzacher
Copy link
Member Author

Enum is not behaving in exactly same way as object.

Sure it's not the same as an object, but in your example, it is equivalent to the following:

const f = 777;
function E1() {
    const f = 1; // Shadows the upper `f`.
    const g = f; // = 1
}
function E2() {
    const f1 = 1;
    const g = f; // This is the upper `f`. = 777
}

Which is kinda my point - it behaves more like a new block scope (see functions, or BlockStatements)

alignment between babel ast for typescript and typescript itself

To me it seems like we should look to mirroring what ESTree does, instead of what babel and TS do. Babel and TS use their AST much differently than ESTree.

@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
@JoshuaKGoldberg JoshuaKGoldberg added this to the 7.0.0 milestone Oct 19, 2023
@JoshuaKGoldberg
Copy link
Member

#8920 was merged into the v8 branch. ✅

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 23, 2024
@bradzacher bradzacher 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 Apr 14, 2025
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 breaking change This change will require a new major version to be released enhancement New feature or request locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: typescript-estree Issues related to @typescript-eslint/typescript-estree
Projects
None yet
3 participants