Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub struct TSEnumBody<'a> {
pub struct TSEnumMember<'a> {
pub span: Span,
pub id: TSEnumMemberName<'a>,
pub computed: bool,
pub initializer: Option<Expression<'a>>,
}

Expand All @@ -132,6 +133,7 @@ pub struct TSEnumMember<'a> {
pub enum TSEnumMemberName<'a> {
Identifier(Box<'a, IdentifierName<'a>>) = 0,
String(Box<'a, StringLiteral<'a>>) = 1,
TemplateString(Box<'a, TemplateLiteral<'a>>) = 2,
}

/// TypeScript Type Annotation
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_ast/src/ast_impl/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ use crate::ast::*;

impl<'a> TSEnumMemberName<'a> {
/// Get the name of this enum member.
/// # Panics
/// Panics if `self` is a `TemplateString` with no quasi.
pub fn static_name(&self) -> Atom<'a> {
match self {
Self::Identifier(ident) => ident.name,
Self::String(lit) => lit.value,
Self::TemplateString(template) => template
.quasi()
.expect("`TSEnumMemberName::TemplateString` should have no substitution and at least one quasi"),
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,11 +925,12 @@ const _: () = {
assert!(offset_of!(TSEnumBody, span) == 0);
assert!(offset_of!(TSEnumBody, members) == 8);

assert!(size_of::<TSEnumMember>() == 40);
assert!(size_of::<TSEnumMember>() == 48);
assert!(align_of::<TSEnumMember>() == 8);
assert!(offset_of!(TSEnumMember, span) == 0);
assert!(offset_of!(TSEnumMember, id) == 8);
assert!(offset_of!(TSEnumMember, initializer) == 24);
assert!(offset_of!(TSEnumMember, computed) == 24);
assert!(offset_of!(TSEnumMember, initializer) == 32);

assert!(size_of::<TSEnumMemberName>() == 16);
assert!(align_of::<TSEnumMemberName>() == 8);
Expand Down Expand Up @@ -2320,11 +2321,12 @@ const _: () = {
assert!(offset_of!(TSEnumBody, span) == 0);
assert!(offset_of!(TSEnumBody, members) == 8);

assert!(size_of::<TSEnumMember>() == 24);
assert!(size_of::<TSEnumMember>() == 28);
assert!(align_of::<TSEnumMember>() == 4);
assert!(offset_of!(TSEnumMember, span) == 0);
assert!(offset_of!(TSEnumMember, id) == 8);
assert!(offset_of!(TSEnumMember, initializer) == 16);
assert!(offset_of!(TSEnumMember, computed) == 16);
assert!(offset_of!(TSEnumMember, initializer) == 20);

assert!(size_of::<TSEnumMemberName>() == 8);
assert!(align_of::<TSEnumMemberName>() == 4);
Expand Down
22 changes: 21 additions & 1 deletion crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9748,15 +9748,17 @@ impl<'a> AstBuilder<'a> {
/// ## Parameters
/// * `span`: The [`Span`] covering this node
/// * `id`
/// * `computed`
/// * `initializer`
#[inline]
pub fn ts_enum_member(
self,
span: Span,
id: TSEnumMemberName<'a>,
computed: bool,
initializer: Option<Expression<'a>>,
) -> TSEnumMember<'a> {
TSEnumMember { span, id, initializer }
TSEnumMember { span, id, computed, initializer }
}

/// Build a [`TSEnumMemberName::Identifier`].
Expand Down Expand Up @@ -9823,6 +9825,24 @@ impl<'a> AstBuilder<'a> {
))
}

/// Build a [`TSEnumMemberName::TemplateString`].
///
/// This node contains a [`TemplateLiteral`] that will be stored in the memory arena.
///
/// ## Parameters
/// * `span`: The [`Span`] covering this node
/// * `quasis`
/// * `expressions`
#[inline]
pub fn ts_enum_member_name_template_string(
self,
span: Span,
quasis: Vec<'a, TemplateElement<'a>>,
expressions: Vec<'a, Expression<'a>>,
) -> TSEnumMemberName<'a> {
TSEnumMemberName::TemplateString(self.alloc_template_literal(span, quasis, expressions))
}

/// Build a [`TSTypeAnnotation`].
///
/// If you want the built node to be allocated in the memory arena,
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_ast/src/generated/derive_clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5746,6 +5746,7 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSEnumMember<'_> {
TSEnumMember {
span: CloneIn::clone_in(&self.span, allocator),
id: CloneIn::clone_in(&self.id, allocator),
computed: CloneIn::clone_in(&self.computed, allocator),
initializer: CloneIn::clone_in(&self.initializer, allocator),
}
}
Expand All @@ -5754,6 +5755,7 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSEnumMember<'_> {
TSEnumMember {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
id: CloneIn::clone_in_with_semantic_ids(&self.id, allocator),
computed: CloneIn::clone_in_with_semantic_ids(&self.computed, allocator),
initializer: CloneIn::clone_in_with_semantic_ids(&self.initializer, allocator),
}
}
Expand All @@ -5766,6 +5768,9 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSEnumMemberName<'_> {
match self {
Self::Identifier(it) => TSEnumMemberName::Identifier(CloneIn::clone_in(it, allocator)),
Self::String(it) => TSEnumMemberName::String(CloneIn::clone_in(it, allocator)),
Self::TemplateString(it) => {
TSEnumMemberName::TemplateString(CloneIn::clone_in(it, allocator))
}
}
}

