From 40f8bc0a65a76d3bb39a2324ae716e113c9595bc Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 2 Jan 2015 01:20:28 +1100 Subject: [PATCH] add optional bluebird coroutine transformer - @phpnode --- lib/6to5/file.js | 10 +++++++ lib/6to5/transformation/transform.js | 2 ++ lib/6to5/transformation/transformer.js | 11 +++---- .../optional-bluebird-coroutines.js | 30 +++++++++++++++++++ .../transformers/optional-core-aliasing.js | 4 +-- .../basic/actual.js | 3 ++ .../basic/expected.js | 11 +++++++ .../optional-bluebird-coroutines/options.json | 3 ++ 8 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 lib/6to5/transformation/transformers/optional-bluebird-coroutines.js create mode 100644 test/fixtures/transformation/optional-bluebird-coroutines/basic/actual.js create mode 100644 test/fixtures/transformation/optional-bluebird-coroutines/basic/expected.js create mode 100644 test/fixtures/transformation/optional-bluebird-coroutines/options.json diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 87c09f5583..7eda4531d5 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -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); diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 02e0d3cfe7..d7c44d377d 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -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"), diff --git a/lib/6to5/transformation/transformer.js b/lib/6to5/transformation/transformer.js index 92b1082836..c0f8049195 100644 --- a/lib/6to5/transformation/transformer.js +++ b/lib/6to5/transformation/transformer.js @@ -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) { diff --git a/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js new file mode 100644 index 0000000000..a08a27e121 --- /dev/null +++ b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js @@ -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]); +}; diff --git a/lib/6to5/transformation/transformers/optional-core-aliasing.js b/lib/6to5/transformation/transformers/optional-core-aliasing.js index 15961756c3..4c41448f23 100644 --- a/lib/6to5/transformation/transformers/optional-core-aliasing.js +++ b/lib/6to5/transformation/transformers/optional-core-aliasing.js @@ -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) { diff --git a/test/fixtures/transformation/optional-bluebird-coroutines/basic/actual.js b/test/fixtures/transformation/optional-bluebird-coroutines/basic/actual.js new file mode 100644 index 0000000000..363ec6d007 --- /dev/null +++ b/test/fixtures/transformation/optional-bluebird-coroutines/basic/actual.js @@ -0,0 +1,3 @@ +var foo = async function () { + var wat = await bar(); +}; diff --git a/test/fixtures/transformation/optional-bluebird-coroutines/basic/expected.js b/test/fixtures/transformation/optional-bluebird-coroutines/basic/expected.js new file mode 100644 index 0000000000..a672903731 --- /dev/null +++ b/test/fixtures/transformation/optional-bluebird-coroutines/basic/expected.js @@ -0,0 +1,11 @@ +"use strict"; + +var _interopRequire = function (obj) { + return obj && (obj["default"] || obj); +}; + +var Bluebird = _interopRequire(require("bluebird")); + +var foo = Bluebird.coroutine(function* () { + var wat = yield bar(); +}); diff --git a/test/fixtures/transformation/optional-bluebird-coroutines/options.json b/test/fixtures/transformation/optional-bluebird-coroutines/options.json new file mode 100644 index 0000000000..264ef8fbf1 --- /dev/null +++ b/test/fixtures/transformation/optional-bluebird-coroutines/options.json @@ -0,0 +1,3 @@ +{ + "optional": ["bluebirdCoroutines"] +}