diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 265099bd6e7..2757c15c524 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -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; } diff --git a/tests/baselines/reference/parserParenthesizedVariableAndFunctionInTernary.js b/tests/baselines/reference/parserParenthesizedVariableAndFunctionInTernary.js new file mode 100644 index 00000000000..dde6826c59c --- /dev/null +++ b/tests/baselines/reference/parserParenthesizedVariableAndFunctionInTernary.js @@ -0,0 +1,8 @@ +//// [parserParenthesizedVariableAndFunctionInTernary.ts] +let a: any; +const c = true ? (a) : function() {}; + + +//// [parserParenthesizedVariableAndFunctionInTernary.js] +var a; +var c = true ? (a) : function () { }; diff --git a/tests/baselines/reference/parserParenthesizedVariableAndFunctionInTernary.symbols b/tests/baselines/reference/parserParenthesizedVariableAndFunctionInTernary.symbols new file mode 100644 index 00000000000..3b64c950750 --- /dev/null +++ b/tests/baselines/reference/parserParenthesizedVariableAndFunctionInTernary.symbols @@ -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)) + diff --git a/tests/baselines/reference/parserParenthesizedVariableAndFunctionInTernary.types b/tests/baselines/reference/parserParenthesizedVariableAndFunctionInTernary.types new file mode 100644 index 00000000000..d3cd1f4fbb6 --- /dev/null +++ b/tests/baselines/reference/parserParenthesizedVariableAndFunctionInTernary.types @@ -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 + diff --git a/tests/cases/conformance/parser/ecmascript5/parserParenthesizedVariableAndFunctionInTernary.ts b/tests/cases/conformance/parser/ecmascript5/parserParenthesizedVariableAndFunctionInTernary.ts new file mode 100644 index 00000000000..c2bca11d934 --- /dev/null +++ b/tests/cases/conformance/parser/ecmascript5/parserParenthesizedVariableAndFunctionInTernary.ts @@ -0,0 +1,2 @@ +let a: any; +const c = true ? (a) : function() {};