Skip to content

Commit feeea64

Browse files
weswighamsandersn
authored andcommitted
Reduce aggression of parenthesis removal in ts transform (#24073)
1 parent 0d5658e commit feeea64

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

src/compiler/transformers/ts.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,6 +2534,11 @@ namespace ts {
25342534
// we can safely elide the parentheses here, as a new synthetic
25352535
// ParenthesizedExpression will be inserted if we remove parentheses too
25362536
// aggressively.
2537+
// HOWEVER - if there are leading comments on the expression itself, to handle ASI
2538+
// correctly for return and throw, we must keep the parenthesis
2539+
if (length(getLeadingCommentRangesOfNode(expression, currentSourceFile))) {
2540+
return updateParen(node, expression);
2541+
}
25372542
return createPartiallyEmittedExpression(expression, node);
25382543
}
25392544

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [parenthesizedArrowExpressionASI.ts]
2+
const x = (a: any[]) => (
3+
// comment
4+
undefined as number
5+
);
6+
7+
8+
//// [parenthesizedArrowExpressionASI.js]
9+
var x = function (a) { return (
10+
// comment
11+
undefined); };
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/parenthesizedArrowExpressionASI.ts ===
2+
const x = (a: any[]) => (
3+
>x : Symbol(x, Decl(parenthesizedArrowExpressionASI.ts, 0, 5))
4+
>a : Symbol(a, Decl(parenthesizedArrowExpressionASI.ts, 0, 11))
5+
6+
// comment
7+
undefined as number
8+
>undefined : Symbol(undefined)
9+
10+
);
11+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/parenthesizedArrowExpressionASI.ts ===
2+
const x = (a: any[]) => (
3+
>x : (a: any[]) => number
4+
>(a: any[]) => ( // comment undefined as number) : (a: any[]) => number
5+
>a : any[]
6+
>( // comment undefined as number) : number
7+
8+
// comment
9+
undefined as number
10+
>undefined as number : number
11+
>undefined : undefined
12+
13+
);
14+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const x = (a: any[]) => (
2+
// comment
3+
undefined as number
4+
);

0 commit comments

Comments
 (0)