PageSourceSearch

https://farese.com/node_modules/@babel/helper-replace-supers/lib/index.js

js farese.com collected 2026-09-25 15:21:15 UTC 7,494 bytes, 278 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.