Skip to content
Merged
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
38 changes: 19 additions & 19 deletions crates/oxc_formatter/src/write/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{

use super::{
FormatWrite,
jsx::element,
type_parameters::{FormatTSTypeParameters, FormatTSTypeParametersOptions},
};

Expand All @@ -44,17 +45,17 @@ 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()
}
}

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)
}
}

Expand Down Expand Up @@ -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>>>,
Expand Down Expand Up @@ -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(";"))
}
}

Expand Down
Loading