From a18177026ca66e4074e85726da05e4666cf79ff0 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 2 Jan 2015 04:58:59 +1100 Subject: [PATCH] add support for statements in `asyncToGenerator` and `bluebirdCoroutines` transformers --- lib/6to5/file.js | 16 ++++--- .../optional-async-to-generator.js | 4 +- .../optional-bluebird-coroutines.js | 19 +++++++-- .../{basic => expression}/actual.js | 0 .../{basic => expression}/expected.js | 0 .../statement/actual.js | 3 ++ .../statement/expected.js | 42 +++++++++++++++++++ .../{basic => expression}/actual.js | 0 .../{basic => expression}/expected.js | 0 .../statement/actual.js | 3 ++ .../statement/expected.js | 11 +++++ 11 files changed, 86 insertions(+), 12 deletions(-) rename test/fixtures/transformation/optional-async-to-generator/{basic => expression}/actual.js (100%) rename test/fixtures/transformation/optional-async-to-generator/{basic => expression}/expected.js (100%) create mode 100644 test/fixtures/transformation/optional-async-to-generator/statement/actual.js create mode 100644 test/fixtures/transformation/optional-async-to-generator/statement/expected.js rename test/fixtures/transformation/optional-bluebird-coroutines/{basic => expression}/actual.js (100%) rename test/fixtures/transformation/optional-bluebird-coroutines/{basic => expression}/expected.js (100%) create mode 100644 test/fixtures/transformation/optional-bluebird-coroutines/statement/actual.js create mode 100644 test/fixtures/transformation/optional-bluebird-coroutines/statement/expected.js diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 57937a47df..df1ba18e1d 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -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"); diff --git a/lib/6to5/transformation/transformers/optional-async-to-generator.js b/lib/6to5/transformation/transformers/optional-async-to-generator.js index 91a9ef3100..074bcd511e 100644 --- a/lib/6to5/transformation/transformers/optional-async-to-generator.js +++ b/lib/6to5/transformation/transformers/optional-async-to-generator.js @@ -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")); }; diff --git a/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js index 2bb993dffd..376f8a7814 100644 --- a/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js +++ b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js @@ -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"))); }; diff --git a/test/fixtures/transformation/optional-async-to-generator/basic/actual.js b/test/fixtures/transformation/optional-async-to-generator/expression/actual.js similarity index 100% rename from test/fixtures/transformation/optional-async-to-generator/basic/actual.js rename to test/fixtures/transformation/optional-async-to-generator/expression/actual.js diff --git a/test/fixtures/transformation/optional-async-to-generator/basic/expected.js b/test/fixtures/transformation/optional-async-to-generator/expression/expected.js similarity index 100% rename from test/fixtures/transformation/optional-async-to-generator/basic/expected.js rename to test/fixtures/transformation/optional-async-to-generator/expression/expected.js diff --git a/test/fixtures/transformation/optional-async-to-generator/statement/actual.js b/test/fixtures/transformation/optional-async-to-generator/statement/actual.js new file mode 100644 index 0000000000..e422835657 --- /dev/null +++ b/test/fixtures/transformation/optional-async-to-generator/statement/actual.js @@ -0,0 +1,3 @@ +async function foo() { + var wat = await bar(); +} diff --git a/test/fixtures/transformation/optional-async-to-generator/statement/expected.js b/test/fixtures/transformation/optional-async-to-generator/statement/expected.js new file mode 100644 index 0000000000..edd71bbb3a --- /dev/null +++ b/test/fixtures/transformation/optional-async-to-generator/statement/expected.js @@ -0,0 +1,42 @@ +"use strict"; + +var _asyncToGenerator = 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(); + }); + }); + }; +}; + +var foo = _asyncToGenerator(function* foo() { + var wat = yield bar(); +}); diff --git a/test/fixtures/transformation/optional-bluebird-coroutines/basic/actual.js b/test/fixtures/transformation/optional-bluebird-coroutines/expression/actual.js similarity index 100% rename from test/fixtures/transformation/optional-bluebird-coroutines/basic/actual.js rename to test/fixtures/transformation/optional-bluebird-coroutines/expression/actual.js diff --git a/test/fixtures/transformation/optional-bluebird-coroutines/basic/expected.js b/test/fixtures/transformation/optional-bluebird-coroutines/expression/expected.js similarity index 100% rename from test/fixtures/transformation/optional-bluebird-coroutines/basic/expected.js rename to test/fixtures/transformation/optional-bluebird-coroutines/expression/expected.js diff --git a/test/fixtures/transformation/optional-bluebird-coroutines/statement/actual.js b/test/fixtures/transformation/optional-bluebird-coroutines/statement/actual.js new file mode 100644 index 0000000000..e422835657 --- /dev/null +++ b/test/fixtures/transformation/optional-bluebird-coroutines/statement/actual.js @@ -0,0 +1,3 @@ +async function foo() { + var wat = await bar(); +} diff --git a/test/fixtures/transformation/optional-bluebird-coroutines/statement/expected.js b/test/fixtures/transformation/optional-bluebird-coroutines/statement/expected.js new file mode 100644 index 0000000000..65d8cff6ef --- /dev/null +++ b/test/fixtures/transformation/optional-bluebird-coroutines/statement/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* foo() { + var wat = yield bar(); +});