Skip to content

Demo: make colon always terminate in conditional expression #48791

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
9 changes: 2 additions & 7 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression13.ts(1,1): error TS2304: Cannot find name 'a'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression13.ts(1,11): error TS2304: Cannot find name 'a'.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression13.ts(1,19): error TS1109: Expression expected.


==== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression13.ts (2 errors) ====
==== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression13.ts (3 errors) ====
a ? () => a() : (): any => null;
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS1109: Expression expected.

3 changes: 2 additions & 1 deletion tests/baselines/reference/parserArrowFunctionExpression13.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ a ? () => a() : (): any => null;


//// [parserArrowFunctionExpression13.js]
a ? function () { return a(); } : function () { return null; };
a ? function () { return a(); } : ;
(function (any) { return null; });
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
=== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression13.ts ===
a ? () => a() : (): any => null;
No type information for this code.
No type information for this code.
>any : Symbol(any, Decl(parserArrowFunctionExpression13.ts, 0, 19))
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
=== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression13.ts ===
a ? () => a() : (): any => null;
>a ? () => a() : (): any => null : () => any
>a ? () => a() : () : any
>a : any
>() => a() : () => any
>a() : any
>a : any
>(): any => null : () => any
> : any
>any => null : (any: any) => any
>any : any
>null : null

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