Skip to content

Commit

Permalink
refactor(ast): remove TSEnumBody (#2509)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Feb 26, 2024
1 parent b8764a8 commit 540f917
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 45 deletions.
14 changes: 1 addition & 13 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ impl<'a> AstBuilder<'a> {
Declaration::TSEnumDeclaration(self.alloc(TSEnumDeclaration {
span,
id,
body: TSEnumBody { span, members },
members,
modifiers,
}))
}
Expand Down
3 changes: 0 additions & 3 deletions crates/oxc_ast/src/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),

Expand Down
17 changes: 5 additions & 12 deletions crates/oxc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
17 changes: 5 additions & 12 deletions crates/oxc_ast/src/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/gen_ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 540f917

Please sign in to comment.