Skip to content

Commit f0fd77a

Browse files
committed
Make async/await an ES2017 feature only
1 parent e60e97f commit f0fd77a

File tree

3 files changed

+28
-65
lines changed

3 files changed

+28
-65
lines changed

src/compiler/binder.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,11 +2555,6 @@ namespace ts {
25552555
// extends clause of a class.
25562556
let transformFlags = subtreeFlags | TransformFlags.AssertES6;
25572557

2558-
// propagate ES2017
2559-
if (node.expression.transformFlags & TransformFlags.ContainsES2017) {
2560-
transformFlags |= TransformFlags.ContainsES2017;
2561-
}
2562-
25632558
// If an ExpressionWithTypeArguments contains type arguments, then it
25642559
// is TypeScript syntax.
25652560
if (node.typeArguments) {
@@ -2591,16 +2586,16 @@ namespace ts {
25912586
const typeParameters = node.typeParameters;
25922587
const asteriskToken = node.asteriskToken;
25932588

2594-
// A MethodDeclaration is TypeScript syntax if it is either async, abstract, overloaded,
2589+
// A MethodDeclaration is TypeScript syntax if it is either abstract, overloaded,
25952590
// generic, or has a decorator.
25962591
if (!body
25972592
|| typeParameters
2598-
|| (modifierFlags & (ModifierFlags.Async | ModifierFlags.Abstract))
2593+
|| (modifierFlags & ModifierFlags.Abstract)
25992594
|| (subtreeFlags & TransformFlags.ContainsDecorators)) {
26002595
transformFlags |= TransformFlags.AssertTypeScript;
26012596
}
26022597

2603-
// Async MethodDeclaration is ES2017
2598+
// An async method declaration is ES2017 syntax.
26042599
if (modifierFlags & ModifierFlags.Async) {
26052600
transformFlags |= TransformFlags.AssertES2017;
26062601
}
@@ -2619,10 +2614,10 @@ namespace ts {
26192614
const modifierFlags = getModifierFlags(node);
26202615
const body = node.body;
26212616

2622-
// A MethodDeclaration is TypeScript syntax if it is either async, abstract, overloaded,
2617+
// An accessor is TypeScript syntax if it is either abstract, overloaded,
26232618
// generic, or has a decorator.
26242619
if (!body
2625-
|| (modifierFlags & (ModifierFlags.Async | ModifierFlags.Abstract))
2620+
|| (modifierFlags & ModifierFlags.Abstract)
26262621
|| (subtreeFlags & TransformFlags.ContainsDecorators)) {
26272622
transformFlags |= TransformFlags.AssertTypeScript;
26282623
}
@@ -2664,9 +2659,9 @@ namespace ts {
26642659
transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES6;
26652660
}
26662661

2667-
// If a FunctionDeclaration is async, then it is TypeScript syntax.
2662+
// An async function declaration is ES2017 syntax.
26682663
if (modifierFlags & ModifierFlags.Async) {
2669-
transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES2017;
2664+
transformFlags |= TransformFlags.AssertES2017;
26702665
}
26712666

26722667
// If a FunctionDeclaration's subtree has marked the container as needing to capture the
@@ -2695,9 +2690,9 @@ namespace ts {
26952690
const modifierFlags = getModifierFlags(node);
26962691
const asteriskToken = node.asteriskToken;
26972692

2698-
// An async function expression is TypeScript syntax.
2693+
// An async function expression is ES2017 syntax.
26992694
if (modifierFlags & ModifierFlags.Async) {
2700-
transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES2017;
2695+
transformFlags |= TransformFlags.AssertES2017;
27012696
}
27022697

27032698
// If a FunctionExpression's subtree has marked the container as needing to capture the
@@ -2725,9 +2720,9 @@ namespace ts {
27252720
let transformFlags = subtreeFlags | TransformFlags.AssertES6;
27262721
const modifierFlags = getModifierFlags(node);
27272722

2728-
// An async arrow function is TypeScript syntax.
2723+
// An async arrow function is ES2017 syntax.
27292724
if (modifierFlags & ModifierFlags.Async) {
2730-
transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES2017;
2725+
transformFlags |= TransformFlags.AssertES2017;
27312726
}
27322727

27332728
// If an ArrowFunction contains a lexical this, its container must capture the lexical this.
@@ -2868,8 +2863,8 @@ namespace ts {
28682863
switch (kind) {
28692864
case SyntaxKind.AsyncKeyword:
28702865
case SyntaxKind.AwaitExpression:
2871-
// Typescript async/await are ES2017 features
2872-
transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES2017;
2866+
// async/await is ES2017 syntax
2867+
transformFlags |= TransformFlags.AssertES2017;
28732868
break;
28742869

28752870
case SyntaxKind.PublicKeyword:

src/compiler/transformers/es2017.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,27 @@ namespace ts {
7777
function visitorWorker(node: Node): VisitResult<Node> {
7878
switch (node.kind) {
7979
case SyntaxKind.AsyncKeyword:
80+
// ES2017 async modifier should be elided for targets < ES2017
8081
return undefined;
8182

8283
case SyntaxKind.AwaitExpression:
83-
// Typescript 'await' expressions must be transformed for targets < ES2017.
84+
// ES2017 'await' expressions must be transformed for targets < ES2017.
8485
return visitAwaitExpression(<AwaitExpression>node);
8586

8687
case SyntaxKind.MethodDeclaration:
87-
// TypeScript method declarations may be 'async'
88+
// ES2017 method declarations may be 'async'
8889
return visitMethodDeclaration(<MethodDeclaration>node);
8990

9091
case SyntaxKind.FunctionDeclaration:
91-
// TypeScript function declarations may be 'async'
92+
// ES2017 function declarations may be 'async'
9293
return visitFunctionDeclaration(<FunctionDeclaration>node);
9394

9495
case SyntaxKind.FunctionExpression:
95-
// TypeScript function expressions may be 'async'
96+
// ES2017 function expressions may be 'async'
9697
return visitFunctionExpression(<FunctionExpression>node);
9798

9899
case SyntaxKind.ArrowFunction:
99-
// TypeScript arrow functions may be 'async'
100+
// ES2017 arrow functions may be 'async'
100101
return visitArrowFunction(<ArrowFunction>node);
101102

102103
default:
@@ -160,9 +161,7 @@ namespace ts {
160161
* Visits a function declaration.
161162
*
162163
* This function will be called when one of the following conditions are met:
163-
* - The node is an overload
164164
* - The node is marked async
165-
* - The node is exported from a TypeScript namespace
166165
*
167166
* @param node The function node.
168167
*/

src/compiler/transformers/ts.ts

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,6 @@ namespace ts {
230230
// ES6 export and default modifiers are elided when inside a namespace.
231231
return currentNamespace ? undefined : node;
232232

233-
// Typescript ES2017 async/await are handled by ES2017 transformer
234-
case SyntaxKind.AsyncKeyword:
235-
return node;
236-
237233
case SyntaxKind.PublicKeyword:
238234
case SyntaxKind.PrivateKeyword:
239235
case SyntaxKind.ProtectedKeyword:
@@ -297,7 +293,6 @@ namespace ts {
297293
// - property declarations
298294
// - index signatures
299295
// - method overload signatures
300-
// - async methods
301296
return visitClassDeclaration(<ClassDeclaration>node);
302297

303298
case SyntaxKind.ClassExpression:
@@ -310,7 +305,6 @@ namespace ts {
310305
// - property declarations
311306
// - index signatures
312307
// - method overload signatures
313-
// - async methods
314308
return visitClassExpression(<ClassExpression>node);
315309

316310
case SyntaxKind.HeritageClause:
@@ -325,7 +319,7 @@ namespace ts {
325319
return visitExpressionWithTypeArguments(<ExpressionWithTypeArguments>node);
326320

327321
case SyntaxKind.MethodDeclaration:
328-
// TypeScript method declarations may be 'async', and may have decorators, modifiers
322+
// TypeScript method declarations may have decorators, modifiers
329323
// or type annotations.
330324
return visitMethodDeclaration(<MethodDeclaration>node);
331325

@@ -334,19 +328,19 @@ namespace ts {
334328
return visitGetAccessor(<GetAccessorDeclaration>node);
335329

336330
case SyntaxKind.SetAccessor:
337-
// Set Accessors can have TypeScript modifiers, decorators, and type annotations.
331+
// Set Accessors can have TypeScript modifiers and type annotations.
338332
return visitSetAccessor(<SetAccessorDeclaration>node);
339333

340334
case SyntaxKind.FunctionDeclaration:
341-
// TypeScript function declarations may be 'async'
335+
// Typescript function declarations can have modifiers, decorators, and type annotations.
342336
return visitFunctionDeclaration(<FunctionDeclaration>node);
343337

344338
case SyntaxKind.FunctionExpression:
345-
// TypeScript function expressions may be 'async'
339+
// TypeScript function expressions can have modifiers and type annotations.
346340
return visitFunctionExpression(<FunctionExpression>node);
347341

348342
case SyntaxKind.ArrowFunction:
349-
// TypeScript arrow functions may be 'async'
343+
// TypeScript arrow functions can have modifiers and type annotations.
350344
return visitArrowFunction(<ArrowFunction>node);
351345

352346
case SyntaxKind.Parameter:
@@ -378,10 +372,6 @@ namespace ts {
378372
// TypeScript enum declarations do not exist in ES6 and must be rewritten.
379373
return visitEnumDeclaration(<EnumDeclaration>node);
380374

381-
case SyntaxKind.AwaitExpression:
382-
// Typescript ES2017 async/await are handled by ES2017 transformer
383-
return visitAwaitExpression(<AwaitExpression>node);
384-
385375
case SyntaxKind.VariableStatement:
386376
// TypeScript namespace exports for variable statements must be transformed.
387377
return visitVariableStatement(<VariableStatement>node);
@@ -2050,7 +2040,7 @@ namespace ts {
20502040
*
20512041
* This function will be called when one of the following conditions are met:
20522042
* - The node is an overload
2053-
* - The node is marked as abstract, async, public, private, protected, or readonly
2043+
* - The node is marked as abstract, public, private, protected, or readonly
20542044
* - The node has both a decorator and a computed property name
20552045
*
20562046
* @param node The method node.
@@ -2161,8 +2151,8 @@ namespace ts {
21612151
*
21622152
* This function will be called when one of the following conditions are met:
21632153
* - The node is an overload
2164-
* - The node is marked async
21652154
* - The node is exported from a TypeScript namespace
2155+
* - The node has decorators
21662156
*
21672157
* @param node The function node.
21682158
*/
@@ -2197,7 +2187,7 @@ namespace ts {
21972187
* Visits a function expression node.
21982188
*
21992189
* This function will be called when one of the following conditions are met:
2200-
* - The node is marked async
2190+
* - The node has type annotations
22012191
*
22022192
* @param node The function expression node.
22032193
*/
@@ -2216,10 +2206,6 @@ namespace ts {
22162206
/*location*/ node
22172207
);
22182208

2219-
// Keep modifiers in case of async functions
2220-
const funcModifiers = visitNodes(node.modifiers, visitor, isModifier);
2221-
func.modifiers = createNodeArray(funcModifiers);
2222-
22232209
setOriginalNode(func, node);
22242210

22252211
return func;
@@ -2228,7 +2214,7 @@ namespace ts {
22282214
/**
22292215
* @remarks
22302216
* This function will be called when one of the following conditions are met:
2231-
* - The node is marked async
2217+
* - The node has type annotations
22322218
*/
22332219
function visitArrowFunction(node: ArrowFunction) {
22342220
const func = createArrowFunction(
@@ -2368,23 +2354,6 @@ namespace ts {
23682354
}
23692355
}
23702356

2371-
/**
2372-
* Visits an await expression.
2373-
*
2374-
* This function will be called any time a ES2017 await expression is encountered.
2375-
*
2376-
* @param node The await expression node.
2377-
*/
2378-
function visitAwaitExpression(node: AwaitExpression): Expression {
2379-
return updateNode(
2380-
createAwait(
2381-
visitNode(node.expression, visitor, isExpression),
2382-
/*location*/ node
2383-
),
2384-
node
2385-
);
2386-
}
2387-
23882357
/**
23892358
* Visits a parenthesized expression that contains either a type assertion or an `as`
23902359
* expression.

0 commit comments

Comments
 (0)