source: trip-planner-front/node_modules/@babel/helper-replace-supers/lib/index.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 7.5 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.skipAllButComputedKey = skipAllButComputedKey;
7exports.default = exports.environmentVisitor = void 0;
8
9var _traverse = require("@babel/traverse");
10
11var _helperMemberExpressionToFunctions = require("@babel/helper-member-expression-to-functions");
12
13var _helperOptimiseCallExpression = require("@babel/helper-optimise-call-expression");
14
15var _t = require("@babel/types");
16
17const {
18 VISITOR_KEYS,
19 assignmentExpression,
20 booleanLiteral,
21 callExpression,
22 cloneNode,
23 identifier,
24 memberExpression,
25 sequenceExpression,
26 staticBlock,
27 stringLiteral,
28 thisExpression
29} = _t;
30
31function getPrototypeOfExpression(objectRef, isStatic, file, isPrivateMethod) {
32 objectRef = cloneNode(objectRef);
33 const targetRef = isStatic || isPrivateMethod ? objectRef : memberExpression(objectRef, identifier("prototype"));
34 return callExpression(file.addHelper("getPrototypeOf"), [targetRef]);
35}
36
37function skipAllButComputedKey(path) {
38 if (!path.node.computed) {
39 path.skip();
40 return;
41 }
42
43 const keys = VISITOR_KEYS[path.type];
44
45 for (const key of keys) {
46 if (key !== "key") path.skipKey(key);
47 }
48}
49
50const environmentVisitor = {
51 [`${staticBlock ? "StaticBlock|" : ""}ClassPrivateProperty|TypeAnnotation`](path) {
52 path.skip();
53 },
54
55 Function(path) {
56 if (path.isMethod()) return;
57 if (path.isArrowFunctionExpression()) return;
58 path.skip();
59 },
60
61 "Method|ClassProperty"(path) {
62 skipAllButComputedKey(path);
63 }
64
65};
66exports.environmentVisitor = environmentVisitor;
67
68const visitor = _traverse.default.visitors.merge([environmentVisitor, {
69 Super(path, state) {
70 const {
71 node,
72 parentPath
73 } = path;
74 if (!parentPath.isMemberExpression({
75 object: node
76 })) return;
77 state.handle(parentPath);
78 }
79
80}]);
81
82const unshadowSuperBindingVisitor = _traverse.default.visitors.merge([environmentVisitor, {
83 Scopable(path, {
84 refName
85 }) {
86 const binding = path.scope.getOwnBinding(refName);
87
88 if (binding && binding.identifier.name === refName) {
89 path.scope.rename(refName);
90 }
91 }
92
93}]);
94
95const specHandlers = {
96 memoise(superMember, count) {
97 const {
98 scope,
99 node
100 } = superMember;
101 const {
102 computed,
103 property
104 } = node;
105
106 if (!computed) {
107 return;
108 }
109
110 const memo = scope.maybeGenerateMemoised(property);
111
112 if (!memo) {
113 return;
114 }
115
116 this.memoiser.set(property, memo, count);
117 },
118
119 prop(superMember) {
120 const {
121 computed,
122 property
123 } = superMember.node;
124
125 if (this.memoiser.has(property)) {
126 return cloneNode(this.memoiser.get(property));
127 }
128
129 if (computed) {
130 return cloneNode(property);
131 }
132
133 return stringLiteral(property.name);
134 },
135
136 get(superMember) {
137 return this._get(superMember, this._getThisRefs());
138 },
139
140 _get(superMember, thisRefs) {
141 const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
142 return callExpression(this.file.addHelper("get"), [thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), thisRefs.this]);
143 },
144
145 _getThisRefs() {
146 if (!this.isDerivedConstructor) {
147 return {
148 this: thisExpression()
149 };
150 }
151
152 const thisRef = this.scope.generateDeclaredUidIdentifier("thisSuper");
153 return {
154 memo: assignmentExpression("=", thisRef, thisExpression()),
155 this: cloneNode(thisRef)
156 };
157 },
158
159 set(superMember, value) {
160 const thisRefs = this._getThisRefs();
161
162 const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
163 return callExpression(this.file.addHelper("set"), [thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), value, thisRefs.this, booleanLiteral(superMember.isInStrictMode())]);
164 },
165
166 destructureSet(superMember) {
167 throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`);
168 },
169
170 call(superMember, args) {
171 const thisRefs = this._getThisRefs();
172
173 return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, false);
174 },
175
176 optionalCall(superMember, args) {
177 const thisRefs = this._getThisRefs();
178
179 return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, true);
180 }
181
182};
183const looseHandlers = Object.assign({}, specHandlers, {
184 prop(superMember) {
185 const {
186 property
187 } = superMember.node;
188
189 if (this.memoiser.has(property)) {
190 return cloneNode(this.memoiser.get(property));
191 }
192
193 return cloneNode(property);
194 },
195
196 get(superMember) {
197 const {
198 isStatic,
199 getSuperRef
200 } = this;
201 const {
202 computed
203 } = superMember.node;
204 const prop = this.prop(superMember);
205 let object;
206
207 if (isStatic) {
208 var _getSuperRef;
209
210 object = (_getSuperRef = getSuperRef()) != null ? _getSuperRef : memberExpression(identifier("Function"), identifier("prototype"));
211 } else {
212 var _getSuperRef2;
213
214 object = memberExpression((_getSuperRef2 = getSuperRef()) != null ? _getSuperRef2 : identifier("Object"), identifier("prototype"));
215 }
216
217 return memberExpression(object, prop, computed);
218 },
219
220 set(superMember, value) {
221 const {
222 computed
223 } = superMember.node;
224 const prop = this.prop(superMember);
225 return assignmentExpression("=", memberExpression(thisExpression(), prop, computed), value);
226 },
227
228 destructureSet(superMember) {
229 const {
230 computed
231 } = superMember.node;
232 const prop = this.prop(superMember);
233 return memberExpression(thisExpression(), prop, computed);
234 },
235
236 call(superMember, args) {
237 return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, false);
238 },
239
240 optionalCall(superMember, args) {
241 return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, true);
242 }
243
244});
245
246class ReplaceSupers {
247 constructor(opts) {
248 var _opts$constantSuper;
249
250 const path = opts.methodPath;
251 this.methodPath = path;
252 this.isDerivedConstructor = path.isClassMethod({
253 kind: "constructor"
254 }) && !!opts.superRef;
255 this.isStatic = path.isObjectMethod() || path.node.static || (path.isStaticBlock == null ? void 0 : path.isStaticBlock());
256 this.isPrivateMethod = path.isPrivate() && path.isMethod();
257 this.file = opts.file;
258 this.constantSuper = (_opts$constantSuper = opts.constantSuper) != null ? _opts$constantSuper : opts.isLoose;
259 this.opts = opts;
260 }
261
262 getObjectRef() {
263 return cloneNode(this.opts.objectRef || this.opts.getObjectRef());
264 }
265
266 getSuperRef() {
267 if (this.opts.superRef) return cloneNode(this.opts.superRef);
268 if (this.opts.getSuperRef) return cloneNode(this.opts.getSuperRef());
269 }
270
271 replace() {
272 if (this.opts.refToPreserve) {
273 this.methodPath.traverse(unshadowSuperBindingVisitor, {
274 refName: this.opts.refToPreserve.name
275 });
276 }
277
278 const handler = this.constantSuper ? looseHandlers : specHandlers;
279 (0, _helperMemberExpressionToFunctions.default)(this.methodPath, visitor, Object.assign({
280 file: this.file,
281 scope: this.methodPath.scope,
282 isDerivedConstructor: this.isDerivedConstructor,
283 isStatic: this.isStatic,
284 isPrivateMethod: this.isPrivateMethod,
285 getObjectRef: this.getObjectRef.bind(this),
286 getSuperRef: this.getSuperRef.bind(this),
287 boundGet: handler.get
288 }, handler));
289 }
290
291}
292
293exports.default = ReplaceSupers;
Note: See TracBrowser for help on using the repository browser.