From 76a4694bd44797bd4390fbdc514a324c97109c17 Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 3 Jul 2018 12:37:20 -0700 Subject: [PATCH] parser: Fix testing for missing list (#25411) * parser: Fix testing for missing list * Fix return type --- src/compiler/parser.ts | 17 +++++++++++++---- .../parseArrowFunctionWithFunctionReturnType.js | 6 ++++++ ...eArrowFunctionWithFunctionReturnType.symbols | 6 ++++++ ...rseArrowFunctionWithFunctionReturnType.types | 9 +++++++++ .../parseArrowFunctionWithFunctionReturnType.ts | 1 + 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.js create mode 100644 tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.symbols create mode 100644 tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.types create mode 100644 tests/cases/compiler/parseArrowFunctionWithFunctionReturnType.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index bfc502ce2d..489894631f 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2090,8 +2090,18 @@ namespace ts { return result; } - function createMissingList(): NodeArray { - return createNodeArray([], getNodePos()); + interface MissingList extends NodeArray { + isMissingList: true; + } + + function createMissingList(): MissingList { + const list = createNodeArray([], getNodePos()) as MissingList; + list.isMissingList = true; + return list; + } + + function isMissingList(arr: NodeArray): boolean { + return !!(arr as MissingList).isMissingList; } function parseBracketedList(kind: ParsingContext, parseElement: () => T, open: SyntaxKind, close: SyntaxKind): NodeArray { @@ -2260,8 +2270,7 @@ namespace ts { case SyntaxKind.FunctionType: case SyntaxKind.ConstructorType: { const { parameters, type } = node as FunctionOrConstructorTypeNode; - // parameters.pos === parameters.end only if we used parseMissingList, else should contain at least `()` - return parameters.pos === parameters.end || typeHasArrowFunctionBlockingParseError(type); + return isMissingList(parameters) || typeHasArrowFunctionBlockingParseError(type); } case SyntaxKind.ParenthesizedType: return typeHasArrowFunctionBlockingParseError((node as ParenthesizedTypeNode).type); diff --git a/tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.js b/tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.js new file mode 100644 index 0000000000..1e8b8db346 --- /dev/null +++ b/tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.js @@ -0,0 +1,6 @@ +//// [parseArrowFunctionWithFunctionReturnType.ts] +const fn = (): (() => T) => null as any; + + +//// [parseArrowFunctionWithFunctionReturnType.js] +var fn = function () { return null; }; diff --git a/tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.symbols b/tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.symbols new file mode 100644 index 0000000000..fc34d57047 --- /dev/null +++ b/tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.symbols @@ -0,0 +1,6 @@ +=== tests/cases/compiler/parseArrowFunctionWithFunctionReturnType.ts === +const fn = (): (() => T) => null as any; +>fn : Symbol(fn, Decl(parseArrowFunctionWithFunctionReturnType.ts, 0, 5)) +>T : Symbol(T, Decl(parseArrowFunctionWithFunctionReturnType.ts, 0, 12)) +>T : Symbol(T, Decl(parseArrowFunctionWithFunctionReturnType.ts, 0, 12)) + diff --git a/tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.types b/tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.types new file mode 100644 index 0000000000..3c50899ceb --- /dev/null +++ b/tests/baselines/reference/parseArrowFunctionWithFunctionReturnType.types @@ -0,0 +1,9 @@ +=== tests/cases/compiler/parseArrowFunctionWithFunctionReturnType.ts === +const fn = (): (() => T) => null as any; +>fn : () => () => T +>(): (() => T) => null as any : () => () => T +>T : T +>T : T +>null as any : any +>null : null + diff --git a/tests/cases/compiler/parseArrowFunctionWithFunctionReturnType.ts b/tests/cases/compiler/parseArrowFunctionWithFunctionReturnType.ts new file mode 100644 index 0000000000..60c55b9c3e --- /dev/null +++ b/tests/cases/compiler/parseArrowFunctionWithFunctionReturnType.ts @@ -0,0 +1 @@ +const fn = (): (() => T) => null as any;