From d7fb48d2c6671b296eb52de9bf37f7011cf78e38 Mon Sep 17 00:00:00 2001 From: Jack Bates Date: Sat, 2 Nov 2019 12:46:16 -0700 Subject: [PATCH] Add tests --- src/compiler/checker.ts | 4 ++-- ...syncArrowFunctionCapturesThis_es2017.types | 15 +++++++++++++++ .../asyncArrowFunctionCapturesThis_es5.types | 15 +++++++++++++++ .../asyncArrowFunctionCapturesThis_es6.types | 15 +++++++++++++++ .../reference/asyncFunctionReturnType.js | 17 ++++++++++++++++- .../reference/asyncFunctionReturnType.symbols | 19 +++++++++++++++++++ .../reference/asyncFunctionReturnType.types | 17 +++++++++++++++++ .../reference/forAwaitForUnion.types | 11 +++++++++++ .../cases/compiler/asyncFunctionReturnType.ts | 9 ++++++++- 9 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.types create mode 100644 tests/baselines/reference/asyncArrowFunctionCapturesThis_es5.types create mode 100644 tests/baselines/reference/asyncArrowFunctionCapturesThis_es6.types create mode 100644 tests/baselines/reference/forAwaitForUnion.types diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 09c121c8f490a..59a513a4f65a1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4012,7 +4012,7 @@ namespace ts { } if (type.flags & TypeFlags.TypeParameter || objectFlags & ObjectFlags.ClassOrInterface) { if (type.flags & TypeFlags.TypeParameter && contains(context.inferTypeParameters, type)) { - context.approximateLength += (symbolName(type.symbol).length + 6); + context.approximateLength += 6 + (type.symbol ? symbolName(type.symbol).length : 1); return createInferTypeNode(typeParameterToDeclarationWithConstraint(type as TypeParameter, context, /*constraintNode*/ undefined)); } if (context.flags & NodeBuilderFlags.GenerateNamesForShadowedTypeParams && @@ -4947,7 +4947,7 @@ namespace ts { return cached; } } - let result = symbolToName(type.symbol, context, SymbolFlags.Type, /*expectsIdentifier*/ true); + let result = type.symbol ? symbolToName(type.symbol, context, SymbolFlags.Type, /*expectsIdentifier*/ true) : createIdentifier("?"); if (!(result.kind & SyntaxKind.Identifier)) { return createIdentifier("(Missing type parameter)"); } diff --git a/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.types b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.types new file mode 100644 index 0000000000000..012a817f62f49 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.types @@ -0,0 +1,15 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunctionCapturesThis_es2017.ts === +class C { +>C : C + + method() { +>method : () => void + + var fn = async () => await this; +>fn : () => Promise ? ? : this> +>async () => await this : () => Promise ? ? : this> +>await this : this extends undefined ? this : this extends PromiseLike ? ? : this +>this : this + } +} + diff --git a/tests/baselines/reference/asyncArrowFunctionCapturesThis_es5.types b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es5.types new file mode 100644 index 0000000000000..925e6374bb862 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es5.types @@ -0,0 +1,15 @@ +=== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunctionCapturesThis_es5.ts === +class C { +>C : C + + method() { +>method : () => void + + var fn = async () => await this; +>fn : () => Promise ? ? : this> +>async () => await this : () => Promise ? ? : this> +>await this : this extends undefined ? this : this extends PromiseLike ? ? : this +>this : this + } +} + diff --git a/tests/baselines/reference/asyncArrowFunctionCapturesThis_es6.types b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es6.types new file mode 100644 index 0000000000000..49b72b0dab5c0 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es6.types @@ -0,0 +1,15 @@ +=== tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunctionCapturesThis_es6.ts === +class C { +>C : C + + method() { +>method : () => void + + var fn = async () => await this; +>fn : () => Promise ? ? : this> +>async () => await this : () => Promise ? ? : this> +>await this : this extends undefined ? this : this extends PromiseLike ? ? : this +>this : this + } +} + diff --git a/tests/baselines/reference/asyncFunctionReturnType.js b/tests/baselines/reference/asyncFunctionReturnType.js index 04b3a04a036d3..56428e10826e8 100644 --- a/tests/baselines/reference/asyncFunctionReturnType.js +++ b/tests/baselines/reference/asyncFunctionReturnType.js @@ -73,7 +73,15 @@ async function fGenericIndexedTypeForPromiseOfKProp(obj: TObj, key: K): Promise { return Promise.resolve(obj[key]); -} +} + +// #27711 + +async function fGeneric(x: T) { + return x; +} +const expected: Promise = fGeneric(undefined as Promise); + //// [asyncFunctionReturnType.js] var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { @@ -172,3 +180,10 @@ function fGenericIndexedTypeForExplicitPromiseOfKProp(obj, key) { return Promise.resolve(obj[key]); }); } +// #27711 +function fGeneric(x) { + return __awaiter(this, void 0, void 0, function* () { + return x; + }); +} +const expected = fGeneric(undefined); diff --git a/tests/baselines/reference/asyncFunctionReturnType.symbols b/tests/baselines/reference/asyncFunctionReturnType.symbols index 25a7e944c6a7a..fe5181eb9e603 100644 --- a/tests/baselines/reference/asyncFunctionReturnType.symbols +++ b/tests/baselines/reference/asyncFunctionReturnType.symbols @@ -285,3 +285,22 @@ async function fGenericIndexedTypeForExplicitPromiseOfKPropobj : Symbol(obj, Decl(asyncFunctionReturnType.ts, 72, 100)) >key : Symbol(key, Decl(asyncFunctionReturnType.ts, 72, 110)) } + +// #27711 + +async function fGeneric(x: T) { +>fGeneric : Symbol(fGeneric, Decl(asyncFunctionReturnType.ts, 74, 1)) +>T : Symbol(T, Decl(asyncFunctionReturnType.ts, 78, 24)) +>x : Symbol(x, Decl(asyncFunctionReturnType.ts, 78, 27)) +>T : Symbol(T, Decl(asyncFunctionReturnType.ts, 78, 24)) + + return x; +>x : Symbol(x, Decl(asyncFunctionReturnType.ts, 78, 27)) +} +const expected: Promise = fGeneric(undefined as Promise); +>expected : Symbol(expected, Decl(asyncFunctionReturnType.ts, 81, 5)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) +>fGeneric : Symbol(fGeneric, Decl(asyncFunctionReturnType.ts, 74, 1)) +>undefined : Symbol(undefined) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) + diff --git a/tests/baselines/reference/asyncFunctionReturnType.types b/tests/baselines/reference/asyncFunctionReturnType.types index d2cb63e0a35f3..c6148cd6c1463 100644 --- a/tests/baselines/reference/asyncFunctionReturnType.types +++ b/tests/baselines/reference/asyncFunctionReturnType.types @@ -220,3 +220,20 @@ async function fGenericIndexedTypeForExplicitPromiseOfKPropobj : TObj >key : K } + +// #27711 + +async function fGeneric(x: T) { +>fGeneric : (x: T) => Promise ? ? : T> +>x : T + + return x; +>x : T +} +const expected: Promise = fGeneric(undefined as Promise); +>expected : Promise +>fGeneric(undefined as Promise) : Promise +>fGeneric : (x: T) => Promise ? ? : T> +>undefined as Promise : Promise +>undefined : undefined + diff --git a/tests/baselines/reference/forAwaitForUnion.types b/tests/baselines/reference/forAwaitForUnion.types new file mode 100644 index 0000000000000..9c7e3138b1c3a --- /dev/null +++ b/tests/baselines/reference/forAwaitForUnion.types @@ -0,0 +1,11 @@ +=== tests/cases/compiler/forAwaitForUnion.ts === +async function f(source: Iterable | AsyncIterable) { +>f : (source: Iterable | AsyncIterable) => Promise +>source : Iterable | AsyncIterable + + for await (const x of source) { +>x : T | (T extends undefined ? T : T extends PromiseLike ? ? : T) +>source : Iterable | AsyncIterable + } +} + diff --git a/tests/cases/compiler/asyncFunctionReturnType.ts b/tests/cases/compiler/asyncFunctionReturnType.ts index 3bd7a0e998ac2..b48a6a89e422f 100644 --- a/tests/cases/compiler/asyncFunctionReturnType.ts +++ b/tests/cases/compiler/asyncFunctionReturnType.ts @@ -73,4 +73,11 @@ async function fGenericIndexedTypeForPromiseOfKProp(obj: TObj, key: K): Promise { return Promise.resolve(obj[key]); -} \ No newline at end of file +} + +// #27711 + +async function fGeneric(x: T) { + return x; +} +const expected: Promise = fGeneric(undefined as Promise);