add support for statements in asyncToGenerator and bluebirdCoroutines transformers

This commit is contained in:
Sebastian McKenzie
2015-01-02 04:58:59 +11:00
parent a813341433
commit a18177026c
11 changed files with 86 additions and 12 deletions

View File

@@ -10,10 +10,11 @@ var t = require("./types");
var _ = require("lodash");
function File(opts) {
this.opts = File.normaliseOptions(opts);
this.transformers = this.getTransformers();
this.uids = {};
this.ast = {};
this.unshiftProgramAtEnd = [];
this.opts = File.normaliseOptions(opts);
this.transformers = this.getTransformers();
this.uids = {};
this.ast = {};
}
File.declarations = [
@@ -156,7 +157,7 @@ File.prototype.parseShebang = function (code) {
File.prototype.addImport = function (id, source) {
var specifiers = [t.importSpecifier(t.identifier("default"), id)];
var declar = t.importDeclaration(specifiers, t.literal(source));
this.ast.program.body.unshift(declar);
this.unshiftProgramAtEnd.push(declar);
};
File.prototype.addDeclaration = function (name) {
@@ -230,6 +231,11 @@ File.prototype.transform = function (ast) {
_.each(this.transformers, function (transformer) {
transformer.transform(self);
var unshift = self.unshiftProgramAtEnd;
if (unshift.length) {
ast.program.body = unshift.concat(ast.program.body);
self.unshiftProgramAtEnd = [];
}
});
astRun("exit");

View File

@@ -8,7 +8,5 @@ exports.manipulateOptions = bluebirdCoroutines.manipulateOptions;
exports.Function = function (node, parent, file) {
if (!node.async || node.generator) return;
bluebirdCoroutines._Function(node);
return t.callExpression(file.addDeclaration("async-to-generator"), [node]);
return bluebirdCoroutines._Function(node, file.addDeclaration("async-to-generator"));
};

View File

@@ -8,7 +8,7 @@ exports.manipulateOptions = function (opts) {
exports.optional = true;
exports._Function = function (node) {
exports._Function = function (node, callId) {
node.async = false;
node.generator = true;
@@ -21,14 +21,25 @@ exports._Function = function (node) {
}
}
});
var call = t.callExpression(callId, [node]);
if (t.isFunctionDeclaration(node)) {
var declar = t.variableDeclaration("var", [
t.variableDeclarator(node.id, call)
]);
declar._blockHoist = true;
return declar;
} else {
return call;
}
};
exports.Function = function (node, parent, file) {
if (!node.async || node.generator) return;
exports._Function(node);
var id = t.identifier("Bluebird");
file.addImport(id, "bluebird");
return t.callExpression(t.memberExpression(id, t.identifier("coroutine")), [node]);
return exports._Function(node, t.memberExpression(id, t.identifier("coroutine")));
};