Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add parse support for the "structs" proposal (only non-shared)#17624

Open
JLHwung wants to merge 4 commits intobabel:mainfrom
JLHwung:parse-structs
Open

Add parse support for the "structs" proposal (only non-shared)#17624
JLHwung wants to merge 4 commits intobabel:mainfrom
JLHwung:parse-structs

Conversation

@JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Nov 28, 2025

Q                       A
Fixed Issues? Parse & print the non-shared struct proposed in https://github.com/tc39/proposal-structs
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This PR adds support of the non-shared structs with the following AST design:

interface StructDeclaration <: Declaration {
  type: "StructDeclaration";
  id: Identifier | null;
  superClass: Expression | null;
  body: StructBody;
}
interface StructBody <: Node {
  type: "StructBody";
  body: [ ClassMethod | ClassPrivateMethod | ClassProperty | ClassPrivateProperty | StaticBlock ];
}

The member within the struct body is still class members. I have not yet decided whether we should leave it as-is or rename them from Class* to Struct*.

@JLHwung JLHwung added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Nov 28, 2025
@babel-bot
Copy link
Collaborator

babel-bot commented Nov 28, 2025

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/60773

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 28, 2025

Open in StackBlitz

commit: 49299be

@JLHwung JLHwung force-pushed the parse-structs branch 2 times, most recently from 7ecda07 to 7f6e64d Compare December 1, 2025 21:46
@JLHwung JLHwung marked this pull request as ready for review December 2, 2025 13:51
@nicolo-ribaudo nicolo-ribaudo self-requested a review December 5, 2025 16:34
@@ -0,0 +1,4 @@
@f
struct S {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with disallowing this but allowing accessors

Copy link
Contributor Author

@JLHwung JLHwung Jan 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, and the member decorators are not allowed as well.

IMO accessors are essentially syntax sugar serving for the purpose of decorating otherwise accessor pairs once. They can be decoupled from decorators so I see no reason to ban them in non-shared structs.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :)

@nicolo-ribaudo nicolo-ribaudo changed the title Parse structs Add parse support for the "structs" proposal (only non-shared) Jan 23, 2026
@nicolo-ribaudo
Copy link
Member

@JLHwung could you rebase?

@nicolo-ribaudo nicolo-ribaudo added PR: New Feature (next major) 🚀 A type of pull request used for our changelog categories for next major release and removed PR: New Feature 🚀 A type of pull request used for our changelog categories labels Jan 29, 2026
@nicolo-ribaudo nicolo-ribaudo added this to the v7.29.0 milestone Jan 29, 2026
@JLHwung
Copy link
Contributor Author

JLHwung commented Jan 29, 2026

@JLHwung could you rebase?

Rebased.

Given that this PR only implements the non-shared part of the proposal, I think it'd be better shipped as Babel 8 only, otherwise Babel 7 will come with an incomplete proposal, plus we have not yet implemented the transform part.

WDYT about the AST shape? Currently the member within the struct body is still class members. Do you think it is better stay with class members or rename all Class* members to Struct* members?

@liuxingbaoyu
Copy link
Member

I agree that this only applies to releases in Babel 8.
I'm slightly inclined to rename them.
How does estree handle this?

@JLHwung
Copy link
Contributor Author

JLHwung commented Jan 29, 2026

I agree that this only applies to releases in Babel 8. I'm slightly inclined to rename them. How does estree handle this?

Here is the ESTree proposal: https://github.com/estree/estree/blob/master/experimental/structs.md

ESTree reuses class members because their names do not contain the Class prefix. For Babel an alternative approach is to stick with ESTree's MethodDefinition and PropertyDefinition, but we will have deal with two slightly different member AST.

@nicolo-ribaudo
Copy link
Member

Maybe let's just wait for 8.1, targeting end of march then? So we don't need to rush this.

WDYT about the AST shape? Currently the member within the struct body is still class members. Do you think it is better stay with class members or rename all Class* members to Struct* members?

Do we have transforms that just look for the Class* member nodes, rather than for a whole class, and that would have problem with structs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Needs Docs PR: New Feature (next major) 🚀 A type of pull request used for our changelog categories for next major release Spec: JavaScript Structs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants