diff --git a/packages/babel-plugin-transform-async-to-generator/README.md b/packages/babel-plugin-transform-async-to-generator/README.md index c386d396a9..b7942732d2 100644 --- a/packages/babel-plugin-transform-async-to-generator/README.md +++ b/packages/babel-plugin-transform-async-to-generator/README.md @@ -2,6 +2,8 @@ > Turn async functions into ES2015 generators +> In Babel 7, `transform-async-to-module-method` was merged into this plugin + ## Example **In** @@ -23,6 +25,18 @@ var foo = _asyncToGenerator(function* () { }); ``` +**Out with options** + +> Turn async functions into a Bluebird coroutine + +```javascript +var Bluebird = require("bluebird"); + +var foo = Bluebird.coroutine(function* () { + yield bar(); +}); +``` + ## Installation ```sh @@ -35,12 +49,27 @@ npm install --save-dev @babel/plugin-transform-async-to-generator **.babelrc** +Without options: + ```json { "plugins": ["@babel/transform-async-to-generator"] } ``` +With options: + +```json +{ + "plugins": [ + ["@babel/transform-async-to-generator", { + "module": "bluebird", + "method": "coroutine" + }] + ] +} +``` + ### Via CLI ```sh diff --git a/packages/babel-plugin-transform-async-to-generator/package.json b/packages/babel-plugin-transform-async-to-generator/package.json index 0975596dfe..bb9ac97ba5 100644 --- a/packages/babel-plugin-transform-async-to-generator/package.json +++ b/packages/babel-plugin-transform-async-to-generator/package.json @@ -9,6 +9,7 @@ "babel-plugin" ], "dependencies": { + "@babel/helper-module-imports": "7.0.0-beta.3", "@babel/helper-remap-async-to-generator": "7.0.0-beta.3" }, "peerDependencies": { diff --git a/packages/babel-plugin-transform-async-to-generator/src/index.js b/packages/babel-plugin-transform-async-to-generator/src/index.js index 29154457d1..79043c63f6 100644 --- a/packages/babel-plugin-transform-async-to-generator/src/index.js +++ b/packages/babel-plugin-transform-async-to-generator/src/index.js @@ -1,6 +1,30 @@ import remapAsyncToGenerator from "@babel/helper-remap-async-to-generator"; +import { addNamed } from "@babel/helper-module-imports"; + +export default function({ types: t }, options) { + const { method, module } = options; + + if (method && module) { + return { + visitor: { + Function(path, state) { + if (!path.node.async || path.node.generator) return; + + let wrapAsync = state.methodWrapper; + if (wrapAsync) { + wrapAsync = t.cloneDeep(wrapAsync); + } else { + wrapAsync = state.methodWrapper = addNamed(path, method, module); + } + + remapAsyncToGenerator(path, state.file, { + wrapAsync, + }); + }, + }, + }; + } -export default function() { return { visitor: { Function(path, state) { diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/arrow-function/actual.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/arrow-function/actual.js similarity index 100% rename from packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/arrow-function/actual.js rename to packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/arrow-function/actual.js diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/arrow-function/expected.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/arrow-function/expected.js similarity index 100% rename from packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/arrow-function/expected.js rename to packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/arrow-function/expected.js diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/class/actual.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/class/actual.js similarity index 100% rename from packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/class/actual.js rename to packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/class/actual.js diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/class/expected.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/class/expected.js similarity index 100% rename from packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/class/expected.js rename to packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/class/expected.js diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/expression/actual.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/expression/actual.js similarity index 100% rename from packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/expression/actual.js rename to packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/expression/actual.js diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/expression/expected.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/expression/expected.js similarity index 100% rename from packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/expression/expected.js rename to packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/expression/expected.js diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/named-expression/actual.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/named-expression/actual.js similarity index 100% rename from packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/named-expression/actual.js rename to packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/named-expression/actual.js diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/named-expression/expected.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/named-expression/expected.js similarity index 100% rename from packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/named-expression/expected.js rename to packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/named-expression/expected.js diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/options.json b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/options.json new file mode 100644 index 0000000000..f178c0253e --- /dev/null +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + "external-helpers", + ["transform-async-to-generator", { + "module": "bluebird", + "method": "coroutine" + }] + ] +} diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/statement/actual.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/statement/actual.js similarity index 100% rename from packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/statement/actual.js rename to packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/statement/actual.js diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/statement/expected.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/statement/expected.js similarity index 100% rename from packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/statement/expected.js rename to packages/babel-plugin-transform-async-to-generator/test/fixtures/bluebird-coroutines/statement/expected.js diff --git a/packages/babel-plugin-transform-async-to-module-method/.npmignore b/packages/babel-plugin-transform-async-to-module-method/.npmignore deleted file mode 100644 index f980694583..0000000000 --- a/packages/babel-plugin-transform-async-to-module-method/.npmignore +++ /dev/null @@ -1,3 +0,0 @@ -src -test -*.log diff --git a/packages/babel-plugin-transform-async-to-module-method/README.md b/packages/babel-plugin-transform-async-to-module-method/README.md deleted file mode 100644 index d408a63c3a..0000000000 --- a/packages/babel-plugin-transform-async-to-module-method/README.md +++ /dev/null @@ -1,70 +0,0 @@ -# @babel/plugin-transform-async-to-module-method - -> Turn async functions into a Bluebird coroutine - -## Example - -**In** - -```javascript -async function foo() { - await bar(); -} -``` - -**Out** - -```javascript -var Bluebird = require("bluebird"); - -var foo = Bluebird.coroutine(function* () { - yield bar(); -}); -``` - -## Installation - -```sh -npm install --save-dev @babel/plugin-transform-async-to-module-method -``` - -## Usage - -### Via `.babelrc` (Recommended) - -**.babelrc** - -Without options: - -```json -{ - "plugins": ["@babel/transform-async-to-module-method"] -} -``` - -With options: - -```json -{ - "plugins": [ - ["@babel/transform-async-to-module-method", { - "module": "bluebird", - "method": "coroutine" - }] - ] -} -``` - -### Via CLI - -```sh -babel --plugins @babel/transform-async-to-module-method script.js -``` - -### Via Node API - -```javascript -require("@babel/core").transform("code", { - plugins: ["@babel/transform-async-to-module-method"] -}); -``` diff --git a/packages/babel-plugin-transform-async-to-module-method/package.json b/packages/babel-plugin-transform-async-to-module-method/package.json deleted file mode 100644 index fc2082f77a..0000000000 --- a/packages/babel-plugin-transform-async-to-module-method/package.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "name": "@babel/plugin-transform-async-to-module-method", - "version": "7.0.0-beta.3", - "description": "Turn async functions into a module method", - "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-async-to-module-method", - "license": "MIT", - "main": "lib/index.js", - "keywords": [ - "babel-plugin" - ], - "dependencies": { - "@babel/helper-module-imports": "7.0.0-beta.3", - "@babel/helper-remap-async-to-generator": "7.0.0-beta.3", - "@babel/types": "7.0.0-beta.3" - }, - "peerDependencies": { - "@babel/core": "7.0.0-beta.3" - }, - "devDependencies": { - "@babel/helper-plugin-test-runner": "7.0.0-beta.3" - } -} diff --git a/packages/babel-plugin-transform-async-to-module-method/src/index.js b/packages/babel-plugin-transform-async-to-module-method/src/index.js deleted file mode 100644 index c116fd30ba..0000000000 --- a/packages/babel-plugin-transform-async-to-module-method/src/index.js +++ /dev/null @@ -1,25 +0,0 @@ -import remapAsyncToGenerator from "@babel/helper-remap-async-to-generator"; - -import { addNamed } from "@babel/helper-module-imports"; - -export default function({ types: t }, options) { - const { method, module } = options; - return { - visitor: { - Function(path, state) { - if (!path.node.async || path.node.generator) return; - - let wrapAsync = state.methodWrapper; - if (wrapAsync) { - wrapAsync = t.cloneDeep(wrapAsync); - } else { - wrapAsync = state.methodWrapper = addNamed(path, method, module); - } - - remapAsyncToGenerator(path, state.file, { - wrapAsync, - }); - }, - }, - }; -} diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/options.json b/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/options.json deleted file mode 100644 index e6402afd2d..0000000000 --- a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "plugins": ["external-helpers", ["transform-async-to-module-method", { "module": "bluebird", "method": "coroutine" }]] -} diff --git a/packages/babel-plugin-transform-async-to-module-method/test/index.js b/packages/babel-plugin-transform-async-to-module-method/test/index.js deleted file mode 100644 index 1b534b8fc6..0000000000 --- a/packages/babel-plugin-transform-async-to-module-method/test/index.js +++ /dev/null @@ -1,3 +0,0 @@ -import runner from "@babel/helper-plugin-test-runner"; - -runner(__dirname); diff --git a/packages/babel-standalone/package.json b/packages/babel-standalone/package.json index ce16e5f5c0..08c3d1a486 100644 --- a/packages/babel-standalone/package.json +++ b/packages/babel-standalone/package.json @@ -26,7 +26,6 @@ "@babel/plugin-syntax-optional-catch-binding": "7.0.0-beta.3", "@babel/plugin-proposal-async-generator-functions": "7.0.0-beta.3", "@babel/plugin-transform-async-to-generator": "7.0.0-beta.3", - "@babel/plugin-transform-async-to-module-method": "7.0.0-beta.3", "@babel/plugin-proposal-class-properties": "7.0.0-beta.3", "@babel/plugin-proposal-decorators": "7.0.0-beta.3", "@babel/plugin-proposal-do-expressions": "7.0.0-beta.3", diff --git a/packages/babel-standalone/src/index.js b/packages/babel-standalone/src/index.js index 1726fdd67c..34d5bec181 100644 --- a/packages/babel-standalone/src/index.js +++ b/packages/babel-standalone/src/index.js @@ -153,7 +153,6 @@ registerPlugins({ "syntax-object-rest-spread": require("@babel/plugin-syntax-object-rest-spread"), "syntax-optional-catch-binding": require("@babel/plugin-syntax-optional-catch-binding"), "transform-async-to-generator": require("@babel/plugin-transform-async-to-generator"), - "transform-async-to-module-method": require("@babel/plugin-transform-async-to-module-method"), "proposal-class-properties": require("@babel/plugin-proposal-class-properties"), "proposal-decorators": require("@babel/plugin-proposal-decorators"), "proposal-do-expressions": require("@babel/plugin-proposal-do-expressions"),