add optional bluebird coroutine transformer - @phpnode
This commit is contained in:
@@ -95,6 +95,10 @@ File.prototype.getTransformers = function () {
|
||||
_.each(transform.transformers, function (transformer) {
|
||||
if (transformer.canRun(file)) {
|
||||
transformers.push(transformer);
|
||||
|
||||
if (transformer.manipulateOptions) {
|
||||
transformer.manipulateOptions(file.opts, file);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -144,6 +148,12 @@ File.prototype.parseShebang = function (code) {
|
||||
return 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);
|
||||
};
|
||||
|
||||
File.prototype.addDeclaration = function (name) {
|
||||
if (!_.contains(File.declarations, name)) {
|
||||
throw new ReferenceError("unknown declaration " + name);
|
||||
|
||||
@@ -49,6 +49,8 @@ _.each({
|
||||
memoizationOperator: require("./transformers/playground-memoization-operator"),
|
||||
objectGetterMemoization: require("./transformers/playground-object-getter-memoization"),
|
||||
|
||||
bluebirdCoroutines: require("./transformers/optional-bluebird-coroutines"),
|
||||
|
||||
react: require("./transformers/react"),
|
||||
modules: require("./transformers/es6-modules"),
|
||||
propertyNameShorthand: require("./transformers/es6-property-name-shorthand"),
|
||||
|
||||
@@ -5,11 +5,12 @@ var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function Transformer(key, transformer, opts) {
|
||||
this.experimental = !!transformer.experimental;
|
||||
this.transformer = Transformer.normalise(transformer);
|
||||
this.optional = !!transformer.optional;
|
||||
this.opts = opts || {};
|
||||
this.key = key;
|
||||
this.manipulateOptions = transformer.manipulateOptions;
|
||||
this.experimental = !!transformer.experimental;
|
||||
this.transformer = Transformer.normalise(transformer);
|
||||
this.optional = !!transformer.optional;
|
||||
this.opts = opts || {};
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
Transformer.normalise = function (transformer) {
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
var traverse = require("../../traverse");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.manipulateOptions = function (opts) {
|
||||
opts.experimental = true;
|
||||
opts.blacklist.push("generators");
|
||||
};
|
||||
|
||||
exports.optional = true;
|
||||
|
||||
exports.Function = function (node, parent, file) {
|
||||
if (!node.async || node.generator) return;
|
||||
|
||||
node.async = false;
|
||||
node.generator = true;
|
||||
|
||||
traverse(node, {
|
||||
enter: function (node) {
|
||||
if (t.isFunction(node)) this.stop();
|
||||
|
||||
if (t.isAwaitExpression(node)) {
|
||||
node.type = "YieldExpression";
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var id = t.identifier("Bluebird");
|
||||
file.addImport(id, "bluebird");
|
||||
return t.callExpression(t.memberExpression(id, t.identifier("coroutine")), [node]);
|
||||
};
|
||||
@@ -9,9 +9,7 @@ exports.optional = true;
|
||||
exports.ast = {
|
||||
enter: function (ast, file) {
|
||||
file._coreId = file.generateUidIdentifier("core");
|
||||
var specifiers = [t.importSpecifier(t.identifier("default"), file._coreId)];
|
||||
var declar = t.importDeclaration(specifiers, t.literal("core-js/library"));
|
||||
ast.program.body.unshift(declar);
|
||||
file.addImport(file._coreId, "core-js/library");
|
||||
},
|
||||
|
||||
exit: function (ast, file) {
|
||||
|
||||
Reference in New Issue
Block a user