Merge pull request #30699 from andrewbranch/bug/30635

Fix ternaries where "true" is a parenthesized variable and "false" is a function expression
This commit is contained in:
Andrew Branch 2019-04-02 14:03:11 -07:00 committed by GitHub
commit 3f3444be80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 1 deletions

View File

@ -3693,9 +3693,11 @@ namespace ts {
// - "(x = 10)" is an assignment expression parsed as a signature with a default parameter value.
// - "(x,y)" is a comma expression parsed as a signature with two parameters.
// - "a ? (b): c" will have "(b):" parsed as a signature with a return type annotation.
// - "a ? (b): function() {}" will too, since function() is a valid JSDoc function type.
//
// So we need just a bit of lookahead to ensure that it can only be a signature.
if (!allowAmbiguity && token() !== SyntaxKind.EqualsGreaterThanToken && token() !== SyntaxKind.OpenBraceToken) {
const hasJSDocFunctionType = node.type && isJSDocFunctionType(node.type);
if (!allowAmbiguity && token() !== SyntaxKind.EqualsGreaterThanToken && (hasJSDocFunctionType || token() !== SyntaxKind.OpenBraceToken)) {
// Returning undefined here will cause our caller to rewind to where we started from.
return undefined;
}

View File

@ -0,0 +1,8 @@
//// [parserParenthesizedVariableAndFunctionInTernary.ts]
let a: any;
const c = true ? (a) : function() {};
//// [parserParenthesizedVariableAndFunctionInTernary.js]
var a;
var c = true ? (a) : function () { };

View File

@ -0,0 +1,8 @@
=== tests/cases/conformance/parser/ecmascript5/parserParenthesizedVariableAndFunctionInTernary.ts ===
let a: any;
>a : Symbol(a, Decl(parserParenthesizedVariableAndFunctionInTernary.ts, 0, 3))
const c = true ? (a) : function() {};
>c : Symbol(c, Decl(parserParenthesizedVariableAndFunctionInTernary.ts, 1, 5))
>a : Symbol(a, Decl(parserParenthesizedVariableAndFunctionInTernary.ts, 0, 3))

View File

@ -0,0 +1,12 @@
=== tests/cases/conformance/parser/ecmascript5/parserParenthesizedVariableAndFunctionInTernary.ts ===
let a: any;
>a : any
const c = true ? (a) : function() {};
>c : any
>true ? (a) : function() {} : any
>true : true
>(a) : any
>a : any
>function() {} : () => void

View File

@ -0,0 +1,2 @@
let a: any;
const c = true ? (a) : function() {};