flesh out async-to-generator and bluebird-coroutines transformers, add babel-helper-async-to-generator module

This commit is contained in:
Sebastian McKenzie 2015-10-05 17:25:43 +01:00
parent de45daaef8
commit 9320d661f9
7 changed files with 123 additions and 4 deletions

View File

@ -0,0 +1,5 @@
# babel-helper-remap-async-to-generator
## Usage
TODO

View File

@ -0,0 +1,15 @@
{
"name": "babel-helper-remap-async-to-generator",
"version": "1.0.0",
"description": "",
"repository": "babel/babel",
"license": "MIT",
"main": "lib/index.js",
"dependencies": {
"babel-runtime": "^5.8.20",
"babel-types": "^5.8.20",
"babel-traverse": "^5.8.20",
"babel-helper-explode-class": "^5.0.0",
"babel-helper-function-name": "^5.0.0"
}
}

View File

@ -0,0 +1,68 @@
/* @flow */
import type { NodePath } from "babel-traverse";
import explodeClass from "babel-helper-explode-class";
import { bare as nameMethod } from "babel-helper-function-name";
import * as t from "babel-types";
let awaitVisitor = {
Function(path) {
path.skip();
},
AwaitExpression({ node }) {
node.type = "YieldExpression";
}
};
let referenceVisitor = {
ReferencedIdentifier({ node, scope }, state) {
let name = state.id.name;
if (node.name === name && scope.bindingIdentifierEquals(name, state.id)) {
return state.ref = state.ref || scope.generateUidIdentifier(name);
}
}
};
export default function (path: NodePath, callId: Object) {
let node = path.node;
if (node.generator) return;
node.async = false;
node.generator = true;
path.traverse(awaitVisitor);
let container = t.functionExpression(null, [], t.blockStatement([
t.returnStatement(t.callExpression(callId, [node]))
]));
node.shadow = container;
if (path.isFunctionDeclaration()) {
let declar = t.variableDeclaration("let", [
t.variableDeclarator(id, container)
]);
declar._blockHoist = true;
nameMethod({
node: container,
parent: declar.declarations[0],
scope: path.scope
});
path.replaceWith(declar);
} else {
node.type = "FunctionExpression";
if (path.parentPath.isMethodDefinition({ value: node })) {
// we're a class method
let classPath = path.parentPath.parentPath.parentPath;
explodeClass(classPath);
// remove method since we've injected ourselves already
path.parentPath.remove();
} else {
path.replaceWith(container);
}
}
}

View File

@ -7,5 +7,9 @@
"main": "lib/index.js",
"keywords": [
"babel-plugin"
]
],
"dependencies": {
"babel-helper-remap-async-to-generator": "^5.0.0",
"babel-types": "^5.0.0"
}
}

View File

@ -1,7 +1,21 @@
import remapAsyncToGenerator from "babel-helper-remap-async-to-generator";
import * as t from "babel-types";
export default function () {
return {
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("asyncFunctions");
},
visitor: {
// your visitor methods go here
Function(path, state) {
if (!path.node.async || path.node.generator) return;
remapAsyncToGenerator(
path,
t.memberExpression(state.addImport("bluebird", null, "absolute"), t.identifier("coroutine"))
);
}
}
};
}

View File

@ -7,5 +7,8 @@
"main": "lib/index.js",
"keywords": [
"babel-plugin"
]
],
"dependencies": {
"babel-helper-remap-async-to-generator": "^5.0.0"
}
}

View File

@ -1,7 +1,17 @@
import remapAsyncToGenerator from "babel-helper-remap-async-to-generator";
export default function () {
return {
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("asyncFunctions");
},
visitor: {
// your visitor methods go here
Function(path, state) {
if (!path.node.async || path.node.generator) return;
remapAsyncToGenerator(path, state.addHelper("async-to-generator"));
}
}
};
}