Skip to content

Commit 6e6c777

Browse files
leaysgurautofix-ci[bot]overlookmotel
authored
refactor(ast): Add TSEnumMemberName variant to replace computed field (oxc-project#10346)
Fixes oxc-project#10332 --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: overlookmotel <[email protected]>
1 parent 08a1d4b commit 6e6c777

File tree

24 files changed

+163
-78
lines changed

24 files changed

+163
-78
lines changed

crates/oxc_ast/src/ast/ts.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,38 @@ pub struct TSEnumBody<'a> {
119119
#[ast(visit)]
120120
#[derive(Debug)]
121121
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
122+
#[estree(
123+
add_fields(computed = TSEnumMemberComputed),
124+
field_order(span, id, computed, initializer),
125+
)]
122126
pub struct TSEnumMember<'a> {
123127
pub span: Span,
124128
pub id: TSEnumMemberName<'a>,
125-
pub computed: bool,
126129
pub initializer: Option<Expression<'a>>,
127130
}
128131

129132
/// TS Enum Member Name
133+
///
134+
/// ## Example
135+
/// ```ts
136+
/// enum ValidEnum {
137+
/// identifier,
138+
/// 'string',
139+
/// ['computed-string'],
140+
/// [`computed-template`],
141+
/// // These are invalid syntax
142+
/// // `template`,
143+
/// // [computedIdentifier],
144+
/// }
145+
/// ```
130146
#[ast(visit)]
131147
#[derive(Debug)]
132148
#[generate_derive(CloneIn, Dummy, TakeIn, GetSpan, GetSpanMut, GetAddress, ContentEq, ESTree)]
133149
pub enum TSEnumMemberName<'a> {
134150
Identifier(Box<'a, IdentifierName<'a>>) = 0,
135151
String(Box<'a, StringLiteral<'a>>) = 1,
136-
TemplateString(Box<'a, TemplateLiteral<'a>>) = 2,
152+
ComputedString(Box<'a, StringLiteral<'a>>) = 2,
153+
ComputedTemplateString(Box<'a, TemplateLiteral<'a>>) = 3,
137154
}
138155

139156
/// TypeScript Type Annotation

