mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-15 03:23:08 -06:00
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:
commit
3f3444be80
@ -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;
|
||||
}
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
//// [parserParenthesizedVariableAndFunctionInTernary.ts]
|
||||
let a: any;
|
||||
const c = true ? (a) : function() {};
|
||||
|
||||
|
||||
//// [parserParenthesizedVariableAndFunctionInTernary.js]
|
||||
var a;
|
||||
var c = true ? (a) : function () { };
|
||||
@ -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))
|
||||
|
||||
@ -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
|
||||
|
||||
@ -0,0 +1,2 @@
|
||||
let a: any;
|
||||
const c = true ? (a) : function() {};
|
||||
Loading…
x
Reference in New Issue
Block a user