diff --git a/crates/oxc_prettier/src/binaryish.rs b/crates/oxc_prettier/src/binaryish.rs index 260a507157ee0..f3213ee31287e 100644 --- a/crates/oxc_prettier/src/binaryish.rs +++ b/crates/oxc_prettier/src/binaryish.rs @@ -1,4 +1,5 @@ use oxc_ast::ast::*; +use oxc_span::{GetSpan, Span}; use oxc_syntax::{ operator::{BinaryOperator, LogicalOperator}, precedence::{GetPrecedence, Precedence}, @@ -36,6 +37,12 @@ impl<'a, 'b> BinaryishLeft<'a, 'b> { _ => None, } } + pub fn span(&self) -> Span { + match self { + Self::Expression(e) => e.span(), + Self::PrivateIdentifier(e) => e.span, + } + } } impl<'a, 'b> Format<'a> for BinaryishLeft<'a, 'b> { diff --git a/crates/oxc_prettier/src/format/binaryish.rs b/crates/oxc_prettier/src/format/binaryish.rs index b9307409e4d46..460fec7add21d 100644 --- a/crates/oxc_prettier/src/format/binaryish.rs +++ b/crates/oxc_prettier/src/format/binaryish.rs @@ -2,6 +2,7 @@ use oxc_ast::ast::*; use crate::{ binaryish::{BinaryishLeft, BinaryishOperator}, + comments::CommentFlags, doc::{Doc, DocBuilder, Group}, group, line, ss, Format, Prettier, }; @@ -11,31 +12,50 @@ pub(super) fn print_binaryish_expression<'a>( left: BinaryishLeft<'a, '_>, operator: BinaryishOperator, right: &Expression<'a>, +) -> Doc<'a> { + print_binaryish_expressions(p, left, operator, right) +} + +fn print_binaryish_expressions<'a>( + p: &mut Prettier<'a>, + left: BinaryishLeft<'a, '_>, + operator: BinaryishOperator, + right: &Expression<'a>, ) -> Doc<'a> { let mut parts = p.vec(); if left.operator().is_some_and(|left_operator| operator.should_flatten(left_operator)) { parts.push(match left { BinaryishLeft::Expression(Expression::BinaryExpression(e)) => { - print_binaryish_expression(p, (&e.left).into(), e.operator.into(), &e.right) + print_binaryish_expressions(p, (&e.left).into(), e.operator.into(), &e.right) } BinaryishLeft::Expression(Expression::LogicalExpression(e)) => { - print_binaryish_expression(p, (&e.left).into(), e.operator.into(), &e.right) + print_binaryish_expressions(p, (&e.left).into(), e.operator.into(), &e.right) } _ => unreachable!(), }); } else { parts.push(group!(p, left.format(p))); } + let should_inline = should_inline_logical_expression(right); + let mut right_parts = p.vec(); + right_parts.push(ss!(operator.as_str())); + right_parts.push(if should_inline { ss!(" ") } else { line!() }); + right_parts.push(right.format(p)); + + let should_break = p.has_comment(left.span(), CommentFlags::Trailing | CommentFlags::Line); + parts.push(ss!(" ")); + if should_break { + let group = Doc::Group(Group::new(right_parts, should_break)); + parts.push(group); + } else { + parts.push(Doc::Array(right_parts)); + } if operator.is_binary() { - parts.push(group!(p, ss!(operator.as_str()), line!(), right.format(p))); Doc::Group(Group::new(parts, false)) } else { - parts.push(ss!(operator.as_str())); - parts.push(line!()); - parts.push(right.format(p)); Doc::Array(parts) } } diff --git a/tasks/prettier_conformance/prettier.snap.md b/tasks/prettier_conformance/prettier.snap.md index 1da39942c449f..1ca1bde9d09f3 100644 --- a/tasks/prettier_conformance/prettier.snap.md +++ b/tasks/prettier_conformance/prettier.snap.md @@ -1,4 +1,4 @@ -Compatibility: 231/550 (42.00%) +Compatibility: 232/550 (42.18%) # Failed @@ -91,7 +91,6 @@ Compatibility: 231/550 (42.00%) * chain-expression/test.js ### class-comment -* class-comment/class-property.js * class-comment/misc.js * class-comment/superclass.js