Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prettier): add print_binaryish_expressions #1664

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions crates/oxc_prettier/src/binaryish.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use oxc_ast::ast::*;
use oxc_span::{GetSpan, Span};
use oxc_syntax::{
operator::{BinaryOperator, LogicalOperator},
precedence::{GetPrecedence, Precedence},
Expand Down Expand Up @@ -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> {
Expand Down
32 changes: 26 additions & 6 deletions crates/oxc_prettier/src/format/binaryish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use oxc_ast::ast::*;

use crate::{
binaryish::{BinaryishLeft, BinaryishOperator},
comments::CommentFlags,
doc::{Doc, DocBuilder, Group},
group, line, ss, Format, Prettier,
};
Expand All @@ -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)
}
}
Expand Down
3 changes: 1 addition & 2 deletions tasks/prettier_conformance/prettier.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Compatibility: 231/550 (42.00%)
Compatibility: 232/550 (42.18%)

# Failed

Expand Down Expand Up @@ -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

Expand Down