Skip to content

Commit 49732ff

Browse files
feat(ast)!: Re-introduce TSEnumBody AST node (#10284)
Fixes #10087 --- Follow up for #10017 . This PR eliminates the part where nodes were manually generated for JS-land AST. NOTE: This node was originally present, but was removed at #2509 and is now being reintroduced to get the correct `Span` position for #9705 . --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 5850a0d commit 49732ff

File tree

39 files changed

+492
-303
lines changed

39 files changed

+492
-303
lines changed

crates/oxc_ast/src/ast/ts.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,40 @@ pub struct TSThisParameter<'a> {
6868
#[scope]
6969
#[derive(Debug)]
7070
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
71-
#[estree(add_ts_def = "
72-
interface TSEnumBody extends Span {
73-
type: 'TSEnumBody';
74-
members: TSEnumMember[];
75-
}
76-
")]
7771
pub struct TSEnumDeclaration<'a> {
7872
pub span: Span,
7973
pub id: BindingIdentifier<'a>,
8074
#[scope(enter_before)]
81-
#[estree(rename = "body", via = TSEnumDeclarationBody)]
82-
pub members: Vec<'a, TSEnumMember<'a>>,
75+
pub body: TSEnumBody<'a>,
8376
/// `true` for const enums
8477
pub r#const: bool,
8578
pub declare: bool,
8679
pub scope_id: Cell<Option<ScopeId>>,
8780
}
8881

82+
/// Enum Body
83+
///
84+
/// The body of a [`TSEnumDeclaration`].
85+
///
86+
/// ## Example
87+
/// ```ts
88+
/// enum Foo { A }
89+
/// ^^^^^
90+
/// enum Bar
91+
/// { B }
92+
/// ^^^^^
93+
/// ```
94+
#[ast(visit)]
95+
#[derive(Debug)]
96+
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
97+
pub struct TSEnumBody<'a> {
98+
pub span: Span,
99+
pub members: Vec<'a, TSEnumMember<'a>>,
100+
}
101+
89102
/// Enum Member
90103
///
91-
/// A member property in a [`TSEnumDeclaration`].
104+
/// A member property in a [`TSEnumBody`].
92105
///
93106
/// ## Example
94107
/// ```ts

crates/oxc_ast/src/ast_kind_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl AstKind<'_> {
381381
Self::TSInstantiationExpression(_) => "TSInstantiationExpression".into(),
382382

383383
Self::TSEnumDeclaration(decl) => format!("TSEnumDeclaration({})", &decl.id.name).into(),
384-
384+
Self::TSEnumBody(_) => "TSEnumBody".into(),
385385
Self::TSEnumMember(_) => "TSEnumMember".into(),
386386

387387
Self::TSImportEqualsDeclaration(_) => "TSImportEqualsDeclaration".into(),

