mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(prettier): remove BinaryishLeft (#2381)
This commit is contained in:
parent
7d7a3fcef8
commit
4e8fcb5110
3 changed files with 24 additions and 66 deletions
|
|
@ -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<BinaryishOperator> {
|
||||
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),
|
||||
|
|
|
|||
|
|
@ -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!(),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in a new issue