Skip to content

Commit

Permalink
Make (): unambiguous again
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Apr 20, 2022
1 parent f5ecb51 commit fb6ba84
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 18 deletions.
15 changes: 5 additions & 10 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4452,7 +4452,7 @@ namespace ts {
}

function tryParseParenthesizedArrowFunctionExpression(disallowReturnTypeInArrowFunction: boolean): Expression | undefined {
const triState = isParenthesizedArrowFunctionExpression(disallowReturnTypeInArrowFunction);
const triState = isParenthesizedArrowFunctionExpression();
if (triState === Tristate.False) {
// It's definitely not a parenthesized arrow function expression.
return undefined;
Expand All @@ -4471,9 +4471,9 @@ namespace ts {
// False -> There *cannot* be a parenthesized arrow function here.
// Unknown -> There *might* be a parenthesized arrow function here.
// Speculatively look ahead to be sure, and rollback if not.
function isParenthesizedArrowFunctionExpression(disallowReturnTypeInArrowFunction: boolean): Tristate {
function isParenthesizedArrowFunctionExpression(): Tristate {
if (token() === SyntaxKind.OpenParenToken || token() === SyntaxKind.LessThanToken || token() === SyntaxKind.AsyncKeyword) {
return lookAhead(() => isParenthesizedArrowFunctionExpressionWorker(disallowReturnTypeInArrowFunction));
return lookAhead(isParenthesizedArrowFunctionExpressionWorker);
}

if (token() === SyntaxKind.EqualsGreaterThanToken) {
Expand All @@ -4486,7 +4486,7 @@ namespace ts {
return Tristate.False;
}

function isParenthesizedArrowFunctionExpressionWorker(disallowReturnTypeInArrowFunction: boolean) {
function isParenthesizedArrowFunctionExpressionWorker() {
if (token() === SyntaxKind.AsyncKeyword) {
nextToken();
if (scanner.hasPrecedingLineBreak()) {
Expand All @@ -4508,13 +4508,8 @@ namespace ts {
// but this is probably what the user intended.
const third = nextToken();
switch (third) {
case SyntaxKind.ColonToken:
if (disallowReturnTypeInArrowFunction) {
// "a ? () : ..."
return Tristate.False;
}
return Tristate.True;
case SyntaxKind.EqualsGreaterThanToken:
case SyntaxKind.ColonToken:
case SyntaxKind.OpenBraceToken:
return Tristate.True;
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +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,18): error TS1109: Expression expected.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression13.ts(1,19): error TS1005: ';' expected.
tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression13.ts(1,19): error TS1109: Expression expected.


==== tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression13.ts (4 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.
~
!!! error TS1005: ';' expected.
!!! error TS1109: Expression expected.

Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ a ? () => a() : (): any => null;


//// [parserArrowFunctionExpression13.js]
a ? function () { return a(); } : ();
a ? function () { return a(); } : ;
(function (any) { return null; });
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ a ? () => a() : (): any => null;
>() => a() : () => any
>a() : any
>a : any
>() : any
> : any
>any => null : (any: any) => any
>any : any
Expand Down

0 comments on commit fb6ba84

Please sign in to comment.