1
|
"use strict";
|
2
|
|
3
|
exports.__esModule = true;
|
4
|
|
5
|
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
|
6
|
|
7
|
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
8
|
|
9
|
var _symbol = require("babel-runtime/core-js/symbol");
|
10
|
|
11
|
var _symbol2 = _interopRequireDefault(_symbol);
|
12
|
|
13
|
var _babelHelperOptimiseCallExpression = require("babel-helper-optimise-call-expression");
|
14
|
|
15
|
var _babelHelperOptimiseCallExpression2 = _interopRequireDefault(_babelHelperOptimiseCallExpression);
|
16
|
|
17
|
var _babelMessages = require("babel-messages");
|
18
|
|
19
|
var messages = _interopRequireWildcard(_babelMessages);
|
20
|
|
21
|
var _babelTypes = require("babel-types");
|
22
|
|
23
|
var t = _interopRequireWildcard(_babelTypes);
|
24
|
|
25
|
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; } }
|
26
|
|
27
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
28
|
|
29
|
var HARDCORE_THIS_REF = (0, _symbol2.default)();
|
30
|
|
31
|
function isIllegalBareSuper(node, parent) {
|
32
|
if (!t.isSuper(node)) return false;
|
33
|
if (t.isMemberExpression(parent, { computed: false })) return false;
|
34
|
if (t.isCallExpression(parent, { callee: node })) return false;
|
35
|
return true;
|
36
|
}
|
37
|
|
38
|
function isMemberExpressionSuper(node) {
|
39
|
return t.isMemberExpression(node) && t.isSuper(node.object);
|
40
|
}
|
41
|
|
42
|
function getPrototypeOfExpression(objectRef, isStatic) {
|
43
|
var targetRef = isStatic ? objectRef : t.memberExpression(objectRef, t.identifier("prototype"));
|
44
|
|
45
|
return t.logicalExpression("||", t.memberExpression(targetRef, t.identifier("__proto__")), t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")), [targetRef]));
|
46
|
}
|
47
|
|
48
|
var visitor = {
|
49
|
Function: function Function(path) {
|
50
|
if (!path.inShadow("this")) {
|
51
|
path.skip();
|
52
|
}
|
53
|
},
|
54
|
ReturnStatement: function ReturnStatement(path, state) {
|
55
|
if (!path.inShadow("this")) {
|
56
|
state.returns.push(path);
|
57
|
}
|
58
|
},
|
59
|
ThisExpression: function ThisExpression(path, state) {
|
60
|
if (!path.node[HARDCORE_THIS_REF]) {
|
61
|
state.thises.push(path);
|
62
|
}
|
63
|
},
|
64
|
enter: function enter(path, state) {
|
65
|
var callback = state.specHandle;
|
66
|
if (state.isLoose) callback = state.looseHandle;
|
67
|
|
68
|
var isBareSuper = path.isCallExpression() && path.get("callee").isSuper();
|
69
|
|
70
|
var result = callback.call(state, path);
|
71
|
|
72
|
if (result) {
|
73
|
state.hasSuper = true;
|
74
|
}
|
75
|
|
76
|
if (isBareSuper) {
|
77
|
state.bareSupers.push(path);
|
78
|
}
|
79
|
|
80
|
if (result === true) {
|
81
|
path.requeue();
|
82
|
}
|
83
|
|
84
|
if (result !== true && result) {
|
85
|
if (Array.isArray(result)) {
|
86
|
path.replaceWithMultiple(result);
|
87
|
} else {
|
88
|
path.replaceWith(result);
|
89
|
}
|
90
|
}
|
91
|
}
|
92
|
};
|
93
|
|
94
|
var ReplaceSupers = function () {
|
95
|
function ReplaceSupers(opts) {
|
96
|
var inClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
97
|
(0, _classCallCheck3.default)(this, ReplaceSupers);
|
98
|
|
99
|
this.forceSuperMemoisation = opts.forceSuperMemoisation;
|
100
|
this.methodPath = opts.methodPath;
|
101
|
this.methodNode = opts.methodNode;
|
102
|
this.superRef = opts.superRef;
|
103
|
this.isStatic = opts.isStatic;
|
104
|
this.hasSuper = false;
|
105
|
this.inClass = inClass;
|
106
|
this.isLoose = opts.isLoose;
|
107
|
this.scope = this.methodPath.scope;
|
108
|
this.file = opts.file;
|
109
|
this.opts = opts;
|
110
|
|
111
|
this.bareSupers = [];
|
112
|
this.returns = [];
|
113
|
this.thises = [];
|
114
|
}
|
115
|
|
116
|
ReplaceSupers.prototype.getObjectRef = function getObjectRef() {
|
117
|
return this.opts.objectRef || this.opts.getObjectRef();
|
118
|
};
|
119
|
|
120
|
ReplaceSupers.prototype.setSuperProperty = function setSuperProperty(property, value, isComputed) {
|
121
|
return t.callExpression(this.file.addHelper("set"), [getPrototypeOfExpression(this.getObjectRef(), this.isStatic), isComputed ? property : t.stringLiteral(property.name), value, t.thisExpression()]);
|
122
|
};
|
123
|
|
124
|
ReplaceSupers.prototype.getSuperProperty = function getSuperProperty(property, isComputed) {
|
125
|
return t.callExpression(this.file.addHelper("get"), [getPrototypeOfExpression(this.getObjectRef(), this.isStatic), isComputed ? property : t.stringLiteral(property.name), t.thisExpression()]);
|
126
|
};
|
127
|
|
128
|
ReplaceSupers.prototype.replace = function replace() {
|
129
|
this.methodPath.traverse(visitor, this);
|
130
|
};
|
131
|
|
132
|
ReplaceSupers.prototype.getLooseSuperProperty = function getLooseSuperProperty(id, parent) {
|
133
|
var methodNode = this.methodNode;
|
134
|
var superRef = this.superRef || t.identifier("Function");
|
135
|
|
136
|
if (parent.property === id) {
|
137
|
return;
|
138
|
} else if (t.isCallExpression(parent, { callee: id })) {
|
139
|
return;
|
140
|
} else if (t.isMemberExpression(parent) && !methodNode.static) {
|
141
|
return t.memberExpression(superRef, t.identifier("prototype"));
|
142
|
} else {
|
143
|
return superRef;
|
144
|
}
|
145
|
};
|
146
|
|
147
|
ReplaceSupers.prototype.looseHandle = function looseHandle(path) {
|
148
|
var node = path.node;
|
149
|
if (path.isSuper()) {
|
150
|
return this.getLooseSuperProperty(node, path.parent);
|
151
|
} else if (path.isCallExpression()) {
|
152
|
var callee = node.callee;
|
153
|
if (!t.isMemberExpression(callee)) return;
|
154
|
if (!t.isSuper(callee.object)) return;
|
155
|
|
156
|
t.appendToMemberExpression(callee, t.identifier("call"));
|
157
|
node.arguments.unshift(t.thisExpression());
|
158
|
return true;
|
159
|
}
|
160
|
};
|
161
|
|
162
|
ReplaceSupers.prototype.specHandleAssignmentExpression = function specHandleAssignmentExpression(ref, path, node) {
|
163
|
if (node.operator === "=") {
|
164
|
return this.setSuperProperty(node.left.property, node.right, node.left.computed);
|
165
|
} else {
|
166
|
ref = ref || path.scope.generateUidIdentifier("ref");
|
167
|
return [t.variableDeclaration("var", [t.variableDeclarator(ref, node.left)]), t.expressionStatement(t.assignmentExpression("=", node.left, t.binaryExpression(node.operator[0], ref, node.right)))];
|
168
|
}
|
169
|
};
|
170
|
|
171
|
ReplaceSupers.prototype.specHandle = function specHandle(path) {
|
172
|
var property = void 0;
|
173
|
var computed = void 0;
|
174
|
var args = void 0;
|
175
|
|
176
|
var parent = path.parent;
|
177
|
var node = path.node;
|
178
|
|
179
|
if (isIllegalBareSuper(node, parent)) {
|
180
|
throw path.buildCodeFrameError(messages.get("classesIllegalBareSuper"));
|
181
|
}
|
182
|
|
183
|
if (t.isCallExpression(node)) {
|
184
|
var callee = node.callee;
|
185
|
if (t.isSuper(callee)) {
|
186
|
return;
|
187
|
} else if (isMemberExpressionSuper(callee)) {
|
188
|
property = callee.property;
|
189
|
computed = callee.computed;
|
190
|
args = node.arguments;
|
191
|
}
|
192
|
} else if (t.isMemberExpression(node) && t.isSuper(node.object)) {
|
193
|
property = node.property;
|
194
|
computed = node.computed;
|
195
|
} else if (t.isUpdateExpression(node) && isMemberExpressionSuper(node.argument)) {
|
196
|
var binary = t.binaryExpression(node.operator[0], node.argument, t.numericLiteral(1));
|
197
|
if (node.prefix) {
|
198
|
return this.specHandleAssignmentExpression(null, path, binary);
|
199
|
} else {
|
200
|
var ref = path.scope.generateUidIdentifier("ref");
|
201
|
return this.specHandleAssignmentExpression(ref, path, binary).concat(t.expressionStatement(ref));
|
202
|
}
|
203
|
} else if (t.isAssignmentExpression(node) && isMemberExpressionSuper(node.left)) {
|
204
|
return this.specHandleAssignmentExpression(null, path, node);
|
205
|
}
|
206
|
|
207
|
if (!property) return;
|
208
|
|
209
|
var superProperty = this.getSuperProperty(property, computed);
|
210
|
|
211
|
if (args) {
|
212
|
return this.optimiseCall(superProperty, args);
|
213
|
} else {
|
214
|
return superProperty;
|
215
|
}
|
216
|
};
|
217
|
|
218
|
ReplaceSupers.prototype.optimiseCall = function optimiseCall(callee, args) {
|
219
|
var thisNode = t.thisExpression();
|
220
|
thisNode[HARDCORE_THIS_REF] = true;
|
221
|
return (0, _babelHelperOptimiseCallExpression2.default)(callee, thisNode, args);
|
222
|
};
|
223
|
|
224
|
return ReplaceSupers;
|
225
|
}();
|
226
|
|
227
|
exports.default = ReplaceSupers;
|
228
|
module.exports = exports["default"];
|