Expand All @@ -5777,6 +5782,9 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSEnumMemberName<'_> {
Self::String(it) => {
TSEnumMemberName::String(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
Self::TemplateString(it) => {
TSEnumMemberName::TemplateString(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_ast/src/generated/derive_content_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,7 @@ impl ContentEq for TSEnumBody<'_> {
impl ContentEq for TSEnumMember<'_> {
fn content_eq(&self, other: &Self) -> bool {
ContentEq::content_eq(&self.id, &other.id)
&& ContentEq::content_eq(&self.computed, &other.computed)
&& ContentEq::content_eq(&self.initializer, &other.initializer)
}
}
Expand All @@ -1754,6 +1755,7 @@ impl ContentEq for TSEnumMemberName<'_> {
match (self, other) {
(Self::Identifier(a), Self::Identifier(b)) => a.content_eq(b),
(Self::String(a), Self::String(b)) => a.content_eq(b),
(Self::TemplateString(a), Self::TemplateString(b)) => a.content_eq(b),
_ => false,
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/generated/derive_dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,7 @@ impl<'a> Dummy<'a> for TSEnumMember<'a> {
Self {
span: Dummy::dummy(allocator),
id: Dummy::dummy(allocator),
computed: Dummy::dummy(allocator),
initializer: Dummy::dummy(allocator),
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2310,6 +2310,7 @@ impl ESTree for TSEnumMember<'_> {
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("id", &self.id);
state.serialize_field("computed", &self.computed);
state.serialize_field("initializer", &self.initializer);
state.end();
}
Expand All @@ -2320,6 +2321,7 @@ impl ESTree for TSEnumMemberName<'_> {
match self {
Self::Identifier(it) => it.serialize(serializer),
Self::String(it) => it.serialize(serializer),
Self::TemplateString(it) => it.serialize(serializer),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/generated/derive_get_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ impl GetAddress for TSEnumMemberName<'_> {
match self {
Self::Identifier(it) => GetAddress::address(it),
Self::String(it) => GetAddress::address(it),
Self::TemplateString(it) => GetAddress::address(it),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/generated/derive_get_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,7 @@ impl GetSpan for TSEnumMemberName<'_> {
match self {
Self::Identifier(it) => GetSpan::span(&**it),
Self::String(it) => GetSpan::span(&**it),
Self::TemplateString(it) => GetSpan::span(&**it),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/generated/derive_get_span_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,7 @@ impl GetSpanMut for TSEnumMemberName<'_> {
match self {
Self::Identifier(it) => GetSpanMut::span_mut(&mut **it),
Self::String(it) => GetSpanMut::span_mut(&mut **it),
Self::TemplateString(it) => GetSpanMut::span_mut(&mut **it),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast_visit/src/generated/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3211,6 +3211,7 @@ pub mod walk {
match it {
TSEnumMemberName::Identifier(it) => visitor.visit_identifier_name(it),
TSEnumMemberName::String(it) => visitor.visit_string_literal(it),
TSEnumMemberName::TemplateString(it) => visitor.visit_template_literal(it),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast_visit/src/generated/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3369,6 +3369,7 @@ pub mod walk_mut {
match it {
TSEnumMemberName::Identifier(it) => visitor.visit_identifier_name(it),
TSEnumMemberName::String(it) => visitor.visit_string_literal(it),
TSEnumMemberName::TemplateString(it) => visitor.visit_template_literal(it),
}
}

Expand Down
24 changes: 23 additions & 1 deletion crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3757,8 +3757,30 @@ impl Gen for TSEnumMember<'_> {
fn r#gen(&self, p: &mut Codegen, ctx: Context) {
match &self.id {
TSEnumMemberName::Identifier(decl) => decl.print(p, ctx),
TSEnumMemberName::String(decl) => p.print_string_literal(decl, false),
TSEnumMemberName::String(decl) => {
if self.computed {
p.print_ascii_byte(b'[');
}
p.print_string_literal(decl, false);
if self.computed {
p.print_ascii_byte(b']');
}
}
TSEnumMemberName::TemplateString(decl) => {
if self.computed {
p.print_ascii_byte(b'[');
}
let quasi = decl.quasis.first().unwrap();
p.add_source_mapping(quasi.span);
p.print_ascii_byte(b'`');
p.print_str(quasi.value.raw.as_str());
p.print_ascii_byte(b'`');
if self.computed {
p.print_ascii_byte(b']');
}
}
}

if let Some(init) = &self.initializer {
p.print_soft_space();
p.print_equal();
Expand Down
6 changes: 2 additions & 4 deletions crates/oxc_isolated_declarations/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,14 @@ impl<'a> IsolatedDeclarations<'a> {
prev_initializer_value.clone_from(&value);

if let Some(value) = &value {
let member_name = match &member.id {
TSEnumMemberName::Identifier(id) => id.name,
TSEnumMemberName::String(str) => str.value,
};
let member_name = member.id.static_name();
prev_members.insert(member_name, value.clone());
}

let member = self.ast.ts_enum_member(
member.span,
member.id.clone_in(self.ast.allocator),
false,
value.map(|v| match v {
ConstantValue::Number(v) => {
let is_negative = v < 0.0;
Expand Down
21 changes: 7 additions & 14 deletions crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,25 @@ impl<'a> ParserImpl<'a> {

pub(crate) fn parse_ts_enum_member(&mut self) -> Result<TSEnumMember<'a>> {
let span = self.start_span();
let id = self.parse_ts_enum_member_name()?;
let (id, computed) = self.parse_ts_enum_member_name()?;
let initializer = if self.eat(Kind::Eq) {
Some(self.parse_assignment_expression_or_higher()?)
} else {
None
};
Ok(self.ast.ts_enum_member(self.end_span(span), id, initializer))
Ok(self.ast.ts_enum_member(self.end_span(span), id, computed, initializer))
}

fn parse_ts_enum_member_name(&mut self) -> Result<TSEnumMemberName<'a>> {
fn parse_ts_enum_member_name(&mut self) -> Result<(TSEnumMemberName<'a>, bool)> {
match self.cur_kind() {
Kind::Str => {
let literal = self.parse_literal_string()?;
Ok(TSEnumMemberName::String(self.alloc(literal)))
Ok((TSEnumMemberName::String(self.alloc(literal)), false))
}
Kind::LBrack => match self.parse_computed_property_name()? {
Expression::StringLiteral(literal) => Ok(TSEnumMemberName::String(literal)),
Expression::StringLiteral(literal) => Ok((TSEnumMemberName::String(literal), true)),
Expression::TemplateLiteral(template) if template.is_no_substitution_template() => {
Ok(self.ast.ts_enum_member_name_string(
template.span,
template.quasi().unwrap(),
Some(Atom::from(
Span::new(template.span.start + 1, template.span.end - 1)
.source_text(self.source_text),
)),
))
Ok((TSEnumMemberName::TemplateString(template), true))
}
Expression::NumericLiteral(literal) => {
Err(diagnostics::enum_member_cannot_have_numeric_name(literal.span()))
Expand All @@ -96,7 +89,7 @@ impl<'a> ParserImpl<'a> {
}
_ => {
let ident_name = self.parse_identifier_name()?;
Ok(TSEnumMemberName::Identifier(self.alloc(ident_name)))
Ok((TSEnumMemberName::Identifier(self.alloc(ident_name)), false))
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions crates/oxc_transformer/src/typescript/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,7 @@ impl<'a> TypeScriptEnum<'a> {
let mut prev_member_name = None;

for member in members.iter_mut() {
let member_name = match &member.id {
TSEnumMemberName::Identifier(id) => id.name,
TSEnumMemberName::String(str) => str.value,
};
let member_name = member.id.static_name();

let init = if let Some(initializer) = &mut member.initializer {
let constant_value =
Expand Down
11 changes: 11 additions & 0 deletions crates/oxc_traverse/src/generated/ancestor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11249,6 +11249,7 @@ impl<'a, 't> GetAddress for TSEnumBodyWithoutMembers<'a, 't> {

pub(crate) const OFFSET_TS_ENUM_MEMBER_SPAN: usize = offset_of!(TSEnumMember, span);
pub(crate) const OFFSET_TS_ENUM_MEMBER_ID: usize = offset_of!(TSEnumMember, id);
pub(crate) const OFFSET_TS_ENUM_MEMBER_COMPUTED: usize = offset_of!(TSEnumMember, computed);
pub(crate) const OFFSET_TS_ENUM_MEMBER_INITIALIZER: usize = offset_of!(TSEnumMember, initializer);

#[repr(transparent)]
Expand All @@ -11264,6 +11265,11 @@ impl<'a, 't> TSEnumMemberWithoutId<'a, 't> {
unsafe { &*((self.0 as *const u8).add(OFFSET_TS_ENUM_MEMBER_SPAN) as *const Span) }
}

#[inline]
pub fn computed(self) -> &'t bool {
unsafe { &*((self.0 as *const u8).add(OFFSET_TS_ENUM_MEMBER_COMPUTED) as *const bool) }
}

#[inline]
pub fn initializer(self) -> &'t Option<Expression<'a>> {
unsafe {
Expand Down Expand Up @@ -11299,6 +11305,11 @@ impl<'a, 't> TSEnumMemberWithoutInitializer<'a, 't> {
&*((self.0 as *const u8).add(OFFSET_TS_ENUM_MEMBER_ID) as *const TSEnumMemberName<'a>)
}
}

#[inline]
pub fn computed(self) -> &'t bool {
unsafe { &*((self.0 as *const u8).add(OFFSET_TS_ENUM_MEMBER_COMPUTED) as *const bool) }
}
}

impl<'a, 't> GetAddress for TSEnumMemberWithoutInitializer<'a, 't> {
Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_traverse/src/generated/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3805,6 +3805,9 @@ unsafe fn walk_ts_enum_member_name<'a, Tr: Traverse<'a>>(
TSEnumMemberName::String(node) => {
walk_string_literal(traverser, (&mut **node) as *mut _, ctx)
}
TSEnumMemberName::TemplateString(node) => {
walk_template_literal(traverser, (&mut **node) as *mut _, ctx)
}
}
traverser.exit_ts_enum_member_name(&mut *node, ctx);
}
Expand Down
Loading
Loading