rewrite named function expressions in async function transformers - fixes #979

This commit is contained in:
Sebastian McKenzie
2015-03-10 01:16:38 +11:00
parent 62f37c1e62
commit 644b4373fc
6 changed files with 54 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
import t from "../../types";
var visitor = {
enter(node) {
var awaitVisitor = {
enter(node, parent, scope, state) {
if (t.isFunction(node)) this.skip();
if (t.isAwaitExpression(node)) {
@@ -16,13 +16,23 @@ var visitor = {
}
};
var referenceVisitor = {
enter(node, parent, scope, state) {
var name = state.id.name;
if (t.isReferencedIdentifier(node, parent, { name: name }) && scope.bindingIdentifierEquals(name, state.id)) {
return state.ref ||= scope.generateUidIdentifier(name);
}
}
};
export default function (node, callId, scope) {
node.async = false;
node.generator = true;
scope.traverse(node, visitor);
scope.traverse(node, awaitVisitor, state);
var call = t.callExpression(callId, [node]);
var id = node.id;
node.id = null;
@@ -33,6 +43,16 @@ export default function (node, callId, scope) {
declar._blockHoist = true;
return declar;
} else {
if (id) {
var state = { id: id };
scope.traverse(node, referenceVisitor, state);
if (state.ref) {
scope.parent.push({ id: state.ref });
return t.assignmentExpression("=", state.ref, call);
}
}
return call;
}
};

View File

@@ -583,7 +583,7 @@ export default class Scope {
if (t.isBlockStatement(block) || t.isProgram(block)) {
block._declarations ||= {};
block._declarations[opts.key] = {
block._declarations[opts.key || opts.id.name] = {
kind: opts.kind || "var",
id: opts.id,
init: opts.init