crates/oxc_ast/src/generated/assert_layouts.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -911,14 +911,19 @@ const _: () = {
911911
assert!(offset_of!(TSThisParameter, this_span) == 8);
912912
assert!(offset_of!(TSThisParameter, type_annotation) == 16);
913913

914-
assert!(size_of::<TSEnumDeclaration>() == 80);
914+
assert!(size_of::<TSEnumDeclaration>() == 88);
915915
assert!(align_of::<TSEnumDeclaration>() == 8);
916916
assert!(offset_of!(TSEnumDeclaration, span) == 0);
917917
assert!(offset_of!(TSEnumDeclaration, id) == 8);
918-
assert!(offset_of!(TSEnumDeclaration, members) == 40);
919-
assert!(offset_of!(TSEnumDeclaration, r#const) == 72);
920-
assert!(offset_of!(TSEnumDeclaration, declare) == 73);
921-
assert!(offset_of!(TSEnumDeclaration, scope_id) == 76);
918+
assert!(offset_of!(TSEnumDeclaration, body) == 40);
919+
assert!(offset_of!(TSEnumDeclaration, r#const) == 80);
920+
assert!(offset_of!(TSEnumDeclaration, declare) == 81);
921+
assert!(offset_of!(TSEnumDeclaration, scope_id) == 84);
922+
923+
assert!(size_of::<TSEnumBody>() == 40);
924+
assert!(align_of::<TSEnumBody>() == 8);
925+
assert!(offset_of!(TSEnumBody, span) == 0);
926+
assert!(offset_of!(TSEnumBody, members) == 8);
922927

923928
assert!(size_of::<TSEnumMember>() == 40);
924929
assert!(align_of::<TSEnumMember>() == 8);
@@ -2301,14 +2306,19 @@ const _: () = {
23012306
assert!(offset_of!(TSThisParameter, this_span) == 8);
23022307
assert!(offset_of!(TSThisParameter, type_annotation) == 16);
23032308

2304-
assert!(size_of::<TSEnumDeclaration>() == 52);
2309+
assert!(size_of::<TSEnumDeclaration>() == 60);
23052310
assert!(align_of::<TSEnumDeclaration>() == 4);
23062311
assert!(offset_of!(TSEnumDeclaration, span) == 0);
23072312
assert!(offset_of!(TSEnumDeclaration, id) == 8);
2308-
assert!(offset_of!(TSEnumDeclaration, members) == 28);
2309-
assert!(offset_of!(TSEnumDeclaration, r#const) == 44);
2310-
assert!(offset_of!(TSEnumDeclaration, declare) == 45);
2311-
assert!(offset_of!(TSEnumDeclaration, scope_id) == 48);
2313+
assert!(offset_of!(TSEnumDeclaration, body) == 28);
2314+
assert!(offset_of!(TSEnumDeclaration, r#const) == 52);
2315+
assert!(offset_of!(TSEnumDeclaration, declare) == 53);
2316+
assert!(offset_of!(TSEnumDeclaration, scope_id) == 56);
2317+
2318+
assert!(size_of::<TSEnumBody>() == 24);
2319+
assert!(align_of::<TSEnumBody>() == 4);
2320+
assert!(offset_of!(TSEnumBody, span) == 0);
2321+
assert!(offset_of!(TSEnumBody, members) == 8);
23122322

23132323
assert!(size_of::<TSEnumMember>() == 24);
23142324
assert!(align_of::<TSEnumMember>() == 4);

crates/oxc_ast/src/generated/ast_builder.rs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4225,20 +4225,20 @@ impl<'a> AstBuilder<'a> {
42254225
/// ## Parameters
42264226
/// * `span`: The [`Span`] covering this node
42274227
/// * `id`
4228-
/// * `members`
4228+
/// * `body`
42294229
/// * `const`: `true` for const enums
42304230
/// * `declare`
42314231
#[inline]
42324232
pub fn declaration_ts_enum(
42334233
self,
42344234
span: Span,
42354235
id: BindingIdentifier<'a>,
4236-
members: Vec<'a, TSEnumMember<'a>>,
4236+
body: TSEnumBody<'a>,
42374237
r#const: bool,
42384238
declare: bool,
42394239
) -> Declaration<'a> {
42404240
Declaration::TSEnumDeclaration(
4241-
self.alloc_ts_enum_declaration(span, id, members, r#const, declare),
4241+
self.alloc_ts_enum_declaration(span, id, body, r#const, declare),
42424242
)
42434243
}
42444244

@@ -4249,7 +4249,7 @@ impl<'a> AstBuilder<'a> {
42494249
/// ## Parameters
42504250
/// * `span`: The [`Span`] covering this node
42514251
/// * `id`
4252-
/// * `members`
4252+
/// * `body`
42534253
/// * `const`: `true` for const enums
42544254
/// * `declare`
42554255
/// * `scope_id`
@@ -4258,14 +4258,14 @@ impl<'a> AstBuilder<'a> {
42584258
self,
42594259
span: Span,
42604260
id: BindingIdentifier<'a>,
4261-
members: Vec<'a, TSEnumMember<'a>>,
4261+
body: TSEnumBody<'a>,
42624262
r#const: bool,
42634263
declare: bool,
42644264
scope_id: ScopeId,
42654265
) -> Declaration<'a> {
42664266
Declaration::TSEnumDeclaration(
42674267
self.alloc_ts_enum_declaration_with_scope_id(
4268-
span, id, members, r#const, declare, scope_id,
4268+
span, id, body, r#const, declare, scope_id,
42694269
),
42704270
)
42714271
}
@@ -9642,19 +9642,19 @@ impl<'a> AstBuilder<'a> {
96429642
/// ## Parameters
96439643
/// * `span`: The [`Span`] covering this node
96449644
/// * `id`
9645-
/// * `members`
9645+
/// * `body`
96469646
/// * `const`: `true` for const enums
96479647
/// * `declare`
96489648
#[inline]
96499649
pub fn ts_enum_declaration(
96509650
self,
96519651
span: Span,
96529652
id: BindingIdentifier<'a>,
9653-
members: Vec<'a, TSEnumMember<'a>>,
9653+
body: TSEnumBody<'a>,
96549654
r#const: bool,
96559655
declare: bool,
96569656
) -> TSEnumDeclaration<'a> {
9657-
TSEnumDeclaration { span, id, members, r#const, declare, scope_id: Default::default() }
9657+
TSEnumDeclaration { span, id, body, r#const, declare, scope_id: Default::default() }
96589658
}
96599659

96609660
/// Build a [`TSEnumDeclaration`], and store it in the memory arena.
@@ -9665,19 +9665,19 @@ impl<'a> AstBuilder<'a> {
96659665
/// ## Parameters
96669666
/// * `span`: The [`Span`] covering this node
96679667
/// * `id`
9668-
/// * `members`
9668+
/// * `body`
96699669
/// * `const`: `true` for const enums
96709670
/// * `declare`
96719671
#[inline]
96729672
pub fn alloc_ts_enum_declaration(
96739673
self,
96749674
span: Span,
96759675
id: BindingIdentifier<'a>,
9676-
members: Vec<'a, TSEnumMember<'a>>,
9676+
body: TSEnumBody<'a>,
96779677
r#const: bool,
96789678
declare: bool,
96799679
) -> Box<'a, TSEnumDeclaration<'a>> {
9680-
Box::new_in(self.ts_enum_declaration(span, id, members, r#const, declare), self.allocator)
9680+
Box::new_in(self.ts_enum_declaration(span, id, body, r#const, declare), self.allocator)
96819681
}
96829682

96839683
/// Build a [`TSEnumDeclaration`] with `scope_id`.
@@ -9688,7 +9688,7 @@ impl<'a> AstBuilder<'a> {
96889688
/// ## Parameters
96899689
/// * `span`: The [`Span`] covering this node
96909690
/// * `id`
9691-
/// * `members`
9691+
/// * `body`
96929692
/// * `const`: `true` for const enums
96939693
/// * `declare`
96949694
/// * `scope_id`
@@ -9697,19 +9697,12 @@ impl<'a> AstBuilder<'a> {
96979697
self,
96989698
span: Span,
96999699
id: BindingIdentifier<'a>,
9700-
members: Vec<'a, TSEnumMember<'a>>,
9700+
body: TSEnumBody<'a>,
97019701
r#const: bool,
97029702
declare: bool,
97039703
scope_id: ScopeId,
97049704
) -> TSEnumDeclaration<'a> {
9705-
TSEnumDeclaration {
9706-
span,
9707-
id,
9708-
members,
9709-
r#const,
9710-
declare,
9711-
scope_id: Cell::new(Some(scope_id)),
9712-
}
9705+
TSEnumDeclaration { span, id, body, r#const, declare, scope_id: Cell::new(Some(scope_id)) }
97139706
}
97149707

97159708
/// Build a [`TSEnumDeclaration`] with `scope_id`, and store it in the memory arena.
@@ -9720,7 +9713,7 @@ impl<'a> AstBuilder<'a> {
97209713
/// ## Parameters
97219714
/// * `span`: The [`Span`] covering this node
97229715
/// * `id`
9723-
/// * `members`
9716+
/// * `body`
97249717
/// * `const`: `true` for const enums
97259718
/// * `declare`
97269719
/// * `scope_id`
@@ -9729,17 +9722,27 @@ impl<'a> AstBuilder<'a> {
97299722
self,
97309723
span: Span,
97319724
id: BindingIdentifier<'a>,
9732-
members: Vec<'a, TSEnumMember<'a>>,
9725+
body: TSEnumBody<'a>,
97339726
r#const: bool,
97349727
declare: bool,
97359728
scope_id: ScopeId,
97369729
) -> Box<'a, TSEnumDeclaration<'a>> {
97379730
Box::new_in(
9738-
self.ts_enum_declaration_with_scope_id(span, id, members, r#const, declare, scope_id),
9731+
self.ts_enum_declaration_with_scope_id(span, id, body, r#const, declare, scope_id),
97399732
self.allocator,
97409733
)
97419734
}
97429735

9736+
/// Build a [`TSEnumBody`].
9737+
///
9738+
/// ## Parameters
9739+
/// * `span`: The [`Span`] covering this node
9740+
/// * `members`
9741+
#[inline]
9742+
pub fn ts_enum_body(self, span: Span, members: Vec<'a, TSEnumMember<'a>>) -> TSEnumBody<'a> {
9743+
TSEnumBody { span, members }
9744+
}
9745+
97439746
/// Build a [`TSEnumMember`].
97449747
///
97459748
/// ## Parameters

0 commit comments

Comments
 (0)