From 7d64291246505423d657a68c82b11381d2dc8bcf Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Wed, 29 Oct 2025 15:46:24 +0000 Subject: [PATCH] refactor(formatter): simplify printing ClassElement with a semicolon (#15030) --- crates/oxc_formatter/src/write/class.rs | 38 ++++++++++++------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/crates/oxc_formatter/src/write/class.rs b/crates/oxc_formatter/src/write/class.rs index d768f75c2b6e4..c59e915bab6c7 100644 --- a/crates/oxc_formatter/src/write/class.rs +++ b/crates/oxc_formatter/src/write/class.rs @@ -28,6 +28,7 @@ use crate::{ use super::{ FormatWrite, + jsx::element, type_parameters::{FormatTSTypeParameters, FormatTSTypeParametersOptions}, }; @@ -44,9 +45,9 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, ClassElement<'a>>> { let mut join = f.join_nodes_with_hardline(); // Iterate through pairs of consecutive elements to handle semicolons properly // Each element is paired with the next one (or None for the last element) - for (e1, e2) in self.iter().zip(self.iter().skip(1).map(Some).chain(std::iter::once(None))) - { - join.entry(e1.span(), &(e1, e2)); + let mut iter = self.iter().peekable(); + while let Some(element) = iter.next() { + join.entry(element.span(), &(element, iter.peek().copied())); } join.finish() } @@ -54,7 +55,7 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, ClassElement<'a>>> { impl<'a> Format<'a> for (&AstNode<'a, ClassElement<'a>>, Option<&AstNode<'a, ClassElement<'a>>>) { fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { - write!(f, [self.0, ClassPropertySemicolon::new(self.0, self.1)]) + FormatClassElementWithSemicolon::new(self.0, self.1).fmt(f) } } @@ -551,12 +552,12 @@ fn should_group<'a>(class: &Class<'a>, f: &Formatter<'_, 'a>) -> bool { false } -pub struct ClassPropertySemicolon<'a, 'b> { +pub struct FormatClassElementWithSemicolon<'a, 'b> { element: &'b AstNode<'a, ClassElement<'a>>, next_element: Option<&'b AstNode<'a, ClassElement<'a>>>, } -impl<'a, 'b> ClassPropertySemicolon<'a, 'b> { +impl<'a, 'b> FormatClassElementWithSemicolon<'a, 'b> { pub fn new( element: &'b AstNode<'a, ClassElement<'a>>, next_element: Option<&'b AstNode<'a, ClassElement<'a>>>, @@ -604,23 +605,22 @@ impl<'a, 'b> ClassPropertySemicolon<'a, 'b> { } } -impl<'a> Format<'a> for ClassPropertySemicolon<'a, '_> { +impl<'a> Format<'a> for FormatClassElementWithSemicolon<'a, '_> { fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { - if !matches!( + write!(f, [self.element])?; + + let needs_semi = matches!( self.element.as_ref(), ClassElement::PropertyDefinition(_) | ClassElement::AccessorProperty(_) - ) { - return Ok(()); - } + ); - if match f.options().semicolons { - Semicolons::Always => true, - Semicolons::AsNeeded => self.needs_semicolon(), - } { - write!(f, ";") - } else { - Ok(()) - } + let needs_semi = needs_semi + && match f.options().semicolons { + Semicolons::Always => true, + Semicolons::AsNeeded => self.needs_semicolon(), + }; + + write!(f, needs_semi.then_some(";")) } }