From f4f67e6e4c44e3b4491412cce0c64daddbe1f2f7 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 | 6 +++--- .../asyncArrowFunctionCapturesThis_es5.types | 6 +++--- .../asyncArrowFunctionCapturesThis_es6.types | 6 +++--- .../reference/asyncFunctionReturnType.js | 17 ++++++++++++++++- .../reference/asyncFunctionReturnType.symbols | 19 +++++++++++++++++++ .../reference/asyncFunctionReturnType.types | 17 +++++++++++++++++ .../reference/forAwaitForUnion.types | 2 +- .../cases/compiler/asyncFunctionReturnType.ts | 9 ++++++++- 9 files changed, 72 insertions(+), 14 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a9937906f3778..589f79d356c0b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3855,7 +3855,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 && @@ -4791,7 +4791,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 index 57f59302bb5bc..012a817f62f49 100644 --- a/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.types +++ b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.types @@ -6,9 +6,9 @@ class C { >method : () => void var fn = async () => await this; ->fn : () => Promise ->async () => await this : () => Promise ->await this : 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 index da378ee2718f9..925e6374bb862 100644 --- a/tests/baselines/reference/asyncArrowFunctionCapturesThis_es5.types +++ b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es5.types @@ -6,9 +6,9 @@ class C { >method : () => void var fn = async () => await this; ->fn : () => Promise ->async () => await this : () => Promise ->await this : 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 index 5386a956ada9a..49b72b0dab5c0 100644 --- a/tests/baselines/reference/asyncArrowFunctionCapturesThis_es6.types +++ b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es6.types @@ -6,9 +6,9 @@ class C { >method : () => void var fn = async () => await this; ->fn : () => Promise ->async () => await this : () => Promise ->await this : 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..3297cca67a62e 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..2162f9c6c79fa 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..3d81a41c48e1b 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 index 180197b26eafc..9c7e3138b1c3a 100644 --- a/tests/baselines/reference/forAwaitForUnion.types +++ b/tests/baselines/reference/forAwaitForUnion.types @@ -4,7 +4,7 @@ async function f(source: Iterable | AsyncIterable) { >source : Iterable | AsyncIterable for await (const x of source) { ->x : T +>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..3cb5126e80c58 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);