aboutsummaryrefslogtreecommitdiff
path: root/babel-plugin-async-to-promises/lib/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'babel-plugin-async-to-promises/lib/utils.js')
-rw-r--r--babel-plugin-async-to-promises/lib/utils.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/babel-plugin-async-to-promises/lib/utils.js b/babel-plugin-async-to-promises/lib/utils.js
new file mode 100644
index 0000000..708adab
--- /dev/null
+++ b/babel-plugin-async-to-promises/lib/utils.js
@@ -0,0 +1,55 @@
+'use strict';
+
+Object.defineProperty(exports, "__esModule", {
+ value: true
+});
+exports.assign = exports.awaitStatement = exports.containsAwait = exports.NoSubFunctionsVisitor = undefined;
+exports.matcher = matcher;
+exports.wrapFunction = wrapFunction;
+
+var _babelTypes = require('babel-types');
+
+var _jsExtend = require('js-extend');
+
+var NoSubFunctionsVisitor = exports.NoSubFunctionsVisitor = {
+ Function: function Function(path) {
+ path.skip();
+ }
+};
+
+var containsAwait = exports.containsAwait = matcher(['AwaitExpression'], NoSubFunctionsVisitor);
+
+function matcher(types, base) {
+ var MatchVisitor = (0, _jsExtend.extend)({}, base);
+ types.forEach(function (type) {
+ MatchVisitor[type] = function (path) {
+ this.match.found = true;
+ path.stop();
+ };
+ });
+ return function (path) {
+ if (!path.node) {
+ return false;
+ }
+ if (types.indexOf(path.node.type) !== -1) {
+ return true;
+ }
+ var match = {};
+ path.traverse(MatchVisitor, { match: match });
+ return match.found;
+ };
+}
+
+function wrapFunction(body) {
+ var func = (0, _babelTypes.functionExpression)(null, [], body, false, true);
+ func.dirtyAllowed = true;
+ return (0, _babelTypes.callExpression)(func, []);
+}
+
+var awaitStatement = exports.awaitStatement = function awaitStatement(arg) {
+ return (0, _babelTypes.expressionStatement)((0, _babelTypes.awaitExpression)(arg));
+};
+
+var assign = exports.assign = function assign(a, b) {
+ return (0, _babelTypes.expressionStatement)((0, _babelTypes.assignmentExpression)('=', a, b));
+}; \ No newline at end of file