Make async/await an ES2017 feature only

This commit is contained in:
Andrej Baran 2016-10-13 11:58:01 +02:00
parent e60e97f5c9
commit f0fd77ae8e
3 changed files with 28 additions and 65 deletions

View File

@ -2555,11 +2555,6 @@ namespace ts {
// extends clause of a class.
let transformFlags = subtreeFlags | TransformFlags.AssertES6;
// propagate ES2017
if (node.expression.transformFlags & TransformFlags.ContainsES2017) {
transformFlags |= TransformFlags.ContainsES2017;
}
// If an ExpressionWithTypeArguments contains type arguments, then it
// is TypeScript syntax.
if (node.typeArguments) {
@ -2591,16 +2586,16 @@ namespace ts {
const typeParameters = node.typeParameters;
const asteriskToken = node.asteriskToken;
// A MethodDeclaration is TypeScript syntax if it is either async, abstract, overloaded,
// A MethodDeclaration is TypeScript syntax if it is either abstract, overloaded,
// generic, or has a decorator.
if (!body
|| typeParameters
|| (modifierFlags & (ModifierFlags.Async | ModifierFlags.Abstract))
|| (modifierFlags & ModifierFlags.Abstract)
|| (subtreeFlags & TransformFlags.ContainsDecorators)) {
transformFlags |= TransformFlags.AssertTypeScript;
}
// Async MethodDeclaration is ES2017
// An async method declaration is ES2017 syntax.
if (modifierFlags & ModifierFlags.Async) {
transformFlags |= TransformFlags.AssertES2017;
}
@ -2619,10 +2614,10 @@ namespace ts {
const modifierFlags = getModifierFlags(node);
const body = node.body;
// A MethodDeclaration is TypeScript syntax if it is either async, abstract, overloaded,
// An accessor is TypeScript syntax if it is either abstract, overloaded,
// generic, or has a decorator.
if (!body
|| (modifierFlags & (ModifierFlags.Async | ModifierFlags.Abstract))
|| (modifierFlags & ModifierFlags.Abstract)
|| (subtreeFlags & TransformFlags.ContainsDecorators)) {
transformFlags |= TransformFlags.AssertTypeScript;
}
@ -2664,9 +2659,9 @@ namespace ts {
transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES6;
}
// If a FunctionDeclaration is async, then it is TypeScript syntax.
// An async function declaration is ES2017 syntax.
if (modifierFlags & ModifierFlags.Async) {
transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES2017;
transformFlags |= TransformFlags.AssertES2017;
}
// If a FunctionDeclaration's subtree has marked the container as needing to capture the
@ -2695,9 +2690,9 @@ namespace ts {
const modifierFlags = getModifierFlags(node);
const asteriskToken = node.asteriskToken;
// An async function expression is TypeScript syntax.
// An async function expression is ES2017 syntax.
if (modifierFlags & ModifierFlags.Async) {
transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES2017;
transformFlags |= TransformFlags.AssertES2017;
}
// If a FunctionExpression's subtree has marked the container as needing to capture the
@ -2725,9 +2720,9 @@ namespace ts {
let transformFlags = subtreeFlags | TransformFlags.AssertES6;
const modifierFlags = getModifierFlags(node);
// An async arrow function is TypeScript syntax.
// An async arrow function is ES2017 syntax.
if (modifierFlags & ModifierFlags.Async) {
transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES2017;
transformFlags |= TransformFlags.AssertES2017;
}
// If an ArrowFunction contains a lexical this, its container must capture the lexical this.
@ -2868,8 +2863,8 @@ namespace ts {
switch (kind) {
case SyntaxKind.AsyncKeyword:
case SyntaxKind.AwaitExpression:
// Typescript async/await are ES2017 features
transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES2017;
// async/await is ES2017 syntax
transformFlags |= TransformFlags.AssertES2017;
break;
case SyntaxKind.PublicKeyword:

View File

@ -77,26 +77,27 @@ namespace ts {
function visitorWorker(node: Node): VisitResult<Node> {
switch (node.kind) {
case SyntaxKind.AsyncKeyword:
// ES2017 async modifier should be elided for targets < ES2017
return undefined;
case SyntaxKind.AwaitExpression:
// Typescript 'await' expressions must be transformed for targets < ES2017.
// ES2017 'await' expressions must be transformed for targets < ES2017.
return visitAwaitExpression(<AwaitExpression>node);
case SyntaxKind.MethodDeclaration:
// TypeScript method declarations may be 'async'
// ES2017 method declarations may be 'async'
return visitMethodDeclaration(<MethodDeclaration>node);
case SyntaxKind.FunctionDeclaration:
// TypeScript function declarations may be 'async'
// ES2017 function declarations may be 'async'
return visitFunctionDeclaration(<FunctionDeclaration>node);
case SyntaxKind.FunctionExpression:
// TypeScript function expressions may be 'async'
// ES2017 function expressions may be 'async'
return visitFunctionExpression(<FunctionExpression>node);
case SyntaxKind.ArrowFunction:
// TypeScript arrow functions may be 'async'
// ES2017 arrow functions may be 'async'
return visitArrowFunction(<ArrowFunction>node);
default:
@ -160,9 +161,7 @@ namespace ts {
* Visits a function declaration.
*
* This function will be called when one of the following conditions are met:
* - The node is an overload
* - The node is marked async
* - The node is exported from a TypeScript namespace
*
* @param node The function node.
*/

View File

@ -230,10 +230,6 @@ namespace ts {
// ES6 export and default modifiers are elided when inside a namespace.
return currentNamespace ? undefined : node;
// Typescript ES2017 async/await are handled by ES2017 transformer
case SyntaxKind.AsyncKeyword:
return node;
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
@ -297,7 +293,6 @@ namespace ts {
// - property declarations
// - index signatures
// - method overload signatures
// - async methods
return visitClassDeclaration(<ClassDeclaration>node);
case SyntaxKind.ClassExpression:
@ -310,7 +305,6 @@ namespace ts {
// - property declarations
// - index signatures
// - method overload signatures
// - async methods
return visitClassExpression(<ClassExpression>node);
case SyntaxKind.HeritageClause:
@ -325,7 +319,7 @@ namespace ts {
return visitExpressionWithTypeArguments(<ExpressionWithTypeArguments>node);
case SyntaxKind.MethodDeclaration:
// TypeScript method declarations may be 'async', and may have decorators, modifiers
// TypeScript method declarations may have decorators, modifiers
// or type annotations.
return visitMethodDeclaration(<MethodDeclaration>node);
@ -334,19 +328,19 @@ namespace ts {
return visitGetAccessor(<GetAccessorDeclaration>node);
case SyntaxKind.SetAccessor:
// Set Accessors can have TypeScript modifiers, decorators, and type annotations.
// Set Accessors can have TypeScript modifiers and type annotations.
return visitSetAccessor(<SetAccessorDeclaration>node);
case SyntaxKind.FunctionDeclaration:
// TypeScript function declarations may be 'async'
// Typescript function declarations can have modifiers, decorators, and type annotations.
return visitFunctionDeclaration(<FunctionDeclaration>node);
case SyntaxKind.FunctionExpression:
// TypeScript function expressions may be 'async'
// TypeScript function expressions can have modifiers and type annotations.
return visitFunctionExpression(<FunctionExpression>node);
case SyntaxKind.ArrowFunction:
// TypeScript arrow functions may be 'async'
// TypeScript arrow functions can have modifiers and type annotations.
return visitArrowFunction(<ArrowFunction>node);
case SyntaxKind.Parameter:
@ -378,10 +372,6 @@ namespace ts {
// TypeScript enum declarations do not exist in ES6 and must be rewritten.
return visitEnumDeclaration(<EnumDeclaration>node);
case SyntaxKind.AwaitExpression:
// Typescript ES2017 async/await are handled by ES2017 transformer
return visitAwaitExpression(<AwaitExpression>node);
case SyntaxKind.VariableStatement:
// TypeScript namespace exports for variable statements must be transformed.
return visitVariableStatement(<VariableStatement>node);
@ -2050,7 +2040,7 @@ namespace ts {
*
* This function will be called when one of the following conditions are met:
* - The node is an overload
* - The node is marked as abstract, async, public, private, protected, or readonly
* - The node is marked as abstract, public, private, protected, or readonly
* - The node has both a decorator and a computed property name
*
* @param node The method node.
@ -2161,8 +2151,8 @@ namespace ts {
*
* This function will be called when one of the following conditions are met:
* - The node is an overload
* - The node is marked async
* - The node is exported from a TypeScript namespace
* - The node has decorators
*
* @param node The function node.
*/
@ -2197,7 +2187,7 @@ namespace ts {
* Visits a function expression node.
*
* This function will be called when one of the following conditions are met:
* - The node is marked async
* - The node has type annotations
*
* @param node The function expression node.
*/
@ -2216,10 +2206,6 @@ namespace ts {
/*location*/ node
);
// Keep modifiers in case of async functions
const funcModifiers = visitNodes(node.modifiers, visitor, isModifier);
func.modifiers = createNodeArray(funcModifiers);
setOriginalNode(func, node);
return func;
@ -2228,7 +2214,7 @@ namespace ts {
/**
* @remarks
* This function will be called when one of the following conditions are met:
* - The node is marked async
* - The node has type annotations
*/
function visitArrowFunction(node: ArrowFunction) {
const func = createArrowFunction(
@ -2368,23 +2354,6 @@ namespace ts {
}
}
/**
* Visits an await expression.
*
* This function will be called any time a ES2017 await expression is encountered.
*
* @param node The await expression node.
*/
function visitAwaitExpression(node: AwaitExpression): Expression {
return updateNode(
createAwait(
visitNode(node.expression, visitor, isExpression),
/*location*/ node
),
node
);
}
/**
* Visits a parenthesized expression that contains either a type assertion or an `as`
* expression.