diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index b9ee80eb4c0..546b4159259 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2292,6 +2292,11 @@ namespace ts { transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES6; } + // If a FunctionDeclaration is async, then it is TypeScript syntax. + if (modifiers & ModifierFlags.Async) { + transformFlags |= TransformFlags.AssertTypeScript; + } + // If a FunctionDeclaration has an asterisk token, is exported, or its // subtree has marked the container as needing to capture the lexical `this`, // then this node is ES6 syntax. diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 464bfb6ce2c..93fee933bc5 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -1978,7 +1978,7 @@ namespace ts { } function transformAsyncFunctionBody(node: FunctionLikeDeclaration): ConciseBody | FunctionBody { - const promiseConstructor = getEntityNameFromTypeNode(node.type); + const promiseConstructor = languageVersion < ScriptTarget.ES6 ? getEntityNameFromTypeNode(node.type) : undefined; const isArrowFunction = node.kind === SyntaxKind.ArrowFunction; const hasLexicalArguments = (resolver.getNodeCheckFlags(node) & NodeCheckFlags.CaptureArguments) !== 0; @@ -1987,77 +1987,12 @@ namespace ts { // `this` and `arguments` objects to `__awaiter`. The generator function // passed to `__awaiter` is executed inside of the callback to the // promise constructor. - // - // The emit for an async arrow without a lexical `arguments` binding might be: - // - // // input - // let a = async (b) => { await b; } - // - // // output - // let a = (b) => __awaiter(this, void 0, void 0, function* () { - // yield b; - // }); - // - // The emit for an async arrow with a lexical `arguments` binding might be: - // - // // input - // let a = async (b) => { await arguments[0]; } - // - // // output - // let a = (b) => __awaiter(this, arguments, void 0, function* (arguments) { - // yield arguments[0]; - // }); - // - // The emit for an async function expression without a lexical `arguments` binding - // might be: - // - // // input - // let a = async function (b) { - // await b; - // } - // - // // output - // let a = function (b) { - // return __awaiter(this, void 0, void 0, function* () { - // yield b; - // }); - // } - // - // The emit for an async function expression with a lexical `arguments` binding - // might be: - // - // // input - // let a = async function (b) { - // await arguments[0]; - // } - // - // // output - // let a = function (b) { - // return __awaiter(this, arguments, void 0, function* (_arguments) { - // yield _arguments[0]; - // }); - // } - // - // The emit for an async function expression with a lexical `arguments` binding - // and a return type annotation might be: - // - // // input - // let a = async function (b): MyPromise { - // await arguments[0]; - // } - // - // // output - // let a = function (b) { - // return __awaiter(this, arguments, MyPromise, function* (_arguments) { - // yield _arguments[0]; - // }); - // } - // + if (!isArrowFunction) { const statements: Statement[] = []; - addNode(statements, + statements.push( createReturn( createAwaiterHelper( hasLexicalArguments,