diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index a02a73fb08792..fab7f1d392cb7 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -3087,16 +3087,14 @@ impl Gen for TSTupleType<'_> { impl Gen for TSUnionType<'_> { fn r#gen(&self, p: &mut Codegen, ctx: Context) { - if self.types.len() == 1 { - self.types[0].print(p, ctx); + let Some((first, rest)) = self.types.split_first() else { return; - } - for (index, item) in self.types.iter().enumerate() { - if index != 0 { - p.print_soft_space(); - p.print_str("|"); - p.print_soft_space(); - } + }; + first.print(p, ctx); + for item in rest { + p.print_soft_space(); + p.print_str("|"); + p.print_soft_space(); item.print(p, ctx); } } @@ -3112,16 +3110,14 @@ impl Gen for TSParenthesizedType<'_> { impl Gen for TSIntersectionType<'_> { fn r#gen(&self, p: &mut Codegen, ctx: Context) { - if self.types.len() == 1 { - self.types[0].print(p, ctx); + let Some((first, rest)) = self.types.split_first() else { return; - } - for (index, item) in self.types.iter().enumerate() { - if index != 0 { - p.print_soft_space(); - p.print_str("&"); - p.print_soft_space(); - } + }; + first.print(p, ctx); + for item in rest { + p.print_soft_space(); + p.print_str("&"); + p.print_soft_space(); item.print(p, ctx); } } @@ -3219,17 +3215,8 @@ impl Gen for TSQualifiedName<'_> { impl Gen for TSTypeOperator<'_> { fn r#gen(&self, p: &mut Codegen, ctx: Context) { - match self.operator { - TSTypeOperatorOperator::Keyof => { - p.print_str("keyof "); - } - TSTypeOperatorOperator::Unique => { - p.print_str("unique "); - } - TSTypeOperatorOperator::Readonly => { - p.print_str("readonly "); - } - } + p.print_str(self.operator.to_str()); + p.print_hard_space(); self.type_annotation.print(p, ctx); } } diff --git a/crates/oxc_codegen/src/lib.rs b/crates/oxc_codegen/src/lib.rs index ff6312999ab1f..993c62c769516 100644 --- a/crates/oxc_codegen/src/lib.rs +++ b/crates/oxc_codegen/src/lib.rs @@ -469,10 +469,18 @@ impl<'a> Codegen<'a> { } fn print_list_with_comments(&mut self, items: &[Argument<'_>], ctx: Context) { - for (index, item) in items.iter().enumerate() { - if index != 0 { - self.print_comma(); - } + let Some((first, rest)) = items.split_first() else { + return; + }; + if self.print_expr_comments(first.span().start) { + self.print_indent(); + } else { + self.print_soft_newline(); + self.print_indent(); + } + first.print(self, ctx); + for item in rest { + self.print_comma(); if self.print_expr_comments(item.span().start) { self.print_indent(); } else { @@ -483,12 +491,15 @@ impl<'a> Codegen<'a> { } } + #[inline] fn print_expressions(&mut self, items: &[T], precedence: Precedence, ctx: Context) { - for (index, item) in items.iter().enumerate() { - if index != 0 { - self.print_comma(); - self.print_soft_space(); - } + let Some((first, rest)) = items.split_first() else { + return; + }; + first.print_expr(self, precedence, ctx); + for item in rest { + self.print_comma(); + self.print_soft_space(); item.print_expr(self, precedence, ctx); } }