add optional asyncToGenerator transformer - closes #321

This commit is contained in:
Sebastian McKenzie
2015-01-02 01:31:14 +11:00
parent 40f8bc0a65
commit 925b1f7600
9 changed files with 119 additions and 5 deletions

View File

@@ -27,7 +27,12 @@ File.declarations = [
"object-without-properties",
"has-own",
"slice",
"define-property"
"define-property",
"async-to-generator"
];
File.excludeDeclarationsFromRuntime = [
"async-to-generator"
];
File.normaliseOptions = function (opts) {
@@ -166,7 +171,7 @@ File.prototype.addDeclaration = function (name) {
var ref;
var runtimeNamespace = this.opts.runtime;
if (runtimeNamespace) {
if (runtimeNamespace && !_.contains(File.excludeDeclarationsFromRuntime, name)) {
name = t.identifier(t.toIdentifier(name));
return t.memberExpression(t.identifier(runtimeNamespace), name);
} else {

View File

@@ -19,6 +19,10 @@ module.exports = function (namespace) {
]));
_.each(File.declarations, function (name) {
if (_.contains(File.excludeDeclarationsFromRuntime, name)) {
return false;
}
var key = t.identifier(t.toIdentifier(name));
body.push(t.expressionStatement(
t.assignmentExpression("=", t.memberExpression(namespace, key), util.template(name))

View File

@@ -0,0 +1,37 @@
(function (fn) {
return function () {
var gen = fn.apply(this, arguments);
return new Promise(function (resolve, reject) {
function step(getNext) {
var next;
try {
next = getNext();
} catch(e) {
reject(e);
return;
}
if (next.done) {
resolve(next.value);
return;
}
Promise.resolve(next.value).then(function (v) {
step(function () {
return gen.next(v);
});
}, function (e) {
step(function () {
return gen["throw"](e);
});
});
}
step(function () {
return gen.next();
});
});
}
})

View File

@@ -49,6 +49,7 @@ _.each({
memoizationOperator: require("./transformers/playground-memoization-operator"),
objectGetterMemoization: require("./transformers/playground-object-getter-memoization"),
asyncToGenerator: require("./transformers/optional-async-to-generator"),
bluebirdCoroutines: require("./transformers/optional-bluebird-coroutines"),
react: require("./transformers/react"),

View File

@@ -0,0 +1,15 @@
var bluebirdCoroutines = require("./optional-bluebird-coroutines");
var traverse = require("../../traverse");
var t = require("../../types");
exports.optional = true;
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]);
};

View File

@@ -8,9 +8,7 @@ exports.manipulateOptions = function (opts) {
exports.optional = true;
exports.Function = function (node, parent, file) {
if (!node.async || node.generator) return;
exports._Function = function (node) {
node.async = false;
node.generator = true;
@@ -23,6 +21,12 @@ exports.Function = function (node, parent, file) {
}
}
});
};
exports.Function = function (node, parent, file) {
if (!node.async || node.generator) return;
exports._Function(node);
var id = t.identifier("Bluebird");
file.addImport(id, "bluebird");