crates/oxc_ast/src/ast_impl/ts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ impl<'a> TSEnumMemberName<'a> {
1717
pub fn static_name(&self) -> Atom<'a> {
1818
match self {
1919
Self::Identifier(ident) => ident.name,
20-
Self::String(lit) => lit.value,
21-
Self::TemplateString(template) => template
20+
Self::String(lit) | Self::ComputedString(lit) => lit.value,
21+
Self::ComputedTemplateString(template) => template
2222
.quasi()
2323
.expect("`TSEnumMemberName::TemplateString` should have no substitution and at least one quasi"),
2424
}

crates/oxc_ast/src/generated/assert_layouts.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -925,12 +925,11 @@ const _: () = {
925925
assert!(offset_of!(TSEnumBody, span) == 0);
926926
assert!(offset_of!(TSEnumBody, members) == 8);
927927

928-
assert!(size_of::<TSEnumMember>() == 48);
928+
assert!(size_of::<TSEnumMember>() == 40);
929929
assert!(align_of::<TSEnumMember>() == 8);
930930
assert!(offset_of!(TSEnumMember, span) == 0);
931931
assert!(offset_of!(TSEnumMember, id) == 8);
932-
assert!(offset_of!(TSEnumMember, computed) == 24);
933-
assert!(offset_of!(TSEnumMember, initializer) == 32);
932+
assert!(offset_of!(TSEnumMember, initializer) == 24);
934933

935934
assert!(size_of::<TSEnumMemberName>() == 16);
936935
assert!(align_of::<TSEnumMemberName>() == 8);
@@ -2321,12 +2320,11 @@ const _: () = {
23212320
assert!(offset_of!(TSEnumBody, span) == 0);
23222321
assert!(offset_of!(TSEnumBody, members) == 8);
23232322

2324-
assert!(size_of::<TSEnumMember>() == 28);
2323+
assert!(size_of::<TSEnumMember>() == 24);
23252324
assert!(align_of::<TSEnumMember>() == 4);
23262325
assert!(offset_of!(TSEnumMember, span) == 0);
23272326
assert!(offset_of!(TSEnumMember, id) == 8);
2328-
assert!(offset_of!(TSEnumMember, computed) == 16);
2329-
assert!(offset_of!(TSEnumMember, initializer) == 20);
2327+
assert!(offset_of!(TSEnumMember, initializer) == 16);
23302328

23312329
assert!(size_of::<TSEnumMemberName>() == 8);
23322330
assert!(align_of::<TSEnumMemberName>() == 4);

crates/oxc_ast/src/generated/ast_builder.rs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9748,17 +9748,15 @@ impl<'a> AstBuilder<'a> {
97489748
/// ## Parameters
97499749
/// * `span`: The [`Span`] covering this node
97509750
/// * `id`
9751-
/// * `computed`
97529751
/// * `initializer`
97539752
#[inline]
97549753
pub fn ts_enum_member(
97559754
self,
97569755
span: Span,
97579756
id: TSEnumMemberName<'a>,
9758-
computed: bool,
97599757
initializer: Option<Expression<'a>>,
97609758
) -> TSEnumMember<'a> {
9761-
TSEnumMember { span, id, computed, initializer }
9759+
TSEnumMember { span, id, initializer }
97629760
}
97639761

97649762
/// Build a [`TSEnumMemberName::Identifier`].
@@ -9825,7 +9823,56 @@ impl<'a> AstBuilder<'a> {
98259823
))
98269824
}
98279825

9828-
/// Build a [`TSEnumMemberName::TemplateString`].
9826+
/// Build a [`TSEnumMemberName::ComputedString`].
9827+
///
9828+
/// This node contains a [`StringLiteral`] that will be stored in the memory arena.
9829+
///
9830+
/// ## Parameters
9831+
/// * `span`: Node location in source code
9832+
/// * `value`: The value of the string.
9833+
/// * `raw`: The raw string as it appears in source code.
9834+
#[inline]
9835+
pub fn ts_enum_member_name_computed_string<A>(
9836+
self,
9837+
span: Span,
9838+
value: A,
9839+
raw: Option<Atom<'a>>,
9840+
) -> TSEnumMemberName<'a>
9841+
where
9842+
A: IntoIn<'a, Atom<'a>>,
9843+
{
9844+
TSEnumMemberName::ComputedString(self.alloc_string_literal(span, value, raw))
9845+
}
9846+
9847+
/// Build a [`TSEnumMemberName::ComputedString`] with `lone_surrogates`.
9848+
///
9849+
/// This node contains a [`StringLiteral`] that will be stored in the memory arena.
9850+
///
9851+
/// ## Parameters
9852+
/// * `span`: Node location in source code
9853+
/// * `value`: The value of the string.
9854+
/// * `raw`: The raw string as it appears in source code.
9855+
/// * `lone_surrogates`: The string value contains lone surrogates.
9856+
#[inline]
9857+
pub fn ts_enum_member_name_computed_string_with_lone_surrogates<A>(
9858+
self,
9859+
span: Span,
9860+
value: A,
9861+
raw: Option<Atom<'a>>,
9862+
lone_surrogates: bool,
9863+
) -> TSEnumMemberName<'a>
9864+
where
9865+
A: IntoIn<'a, Atom<'a>>,
9866+
{
9867+
TSEnumMemberName::ComputedString(self.alloc_string_literal_with_lone_surrogates(
9868+
span,
9869+
value,
9870+
raw,
9871+
lone_surrogates,
9872+
))
9873+
}
9874+
9875+
/// Build a [`TSEnumMemberName::ComputedTemplateString`].
98299876
///
98309877
/// This node contains a [`TemplateLiteral`] that will be stored in the memory arena.
98319878
///
@@ -9834,13 +9881,17 @@ impl<'a> AstBuilder<'a> {
98349881
/// * `quasis`
98359882
/// * `expressions`
98369883
#[inline]
9837-
pub fn ts_enum_member_name_template_string(
9884+
pub fn ts_enum_member_name_computed_template_string(
98389885
self,
98399886
span: Span,
98409887
quasis: Vec<'a, TemplateElement<'a>>,
98419888
expressions: Vec<'a, Expression<'a>>,
98429889
) -> TSEnumMemberName<'a> {
9843-
TSEnumMemberName::TemplateString(self.alloc_template_literal(span, quasis, expressions))
9890+
TSEnumMemberName::ComputedTemplateString(self.alloc_template_literal(
9891+
span,
9892+
quasis,
9893+
expressions,
9894+
))
98449895
}
98459896

98469897
/// Build a [`TSTypeAnnotation`].

crates/oxc_ast/src/generated/derive_clone_in.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5746,7 +5746,6 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSEnumMember<'_> {
57465746
TSEnumMember {
57475747
span: CloneIn::clone_in(&self.span, allocator),
57485748
id: CloneIn::clone_in(&self.id, allocator),
5749-
computed: CloneIn::clone_in(&self.computed, allocator),
57505749
initializer: CloneIn::clone_in(&self.initializer, allocator),
57515750
}
57525751
}
@@ -5755,7 +5754,6 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSEnumMember<'_> {
57555754
TSEnumMember {
57565755
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
57575756
id: CloneIn::clone_in_with_semantic_ids(&self.id, allocator),
5758-
computed: CloneIn::clone_in_with_semantic_ids(&self.computed, allocator),
57595757
initializer: CloneIn::clone_in_with_semantic_ids(&self.initializer, allocator),
57605758
}
57615759
}
@@ -5768,8 +5766,11 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSEnumMemberName<'_> {
57685766
match self {
57695767
Self::Identifier(it) => TSEnumMemberName::Identifier(CloneIn::clone_in(it, allocator)),
57705768
Self::String(it) => TSEnumMemberName::String(CloneIn::clone_in(it, allocator)),
5771-
Self::TemplateString(it) => {
5772-
TSEnumMemberName::TemplateString(CloneIn::clone_in(it, allocator))
5769+
Self::ComputedString(it) => {
5770+
TSEnumMemberName::ComputedString(CloneIn::clone_in(it, allocator))
5771+
}
5772+
Self::ComputedTemplateString(it) => {
5773+
TSEnumMemberName::ComputedTemplateString(CloneIn::clone_in(it, allocator))
57735774
}
57745775
}
57755776
}
@@ -5782,9 +5783,12 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSEnumMemberName<'_> {
57825783
Self::String(it) => {
57835784
TSEnumMemberName::String(CloneIn::clone_in_with_semantic_ids(it, allocator))
57845785
}
5785-
Self::TemplateString(it) => {
5786-
TSEnumMemberName::TemplateString(CloneIn::clone_in_with_semantic_ids(it, allocator))
5786+
Self::ComputedString(it) => {
5787+
TSEnumMemberName::ComputedString(CloneIn::clone_in_with_semantic_ids(it, allocator))
57875788
}
5789+
Self::ComputedTemplateString(it) => TSEnumMemberName::ComputedTemplateString(
5790+
CloneIn::clone_in_with_semantic_ids(it, allocator),
5791+
),
57885792
}
57895793
}
57905794
}

