babel/lib/6to5/transformation/helpers/remap-async-to-generator.js
2015-01-29 15:11:17 +11:00

42 lines
965 B
JavaScript

"use strict";
var traverse = require("../../traverse");
var t = require("../../types");
var visitor = {
enter: function (node, parent, scope, context) {
if (t.isFunction(node)) context.skip();
if (t.isAwaitExpression(node)) {
node.type = "YieldExpression";
if (node.all) {
// await* foo; -> yield Promise.all(foo);
node.all = false;
node.argument = t.callExpression(t.memberExpression(t.identifier("Promise"), t.identifier("all")), [node.argument]);
}
}
}
};
module.exports = function (node, callId, scope) {
node.async = false;
node.generator = true;
traverse(node, visitor, scope);
var call = t.callExpression(callId, [node]);
var id = node.id;
delete node.id;
if (t.isFunctionDeclaration(node)) {
var declar = t.variableDeclaration("let", [
t.variableDeclarator(id, call)
]);
declar._blockHoist = true;
return declar;
} else {
return call;
}
};