rewrite named function expressions in async function transformers - fixes #979
This commit is contained in:
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user