diff --git a/crates/oxc_ast/src/ast/ts.rs b/crates/oxc_ast/src/ast/ts.rs index 4769446e6838d..13498854ae04e 100644 --- a/crates/oxc_ast/src/ast/ts.rs +++ b/crates/oxc_ast/src/ast/ts.rs @@ -41,22 +41,10 @@ pub struct TSEnumDeclaration<'a> { #[cfg_attr(feature = "serde", serde(flatten))] pub span: Span, pub id: BindingIdentifier, - #[cfg_attr(feature = "serde", serde(flatten))] - pub body: TSEnumBody<'a>, + pub members: Vec<'a, TSEnumMember<'a>>, /// Valid Modifiers: `const`, `export`, `declare` pub modifiers: Modifiers<'a>, } -/// Enum Body -/// -/// A scope must be created on the enum body so this abstraction exists -#[derive(Debug, Hash)] -#[cfg_attr(feature = "serde", derive(Serialize))] -#[cfg_attr(all(feature = "serde", feature = "wasm"), derive(tsify::Tsify))] -pub struct TSEnumBody<'a> { - #[cfg_attr(feature = "serde", serde(skip_serializing))] - pub span: Span, - pub members: Vec<'a, TSEnumMember<'a>>, -} #[derive(Debug, Hash)] #[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))] diff --git a/crates/oxc_ast/src/ast_builder.rs b/crates/oxc_ast/src/ast_builder.rs index eda5b029bee2e..42f5933890f76 100644 --- a/crates/oxc_ast/src/ast_builder.rs +++ b/crates/oxc_ast/src/ast_builder.rs @@ -1542,7 +1542,7 @@ impl<'a> AstBuilder<'a> { Declaration::TSEnumDeclaration(self.alloc(TSEnumDeclaration { span, id, - body: TSEnumBody { span, members }, + members, modifiers, })) } diff --git a/crates/oxc_ast/src/ast_kind.rs b/crates/oxc_ast/src/ast_kind.rs index b6e744071585c..a6da6a4120b10 100644 --- a/crates/oxc_ast/src/ast_kind.rs +++ b/crates/oxc_ast/src/ast_kind.rs @@ -156,7 +156,6 @@ pub enum AstKind<'a> { TSEnumDeclaration(&'a TSEnumDeclaration<'a>), TSEnumMember(&'a TSEnumMember<'a>), - TSEnumBody(&'a TSEnumBody<'a>), TSImportEqualsDeclaration(&'a TSImportEqualsDeclaration<'a>), TSTypeName(&'a TSTypeName<'a>), @@ -472,7 +471,6 @@ impl<'a> GetSpan for AstKind<'a> { Self::TSEnumDeclaration(x) => x.span, Self::TSEnumMember(x) => x.span, - Self::TSEnumBody(x) => x.span, Self::TSImportEqualsDeclaration(x) => x.span, Self::TSTypeName(x) => x.span(), @@ -658,7 +656,6 @@ impl<'a> AstKind<'a> { Self::TSInstantiationExpression(_) => "TSInstantiationExpression".into(), Self::TSEnumDeclaration(decl) => format!("TSEnumDeclaration({})", &decl.id.name).into(), - Self::TSEnumBody(_) => "TSEnumBody".into(), Self::TSEnumMember(_) => "TSEnumMember".into(), diff --git a/crates/oxc_ast/src/visit.rs b/crates/oxc_ast/src/visit.rs index 61a4bb719542c..43dc880ada4c5 100644 --- a/crates/oxc_ast/src/visit.rs +++ b/crates/oxc_ast/src/visit.rs @@ -1426,22 +1426,15 @@ pub trait Visit<'a>: Sized { self.leave_node(kind); } - fn visit_enum_body(&mut self, body: &TSEnumBody<'a>) { - let kind = AstKind::TSEnumBody(self.alloc(body)); - self.enter_scope(ScopeFlags::empty()); - self.enter_node(kind); - for member in &body.members { - self.visit_enum_member(member); - } - self.leave_node(kind); - self.leave_scope(); - } - fn visit_enum(&mut self, decl: &TSEnumDeclaration<'a>) { let kind = AstKind::TSEnumDeclaration(self.alloc(decl)); self.enter_node(kind); self.visit_binding_identifier(&decl.id); - self.visit_enum_body(&decl.body); + self.enter_scope(ScopeFlags::empty()); + for member in &decl.members { + self.visit_enum_member(member); + } + self.leave_scope(); self.leave_node(kind); } diff --git a/crates/oxc_ast/src/visit_mut.rs b/crates/oxc_ast/src/visit_mut.rs index 00871c106c1e4..22678ccb0fced 100644 --- a/crates/oxc_ast/src/visit_mut.rs +++ b/crates/oxc_ast/src/visit_mut.rs @@ -1423,22 +1423,15 @@ pub trait VisitMut<'a>: Sized { self.leave_node(kind); } - fn visit_enum_body(&mut self, body: &mut TSEnumBody<'a>) { - let kind = AstKind::TSEnumBody(self.alloc(body)); - self.enter_scope(ScopeFlags::empty()); - self.enter_node(kind); - for member in body.members.iter_mut() { - self.visit_enum_member(member); - } - self.leave_node(kind); - self.leave_scope(); - } - fn visit_enum(&mut self, decl: &mut TSEnumDeclaration<'a>) { let kind = AstKind::TSEnumDeclaration(self.alloc(decl)); self.enter_node(kind); self.visit_binding_identifier(&mut decl.id); - self.visit_enum_body(&mut decl.body); + self.enter_scope(ScopeFlags::empty()); + for member in decl.members.iter_mut() { + self.visit_enum_member(member); + } + self.leave_scope(); self.leave_node(kind); } diff --git a/crates/oxc_codegen/src/gen_ts.rs b/crates/oxc_codegen/src/gen_ts.rs index 7c2fff1e50b5c..5d1f8e2e20c87 100644 --- a/crates/oxc_codegen/src/gen_ts.rs +++ b/crates/oxc_codegen/src/gen_ts.rs @@ -606,7 +606,7 @@ impl<'a, const MINIFY: bool> Gen for TSEnumDeclaration<'a> { self.id.gen(p, ctx); p.print_space_before_identifier(); p.print_block_start(); - p.print_list(&self.body.members, ctx); + p.print_list(&self.members, ctx); p.print_block_end(); p.print_hard_space(); } diff --git a/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs b/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs index 81cfdff840f3c..30a297cb29bd8 100644 --- a/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs +++ b/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs @@ -42,7 +42,7 @@ declare_oxc_lint!( impl Rule for NoDuplicateEnumValues { #[allow(clippy::float_cmp)] fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { - let AstKind::TSEnumBody(enum_body) = node.kind() else { return }; + let AstKind::TSEnumDeclaration(enum_body) = node.kind() else { return }; let mut seen_number_values: Vec<(f64, Span)> = vec![]; let mut seen_string_values: FxHashMap<&Atom, Span> = FxHashMap::default(); for enum_member in &enum_body.members { diff --git a/crates/oxc_transformer/src/typescript/mod.rs b/crates/oxc_transformer/src/typescript/mod.rs index f57280e86944c..cd2687d77df3f 100644 --- a/crates/oxc_transformer/src/typescript/mod.rs +++ b/crates/oxc_transformer/src/typescript/mod.rs @@ -532,8 +532,8 @@ impl<'a> TypeScript<'a> { // Foo[Foo["X"] = 0] = "X"; let enum_name = decl.id.name.clone(); - let statements = self.transform_ts_enum_members(&mut decl.body.members, &enum_name); - let body = self.ast.function_body(decl.body.span, self.ast.new_vec(), statements); + let statements = self.transform_ts_enum_members(&mut decl.members, &enum_name); + let body = self.ast.function_body(decl.span, self.ast.new_vec(), statements); let callee = self.ast.arrow_function_expression(SPAN, false, false, params, body, None, None);