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
159
160
161
162
163
164
165
166
167
|
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _babelTypes = require('babel-types');
var _jsExtend = require('js-extend');
var _utils = require('./utils');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var PromiseChain = function () {
// add, addCatch and addFinally were designed to be called only one time each
// at most. Call them more at your own risk.
//
// addCatch() and addFinally() are not guaranteed to handle return values
// correctly. FIXME.
function PromiseChain(inner, dirtyAllowed, respName, errName) {
_classCallCheck(this, PromiseChain);
this._inner = inner;
this._dirtyAllowed = dirtyAllowed;
this._respName = respName;
this._errName = errName;
this._ast = (0, _babelTypes.callExpression)((0, _babelTypes.memberExpression)((0, _babelTypes.identifier)('Promise'), (0, _babelTypes.identifier)('resolve')), []);
}
_createClass(PromiseChain, [{
key: 'add',
value: function add(block) {
var _this = this;
if (!block.length) {
return;
}
var current = this._addLink('then', []);
block.forEach(function (path) {
var awaitInfos = [];
path.traverse(PromisifyPrepVisitor, { awaitInfos: awaitInfos, respName: _this._respName });
awaitInfos.forEach(function (awaitInfo) {
current.body.push((0, _babelTypes.returnStatement)(awaitInfo.arg));
var params = awaitInfo.passID ? [(0, _babelTypes.identifier)(_this._respName)] : [];
current = _this._addLink('then', params);
});
if (path.node) {
current.body.push(path.node);
}
});
}
}, {
key: '_addLink',
value: function _addLink(type, params, secondParams) {
this._cleanup();
var current = { body: [] };
var handlerBody = (0, _babelTypes.blockStatement)(current.body);
var handlers = [(0, _babelTypes.arrowFunctionExpression)(params, handlerBody, false)];
if (secondParams) {
current.secondBody = [];
var secondHandlerBody = (0, _babelTypes.blockStatement)(current.secondBody);
handlers.push((0, _babelTypes.arrowFunctionExpression)(secondParams, secondHandlerBody, false));
}
var method = (0, _babelTypes.memberExpression)(this._ast, (0, _babelTypes.identifier)(type));
this._ast = (0, _babelTypes.callExpression)(method, handlers);
return current;
}
}, {
key: '_cleanup',
value: function _cleanup() {
// if resolving to non-undefined when there is no return is allowed, and
// the last part of the chain is .then(function () {}), then chop off that
// part
var chopOff = this._dirtyAllowed && this._ast.callee.property.name === 'then' && this._ast.arguments.length === 1 && !this._ast.arguments[0].body.body.length;
if (chopOff) {
this._ast = this._ast.callee.object;
}
}
}, {
key: 'addCatch',
value: function addCatch(block, errID) {
var current = this._addLink('catch', [errID]);
var catchChain = this._subChain();
catchChain.add(block);
current.body.push((0, _babelTypes.returnStatement)(catchChain.toAST()));
}
}, {
key: '_subChain',
value: function _subChain() {
return new PromiseChain(true, true, this._respName, this._errName);
}
}, {
key: 'addFinally',
value: function addFinally(block) {
var errID = (0, _babelTypes.identifier)(this._errName);
var current = this._addLink('then', [], [errID]);
var finallyChain = this._subChain();
// disable optimalizations
finallyChain._inner = false;
finallyChain._dirtyAllowed = false;
finallyChain.add(block);
var secondAST = (0, _babelTypes.cloneDeep)(finallyChain.toAST());
// smuggle in the throw statement
secondAST.arguments[0].body.body.push((0, _babelTypes.throwStatement)(errID));
current.secondBody.push((0, _babelTypes.returnStatement)(secondAST));
// re-enable optimalizations
finallyChain._inner = true;
finallyChain._dirtyAllowed = true;
var ast = (0, _babelTypes.returnStatement)(finallyChain.toAST());
current.body.push(ast);
}
}, {
key: 'toAST',
value: function toAST() {
this._cleanup();
var callee = this._ast.callee.object.callee;
if (this._inner && callee && callee.object.name === 'Promise') {
// only one handler to the promise - because we're in an inner function
// there's no reason to wrap the handler in promise code. Convenienly,
// such a handler is inlineable later on.
//
// Summary:
// ``Promise.resolve().then(function () {...})``
// becomes
// ``function () {...}()``
return (0, _babelTypes.callExpression)(this._ast.arguments[0], []);
}
return this._ast;
}
}]);
return PromiseChain;
}();
exports.default = PromiseChain;
var PromisifyPrepVisitor = (0, _jsExtend.extend)({
AwaitExpression: {
exit: function exit(path) {
// exit so awaits are evaluated inside out if there are multiple in
// the expression
var info = { arg: path.node.argument };
if ((0, _babelTypes.isExpressionStatement)(path.parent)) {
path.remove();
} else {
info.passID = true;
path.replaceWith((0, _babelTypes.identifier)(this.respName));
}
this.awaitInfos.push(info);
}
}
}, _utils.NoSubFunctionsVisitor);
|