diff --git a/crates/oxc_prettier/src/binaryish.rs b/crates/oxc_prettier/src/binaryish.rs index 8f91378fb..987123acc 100644 --- a/crates/oxc_prettier/src/binaryish.rs +++ b/crates/oxc_prettier/src/binaryish.rs @@ -1,59 +1,8 @@ -use oxc_ast::ast::*; -use oxc_span::{GetSpan, Span}; use oxc_syntax::{ operator::{BinaryOperator, LogicalOperator}, precedence::{GetPrecedence, Precedence}, }; -use crate::{Doc, Format, Prettier}; - -#[derive(Clone, Copy)] -pub enum BinaryishLeft<'a, 'b> { - Expression(&'b Expression<'a>), - PrivateIdentifier(&'b PrivateIdentifier), -} - -impl<'a, 'b> From<&'b Expression<'a>> for BinaryishLeft<'a, 'b> { - fn from(e: &'b Expression<'a>) -> Self { - Self::Expression(e) - } -} - -impl<'a, 'b> From<&'b PrivateIdentifier> for BinaryishLeft<'a, 'b> { - fn from(e: &'b PrivateIdentifier) -> Self { - Self::PrivateIdentifier(e) - } -} - -impl<'a, 'b> BinaryishLeft<'a, 'b> { - pub fn operator(&self) -> Option { - match self { - Self::Expression(Expression::BinaryExpression(e)) => { - Some(BinaryishOperator::BinaryOperator(e.operator)) - } - Self::Expression(Expression::LogicalExpression(e)) => { - Some(BinaryishOperator::LogicalOperator(e.operator)) - } - _ => 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> { - fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { - match self { - Self::Expression(expr) => expr.format(p), - Self::PrivateIdentifier(ident) => ident.format(p), - } - } -} - #[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum BinaryishOperator { BinaryOperator(BinaryOperator), diff --git a/crates/oxc_prettier/src/format/binaryish.rs b/crates/oxc_prettier/src/format/binaryish.rs index 3a8976ef5..5cbd9195f 100644 --- a/crates/oxc_prettier/src/format/binaryish.rs +++ b/crates/oxc_prettier/src/format/binaryish.rs @@ -1,7 +1,8 @@ use oxc_ast::ast::*; +use oxc_span::GetSpan; use crate::{ - binaryish::{BinaryishLeft, BinaryishOperator}, + binaryish::BinaryishOperator, comments::CommentFlags, doc::{Doc, DocBuilder, Group}, group, line, space, ss, Format, Prettier, @@ -9,7 +10,7 @@ use crate::{ pub(super) fn print_binaryish_expression<'a>( p: &mut Prettier<'a>, - left: BinaryishLeft<'a, '_>, + left: &Expression<'a>, operator: BinaryishOperator, right: &Expression<'a>, ) -> Doc<'a> { @@ -18,19 +19,25 @@ pub(super) fn print_binaryish_expression<'a>( fn print_binaryish_expressions<'a>( p: &mut Prettier<'a>, - left: BinaryishLeft<'a, '_>, + left: &Expression<'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)) { + let left_operator = match left { + Expression::LogicalExpression(e) => Some(BinaryishOperator::LogicalOperator(e.operator)), + Expression::BinaryExpression(e) => Some(BinaryishOperator::BinaryOperator(e.operator)), + _ => None, + }; + + if left_operator.is_some_and(|left_operator| operator.should_flatten(left_operator)) { parts.push(match left { - BinaryishLeft::Expression(Expression::BinaryExpression(e)) => { - print_binaryish_expressions(p, (&e.left).into(), e.operator.into(), &e.right) + Expression::BinaryExpression(e) => { + print_binaryish_expressions(p, &e.left, e.operator.into(), &e.right) } - BinaryishLeft::Expression(Expression::LogicalExpression(e)) => { - print_binaryish_expressions(p, (&e.left).into(), e.operator.into(), &e.right) + Expression::LogicalExpression(e) => { + print_binaryish_expressions(p, &e.left, e.operator.into(), &e.right) } _ => unreachable!(), }); diff --git a/crates/oxc_prettier/src/format/mod.rs b/crates/oxc_prettier/src/format/mod.rs index 284b30b65..a317de799 100644 --- a/crates/oxc_prettier/src/format/mod.rs +++ b/crates/oxc_prettier/src/format/mod.rs @@ -1673,7 +1673,7 @@ impl<'a> Format<'a> for BinaryExpression<'a> { wrap!(p, self, BinaryExpression, { let doc = binaryish::print_binaryish_expression( p, - (&self.left).into(), + &self.left, self.operator.into(), &self.right, ); @@ -1689,12 +1689,14 @@ impl<'a> Format<'a> for BinaryExpression<'a> { impl<'a> Format<'a> for PrivateInExpression<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { wrap!(p, self, PrivateInExpression, { - binaryish::print_binaryish_expression( + array![ p, - (&self.left).into(), - self.operator.into(), - &self.right, - ) + format!(p, self.left), + space!(), + ss!(self.operator.as_str()), + space!(), + format!(p, self.right) + ] }) } } @@ -1704,7 +1706,7 @@ impl<'a> Format<'a> for LogicalExpression<'a> { wrap!(p, self, LogicalExpression, { let doc = binaryish::print_binaryish_expression( p, - (&self.left).into(), + &self.left, self.operator.into(), &self.right, );