index.js
2.65 KB
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
"use strict";
exports.__esModule = true;
var _getIterator2 = require("babel-runtime/core-js/get-iterator");
var _getIterator3 = _interopRequireDefault(_getIterator2);
exports.default = function (classPath) {
classPath.assertClass();
var memoisedExpressions = [];
function maybeMemoise(path) {
if (!path.node || path.isPure()) return;
var uid = classPath.scope.generateDeclaredUidIdentifier();
memoisedExpressions.push(t.assignmentExpression("=", uid, path.node));
path.replaceWith(uid);
}
function memoiseDecorators(paths) {
if (!Array.isArray(paths) || !paths.length) return;
paths = paths.reverse();
(0, _babelHelperBindifyDecorators2.default)(paths);
for (var _iterator = paths, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var path = _ref;
maybeMemoise(path);
}
}
maybeMemoise(classPath.get("superClass"));
memoiseDecorators(classPath.get("decorators"), true);
var methods = classPath.get("body.body");
for (var _iterator2 = methods, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
var _ref2;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref2 = _iterator2[_i2++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref2 = _i2.value;
}
var methodPath = _ref2;
if (methodPath.is("computed")) {
maybeMemoise(methodPath.get("key"));
}
if (methodPath.has("decorators")) {
memoiseDecorators(classPath.get("decorators"));
}
}
if (memoisedExpressions) {
classPath.insertBefore(memoisedExpressions.map(function (expr) {
return t.expressionStatement(expr);
}));
}
};
var _babelHelperBindifyDecorators = require("babel-helper-bindify-decorators");
var _babelHelperBindifyDecorators2 = _interopRequireDefault(_babelHelperBindifyDecorators);
var _babelTypes = require("babel-types");
var t = _interopRequireWildcard(_babelTypes);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
module.exports = exports["default"];