crates/oxc_ast/src/generated/derive_content_eq.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,6 @@ impl ContentEq for TSEnumBody<'_> {
17451745
impl ContentEq for TSEnumMember<'_> {
17461746
fn content_eq(&self, other: &Self) -> bool {
17471747
ContentEq::content_eq(&self.id, &other.id)
1748-
&& ContentEq::content_eq(&self.computed, &other.computed)
17491748
&& ContentEq::content_eq(&self.initializer, &other.initializer)
17501749
}
17511750
}
@@ -1755,7 +1754,8 @@ impl ContentEq for TSEnumMemberName<'_> {
17551754
match (self, other) {
17561755
(Self::Identifier(a), Self::Identifier(b)) => a.content_eq(b),
17571756
(Self::String(a), Self::String(b)) => a.content_eq(b),
1758-
(Self::TemplateString(a), Self::TemplateString(b)) => a.content_eq(b),
1757+
(Self::ComputedString(a), Self::ComputedString(b)) => a.content_eq(b),
1758+
(Self::ComputedTemplateString(a), Self::ComputedTemplateString(b)) => a.content_eq(b),
17591759
_ => false,
17601760
}
17611761
}

crates/oxc_ast/src/generated/derive_dummy.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,6 @@ impl<'a> Dummy<'a> for TSEnumMember<'a> {
19221922
Self {
19231923
span: Dummy::dummy(allocator),
19241924
id: Dummy::dummy(allocator),
1925-
computed: Dummy::dummy(allocator),
19261925
initializer: Dummy::dummy(allocator),
19271926
}
19281927
}

crates/oxc_ast/src/generated/derive_estree.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,7 @@ impl ESTree for TSEnumMember<'_> {
23102310
state.serialize_field("start", &self.span.start);
23112311
state.serialize_field("end", &self.span.end);
23122312
state.serialize_field("id", &self.id);
2313-
state.serialize_field("computed", &self.computed);
2313+
state.serialize_field("computed", &crate::serialize::TSEnumMemberComputed(self));
23142314
state.serialize_field("initializer", &self.initializer);
23152315
state.end();
23162316
}
@@ -2321,7 +2321,8 @@ impl ESTree for TSEnumMemberName<'_> {
23212321
match self {
23222322
Self::Identifier(it) => it.serialize(serializer),
23232323
Self::String(it) => it.serialize(serializer),
2324-
Self::TemplateString(it) => it.serialize(serializer),
2324+
Self::ComputedString(it) => it.serialize(serializer),
2325+
Self::ComputedTemplateString(it) => it.serialize(serializer),
23252326
}
23262327
}
23272328
}

crates/oxc_ast/src/generated/derive_get_address.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ impl GetAddress for TSEnumMemberName<'_> {
605605
match self {
606606
Self::Identifier(it) => GetAddress::address(it),
607607
Self::String(it) => GetAddress::address(it),
608-
Self::TemplateString(it) => GetAddress::address(it),
608+
Self::ComputedString(it) => GetAddress::address(it),
609+
Self::ComputedTemplateString(it) => GetAddress::address(it),
609610
}
610611
}
611612
}

crates/oxc_ast/src/generated/derive_get_span.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,8 @@ impl GetSpan for TSEnumMemberName<'_> {
15071507
match self {
15081508
Self::Identifier(it) => GetSpan::span(&**it),
15091509
Self::String(it) => GetSpan::span(&**it),
1510-
Self::TemplateString(it) => GetSpan::span(&**it),
1510+
Self::ComputedString(it) => GetSpan::span(&**it),
1511+
Self::ComputedTemplateString(it) => GetSpan::span(&**it),
15111512
}
15121513
}
15131514
}

0 commit comments

Comments
 (0)