Skip to content
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: 4 additions & 3 deletions crates/oxc_formatter/src/parentheses/expression.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ptr;

use oxc_allocator::Address;
use oxc_ast::ast::*;
use oxc_data_structures::stack;
Expand Down Expand Up @@ -313,9 +315,8 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, CallExpression<'a>> {
// when the leftmost expression is not a class expression or a function expression
callee_span != leftmost.span()
&& matches!(
leftmost,
ExpressionLeftSide::Expression(e)
if matches!(e.as_ref(), Expression::ClassExpression(_) | Expression::FunctionExpression(_))
leftmost.as_ref(),
Expression::ClassExpression(_) | Expression::FunctionExpression(_)
)
}
_ => false,
Expand Down
18 changes: 10 additions & 8 deletions crates/oxc_formatter/src/write/arrow_function_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,12 +710,18 @@ impl<'a, 'b> From<&'b AstNode<'a, SimpleAssignmentTarget<'a>>> for ExpressionLef
}

impl<'a, 'b> ExpressionLeftSide<'a, 'b> {
pub fn leftmost(expression: &'b AstNode<'a, Expression<'a>>) -> Self {
pub fn leftmost(
expression: &'b AstNode<'a, Expression<'a>>,
) -> &'b AstNode<'a, Expression<'a>> {
let mut current: Self = expression.into();
loop {
match current.left_expression() {
None => {
break current;
break if let ExpressionLeftSide::Expression(expression) = current {
expression
} else {
unreachable!()
};
}
Some(left) => {
current = left;
Expand Down Expand Up @@ -820,14 +826,10 @@ fn should_add_parens(body: &AstNode<'_, FunctionBody<'_>>) -> bool {
// case and added by the object expression itself
if matches!(&stmt.expression, Expression::ConditionalExpression(_)) {
!matches!(
ExpressionLeftSide::leftmost(stmt.expression()),
ExpressionLeftSide::Expression(
e
) if matches!(e.as_ref(),
Expression::ObjectExpression(_)
ExpressionLeftSide::leftmost(stmt.expression()).as_ref(),
Expression::ObjectExpression(_)
| Expression::FunctionExpression(_)
| Expression::ClassExpression(_)
)
)
} else {
false
Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_formatter/src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ impl<'a> FormatWrite<'a> for AstNode<'a, AwaitExpression<'a>> {

if is_callee_or_object {
let mut parent = self.parent.parent();
let mut ancestor_is_await = false;
loop {
match parent {
AstNodes::AwaitExpression(_)
Expand All @@ -436,7 +435,9 @@ impl<'a> FormatWrite<'a> for AstNode<'a, AwaitExpression<'a>> {
let indented = format_with(|f| write!(f, [soft_block_indent(&format_inner)]));

return if let AstNodes::AwaitExpression(expr) = parent {
if !expr.needs_parentheses(f) {
if !expr.needs_parentheses(f)
&& ExpressionLeftSide::leftmost(expr.argument()).span() != self.span()
{
return write!(f, [group(&indented)]);
}

Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_formatter/tests/fixtures/js/awaits/nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vite = await (
await import('vite')
).createServer({
appType
})
17 changes: 17 additions & 0 deletions crates/oxc_formatter/tests/fixtures/js/awaits/nested.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
vite = await (
await import('vite')
).createServer({
appType
})
==================== Output ====================
vite = await (
await import("vite")
).createServer({
appType,
});

===================== End =====================
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
expect(genCode(createVNodeCall(null, `"div"`, mockProps)))
.toMatchInlineSnapshot(`
`)

expect(genCode(createVNodeCall(null, `"div"`, mockProps)))
.toMatchInlineSnapshot(`
`).toMatchInlineSnapshot(`
`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
expect(genCode(createVNodeCall(null, `"div"`, mockProps)))
.toMatchInlineSnapshot(`
`)

expect(genCode(createVNodeCall(null, `"div"`, mockProps)))
.toMatchInlineSnapshot(`
`).toMatchInlineSnapshot(`
`)

==================== Output ====================
expect(
genCode(createVNodeCall(null, `"div"`, mockProps)),
).toMatchInlineSnapshot(`
`);

expect(genCode(createVNodeCall(null, `"div"`, mockProps)))
.toMatchInlineSnapshot(`
`)
.toMatchInlineSnapshot(`
`);

===================== End =====================
Loading