Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds 'awaited' type to better handle promise resolution and await #17077

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ gulp.task("lint", "Runs tslint on the compiler sources. Optional arguments are:
const fileMatcher = cmdLineOptions["files"];
const files = fileMatcher
? `src/**/${fileMatcher}`
: "Gulpfile.ts 'scripts/tslint/*.ts' 'src/**/*.ts' --exclude src/lib/es5.d.ts --exclude 'src/lib/*.generated.d.ts'";
: "Gulpfile.ts 'scripts/tslint/*.ts' 'src/**/*.ts' --exclude src/lib/es5.d.ts --exclude 'src/lib/*.generated.d.ts' --exclude 'src/lib/es2015.iterable.d.ts' --exclude 'src/lib/es2015.promise.d.ts'";
const cmd = `node node_modules/tslint/bin/tslint ${files} --format stylish`;
console.log("Linting: " + cmd);
child_process.execSync(cmd, { stdio: [0, 1, 2] });
Expand Down
2 changes: 1 addition & 1 deletion Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ task("lint", ["build-rules"], () => {
const fileMatcher = process.env.f || process.env.file || process.env.files;
const files = fileMatcher
? `src/**/${fileMatcher}`
: "Gulpfile.ts 'scripts/tslint/*.ts' 'src/**/*.ts' --exclude src/lib/es5.d.ts --exclude 'src/lib/*.generated.d.ts'";
: "Gulpfile.ts 'scripts/tslint/*.ts' 'src/**/*.ts' --exclude src/lib/es5.d.ts --exclude 'src/lib/*.generated.d.ts' --exclude 'src/lib/es2015.iterable.d.ts' --exclude 'src/lib/es2015.promise.d.ts'";
const cmd = `node node_modules/tslint/bin/tslint ${files} --format stylish`;
console.log("Linting: " + cmd);
jake.exec([cmd], { interactive: true }, () => {
Expand Down
331 changes: 252 additions & 79 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

58 changes: 44 additions & 14 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,20 @@ namespace ts {
return result;
}

export function mapDefined<T, U>(array: ReadonlyArray<T> | undefined, mapFn: (x: T, i: number) => U | undefined): U[] {
const result: U[] = [];
if (array) {
for (let i = 0; i < array.length; i++) {
const item = array[i];
const mapped = mapFn(item, i);
if (mapped !== undefined) {
result.push(mapped);
}
}
}
return result;
}

// Maps from T to T and avoids allocation if all elements map to themselves
export function sameMap<T>(array: T[], f: (x: T, i: number) => T): T[];
export function sameMap<T>(array: ReadonlyArray<T>, f: (x: T, i: number) => T): ReadonlyArray<T>;
Expand All @@ -410,6 +424,36 @@ namespace ts {
return result || array;
}

// Maps from T to T and avoids allocation if all elements map to themselves. Undefined elements are not added.
export function sameMapDefined<T>(array: (T | undefined)[], f: (x: T, i: number) => T | undefined): T[] | undefined;
export function sameMapDefined<T>(array: ReadonlyArray<T | undefined>, f: (x: T, i: number) => T | undefined): ReadonlyArray<T> | undefined;
export function sameMapDefined<T>(array: (T | undefined)[], f: (x: T, i: number) => T | undefined): T[] | undefined {
let result: T[];
if (array) {
let lastUndefinedOffset = -1;
for (let i = 0; i < array.length; i++) {
const item = array[i];
const mapped = item !== undefined ? f(item, i) : undefined;
if (result) {
if (mapped !== undefined) {
result.push(mapped);
}
}
else if (mapped === undefined) {
lastUndefinedOffset = i;
}
else if (item !== mapped) {
result = array.slice(lastUndefinedOffset + 1, i);
result.push(mapped);
}
}
if (!result && lastUndefinedOffset > -1) {
return undefined;
}
}
return result || array;
}

/**
* Flattens an array containing a mix of array or non-array elements.
*
Expand Down Expand Up @@ -501,20 +545,6 @@ namespace ts {
return result || array;
}

export function mapDefined<T, U>(array: ReadonlyArray<T> | undefined, mapFn: (x: T, i: number) => U | undefined): U[] {
const result: U[] = [];
if (array) {
for (let i = 0; i < array.length; i++) {
const item = array[i];
const mapped = mapFn(item, i);
if (mapped !== undefined) {
result.push(mapped);
}
}
}
return result;
}

/**
* Computes the first matching span of elements and returns a tuple of the first span
* and the remaining elements.
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,15 +730,15 @@ namespace ts {
return <ThisTypeNode>createSynthesizedNode(SyntaxKind.ThisType);
}

export function createTypeOperatorNode(type: TypeNode) {
export function createTypeOperatorNode(type: TypeNode, operator: SyntaxKind.KeyOfKeyword | SyntaxKind.AwaitedKeyword) {
const node = createSynthesizedNode(SyntaxKind.TypeOperator) as TypeOperatorNode;
node.operator = SyntaxKind.KeyOfKeyword;
node.operator = operator;
node.type = parenthesizeElementTypeMember(type);
return node;
}

export function updateTypeOperatorNode(node: TypeOperatorNode, type: TypeNode) {
return node.type !== type ? updateNode(createTypeOperatorNode(type), node) : node;
return node.type !== type ? updateNode(createTypeOperatorNode(type, node.operator), node) : node;
}

export function createIndexedAccessTypeNode(objectType: TypeNode, indexType: TypeNode) {
Expand Down
8 changes: 5 additions & 3 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2782,7 +2782,7 @@ namespace ts {
return type;
}

function parseTypeOperator(operator: SyntaxKind.KeyOfKeyword) {
function parseTypeOperator(operator: SyntaxKind.KeyOfKeyword | SyntaxKind.AwaitedKeyword) {
const node = <TypeOperatorNode>createNode(SyntaxKind.TypeOperator);
parseExpected(operator);
node.operator = operator;
Expand All @@ -2791,9 +2791,11 @@ namespace ts {
}

function parseTypeOperatorOrHigher(): TypeNode {
switch (token()) {
const keyword = token();
switch (keyword) {
case SyntaxKind.KeyOfKeyword:
return parseTypeOperator(SyntaxKind.KeyOfKeyword);
case SyntaxKind.AwaitedKeyword:
return parseTypeOperator(keyword);
}
return parseArrayTypeOrHigher();
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace ts {
"abstract": SyntaxKind.AbstractKeyword,
"any": SyntaxKind.AnyKeyword,
"as": SyntaxKind.AsKeyword,
"awaited": SyntaxKind.AwaitedKeyword,
"boolean": SyntaxKind.BooleanKeyword,
"break": SyntaxKind.BreakKeyword,
"case": SyntaxKind.CaseKeyword,
Expand Down
36 changes: 23 additions & 13 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ namespace ts {
ModuleKeyword,
NamespaceKeyword,
NeverKeyword,
AwaitedKeyword,
ReadonlyKeyword,
RequireKeyword,
NumberKeyword,
Expand Down Expand Up @@ -972,7 +973,7 @@ namespace ts {

export interface TypeOperatorNode extends TypeNode {
kind: SyntaxKind.TypeOperator;
operator: SyntaxKind.KeyOfKeyword;
operator: SyntaxKind.KeyOfKeyword | SyntaxKind.AwaitedKeyword;
type: TypeNode;
}

Expand Down Expand Up @@ -3140,17 +3141,18 @@ namespace ts {
Intersection = 1 << 17, // Intersection (T & U)
Index = 1 << 18, // keyof T
IndexedAccess = 1 << 19, // T[K]
Awaited = 1 << 20, // awaited T
/* @internal */
FreshLiteral = 1 << 20, // Fresh literal type
FreshLiteral = 1 << 21, // Fresh literal type
/* @internal */
ContainsWideningType = 1 << 21, // Type is or contains undefined or null widening type
ContainsWideningType = 1 << 22, // Type is or contains undefined or null widening type
/* @internal */
ContainsObjectLiteral = 1 << 22, // Type is or contains object literal type
ContainsObjectLiteral = 1 << 23, // Type is or contains object literal type
/* @internal */
ContainsAnyFunctionType = 1 << 23, // Type is or contains the anyFunctionType
NonPrimitive = 1 << 24, // intrinsic object type
ContainsAnyFunctionType = 1 << 24, // Type is or contains the anyFunctionType
NonPrimitive = 1 << 25, // intrinsic object type
/* @internal */
JsxAttributes = 1 << 25, // Jsx attributes type
JsxAttributes = 1 << 26, // Jsx attributes type

/* @internal */
Nullable = Undefined | Null,
Expand All @@ -3169,12 +3171,12 @@ namespace ts {
EnumLike = Enum | EnumLiteral,
UnionOrIntersection = Union | Intersection,
StructuredType = Object | Union | Intersection,
StructuredOrTypeVariable = StructuredType | TypeParameter | Index | IndexedAccess,
TypeVariable = TypeParameter | IndexedAccess,
StructuredOrTypeVariable = StructuredType | TypeParameter | Index | IndexedAccess | Awaited,
TypeVariable = TypeParameter | IndexedAccess | Awaited,

// 'Narrowable' types are types where narrowing actually narrows.
// This *should* be every type other than null, undefined, void, and never
Narrowable = Any | StructuredType | TypeParameter | Index | IndexedAccess | StringLike | NumberLike | BooleanLike | ESSymbol | NonPrimitive,
Narrowable = Any | StructuredType | TypeParameter | Index | IndexedAccess | StringLike | NumberLike | BooleanLike | ESSymbol | NonPrimitive | Awaited,
NotUnionOrUnit = Any | ESSymbol | Object | NonPrimitive,
/* @internal */
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
Expand Down Expand Up @@ -3296,6 +3298,8 @@ namespace ts {
resolvedBaseConstraint: Type;
/* @internal */
couldContainTypeVariables: boolean;
/* @internal */
resolvedAwaitedType: AwaitedType;
}

export interface UnionType extends UnionOrIntersectionType { }
Expand Down Expand Up @@ -3359,9 +3363,8 @@ namespace ts {

/* @internal */
export interface PromiseOrAwaitableType extends ObjectType, UnionType {
promiseTypeOfPromiseConstructor?: Type;
promisedTypeOfPromise?: Type;
awaitedTypeOfType?: Type;
fulfillmentType?: Type; // Type of `value` parameter of `onfulfilled` callback.
awaitedType?: Type; // The "fulfillment type" if a Promise-like, otherwise this type.
}

/* @internal */
Expand All @@ -3374,6 +3377,8 @@ namespace ts {
resolvedBaseConstraint: Type;
/* @internal */
resolvedIndexType: IndexType;
/* @internal */
resolvedAwaitedType: AwaitedType;
}

// Type parameters (TypeFlags.TypeParameter)
Expand Down Expand Up @@ -3404,6 +3409,11 @@ namespace ts {
type: TypeVariable | UnionOrIntersectionType;
}

// awaited T types (TypeFlags.Awaited)
export interface AwaitedType extends Type {
type: TypeVariable;
}

export const enum SignatureKind {
Call,
Construct,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/es2015.iterable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ interface PromiseConstructor {
* @param values An array of Promises.
* @returns A new Promise.
*/
all<TAll>(values: Iterable<TAll | PromiseLike<TAll>>): Promise<TAll[]>;
all<TAll>(values: Iterable<TAll>): Promise<(awaited TAll)[]>;

/**
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
* or rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
race<T>(values: Iterable<T | PromiseLike<T>>): Promise<T>;
race<T>(values: Iterable<T>): Promise<awaited T>;
}

declare namespace Reflect {
Expand Down
Loading