Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Jan 25, 2020
1 parent b601045 commit ac90de3
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -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)");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>async () => await this : () => Promise<this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>await this : this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this
>this : this
}
}

15 changes: 15 additions & 0 deletions tests/baselines/reference/asyncArrowFunctionCapturesThis_es5.types
Original file line number Diff line number Diff line change
@@ -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 extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>async () => await this : () => Promise<this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>await this : this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this
>this : this
}
}

15 changes: 15 additions & 0 deletions tests/baselines/reference/asyncArrowFunctionCapturesThis_es6.types
Original file line number Diff line number Diff line change
@@ -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 extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>async () => await this : () => Promise<this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>await this : this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this
>this : this
}
}

17 changes: 16 additions & 1 deletion tests/baselines/reference/asyncFunctionReturnType.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends

async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K]> {
return Promise.resolve<TObj[K]>(obj[key]);
}
}

// #27711

async function fGeneric<T>(x: T) {
return x;
}
const expected: Promise<string> = fGeneric(undefined as Promise<string>);


//// [asyncFunctionReturnType.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
Expand Down Expand Up @@ -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);
19 changes: 19 additions & 0 deletions tests/baselines/reference/asyncFunctionReturnType.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,22 @@ async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K
>obj : Symbol(obj, Decl(asyncFunctionReturnType.ts, 72, 100))
>key : Symbol(key, Decl(asyncFunctionReturnType.ts, 72, 110))
}

// #27711

async function fGeneric<T>(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<string> = fGeneric(undefined as Promise<string>);
>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, --, --))

17 changes: 17 additions & 0 deletions tests/baselines/reference/asyncFunctionReturnType.types
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,20 @@ async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K
>obj : TObj
>key : K
}

// #27711

async function fGeneric<T>(x: T) {
>fGeneric : <T>(x: T) => Promise<T extends undefined ? T : T extends PromiseLike<infer ?> ? ? : T>
>x : T

return x;
>x : T
}
const expected: Promise<string> = fGeneric(undefined as Promise<string>);
>expected : Promise<string>
>fGeneric(undefined as Promise<string>) : Promise<string>
>fGeneric : <T>(x: T) => Promise<T extends undefined ? T : T extends PromiseLike<infer ?> ? ? : T>
>undefined as Promise<string> : Promise<string>
>undefined : undefined

11 changes: 11 additions & 0 deletions tests/baselines/reference/forAwaitForUnion.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/forAwaitForUnion.ts ===
async function f<T>(source: Iterable<T> | AsyncIterable<T>) {
>f : <T>(source: Iterable<T> | AsyncIterable<T>) => Promise<void>
>source : Iterable<T> | AsyncIterable<T>

for await (const x of source) {
>x : T | (T extends undefined ? T : T extends PromiseLike<infer ?> ? ? : T)
>source : Iterable<T> | AsyncIterable<T>
}
}

9 changes: 8 additions & 1 deletion tests/cases/compiler/asyncFunctionReturnType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@ async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends

async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K]> {
return Promise.resolve<TObj[K]>(obj[key]);
}
}

// #27711

async function fGeneric<T>(x: T) {
return x;
}
const expected: Promise<string> = fGeneric(undefined as Promise<string>);

0 comments on commit ac90de3

Please sign in to comment.