Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow return type in conditional if body is followed by a colon #48788

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4695,24 +4695,7 @@ namespace ts {
}
}

// Given:
// x ? y => ({ y }) : z => ({ z })
// We try to parse the body of the first arrow function by looking at:
// ({ y }) : z => ({ z })
// This is a valid arrow function with "z" as the return type.
//
// But, if we're in the true side of a conditional expression, this colon
// terminates the expression, so we cannot allow a return type if we aren't
// certain whether or not the preceding text was parsed as a parameter list.
//
// For example,
// a() ? (b: number, c?: string): void => d() : e
// is determined by isParenthesizedArrowFunctionExpression to unambiguously
// be an arrow expression, so we allow a return type.
if (disallowReturnTypeInArrowFunction && token() === SyntaxKind.ColonToken) {
return undefined;
}

const hasReturnColon = token() === SyntaxKind.ColonToken;
const type = parseReturnType(SyntaxKind.ColonToken, /*isType*/ false);
if (type && !allowAmbiguity && typeHasArrowFunctionBlockingParseError(type)) {
return undefined;
Expand Down Expand Up @@ -4748,6 +4731,31 @@ namespace ts {
? parseArrowFunctionExpressionBody(some(modifiers, isAsyncModifier), disallowReturnTypeInArrowFunction)
: parseIdentifier();

// Given:
// x ? y => ({ y }) : z => ({ z })
// We try to parse the body of the first arrow function by looking at:
// ({ y }) : z => ({ z })
// This is a valid arrow function with "z" as the return type.
//
// But, if we're in the true side of a conditional expression, this colon
// terminates the expression, so we cannot allow a return type if we aren't
// certain whether or not the preceding text was parsed as a parameter list.
//
// For example,
// a() ? (b: number, c?: string): void => d() : e
// is determined by isParenthesizedArrowFunctionExpression to unambiguously
// be an arrow expression, so we allow a return type.
if (disallowReturnTypeInArrowFunction && hasReturnColon) {
// However, if the arrow function we were able to parse is followed by another colon
// as in:
// a ? (x): string => x : null
// Then allow the arrow function, and treat the second colon as terminating
// the conditional expression.
if (token() !== SyntaxKind.ColonToken) {
return undefined;
}
}

const node = factory.createArrowFunction(modifiers, typeParameters, parameters, type, equalsGreaterThanToken, body);
return withJSDoc(finishNode(node, pos), hasJSDoc);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression10.ts(1,1): error TS2304: Cannot find name 'a'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression10.ts(1,6): error TS2304: Cannot find name 'b'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression10.ts(1,11): error TS2304: Cannot find name 'c'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression10.ts(1,17): error TS2304: Cannot find name 'd'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression10.ts(1,20): error TS1005: ';' expected.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression10.ts(1,27): error TS2304: Cannot find name 'f'.


==== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression10.ts (5 errors) ====
==== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression10.ts (4 errors) ====
a ? (b) : c => (d) : e => f
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS2304: Cannot find name 'b'.
~
!!! error TS2304: Cannot find name 'c'.
~
!!! error TS2304: Cannot find name 'd'.
~
!!! error TS1005: ';' expected.
~
!!! error TS2304: Cannot find name 'f'.

3 changes: 1 addition & 2 deletions tests/baselines/reference/parserArrowFunctionExpression10.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ a ? (b) : c => (d) : e => f


//// [parserArrowFunctionExpression10.js]
a ? (b) : function (c) { return (d); };
(function (e) { return f; });
a ? function (b) { return (d); } : function (e) { return f; };
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
=== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression10.ts ===
a ? (b) : c => (d) : e => f
>c : Symbol(c, Decl(parserArrowFunctionExpression10.ts, 0, 9))
>b : Symbol(b, Decl(parserArrowFunctionExpression10.ts, 0, 5))
>c : Symbol(c)
>e : Symbol(e, Decl(parserArrowFunctionExpression10.ts, 0, 20))

Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
=== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression10.ts ===
a ? (b) : c => (d) : e => f
>a ? (b) : c => (d) : any
>a ? (b) : c => (d) : e => f : (b: any) => c
>a : any
>(b) : any
>(b) : c => (d) : (b: any) => c
>b : any
>c => (d) : (c: any) => any
>c : any
>(d) : any
>d : any
>e => f : (e: any) => any
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/parserArrowFunctionExpression15.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [parserArrowFunctionExpression15.ts]
false ? (param): string => param : null


//// [parserArrowFunctionExpression15.js]
false ? function (param) { return param; } : null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression15.ts ===
false ? (param): string => param : null
>param : Symbol(param, Decl(parserArrowFunctionExpression15.ts, 0, 9))
>param : Symbol(param, Decl(parserArrowFunctionExpression15.ts, 0, 9))

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression15.ts ===
false ? (param): string => param : null
>false ? (param): string => param : null : (param: any) => string
>false : false
>(param): string => param : (param: any) => string
>param : any
>param : any
>null : null

6 changes: 6 additions & 0 deletions tests/baselines/reference/parserArrowFunctionExpression16.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [parserArrowFunctionExpression16.ts]
true ? false ? (param): string => param : null : null


//// [parserArrowFunctionExpression16.js]
true ? false ? function (param) { return param; } : null : null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression16.ts ===
true ? false ? (param): string => param : null : null
>param : Symbol(param, Decl(parserArrowFunctionExpression16.ts, 0, 16))
>param : Symbol(param, Decl(parserArrowFunctionExpression16.ts, 0, 16))

12 changes: 12 additions & 0 deletions tests/baselines/reference/parserArrowFunctionExpression16.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression16.ts ===
true ? false ? (param): string => param : null : null
>true ? false ? (param): string => param : null : null : (param: any) => string
>true : true
>false ? (param): string => param : null : (param: any) => string
>false : false
>(param): string => param : (param: any) => string
>param : any
>param : any
>null : null
>null : null

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false ? (param): string => param : null
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true ? false ? (param): string => param : null : null