mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
fix(formatter): keep arrow conditional body parens before a leading function/class (#28710)
`should_add_parens` skipped the parens of an arrow's conditional body
when it starts with an object, function or class, assuming the leading
node prints its own.
Only an object does in an arrow body, so `() => (function () {}).a ? b :
c` printed with no parens at all.
Matches Prettier: skip them only for a leading object.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b6f1ac9b05
commit
dbcc358aee
3 files changed
+27
-13
No files matched your search
@@ -683,19 +683,10 @@ fn has_own_line_comment_before_body<'a>(
|
||||
}
|
||||
|
||||
fn should_add_parens(expression: AstNode<'_, '_, Expression<'_>>) -> bool {
|
||||
// Add parentheses to avoid confusion between `a => b ? c : d` and `a <= b ? c : d`
|
||||
// but only if the body isn't an object/function or class expression because parentheses are always required in that
|
||||
// case and added by the object expression itself
|
||||
if matches!(&*expression, Expression::ConditionalExpression(_)) {
|
||||
!matches!(
|
||||
ExpressionLeftSide::leftmost(&expression),
|
||||
Expression::ObjectExpression(_)
|
||||
| Expression::FunctionExpression(_)
|
||||
| Expression::ClassExpression(_)
|
||||
)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
// Add parentheses to avoid confusion between `a => b ? c : d` and `a <= b ? c : d`,
|
||||
// unless the body starts with an object, which always brings one (its own or an ancestor's).
|
||||
matches!(&*expression, Expression::ConditionalExpression(_))
|
||||
&& !matches!(ExpressionLeftSide::leftmost(&expression), Expression::ObjectExpression(_))
|
||||
}
|
||||
|
||||
fn has_rest_object_or_array_parameter(params: &FormalParameters) -> bool {
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
x = () => (function () {}.a ? b : c);
|
||||
x = () => () => (function () {}.a ? b : c);
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
---
|
||||
source: crates/oxc_formatter/tests/fixtures/mod.rs
|
||||
---
|
||||
==================== Input ====================
|
||||
x = () => (function () {}.a ? b : c);
|
||||
x = () => () => (function () {}.a ? b : c);
|
||||
|
||||
==================== Output ====================
|
||||
------------------
|
||||
{ printWidth: 80 }
|
||||
------------------
|
||||
x = () => (function () {}.a ? b : c);
|
||||
x = () => () => (function () {}.a ? b : c);
|
||||
|
||||
-------------------
|
||||
{ printWidth: 100 }
|
||||
-------------------
|
||||
x = () => (function () {}.a ? b : c);
|
||||
x = () => () => (function () {}.a ? b : c);
|
||||
|
||||
===================== End =====================
|
||||
Reference in new issue
Block a user