Skip to content

Commit 3f3444b

Browse files
authored
Merge pull request microsoft#30699 from andrewbranch/bug/30635
Fix ternaries where "true" is a parenthesized variable and "false" is a function expression
2 parents 46ae059 + 96660f6 commit 3f3444b

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

src/compiler/parser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3693,9 +3693,11 @@ namespace ts {
36933693
// - "(x = 10)" is an assignment expression parsed as a signature with a default parameter value.
36943694
// - "(x,y)" is a comma expression parsed as a signature with two parameters.
36953695
// - "a ? (b): c" will have "(b):" parsed as a signature with a return type annotation.
3696+
// - "a ? (b): function() {}" will too, since function() is a valid JSDoc function type.
36963697
//
36973698
// So we need just a bit of lookahead to ensure that it can only be a signature.
3698-
if (!allowAmbiguity && token() !== SyntaxKind.EqualsGreaterThanToken && token() !== SyntaxKind.OpenBraceToken) {
3699+
const hasJSDocFunctionType = node.type && isJSDocFunctionType(node.type);
3700+
if (!allowAmbiguity && token() !== SyntaxKind.EqualsGreaterThanToken && (hasJSDocFunctionType || token() !== SyntaxKind.OpenBraceToken)) {
36993701
// Returning undefined here will cause our caller to rewind to where we started from.
37003702
return undefined;
37013703
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [parserParenthesizedVariableAndFunctionInTernary.ts]
2+
let a: any;
3+
const c = true ? (a) : function() {};
4+
5+
6+
//// [parserParenthesizedVariableAndFunctionInTernary.js]
7+
var a;
8+
var c = true ? (a) : function () { };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/parser/ecmascript5/parserParenthesizedVariableAndFunctionInTernary.ts ===
2+
let a: any;
3+
>a : Symbol(a, Decl(parserParenthesizedVariableAndFunctionInTernary.ts, 0, 3))
4+
5+
const c = true ? (a) : function() {};
6+
>c : Symbol(c, Decl(parserParenthesizedVariableAndFunctionInTernary.ts, 1, 5))
7+
>a : Symbol(a, Decl(parserParenthesizedVariableAndFunctionInTernary.ts, 0, 3))
8+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/conformance/parser/ecmascript5/parserParenthesizedVariableAndFunctionInTernary.ts ===
2+
let a: any;
3+
>a : any
4+
5+
const c = true ? (a) : function() {};
6+
>c : any
7+
>true ? (a) : function() {} : any
8+
>true : true
9+
>(a) : any
10+
>a : any
11+
>function() {} : () => void
12+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let a: any;
2+
const c = true ? (a) : function() {};

0 commit comments

Comments
 (0)