Skip to content

Commit b5cc96c

Browse files
committed
Merge branch 'asyncGeneratorsUpLevel' into asyncGenerators
2 parents 549ac8f + c6ee25d commit b5cc96c

9 files changed

Lines changed: 552 additions & 283 deletions

File tree

Jakefile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ var es2016LibrarySourceMap = es2016LibrarySource.map(function (source) {
301301

302302
var es2017LibrarySource = [
303303
"es2017.object.d.ts",
304-
"es2017.sharedmemory.d.ts"
304+
"es2017.sharedmemory.d.ts",
305+
"es2017.asynciterable.d.ts"
305306
];
306307

307308
var es2017LibrarySourceMap = es2017LibrarySource.map(function (source) {

src/compiler/binder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,7 +2292,7 @@ namespace ts {
22922292

22932293
function bindFunctionDeclaration(node: FunctionDeclaration) {
22942294
if (!isDeclarationFile(file) && !isInAmbientContext(node)) {
2295-
if (isAsyncFunctionLike(node)) {
2295+
if (isAsyncFunction(node)) {
22962296
emitFlags |= NodeFlags.HasAsyncFunctions;
22972297
}
22982298
}
@@ -2309,7 +2309,7 @@ namespace ts {
23092309

23102310
function bindFunctionExpression(node: FunctionExpression) {
23112311
if (!isDeclarationFile(file) && !isInAmbientContext(node)) {
2312-
if (isAsyncFunctionLike(node)) {
2312+
if (isAsyncFunction(node)) {
23132313
emitFlags |= NodeFlags.HasAsyncFunctions;
23142314
}
23152315
}
@@ -2323,7 +2323,7 @@ namespace ts {
23232323

23242324
function bindPropertyOrMethodOrAccessor(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags) {
23252325
if (!isDeclarationFile(file) && !isInAmbientContext(node)) {
2326-
if (isAsyncFunctionLike(node)) {
2326+
if (isAsyncFunction(node)) {
23272327
emitFlags |= NodeFlags.HasAsyncFunctions;
23282328
}
23292329
if (nodeIsDecorated(node)) {

src/compiler/checker.ts

Lines changed: 450 additions & 269 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,23 +175,23 @@
175175
"category": "Error",
176176
"code": 1057
177177
},
178-
"Operand for 'await' does not have a valid callable 'then' member.": {
178+
"Type used as operand to 'await' or the return type of an async function must not contain a callable 'then' member if it is not a promise.": {
179179
"category": "Error",
180180
"code": 1058
181181
},
182-
"Return expression in async function does not have a valid callable 'then' member.": {
182+
"A promise must have a 'then' method.": {
183183
"category": "Error",
184184
"code": 1059
185185
},
186-
"Expression body for async arrow function does not have a valid callable 'then' member.": {
186+
"The first parameter of the 'then' method of a promise must be a callback.": {
187187
"category": "Error",
188188
"code": 1060
189189
},
190190
"Enum member must have initializer.": {
191191
"category": "Error",
192192
"code": 1061
193193
},
194-
"{0} is referenced directly or indirectly in the fulfillment callback of its own 'then' method.": {
194+
"Type is referenced directly or indirectly in the fulfillment callback of its own 'then' method.": {
195195
"category": "Error",
196196
"code": 1062
197197
},
@@ -1611,6 +1611,10 @@
16111611
"category": "Error",
16121612
"code": 2503
16131613
},
1614+
"Type must have a '[Symbol.asyncIterator]()' method that returns an async iterator.": {
1615+
"category": "Error",
1616+
"code": 2504
1617+
},
16141618
"A generator cannot have a 'void' type annotation.": {
16151619
"category": "Error",
16161620
"code": 2505
@@ -1667,6 +1671,10 @@
16671671
"category": "Error",
16681672
"code": 2518
16691673
},
1674+
"An async iterator must have a 'next()' method.": {
1675+
"category": "Error",
1676+
"code": 2519
1677+
},
16701678
"Duplicate identifier '{0}'. Compiler uses declaration '{1}' to support async functions.": {
16711679
"category": "Error",
16721680
"code": 2520
@@ -1759,6 +1767,10 @@
17591767
"category": "Error",
17601768
"code": 2542
17611769
},
1770+
"The type returned by the 'next()' method of an async iterator must be a promise for a type with a 'value' property.": {
1771+
"category": "Error",
1772+
"code": 2543
1773+
},
17621774
"JSX element attributes type '{0}' may not be a union type.": {
17631775
"category": "Error",
17641776
"code": 2600

src/compiler/transformers/ts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,7 @@ namespace ts {
16311631
if (isFunctionLike(node) && node.type) {
16321632
return serializeTypeNode(node.type);
16331633
}
1634-
else if (isAsyncFunctionLike(node)) {
1634+
else if (isAsyncFunction(node)) {
16351635
return createIdentifier("Promise");
16361636
}
16371637

src/compiler/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,8 +2883,16 @@ namespace ts {
28832883
// Just a place to cache element types of iterables and iterators
28842884
/* @internal */
28852885
export interface IterableOrIteratorType extends ObjectType, UnionType {
2886-
iterableElementType?: Type;
2887-
iteratorElementType?: Type;
2886+
iteratedTypeOfIterable?: Type;
2887+
iteratedTypeOfIterator?: Type;
2888+
iteratedTypeOfAsyncIterable?: Type;
2889+
iteratedTypeOfAsyncIterator?: Type;
2890+
}
2891+
2892+
/* @internal */
2893+
export interface PromiseOrAwaitableType extends ObjectType, UnionType {
2894+
promisedTypeOfPromise?: Type;
2895+
awaitedTypeOfType?: Type;
28882896
}
28892897

28902898
// Type parameters (TypeFlags.TypeParameter)

src/compiler/utilities.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,8 +1854,51 @@ namespace ts {
18541854
return SyntaxKind.FirstTriviaToken <= token && token <= SyntaxKind.LastTriviaToken;
18551855
}
18561856

1857-
export function isAsyncFunctionLike(node: Node): boolean {
1858-
return isFunctionLike(node) && hasModifier(node, ModifierFlags.Async) && !isAccessor(node);
1857+
export const enum FunctionFlags {
1858+
Normal = 0,
1859+
Generator = 1 << 0,
1860+
Async = 1 << 1,
1861+
AsyncOrAsyncGenerator = Async | Generator,
1862+
Invalid = 1 << 2,
1863+
InvalidAsyncOrAsyncGenerator = AsyncOrAsyncGenerator | Invalid,
1864+
InvalidGenerator = Generator | Invalid,
1865+
}
1866+
1867+
export function getFunctionFlags(node: FunctionLikeDeclaration) {
1868+
let flags = FunctionFlags.Normal;
1869+
switch (node.kind) {
1870+
case SyntaxKind.FunctionDeclaration:
1871+
case SyntaxKind.FunctionExpression:
1872+
case SyntaxKind.MethodDeclaration:
1873+
if (node.asteriskToken) {
1874+
flags |= FunctionFlags.Generator;
1875+
}
1876+
// fall through
1877+
case SyntaxKind.ArrowFunction:
1878+
if (hasModifier(node, ModifierFlags.Async)) {
1879+
flags |= FunctionFlags.Async;
1880+
}
1881+
break;
1882+
}
1883+
1884+
if (!node.body) {
1885+
flags |= FunctionFlags.Invalid;
1886+
}
1887+
1888+
return flags;
1889+
}
1890+
1891+
export function isAsyncFunction(node: Node): boolean {
1892+
switch (node.kind) {
1893+
case SyntaxKind.FunctionDeclaration:
1894+
case SyntaxKind.FunctionExpression:
1895+
case SyntaxKind.ArrowFunction:
1896+
case SyntaxKind.MethodDeclaration:
1897+
return (<FunctionLikeDeclaration>node).body !== undefined
1898+
&& (<FunctionLikeDeclaration>node).asteriskToken === undefined
1899+
&& hasModifier(node, ModifierFlags.Async);
1900+
}
1901+
return false;
18591902
}
18601903

18611904
export function isStringOrNumericLiteral(kind: SyntaxKind): boolean {

src/lib/es2017.asynciterable.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="lib.es2015.symbol.d.ts" />
2+
3+
interface SymbolConstructor {
4+
/**
5+
* A method that returns the default async iterator for an object. Called by the semantics of
6+
* the for-await-of statement.
7+
*/
8+
readonly asyncIterator: symbol;
9+
}
10+
11+
interface AsyncIterator<T> {
12+
next(value?: any): Promise<IteratorResult<T>>;
13+
return?(value?: any): Promise<IteratorResult<T>>;
14+
throw?(e?: any): Promise<IteratorResult<T>>;
15+
}
16+
17+
interface AsyncIterable<T> {
18+
[Symbol.asyncIterator](): AsyncIterator<T>;
19+
}
20+
21+
interface AsyncIterableIterator<T> extends AsyncIterator<T> {
22+
[Symbol.asyncIterator](): AsyncIterableIterator<T>;
23+
}

src/lib/es2017.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/// <reference path="lib.es2016.d.ts" />
22
/// <reference path="lib.es2017.object.d.ts" />
3-
/// <reference path="lib.es2017.sharedmemory.d.ts" />
3+
/// <reference path="lib.es2017.sharedmemory.d.ts" />
4+
/// <reference path="lib.es2017.asynciterable.d.ts" />

0 commit comments

Comments
 (0)