Skip to content

Commit cdf12f9

Browse files
authored
Fix parsing of parenthesized functions in conditional expressions (microsoft#46960)
1 parent 0f3d0e0 commit cdf12f9

5 files changed

+33
-1
lines changed

src/compiler/parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4539,9 +4539,10 @@ namespace ts {
45394539
// - "(x,y)" is a comma expression parsed as a signature with two parameters.
45404540
// - "a ? (b): c" will have "(b):" parsed as a signature with a return type annotation.
45414541
// - "a ? (b): function() {}" will too, since function() is a valid JSDoc function type.
4542+
// - "a ? (b): (function() {})" as well, but inside of a parenthesized type.
45424543
//
45434544
// So we need just a bit of lookahead to ensure that it can only be a signature.
4544-
const hasJSDocFunctionType = type && isJSDocFunctionType(type);
4545+
const hasJSDocFunctionType = type && (isJSDocFunctionType(type) || isParenthesizedTypeNode(type) && isJSDocFunctionType(type.type));
45454546
if (!allowAmbiguity && token() !== SyntaxKind.EqualsGreaterThanToken && (hasJSDocFunctionType || token() !== SyntaxKind.OpenBraceToken)) {
45464547
// Returning undefined here will cause our caller to rewind to where we started from.
45474548
return undefined;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts]
2+
let a: any;
3+
const c = true ? (a) : (function() {});
4+
5+
6+
//// [parserParenthesizedVariableAndParenthesizedFunctionInTernary.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/parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts ===
2+
let a: any;
3+
>a : Symbol(a, Decl(parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts, 0, 3))
4+
5+
const c = true ? (a) : (function() {});
6+
>c : Symbol(c, Decl(parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts, 1, 5))
7+
>a : Symbol(a, Decl(parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts, 0, 3))
8+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/conformance/parser/ecmascript5/parserParenthesizedVariableAndParenthesizedFunctionInTernary.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+
>function() {} : () => void
13+
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)