mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-11 10:46:28 -05:00
Fix parsing of parenthesized functions in conditional expressions (#46960)
This commit is contained in:
@@ -4539,9 +4539,10 @@ namespace ts {
|
||||
// - "(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.
|
||||
// - "a ? (b): (function() {})" as well, but inside of a parenthesized type.
|
||||
//
|
||||
// So we need just a bit of lookahead to ensure that it can only be a signature.
|
||||
const hasJSDocFunctionType = type && isJSDocFunctionType(type);
|
||||
const hasJSDocFunctionType = type && (isJSDocFunctionType(type) || isParenthesizedTypeNode(type) && isJSDocFunctionType(type.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;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
//// [parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts]
|
||||
let a: any;
|
||||
const c = true ? (a) : (function() {});
|
||||
|
||||
|
||||
//// [parserParenthesizedVariableAndParenthesizedFunctionInTernary.js]
|
||||
var a;
|
||||
var c = true ? (a) : (function () { });
|
||||
@@ -0,0 +1,8 @@
|
||||
=== tests/cases/conformance/parser/ecmascript5/parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts ===
|
||||
let a: any;
|
||||
>a : Symbol(a, Decl(parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts, 0, 3))
|
||||
|
||||
const c = true ? (a) : (function() {});
|
||||
>c : Symbol(c, Decl(parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts, 1, 5))
|
||||
>a : Symbol(a, Decl(parserParenthesizedVariableAndParenthesizedFunctionInTernary.ts, 0, 3))
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
=== tests/cases/conformance/parser/ecmascript5/parserParenthesizedVariableAndParenthesizedFunctionInTernary.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
|
||||
>function() {} : () => void
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
let a: any;
|
||||
const c = true ? (a) : (function() {});
|
||||
Reference in New Issue
Block a user