diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index b358b13b5023e..67c9ddfc788f9 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2534,6 +2534,11 @@ namespace ts { // we can safely elide the parentheses here, as a new synthetic // ParenthesizedExpression will be inserted if we remove parentheses too // aggressively. + // HOWEVER - if there are leading comments on the expression itself, to handle ASI + // correctly for return and throw, we must keep the parenthesis + if (length(getLeadingCommentRangesOfNode(expression, currentSourceFile))) { + return updateParen(node, expression); + } return createPartiallyEmittedExpression(expression, node); } diff --git a/tests/baselines/reference/parenthesizedArrowExpressionASI.js b/tests/baselines/reference/parenthesizedArrowExpressionASI.js new file mode 100644 index 0000000000000..d27ddfb1216fa --- /dev/null +++ b/tests/baselines/reference/parenthesizedArrowExpressionASI.js @@ -0,0 +1,11 @@ +//// [parenthesizedArrowExpressionASI.ts] +const x = (a: any[]) => ( + // comment + undefined as number +); + + +//// [parenthesizedArrowExpressionASI.js] +var x = function (a) { return ( +// comment +undefined); }; diff --git a/tests/baselines/reference/parenthesizedArrowExpressionASI.symbols b/tests/baselines/reference/parenthesizedArrowExpressionASI.symbols new file mode 100644 index 0000000000000..25b3669cca30b --- /dev/null +++ b/tests/baselines/reference/parenthesizedArrowExpressionASI.symbols @@ -0,0 +1,11 @@ +=== tests/cases/compiler/parenthesizedArrowExpressionASI.ts === +const x = (a: any[]) => ( +>x : Symbol(x, Decl(parenthesizedArrowExpressionASI.ts, 0, 5)) +>a : Symbol(a, Decl(parenthesizedArrowExpressionASI.ts, 0, 11)) + + // comment + undefined as number +>undefined : Symbol(undefined) + +); + diff --git a/tests/baselines/reference/parenthesizedArrowExpressionASI.types b/tests/baselines/reference/parenthesizedArrowExpressionASI.types new file mode 100644 index 0000000000000..43af9b4b4f827 --- /dev/null +++ b/tests/baselines/reference/parenthesizedArrowExpressionASI.types @@ -0,0 +1,14 @@ +=== tests/cases/compiler/parenthesizedArrowExpressionASI.ts === +const x = (a: any[]) => ( +>x : (a: any[]) => number +>(a: any[]) => ( // comment undefined as number) : (a: any[]) => number +>a : any[] +>( // comment undefined as number) : number + + // comment + undefined as number +>undefined as number : number +>undefined : undefined + +); + diff --git a/tests/cases/compiler/parenthesizedArrowExpressionASI.ts b/tests/cases/compiler/parenthesizedArrowExpressionASI.ts new file mode 100644 index 0000000000000..373d448f9fc6b --- /dev/null +++ b/tests/cases/compiler/parenthesizedArrowExpressionASI.ts @@ -0,0 +1,4 @@ +const x = (a: any[]) => ( + // comment + undefined as number +);