Skip to content

Commit c841ac1

Browse files
author
Declan Vong
authored
fix: unwrap outer parens if it's the RHS of a var declaration (#458)
1 parent 8254ac0 commit c841ac1

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/generation/generate.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,12 +2722,26 @@ fn should_skip_paren_expr(node: &ParenExpr, context: &Context) -> bool {
27222722
return true;
27232723
}
27242724

2725+
// skip explicitly parsing this as a paren expr as that will be handled
2726+
// in the JSX element/fragment and it might collapse back to not having a paren expr
2727+
if matches!(node.expr.kind(), NodeKind::JSXElement | NodeKind::JSXFragment) {
2728+
return is_jsx_paren_expr_handled_node(node.expr.into(), context);
2729+
}
2730+
27252731
if let Node::AssignExpr(assign_expr) = parent {
27262732
if assign_expr.right.range().contains(&node.range()) {
27272733
return true;
27282734
}
27292735
}
27302736

2737+
if let Node::VarDeclarator(var_decl) = parent {
2738+
if let Some(init) = var_decl.init {
2739+
if init.range().contains(&node.range()) {
2740+
return true;
2741+
}
2742+
}
2743+
}
2744+
27312745
// skip over an expr or spread if not a spread
27322746
if let Some(expr_or_spread) = parent.to::<ExprOrSpread>() {
27332747
// these should only appear in these nodes
@@ -2747,9 +2761,7 @@ fn should_skip_paren_expr(node: &ParenExpr, context: &Context) -> bool {
27472761
}
27482762
}
27492763

2750-
// skip explicitly parsing this as a paren expr as that will be handled
2751-
// in the JSX element/fragment and it might collapse back to not having a paren expr
2752-
is_jsx_paren_expr_handled_node(node.expr.into(), context)
2764+
false
27532765
}
27542766

27552767
fn gen_sequence_expr<'a>(node: &'a SeqExpr, context: &mut Context<'a>) -> PrintItems {

tests/specs/expressions/ParenExpr/ParenExpr_All.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ const test = (
2626
[expect]
2727
(x.test as unknown) = 6;
2828

29+
== should not keep paren expr on right hand side of assignment ==
30+
const a = (1);
31+
const b = ((((1))));
32+
const c = (1 + 1) + 1;
33+
const d = 1 + (1 + 1);
34+
// except here it should because of the type assertion
35+
const e = /** @type {number} */ (((1)));
36+
37+
[expect]
38+
const a = 1;
39+
const b = 1;
40+
const c = (1 + 1) + 1;
41+
const d = 1 + (1 + 1);
42+
// except here it should because of the type assertion
43+
const e = /** @type {number} */ (1);
44+
2945
== should ignore paren exprs within paren exprs ==
3046
(((test)));
3147
(

0 commit comments

Comments
 (0)