aboutsummaryrefslogtreecommitdiff
path: root/babel-plugin-async-to-promises/lib/utils.js
blob: 708adabf1c0b7e2c51cd09b9a0db17e12936a76f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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));
};