Skip to content

Demo: make colon always terminate in conditional expression on true side #48792

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

Closed
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
15 changes: 5 additions & 10 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4369,7 +4369,7 @@ namespace ts {
}

// It wasn't an assignment or a lambda. This is a conditional expression:
return parseConditionalExpressionRest(expr, pos);
return parseConditionalExpressionRest(expr, pos, disallowReturnTypeInArrowFunction);
}

function isYieldExpression(): boolean {
Expand Down Expand Up @@ -4463,7 +4463,7 @@ namespace ts {
// it out, but don't allow any ambiguity, and return 'undefined' if this could be an
// expression instead.
return triState === Tristate.True ?
parseParenthesizedArrowFunctionExpression(/*allowAmbiguity*/ true, /*disallowReturnTypeInArrowFunction*/ false) :
parseParenthesizedArrowFunctionExpression(/*allowAmbiguity*/ true, disallowReturnTypeInArrowFunction) :
tryParse(() => parsePossibleParenthesizedArrowFunctionExpression(disallowReturnTypeInArrowFunction));
}

Expand Down Expand Up @@ -4702,13 +4702,8 @@ namespace ts {
// 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
// terminates the expression, so we cannot allow a return type, even 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;
}
Expand Down Expand Up @@ -4788,7 +4783,7 @@ namespace ts {
return node;
}

function parseConditionalExpressionRest(leftOperand: Expression, pos: number): Expression {
function parseConditionalExpressionRest(leftOperand: Expression, pos: number, disallowReturnTypeInArrowFunction: boolean): Expression {
// Note: we are passed in an expression which was produced from parseBinaryExpressionOrHigher.
const questionToken = parseOptionalToken(SyntaxKind.QuestionToken);
if (!questionToken) {
Expand All @@ -4805,7 +4800,7 @@ namespace ts {
doOutsideOfContext(disallowInAndDecoratorContext, () => parseAssignmentExpressionOrHigher(/*disallowReturnTypeInArrowFunction*/ true)),
colonToken = parseExpectedToken(SyntaxKind.ColonToken),
nodeIsPresent(colonToken)
? parseAssignmentExpressionOrHigher(/*disallowReturnTypeInArrowFunction*/ true)
? parseAssignmentExpressionOrHigher(disallowReturnTypeInArrowFunction) // inherit disallowReturnTypeInArrowFunction in false side
: createMissingNode(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ false, Diagnostics._0_expected, tokenToString(SyntaxKind.ColonToken))
),
pos
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,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,22): error TS2304: Cannot find name 'e'.
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 'd'.
~
!!! error TS1005: ';' expected.
~
!!! error TS2304: Cannot find name 'e'.
~
!!! 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 ? (b) : function (c) { return function (d) { 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))
>e : Symbol(e, Decl(parserArrowFunctionExpression10.ts, 0, 20))
>d : Symbol(d, Decl(parserArrowFunctionExpression10.ts, 0, 16))
>e : Symbol(e)

Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
=== 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 : any
>a : any
>(b) : any
>b : any
>c => (d) : (c: any) => any
>c => (d) : e => f : (c: any) => (d: any) => e
>c : any
>(d) : any
>(d) : e => f : (d: any) => e
>d : any
>e => f : (e: any) => any
>e : any
>f : any

Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression14.ts(1,1): error TS2304: Cannot find name 'a'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression14.ts(1,30): error TS1109: Expression expected.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression14.ts(1,37): error TS1109: Expression expected.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression14.ts(1,40): error TS2304: Cannot find name 'd'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression14.ts(1,44): error TS1005: ';' expected.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression14.ts(1,46): error TS2304: Cannot find name 'e'.


==== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression14.ts (3 errors) ====
==== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression14.ts (6 errors) ====
a() ? (b: number, c?: string): void => d() : e;
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS1109: Expression expected.
~~
!!! error TS1109: Expression expected.
~
!!! error TS2304: Cannot find name 'd'.
~
!!! error TS1005: ';' expected.
~
!!! error TS2304: Cannot find name 'e'.

4 changes: 3 additions & 1 deletion tests/baselines/reference/parserArrowFunctionExpression14.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ a() ? (b: number, c?: string): void => d() : e;


//// [parserArrowFunctionExpression14.js]
a() ? function (b, c) { return d(); } : e;
a() ? : void ;
d();
e;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
=== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression14.ts ===
a() ? (b: number, c?: string): void => d() : e;
>b : Symbol(b, Decl(parserArrowFunctionExpression14.ts, 0, 7))
>c : Symbol(c, Decl(parserArrowFunctionExpression14.ts, 0, 17))

No type information for this code.
No type information for this code.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
=== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression14.ts ===
a() ? (b: number, c?: string): void => d() : e;
>a() ? (b: number, c?: string): void => d() : e : any
>a() ? (b: number, c?: string): void : any
>a() : any
>a : any
>(b: number, c?: string): void => d() : (b: number, c?: string) => void
>b : number
>c : string
> : any
>void : undefined
> : any
>d() : any
>d : any
>e : any
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression15.ts(1,10): error TS2304: Cannot find name 'param'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression15.ts(1,28): error TS2304: Cannot find name 'param'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression15.ts(1,34): error TS1005: ';' expected.


==== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression15.ts (3 errors) ====
false ? (param): string => param : null
~~~~~
!!! error TS2304: Cannot find name 'param'.
~~~~~
!!! error TS2304: Cannot find name 'param'.
~
!!! error TS1005: ';' expected.

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


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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression16.ts(1,17): error TS2304: Cannot find name 'param'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression16.ts(1,35): error TS2304: Cannot find name 'param'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression16.ts(1,48): error TS1005: ';' expected.


==== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression16.ts (3 errors) ====
true ? false ? (param): string => param : null : null
~~~~~
!!! error TS2304: Cannot find name 'param'.
~~~~~
!!! error TS2304: Cannot find name 'param'.
~
!!! error TS1005: ';' expected.

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


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

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