babel-helper-remap-async-to-generator
Advanced tools
Comparing version 7.0.0-alpha.15 to 7.0.0-alpha.16
@@ -9,4 +9,2 @@ "use strict"; | ||
parent = path.parent; | ||
var stepKey = scope.generateUidIdentifier("step"); | ||
@@ -35,5 +33,3 @@ var stepValue = scope.generateUidIdentifier("value"); | ||
}); | ||
template = template.body.body; | ||
var isLabeledParent = t.isLabeledStatement(parent); | ||
@@ -40,0 +36,0 @@ var tryBody = template[3].block.body; |
119
lib/index.js
@@ -7,5 +7,8 @@ "use strict"; | ||
if (!helpers) { | ||
helpers = { wrapAsync: file }; | ||
helpers = { | ||
wrapAsync: file | ||
}; | ||
file = null; | ||
} | ||
path.traverse(awaitVisitor, { | ||
@@ -15,18 +18,11 @@ file: file, | ||
}); | ||
if (path.isClassMethod() || path.isObjectMethod()) { | ||
classOrObjectMethod(path, helpers.wrapAsync); | ||
} else { | ||
plainFunction(path, helpers.wrapAsync); | ||
} | ||
path.node.async = false; | ||
path.node.generator = true; | ||
(0, _babelHelperWrapFunction2.default)(path, helpers.wrapAsync); | ||
}; | ||
var _babelHelperFunctionName = require("babel-helper-function-name"); | ||
var _babelHelperWrapFunction = require("babel-helper-wrap-function"); | ||
var _babelHelperFunctionName2 = _interopRequireDefault(_babelHelperFunctionName); | ||
var _babelHelperWrapFunction2 = _interopRequireDefault(_babelHelperWrapFunction); | ||
var _babelTemplate = require("babel-template"); | ||
var _babelTemplate2 = _interopRequireDefault(_babelTemplate); | ||
var _babelTypes = require("babel-types"); | ||
@@ -44,6 +40,2 @@ | ||
var buildWrapper = (0, _babelTemplate2.default)("\n (() => {\n var REF = FUNCTION;\n return function NAME(PARAMS) {\n return REF.apply(this, arguments);\n };\n })\n"); | ||
var namedBuildWrapper = (0, _babelTemplate2.default)("\n (() => {\n var REF = FUNCTION;\n function NAME(PARAMS) {\n return REF.apply(this, arguments);\n }\n return NAME;\n })\n"); | ||
var awaitVisitor = { | ||
@@ -56,4 +48,4 @@ Function: function Function(path) { | ||
var wrapAwait = _ref2.wrapAwait; | ||
node.type = "YieldExpression"; | ||
node.type = "YieldExpression"; | ||
if (wrapAwait) { | ||
@@ -67,5 +59,3 @@ node.argument = t.callExpression(wrapAwait, [node.argument]); | ||
var node = path.node; | ||
if (!node.await) return; | ||
var build = (0, _forAwait2.default)(path, { | ||
@@ -75,8 +65,5 @@ getAsyncIterator: file.addHelper("asyncIterator"), | ||
}); | ||
var declar = build.declar, | ||
loop = build.loop; | ||
var block = loop.body; | ||
path.ensureBlock(); | ||
@@ -89,3 +76,2 @@ | ||
block.body = block.body.concat(node.body.body); | ||
t.inherits(loop, node); | ||
@@ -100,85 +86,2 @@ t.inherits(loop.body, node.body); | ||
} | ||
}; | ||
function classOrObjectMethod(path, callId) { | ||
var node = path.node; | ||
var body = node.body; | ||
node.async = false; | ||
var container = t.functionExpression(null, [], t.blockStatement(body.body), true); | ||
body.body = [t.returnStatement(t.callExpression(t.callExpression(callId, [container]), []))]; | ||
node.generator = false; | ||
path.get("body.body.0.argument.callee.arguments.0").unwrapFunctionEnvironment(); | ||
} | ||
function plainFunction(path, callId) { | ||
var node = path.node; | ||
var isDeclaration = path.isFunctionDeclaration(); | ||
var asyncFnId = node.id; | ||
var wrapper = buildWrapper; | ||
if (path.isArrowFunctionExpression()) { | ||
path.arrowFunctionToExpression(); | ||
} else if (!isDeclaration && asyncFnId) { | ||
wrapper = namedBuildWrapper; | ||
} | ||
node.async = false; | ||
node.generator = true; | ||
node.id = null; | ||
if (isDeclaration) { | ||
node.type = "FunctionExpression"; | ||
} | ||
var built = t.callExpression(callId, [node]); | ||
var container = wrapper({ | ||
NAME: asyncFnId || null, | ||
REF: path.scope.generateUidIdentifier("ref"), | ||
FUNCTION: built, | ||
PARAMS: node.params.reduce(function (acc, param) { | ||
acc.done = acc.done || t.isAssignmentPattern(param) || t.isRestElement(param); | ||
if (!acc.done) { | ||
acc.params.push(path.scope.generateUidIdentifier("x")); | ||
} | ||
return acc; | ||
}, { | ||
params: [], | ||
done: false | ||
}).params | ||
}).expression; | ||
if (isDeclaration) { | ||
var declar = t.variableDeclaration("let", [t.variableDeclarator(t.identifier(asyncFnId.name), t.callExpression(container, []))]); | ||
declar._blockHoist = true; | ||
if (path.parentPath.isExportDefaultDeclaration()) { | ||
path.parentPath.insertBefore(declar); | ||
path.parentPath.replaceWith(t.exportNamedDeclaration(null, [t.exportSpecifier(t.identifier(asyncFnId.name), t.identifier("default"))])); | ||
return; | ||
} | ||
path.replaceWith(declar); | ||
} else { | ||
var retFunction = container.body.body[1].argument; | ||
if (!asyncFnId) { | ||
(0, _babelHelperFunctionName2.default)({ | ||
node: retFunction, | ||
parent: path.parent, | ||
scope: path.scope | ||
}); | ||
} | ||
if (!retFunction || retFunction.id || node.params.length) { | ||
path.replaceWith(t.callExpression(container, [])); | ||
} else { | ||
path.replaceWith(built); | ||
} | ||
} | ||
} | ||
}; |
{ | ||
"name": "babel-helper-remap-async-to-generator", | ||
"version": "7.0.0-alpha.15", | ||
"version": "7.0.0-alpha.16", | ||
"description": "Helper function to remap async functions to generators", | ||
@@ -9,7 +9,7 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-helper-remap-async-to-generator", | ||
"dependencies": { | ||
"babel-helper-function-name": "7.0.0-alpha.15", | ||
"babel-template": "7.0.0-alpha.15", | ||
"babel-traverse": "7.0.0-alpha.15", | ||
"babel-types": "7.0.0-alpha.15" | ||
"babel-helper-wrap-function": "7.0.0-alpha.16", | ||
"babel-template": "7.0.0-alpha.16", | ||
"babel-traverse": "7.0.0-alpha.16", | ||
"babel-types": "7.0.0-alpha.16" | ||
} | ||
} |
5948
113
+ Addedansi-styles@3.2.1(transitive)
+ Addedbabel-code-frame@7.0.0-alpha.16(transitive)
+ Addedbabel-helper-function-name@7.0.0-alpha.16(transitive)
+ Addedbabel-helper-get-function-arity@7.0.0-alpha.16(transitive)
+ Addedbabel-helper-wrap-function@7.0.0-alpha.16(transitive)
+ Addedbabel-messages@7.0.0-alpha.16(transitive)
+ Addedbabel-template@7.0.0-alpha.16(transitive)
+ Addedbabel-traverse@7.0.0-alpha.16(transitive)
+ Addedbabel-types@7.0.0-alpha.16(transitive)
+ Addedbabylon@7.0.0-beta.17(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedglobals@10.4.0(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedto-fast-properties@2.0.0(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedbabel-code-frame@7.0.0-alpha.15(transitive)
- Removedbabel-helper-function-name@7.0.0-alpha.15(transitive)
- Removedbabel-helper-get-function-arity@7.0.0-alpha.15(transitive)
- Removedbabel-messages@7.0.0-alpha.15(transitive)
- Removedbabel-template@7.0.0-alpha.15(transitive)
- Removedbabel-traverse@7.0.0-alpha.15(transitive)
- Removedbabel-types@7.0.0-alpha.15(transitive)
- Removedbabylon@7.0.0-beta.13(transitive)
- Removedchalk@1.1.3(transitive)
- Removedglobals@9.18.0(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedto-fast-properties@1.0.3(transitive)
Updatedbabel-types@7.0.0-alpha.16