Reduce aggression of parenthesis removal in ts transform (#24073)

This commit is contained in:
Wesley Wigham 2018-05-14 11:20:04 -07:00 committed by GitHub
parent 560371d7a0
commit b58e4e1fa1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 0 deletions

View file

@ -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);
}

View file

@ -0,0 +1,11 @@
//// [parenthesizedArrowExpressionASI.ts]
const x = (a: any[]) => (
// comment
undefined as number
);
//// [parenthesizedArrowExpressionASI.js]
var x = function (a) { return (
// comment
undefined); };

View file

@ -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)
);

View file

@ -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
);

View file

@ -0,0 +1,4 @@
const x = (a: any[]) => (
// comment
undefined as number
);