Skip to content

Commit 797e00d

Browse files
jakebaileymprobst
authored andcommitted
Unwrap parens when checking for JSDocFunctionType in conditional expression (microsoft#46962)
1 parent 98e6c45 commit 797e00d

5 files changed

+25
-2
lines changed

src/compiler/parser.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4539,10 +4539,16 @@ 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.
4542+
// - "a ? (b): (function() {})" as well, but inside of a parenthesized type with an arbitrary amount of nesting.
45434543
//
45444544
// So we need just a bit of lookahead to ensure that it can only be a signature.
4545-
const hasJSDocFunctionType = type && (isJSDocFunctionType(type) || isParenthesizedTypeNode(type) && isJSDocFunctionType(type.type));
4545+
4546+
let unwrappedType = type;
4547+
while (unwrappedType?.kind === SyntaxKind.ParenthesizedType) {
4548+
unwrappedType = (unwrappedType as ParenthesizedTypeNode).type; // Skip parens if need be
4549+
}
4550+
4551+
const hasJSDocFunctionType = unwrappedType && isJSDocFunctionType(unwrappedType);
45464552
if (!allowAmbiguity && token() !== SyntaxKind.EqualsGreaterThanToken && (hasJSDocFunctionType || token() !== SyntaxKind.OpenBraceToken)) {
45474553
// Returning undefined here will cause our caller to rewind to where we started from.
45484554
return undefined;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//// [parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts]
22
let a: any;
33
const c = true ? (a) : (function() {});
4+
const d = true ? (a) : ((function() {}));
45

56

67
//// [parserParenthesizedVariableAndParenthesizedFunctionInTernary.js]
78
var a;
89
var c = true ? (a) : (function () { });
10+
var d = true ? (a) : ((function () { }));

tests/baselines/reference/parserParenthesizedVariableAndParenthesizedFunctionInTernary.symbols

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ const c = true ? (a) : (function() {});
66
>c : Symbol(c, Decl(parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts, 1, 5))
77
>a : Symbol(a, Decl(parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts, 0, 3))
88

9+
const d = true ? (a) : ((function() {}));
10+
>d : Symbol(d, Decl(parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts, 2, 5))
11+
>a : Symbol(a, Decl(parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts, 0, 3))
12+

tests/baselines/reference/parserParenthesizedVariableAndParenthesizedFunctionInTernary.types

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@ const c = true ? (a) : (function() {});
1111
>(function() {}) : () => void
1212
>function() {} : () => void
1313

14+
const d = true ? (a) : ((function() {}));
15+
>d : any
16+
>true ? (a) : ((function() {})) : any
17+
>true : true
18+
>(a) : any
19+
>a : any
20+
>((function() {})) : () => void
21+
>(function() {}) : () => void
22+
>function() {} : () => void
23+
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
let a: any;
22
const c = true ? (a) : (function() {});
3+
const d = true ? (a) : ((function() {}));

0 commit comments

Comments
 (0)