Skip to content

Commit ebaa253

Browse files
committed
Adds 'promised' type to better handle promise resolution and await
1 parent f888c88 commit ebaa253

File tree

59 files changed

+2376
-977
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2376
-977
lines changed

Gulpfile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ gulp.task("lint", "Runs tslint on the compiler sources. Optional arguments are:
10321032
const fileMatcher = cmdLineOptions["files"];
10331033
const files = fileMatcher
10341034
? `src/**/${fileMatcher}`
1035-
: "Gulpfile.ts 'scripts/tslint/*.ts' 'src/**/*.ts' --exclude src/lib/es5.d.ts --exclude 'src/lib/*.generated.d.ts'";
1035+
: "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'";
10361036
const cmd = `node node_modules/tslint/bin/tslint ${files} --format stylish`;
10371037
console.log("Linting: " + cmd);
10381038
child_process.execSync(cmd, { stdio: [0, 1, 2] });

Jakefile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ task("lint", ["build-rules"], () => {
12081208
const fileMatcher = process.env.f || process.env.file || process.env.files;
12091209
const files = fileMatcher
12101210
? `src/**/${fileMatcher}`
1211-
: "Gulpfile.ts 'scripts/tslint/*.ts' 'src/**/*.ts' --exclude src/lib/es5.d.ts --exclude 'src/lib/*.generated.d.ts'";
1211+
: "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'";
12121212
const cmd = `node node_modules/tslint/bin/tslint ${files} --format stylish`;
12131213
console.log("Linting: " + cmd);
12141214
jake.exec([cmd], { interactive: true }, () => {

src/compiler/checker.ts

Lines changed: 233 additions & 68 deletions
Large diffs are not rendered by default.

src/compiler/factory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,15 +627,15 @@ namespace ts {
627627
return <ThisTypeNode>createSynthesizedNode(SyntaxKind.ThisType);
628628
}
629629

630-
export function createTypeOperatorNode(type: TypeNode) {
630+
export function createTypeOperatorNode(type: TypeNode, operator: SyntaxKind.KeyOfKeyword | SyntaxKind.PromisedKeyword) {
631631
const node = createSynthesizedNode(SyntaxKind.TypeOperator) as TypeOperatorNode;
632-
node.operator = SyntaxKind.KeyOfKeyword;
632+
node.operator = operator;
633633
node.type = parenthesizeElementTypeMember(type);
634634
return node;
635635
}
636636

637637
export function updateTypeOperatorNode(node: TypeOperatorNode, type: TypeNode) {
638-
return node.type !== type ? updateNode(createTypeOperatorNode(type), node) : node;
638+
return node.type !== type ? updateNode(createTypeOperatorNode(type, node.operator), node) : node;
639639
}
640640

641641
export function createIndexedAccessTypeNode(objectType: TypeNode, indexType: TypeNode) {

src/compiler/parser.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,7 +2663,7 @@ namespace ts {
26632663
return type;
26642664
}
26652665

2666-
function parseTypeOperator(operator: SyntaxKind.KeyOfKeyword) {
2666+
function parseTypeOperator(operator: SyntaxKind.KeyOfKeyword | SyntaxKind.PromisedKeyword) {
26672667
const node = <TypeOperatorNode>createNode(SyntaxKind.TypeOperator);
26682668
parseExpected(operator);
26692669
node.operator = operator;
@@ -2672,9 +2672,11 @@ namespace ts {
26722672
}
26732673

26742674
function parseTypeOperatorOrHigher(): TypeNode {
2675-
switch (token()) {
2675+
const keyword = token();
2676+
switch (keyword) {
26762677
case SyntaxKind.KeyOfKeyword:
2677-
return parseTypeOperator(SyntaxKind.KeyOfKeyword);
2678+
case SyntaxKind.PromisedKeyword:
2679+
return parseTypeOperator(keyword);
26782680
}
26792681
return parseArrayTypeOrHigher();
26802682
}

src/compiler/scanner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ namespace ts {
104104
"package": SyntaxKind.PackageKeyword,
105105
"private": SyntaxKind.PrivateKeyword,
106106
"protected": SyntaxKind.ProtectedKeyword,
107+
"promised": SyntaxKind.PromisedKeyword,
107108
"public": SyntaxKind.PublicKeyword,
108109
"readonly": SyntaxKind.ReadonlyKeyword,
109110
"require": SyntaxKind.RequireKeyword,

src/compiler/types.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ namespace ts {
192192
ModuleKeyword,
193193
NamespaceKeyword,
194194
NeverKeyword,
195+
PromisedKeyword,
195196
ReadonlyKeyword,
196197
RequireKeyword,
197198
NumberKeyword,
@@ -986,7 +987,7 @@ namespace ts {
986987

987988
export interface TypeOperatorNode extends TypeNode {
988989
kind: SyntaxKind.TypeOperator;
989-
operator: SyntaxKind.KeyOfKeyword;
990+
operator: SyntaxKind.KeyOfKeyword | SyntaxKind.PromisedKeyword;
990991
type: TypeNode;
991992
}
992993

@@ -3157,17 +3158,18 @@ namespace ts {
31573158
Intersection = 1 << 17, // Intersection (T & U)
31583159
Index = 1 << 18, // keyof T
31593160
IndexedAccess = 1 << 19, // T[K]
3161+
Promised = 1 << 20, // promised T
31603162
/* @internal */
3161-
FreshLiteral = 1 << 20, // Fresh literal type
3163+
FreshLiteral = 1 << 21, // Fresh literal type
31623164
/* @internal */
3163-
ContainsWideningType = 1 << 21, // Type is or contains undefined or null widening type
3165+
ContainsWideningType = 1 << 22, // Type is or contains undefined or null widening type
31643166
/* @internal */
3165-
ContainsObjectLiteral = 1 << 22, // Type is or contains object literal type
3167+
ContainsObjectLiteral = 1 << 23, // Type is or contains object literal type
31663168
/* @internal */
3167-
ContainsAnyFunctionType = 1 << 23, // Type is or contains object literal type
3168-
NonPrimitive = 1 << 24, // intrinsic object type
3169+
ContainsAnyFunctionType = 1 << 24, // Type is or contains object literal type
3170+
NonPrimitive = 1 << 25, // intrinsic object type
31693171
/* @internal */
3170-
JsxAttributes = 1 << 25, // Jsx attributes type
3172+
JsxAttributes = 1 << 26, // Jsx attributes type
31713173

31723174
/* @internal */
31733175
Nullable = Undefined | Null,
@@ -3186,12 +3188,12 @@ namespace ts {
31863188
EnumLike = Enum | EnumLiteral,
31873189
UnionOrIntersection = Union | Intersection,
31883190
StructuredType = Object | Union | Intersection,
3189-
StructuredOrTypeVariable = StructuredType | TypeParameter | Index | IndexedAccess,
3191+
StructuredOrTypeVariable = StructuredType | TypeParameter | Index | IndexedAccess | Promised,
31903192
TypeVariable = TypeParameter | IndexedAccess,
31913193

31923194
// 'Narrowable' types are types where narrowing actually narrows.
31933195
// This *should* be every type other than null, undefined, void, and never
3194-
Narrowable = Any | StructuredType | TypeParameter | Index | IndexedAccess | StringLike | NumberLike | BooleanLike | ESSymbol | NonPrimitive,
3196+
Narrowable = Any | StructuredType | TypeParameter | Index | IndexedAccess | StringLike | NumberLike | BooleanLike | ESSymbol | NonPrimitive | Promised,
31953197
NotUnionOrUnit = Any | ESSymbol | Object | NonPrimitive,
31963198
/* @internal */
31973199
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
@@ -3313,6 +3315,8 @@ namespace ts {
33133315
resolvedBaseConstraint: Type;
33143316
/* @internal */
33153317
couldContainTypeVariables: boolean;
3318+
/* @internal */
3319+
resolvedPromisedType: PromisedType;
33163320
}
33173321

33183322
export interface UnionType extends UnionOrIntersectionType { }
@@ -3376,16 +3380,17 @@ namespace ts {
33763380

33773381
/* @internal */
33783382
export interface PromiseOrAwaitableType extends ObjectType, UnionType {
3379-
promiseTypeOfPromiseConstructor?: Type;
3380-
promisedTypeOfPromise?: Type;
3381-
awaitedTypeOfType?: Type;
3383+
fulfillmentType?: Type; // Type of `value` parameter of `onfulfilled` callback.
3384+
promisedType?: Type; // The "fulfillment type" if a Promise-like, otherwise this type.
33823385
}
33833386

33843387
export interface TypeVariable extends Type {
33853388
/* @internal */
33863389
resolvedBaseConstraint: Type;
33873390
/* @internal */
33883391
resolvedIndexType: IndexType;
3392+
/* @internal */
3393+
resolvedPromisedType: PromisedType;
33893394
}
33903395

33913396
// Type parameters (TypeFlags.TypeParameter)
@@ -3415,6 +3420,11 @@ namespace ts {
34153420
type: TypeVariable | UnionOrIntersectionType;
34163421
}
34173422

3423+
// promised T types (TypeFlags.Promised)
3424+
export interface PromisedType extends Type {
3425+
type: TypeVariable;
3426+
}
3427+
34183428
export const enum SignatureKind {
34193429
Call,
34203430
Construct,

src/lib/es2015.iterable.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,15 @@ interface PromiseConstructor {
197197
* @param values An array of Promises.
198198
* @returns A new Promise.
199199
*/
200-
all<TAll>(values: Iterable<TAll | PromiseLike<TAll>>): Promise<TAll[]>;
200+
all<TAll>(values: Iterable<TAll>): Promise<(promised TAll)[]>;
201201

202202
/**
203203
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
204204
* or rejected.
205205
* @param values An array of Promises.
206206
* @returns A new Promise.
207207
*/
208-
race<T>(values: Iterable<T | PromiseLike<T>>): Promise<T>;
208+
race<T>(values: Iterable<T>): Promise<promised T>;
209209
}
210210

211211
declare namespace Reflect {

0 commit comments

Comments
 (0)