aboutsummaryrefslogtreecommitdiff
path: root/babel-plugin-async-to-promises/lib/ifrefactor.js
blob: 904d77d51dbbd98702c78da6454432a483ad2c3b (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SecondPassIfVisitor = exports.FirstPassIfVisitor = undefined;

var _babelTypes = require('babel-types');

var _utils = require('./utils');

var _jsExtend = require('js-extend');

var FirstPassIfVisitor = exports.FirstPassIfVisitor = {
  IfStatement: function IfStatement(path) {
    var _this = this;

    var node = path.node;

    (0, _babelTypes.ensureBlock)(node, 'consequent');
    if (node.alternate) {
      (0, _babelTypes.ensureBlock)(node, 'alternate');
    }
    if (node.consequent.body.some(_babelTypes.isIfStatement) && containsReturnOrAwait(path)) {
      (function () {
        // flatten if statements. There are two ways to reach d() in the below.
        // if a() && !b(), and if !a() && !b(). That's problematic during the
        // promise conversion.
        //
        // if (a()) {
        //   if (b()) {
        //     return c();
        //   }
        // }
        // return d();
        //
        // this becomes instead:
        //
        // var _test = a();
        // if (_test && b()) {
        //   return c();
        // }
        // return d();
        //
        // which is better, but not quite the result we want yet. See for that
        // the BlockStatement handler in the other IfRefactorVisitor below.

        var testID = (0, _babelTypes.identifier)(path.scope.generateUid('test'));
        _this.addVarDecl(testID);
        var block = [(0, _utils.assign)(testID, node.test)];

        var stillToAdd = [];
        var clearQueue = function clearQueue() {
          if (stillToAdd.length) {
            block.push((0, _babelTypes.ifStatement)(testID, (0, _babelTypes.blockStatement)(stillToAdd)));
            stillToAdd = [];
          }
        };
        node.consequent.body.forEach(function (stmt) {
          if ((0, _babelTypes.isIfStatement)(stmt)) {
            clearQueue();
            stmt.test = (0, _babelTypes.logicalExpression)('&&', testID, stmt.test);
            if (stmt.alternate) {
              stmt.alternate = (0, _babelTypes.blockStatement)([(0, _babelTypes.ifStatement)(testID, stmt.alternate)]);
            }
            block.push(stmt);
          } else {
            stillToAdd.push(stmt);
          }
        });
        clearQueue();
        extendElse(block[block.length - 1], (node.alternate || {}).body || []);
        path.replaceWithMultiple(block);
      })();
    }
  }
};

var containsReturnOrAwait = (0, _utils.matcher)(['ReturnStatement', 'AwaitExpression'], _utils.NoSubFunctionsVisitor);

var SecondPassIfVisitor = exports.SecondPassIfVisitor = (0, _jsExtend.extend)({
  IfStatement: function IfStatement(path) {
    var alt = path.node.alternate;
    if (!path.node.consequent.body.length && alt && alt.body.length) {
      path.node.consequent = path.node.alternate;
      path.node.alternate = null;
      path.node.test = (0, _babelTypes.unaryExpression)('!', path.node.test);
    }
    var ifContainsAwait = (0, _utils.containsAwait)(path.get('consequent'));
    var elseContainsAwait = (0, _utils.containsAwait)(path.get('alternate'));

    var node = path.node;

    if (ifContainsAwait) {
      node.consequent = wrapIfBranch(node.consequent);
    }
    if (elseContainsAwait) {
      node.alternate = wrapIfBranch(node.alternate);
    }
    if (ifContainsAwait || elseContainsAwait) {
      path.replaceWith((0, _babelTypes.awaitExpression)((0, _utils.wrapFunction)((0, _babelTypes.blockStatement)([node]))));
    }
  },
  BlockStatement: function BlockStatement(path) {
    // Converts
    //
    // var _test = a();
    // if (_test && b()) {
    //   return c();
    // }
    // return d();
    //
    // into:
    //
    // var _test = a();
    // if (_test && b()) {
    //   return c();
    // } else {
    //   return d();
    // }
    //
    // ... which has at every point in time only two choices: returning
    // directly out of the function, or continueing on. That's what's required
    // for a nice conversion to Promise chains.
    for (var i = 0; i < path.node.body.length; i++) {
      var subNode = path.node.body[i];
      if ((0, _babelTypes.isReturnStatement)(subNode)) {
        // remove everything in the block after the return - it's never going
        // to be executed anyway.
        path.node.body.splice(i + 1);
      }
      if (!(0, _babelTypes.isIfStatement)(subNode)) {
        continue;
      }
      var lastStmt = subNode.consequent.body[subNode.consequent.body.length - 1];
      if (!(0, _babelTypes.isReturnStatement)(lastStmt)) {
        continue;
      }
      var remainder = path.node.body.splice(i + 1);
      if (!lastStmt.argument) {
        // chop off the soon to be useless return statement
        subNode.consequent.body.splice(-1);
      }
      extendElse(subNode, remainder);
    }
  }
}, _utils.NoSubFunctionsVisitor);

var wrapIfBranch = function wrapIfBranch(branch) {
  return (0, _babelTypes.blockStatement)([(0, _babelTypes.returnStatement)((0, _utils.wrapFunction)(branch))]);
};

function extendElse(ifStmt, extraBody) {
  var body = ((ifStmt.alternate || {}).body || []).concat(extraBody);
  if (body.length) {
    ifStmt.alternate = (0, _babelTypes.blockStatement)(body);
  }
}