mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(prettier): refactor binaryish expression formatting (#1412)
This commit is contained in:
parent
f6ecd96e69
commit
b0248acb7b
4 changed files with 33 additions and 25 deletions
|
|
@ -40,7 +40,23 @@ pub(super) fn print_binaryish_expression<'a>(
|
|||
right: &Expression<'a>,
|
||||
) -> Doc<'a> {
|
||||
let mut parts = p.vec();
|
||||
parts.push(left.format(p));
|
||||
match &left {
|
||||
BinaryishLeft::Expression(expr) => {
|
||||
if let Expression::LogicalExpression(logical_expr) = expr {
|
||||
parts.push(print_binaryish_expression(
|
||||
p,
|
||||
&BinaryishLeft::Expression(&logical_expr.left),
|
||||
BinaryishOperator::LogicalOperator(logical_expr.operator),
|
||||
&logical_expr.right,
|
||||
));
|
||||
} else {
|
||||
parts.push(left.format(p));
|
||||
}
|
||||
}
|
||||
BinaryishLeft::PrivateIdentifier(ident) => {
|
||||
parts.push(left.format(p));
|
||||
}
|
||||
}
|
||||
parts.push(ss!(" "));
|
||||
parts.push(ss!(operator.as_str()));
|
||||
parts.push(Doc::Line);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
use crate::{doc::Doc, group, if_break, indent, softline, ss, Format, Prettier};
|
||||
use crate::{doc::Doc, group, if_break, ss, Format, Prettier};
|
||||
|
||||
pub(super) fn print_function<'a>(
|
||||
p: &mut Prettier<'a>,
|
||||
|
|
@ -60,13 +60,7 @@ pub(super) fn print_return_or_throw_argument<'a>(
|
|||
|
||||
if let Some(argument) = argument {
|
||||
parts.push(ss!(" "));
|
||||
parts.push(group![
|
||||
p,
|
||||
if_break!(p, "("),
|
||||
indent!(p, softline!(), argument.format(p)),
|
||||
softline!(),
|
||||
if_break!(p, ")")
|
||||
]);
|
||||
parts.push(group![p, if_break!(p, "("), argument.format(p), if_break!(p, ")")]);
|
||||
}
|
||||
|
||||
parts.push(p.str(";"));
|
||||
|
|
|
|||
|
|
@ -1425,15 +1425,13 @@ impl<'a> Format<'a> for UnaryExpression<'a> {
|
|||
|
||||
impl<'a> Format<'a> for BinaryExpression<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
group!(
|
||||
let doc = binaryish::print_binaryish_expression(
|
||||
p,
|
||||
binaryish::print_binaryish_expression(
|
||||
p,
|
||||
&BinaryishLeft::Expression(&self.left),
|
||||
BinaryishOperator::BinaryOperator(self.operator),
|
||||
&self.right,
|
||||
)
|
||||
)
|
||||
&BinaryishLeft::Expression(&self.left),
|
||||
BinaryishOperator::BinaryOperator(self.operator),
|
||||
&self.right,
|
||||
);
|
||||
group!(p, indent!(p, softline!(), doc), softline!())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1450,12 +1448,13 @@ impl<'a> Format<'a> for PrivateInExpression<'a> {
|
|||
|
||||
impl<'a> Format<'a> for LogicalExpression<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
binaryish::print_binaryish_expression(
|
||||
let doc = binaryish::print_binaryish_expression(
|
||||
p,
|
||||
&BinaryishLeft::Expression(&self.left),
|
||||
BinaryishOperator::LogicalOperator(self.operator),
|
||||
&self.right,
|
||||
)
|
||||
);
|
||||
group!(p, indent!(p, softline!(), doc), softline!())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1584,11 +1583,12 @@ impl<'a> Format<'a> for ParenthesizedExpression<'a> {
|
|||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
// TODO: if shouldHug
|
||||
// array![p, ss!("("), format!(p, self.expression), ss!(")")]
|
||||
group![
|
||||
array![
|
||||
p,
|
||||
ss!("("),
|
||||
indent!(p, array![p, softline!(), format!(p, self.expression)]),
|
||||
softline!(),
|
||||
format!(p, self.expression),
|
||||
// indent!(p, array![p, softline!(), ]),
|
||||
// softline!(),
|
||||
ss!(")")
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Compatibility: 103/838 (12.29%)
|
||||
Compatibility: 105/838 (12.53%)
|
||||
|
||||
# Failed
|
||||
|
||||
|
|
@ -468,7 +468,6 @@ Compatibility: 103/838 (12.29%)
|
|||
|
||||
### expression_statement
|
||||
* expression_statement/no_regression.js
|
||||
* expression_statement/use_strict.js
|
||||
|
||||
### for
|
||||
* for/comment.js
|
||||
|
|
@ -759,7 +758,6 @@ Compatibility: 103/838 (12.29%)
|
|||
* objects/escape-sequence-key.js
|
||||
* objects/expand.js
|
||||
* objects/expression.js
|
||||
* objects/getter-setter.js
|
||||
* objects/range.js
|
||||
* objects/right-break.js
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue