Merge transform-async-to-module-method into transform-async-to-generator (#6573)

This commit is contained in:
Henry Zhu 2017-10-27 17:14:40 -04:00 committed by GitHub
parent f5ec9251c9
commit 5b47e4a6cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 64 additions and 129 deletions

View File

@ -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

View File

@ -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": {

View File

@ -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) {

View File

@ -0,0 +1,9 @@
{
"plugins": [
"external-helpers",
["transform-async-to-generator", {
"module": "bluebird",
"method": "coroutine"
}]
]
}

View File

@ -1,3 +0,0 @@
src
test
*.log

View File

@ -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"]
});
```

View File

@ -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"
}
}

View File

@ -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,
});
},
},
};
}

View File

@ -1,3 +0,0 @@
{
"plugins": ["external-helpers", ["transform-async-to-module-method", { "module": "bluebird", "method": "coroutine" }]]
}

View File

@ -1,3 +0,0 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);

View File

@ -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",

View File

@ -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"),