1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = _default;
|
7 | var _core = require("@babel/core");
|
8 | var _helperReplaceSupers = require("@babel/helper-replace-supers");
|
9 | var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
|
10 | var _fields = require("./fields.js");
|
11 | var _misc = require("./misc.js");
|
12 | function incrementId(id, idx = id.length - 1) {
|
13 | if (idx === -1) {
|
14 | id.unshift(65);
|
15 | return;
|
16 | }
|
17 | const current = id[idx];
|
18 | if (current === 90) {
|
19 | id[idx] = 97;
|
20 | } else if (current === 122) {
|
21 | id[idx] = 65;
|
22 | incrementId(id, idx - 1);
|
23 | } else {
|
24 | id[idx] = current + 1;
|
25 | }
|
26 | }
|
27 | function createPrivateUidGeneratorForClass(classPath) {
|
28 | const currentPrivateId = [];
|
29 | const privateNames = new Set();
|
30 | classPath.traverse({
|
31 | PrivateName(path) {
|
32 | privateNames.add(path.node.id.name);
|
33 | }
|
34 | });
|
35 | return () => {
|
36 | let reifiedId;
|
37 | do {
|
38 | incrementId(currentPrivateId);
|
39 | reifiedId = String.fromCharCode(...currentPrivateId);
|
40 | } while (privateNames.has(reifiedId));
|
41 | return _core.types.privateName(_core.types.identifier(reifiedId));
|
42 | };
|
43 | }
|
44 | function createLazyPrivateUidGeneratorForClass(classPath) {
|
45 | let generator;
|
46 | return () => {
|
47 | if (!generator) {
|
48 | generator = createPrivateUidGeneratorForClass(classPath);
|
49 | }
|
50 | return generator();
|
51 | };
|
52 | }
|
53 | function replaceClassWithVar(path, className) {
|
54 | const id = path.node.id;
|
55 | const scope = path.scope;
|
56 | if (path.type === "ClassDeclaration") {
|
57 | const className = id.name;
|
58 | const varId = scope.generateUidIdentifierBasedOnNode(id);
|
59 | const classId = _core.types.identifier(className);
|
60 | scope.rename(className, varId.name);
|
61 | path.get("id").replaceWith(classId);
|
62 | return {
|
63 | id: _core.types.cloneNode(varId),
|
64 | path
|
65 | };
|
66 | } else {
|
67 | let varId;
|
68 | if (id) {
|
69 | className = id.name;
|
70 | varId = generateLetUidIdentifier(scope.parent, className);
|
71 | scope.rename(className, varId.name);
|
72 | } else {
|
73 | varId = generateLetUidIdentifier(scope.parent, typeof className === "string" ? className : "decorated_class");
|
74 | }
|
75 | const newClassExpr = _core.types.classExpression(typeof className === "string" ? _core.types.identifier(className) : null, path.node.superClass, path.node.body);
|
76 | const [newPath] = path.replaceWith(_core.types.sequenceExpression([newClassExpr, varId]));
|
77 | return {
|
78 | id: _core.types.cloneNode(varId),
|
79 | path: newPath.get("expressions.0")
|
80 | };
|
81 | }
|
82 | }
|
83 | function generateClassProperty(key, value, isStatic) {
|
84 | if (key.type === "PrivateName") {
|
85 | return _core.types.classPrivateProperty(key, value, undefined, isStatic);
|
86 | } else {
|
87 | return _core.types.classProperty(key, value, undefined, undefined, isStatic);
|
88 | }
|
89 | }
|
90 | function assignIdForAnonymousClass(path, className) {
|
91 | if (!path.node.id) {
|
92 | path.node.id = typeof className === "string" ? _core.types.identifier(className) : path.scope.generateUidIdentifier("Class");
|
93 | }
|
94 | }
|
95 | function addProxyAccessorsFor(className, element, getterKey, setterKey, targetKey, isComputed, isStatic, version) {
|
96 | const thisArg = (version === "2023-11" || version === "2023-05") && isStatic ? className : _core.types.thisExpression();
|
97 | const getterBody = _core.types.blockStatement([_core.types.returnStatement(_core.types.memberExpression(_core.types.cloneNode(thisArg), _core.types.cloneNode(targetKey)))]);
|
98 | const setterBody = _core.types.blockStatement([_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.cloneNode(thisArg), _core.types.cloneNode(targetKey)), _core.types.identifier("v")))]);
|
99 | let getter, setter;
|
100 | if (getterKey.type === "PrivateName") {
|
101 | getter = _core.types.classPrivateMethod("get", getterKey, [], getterBody, isStatic);
|
102 | setter = _core.types.classPrivateMethod("set", setterKey, [_core.types.identifier("v")], setterBody, isStatic);
|
103 | } else {
|
104 | getter = _core.types.classMethod("get", getterKey, [], getterBody, isComputed, isStatic);
|
105 | setter = _core.types.classMethod("set", setterKey, [_core.types.identifier("v")], setterBody, isComputed, isStatic);
|
106 | }
|
107 | element.insertAfter(setter);
|
108 | element.insertAfter(getter);
|
109 | }
|
110 | function extractProxyAccessorsFor(targetKey, version) {
|
111 | if (version !== "2023-11" && version !== "2023-05" && version !== "2023-01") {
|
112 | return [_core.template.expression.ast`
|
113 | function () {
|
114 | return this.${_core.types.cloneNode(targetKey)};
|
115 | }
|
116 | `, _core.template.expression.ast`
|
117 | function (value) {
|
118 | this.${_core.types.cloneNode(targetKey)} = value;
|
119 | }
|
120 | `];
|
121 | }
|
122 | return [_core.template.expression.ast`
|
123 | o => o.${_core.types.cloneNode(targetKey)}
|
124 | `, _core.template.expression.ast`
|
125 | (o, v) => o.${_core.types.cloneNode(targetKey)} = v
|
126 | `];
|
127 | }
|
128 | function getComputedKeyLastElement(path) {
|
129 | path = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path);
|
130 | if (path.isSequenceExpression()) {
|
131 | const expressions = path.get("expressions");
|
132 | return getComputedKeyLastElement(expressions[expressions.length - 1]);
|
133 | }
|
134 | return path;
|
135 | }
|
136 | function getComputedKeyMemoiser(path) {
|
137 | const element = getComputedKeyLastElement(path);
|
138 | if (element.isConstantExpression()) {
|
139 | return _core.types.cloneNode(path.node);
|
140 | } else if (element.isIdentifier() && path.scope.hasUid(element.node.name)) {
|
141 | return _core.types.cloneNode(path.node);
|
142 | } else if (element.isAssignmentExpression() && element.get("left").isIdentifier()) {
|
143 | return _core.types.cloneNode(element.node.left);
|
144 | } else {
|
145 | throw new Error(`Internal Error: the computed key ${path.toString()} has not yet been memoised.`);
|
146 | }
|
147 | }
|
148 | function prependExpressionsToComputedKey(expressions, fieldPath) {
|
149 | const key = fieldPath.get("key");
|
150 | if (key.isSequenceExpression()) {
|
151 | expressions.push(...key.node.expressions);
|
152 | } else {
|
153 | expressions.push(key.node);
|
154 | }
|
155 | key.replaceWith(maybeSequenceExpression(expressions));
|
156 | }
|
157 | function appendExpressionsToComputedKey(expressions, fieldPath) {
|
158 | const key = fieldPath.get("key");
|
159 | const completion = getComputedKeyLastElement(key);
|
160 | if (completion.isConstantExpression()) {
|
161 | prependExpressionsToComputedKey(expressions, fieldPath);
|
162 | } else {
|
163 | const scopeParent = key.scope.parent;
|
164 | const maybeAssignment = (0, _misc.memoiseComputedKey)(completion.node, scopeParent, scopeParent.generateUid("computedKey"));
|
165 | if (!maybeAssignment) {
|
166 | prependExpressionsToComputedKey(expressions, fieldPath);
|
167 | } else {
|
168 | const expressionSequence = [...expressions, _core.types.cloneNode(maybeAssignment.left)];
|
169 | const completionParent = completion.parentPath;
|
170 | if (completionParent.isSequenceExpression()) {
|
171 | completionParent.pushContainer("expressions", expressionSequence);
|
172 | } else {
|
173 | completion.replaceWith(maybeSequenceExpression([_core.types.cloneNode(maybeAssignment), ...expressionSequence]));
|
174 | }
|
175 | }
|
176 | }
|
177 | }
|
178 | function prependExpressionsToFieldInitializer(expressions, fieldPath) {
|
179 | const initializer = fieldPath.get("value");
|
180 | if (initializer.node) {
|
181 | expressions.push(initializer.node);
|
182 | } else if (expressions.length > 0) {
|
183 | expressions[expressions.length - 1] = _core.types.unaryExpression("void", expressions[expressions.length - 1]);
|
184 | }
|
185 | initializer.replaceWith(maybeSequenceExpression(expressions));
|
186 | }
|
187 | function prependExpressionsToStaticBlock(expressions, blockPath) {
|
188 | blockPath.unshiftContainer("body", _core.types.expressionStatement(maybeSequenceExpression(expressions)));
|
189 | }
|
190 | function prependExpressionsToConstructor(expressions, constructorPath) {
|
191 | constructorPath.node.body.body.unshift(_core.types.expressionStatement(maybeSequenceExpression(expressions)));
|
192 | }
|
193 | function isProtoInitCallExpression(expression, protoInitCall) {
|
194 | return _core.types.isCallExpression(expression) && _core.types.isIdentifier(expression.callee, {
|
195 | name: protoInitCall.name
|
196 | });
|
197 | }
|
198 | function optimizeSuperCallAndExpressions(expressions, protoInitLocal) {
|
199 | if (protoInitLocal) {
|
200 | if (expressions.length >= 2 && isProtoInitCallExpression(expressions[1], protoInitLocal)) {
|
201 | const mergedSuperCall = _core.types.callExpression(_core.types.cloneNode(protoInitLocal), [expressions[0]]);
|
202 | expressions.splice(0, 2, mergedSuperCall);
|
203 | }
|
204 | if (expressions.length >= 2 && _core.types.isThisExpression(expressions[expressions.length - 1]) && isProtoInitCallExpression(expressions[expressions.length - 2], protoInitLocal)) {
|
205 | expressions.splice(expressions.length - 1, 1);
|
206 | }
|
207 | }
|
208 | return maybeSequenceExpression(expressions);
|
209 | }
|
210 | function insertExpressionsAfterSuperCallAndOptimize(expressions, constructorPath, protoInitLocal) {
|
211 | constructorPath.traverse({
|
212 | CallExpression: {
|
213 | exit(path) {
|
214 | if (!path.get("callee").isSuper()) return;
|
215 | const newNodes = [path.node, ...expressions.map(expr => _core.types.cloneNode(expr))];
|
216 | if (path.isCompletionRecord()) {
|
217 | newNodes.push(_core.types.thisExpression());
|
218 | }
|
219 | path.replaceWith(optimizeSuperCallAndExpressions(newNodes, protoInitLocal));
|
220 | path.skip();
|
221 | }
|
222 | },
|
223 | ClassMethod(path) {
|
224 | if (path.node.kind === "constructor") {
|
225 | path.skip();
|
226 | }
|
227 | }
|
228 | });
|
229 | }
|
230 | function createConstructorFromExpressions(expressions, isDerivedClass) {
|
231 | const body = [_core.types.expressionStatement(maybeSequenceExpression(expressions))];
|
232 | if (isDerivedClass) {
|
233 | body.unshift(_core.types.expressionStatement(_core.types.callExpression(_core.types.super(), [_core.types.spreadElement(_core.types.identifier("args"))])));
|
234 | }
|
235 | return _core.types.classMethod("constructor", _core.types.identifier("constructor"), isDerivedClass ? [_core.types.restElement(_core.types.identifier("args"))] : [], _core.types.blockStatement(body));
|
236 | }
|
237 | function createStaticBlockFromExpressions(expressions) {
|
238 | return _core.types.staticBlock([_core.types.expressionStatement(maybeSequenceExpression(expressions))]);
|
239 | }
|
240 | const FIELD = 0;
|
241 | const ACCESSOR = 1;
|
242 | const METHOD = 2;
|
243 | const GETTER = 3;
|
244 | const SETTER = 4;
|
245 | const STATIC_OLD_VERSION = 5;
|
246 | const STATIC = 8;
|
247 | const DECORATORS_HAVE_THIS = 16;
|
248 | function getElementKind(element) {
|
249 | switch (element.node.type) {
|
250 | case "ClassProperty":
|
251 | case "ClassPrivateProperty":
|
252 | return FIELD;
|
253 | case "ClassAccessorProperty":
|
254 | return ACCESSOR;
|
255 | case "ClassMethod":
|
256 | case "ClassPrivateMethod":
|
257 | if (element.node.kind === "get") {
|
258 | return GETTER;
|
259 | } else if (element.node.kind === "set") {
|
260 | return SETTER;
|
261 | } else {
|
262 | return METHOD;
|
263 | }
|
264 | }
|
265 | }
|
266 | function toSortedDecoratorInfo(info) {
|
267 | return [...info.filter(el => el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER), ...info.filter(el => !el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER), ...info.filter(el => el.isStatic && el.kind === FIELD), ...info.filter(el => !el.isStatic && el.kind === FIELD)];
|
268 | }
|
269 | function generateDecorationList(decorators, decoratorsThis, version) {
|
270 | const decsCount = decorators.length;
|
271 | const haveOneThis = decoratorsThis.some(Boolean);
|
272 | const decs = [];
|
273 | for (let i = 0; i < decsCount; i++) {
|
274 | if ((version === "2023-11" || version === "2023-05") && haveOneThis) {
|
275 | decs.push(decoratorsThis[i] || _core.types.unaryExpression("void", _core.types.numericLiteral(0)));
|
276 | }
|
277 | decs.push(decorators[i].expression);
|
278 | }
|
279 | return {
|
280 | haveThis: haveOneThis,
|
281 | decs
|
282 | };
|
283 | }
|
284 | function generateDecorationExprs(decorationInfo, version) {
|
285 | return _core.types.arrayExpression(decorationInfo.map(el => {
|
286 | let flag = el.kind;
|
287 | if (el.isStatic) {
|
288 | flag += version === "2023-11" || version === "2023-05" ? STATIC : STATIC_OLD_VERSION;
|
289 | }
|
290 | if (el.decoratorsHaveThis) flag += DECORATORS_HAVE_THIS;
|
291 | return _core.types.arrayExpression([el.decoratorsArray, _core.types.numericLiteral(flag), el.name, ...(el.privateMethods || [])]);
|
292 | }));
|
293 | }
|
294 | function extractElementLocalAssignments(decorationInfo) {
|
295 | const localIds = [];
|
296 | for (const el of decorationInfo) {
|
297 | const {
|
298 | locals
|
299 | } = el;
|
300 | if (Array.isArray(locals)) {
|
301 | localIds.push(...locals);
|
302 | } else if (locals !== undefined) {
|
303 | localIds.push(locals);
|
304 | }
|
305 | }
|
306 | return localIds;
|
307 | }
|
308 | function addCallAccessorsFor(version, element, key, getId, setId, isStatic) {
|
309 | element.insertAfter(_core.types.classPrivateMethod("get", _core.types.cloneNode(key), [], _core.types.blockStatement([_core.types.returnStatement(_core.types.callExpression(_core.types.cloneNode(getId), version === "2023-11" && isStatic ? [] : [_core.types.thisExpression()]))]), isStatic));
|
310 | element.insertAfter(_core.types.classPrivateMethod("set", _core.types.cloneNode(key), [_core.types.identifier("v")], _core.types.blockStatement([_core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(setId), version === "2023-11" && isStatic ? [_core.types.identifier("v")] : [_core.types.thisExpression(), _core.types.identifier("v")]))]), isStatic));
|
311 | }
|
312 | function movePrivateAccessor(element, key, methodLocalVar, isStatic) {
|
313 | let params;
|
314 | let block;
|
315 | if (element.node.kind === "set") {
|
316 | params = [_core.types.identifier("v")];
|
317 | block = [_core.types.expressionStatement(_core.types.callExpression(methodLocalVar, [_core.types.thisExpression(), _core.types.identifier("v")]))];
|
318 | } else {
|
319 | params = [];
|
320 | block = [_core.types.returnStatement(_core.types.callExpression(methodLocalVar, [_core.types.thisExpression()]))];
|
321 | }
|
322 | element.replaceWith(_core.types.classPrivateMethod(element.node.kind, _core.types.cloneNode(key), params, _core.types.blockStatement(block), isStatic));
|
323 | }
|
324 | function isClassDecoratableElementPath(path) {
|
325 | const {
|
326 | type
|
327 | } = path;
|
328 | return type !== "TSDeclareMethod" && type !== "TSIndexSignature" && type !== "StaticBlock";
|
329 | }
|
330 | function staticBlockToIIFE(block) {
|
331 | return _core.types.callExpression(_core.types.arrowFunctionExpression([], _core.types.blockStatement(block.body)), []);
|
332 | }
|
333 | function staticBlockToFunctionClosure(block) {
|
334 | return _core.types.functionExpression(null, [], _core.types.blockStatement(block.body));
|
335 | }
|
336 | function fieldInitializerToClosure(value) {
|
337 | return _core.types.functionExpression(null, [], _core.types.blockStatement([_core.types.returnStatement(value)]));
|
338 | }
|
339 | function maybeSequenceExpression(exprs) {
|
340 | if (exprs.length === 0) return _core.types.unaryExpression("void", _core.types.numericLiteral(0));
|
341 | if (exprs.length === 1) return exprs[0];
|
342 | return _core.types.sequenceExpression(exprs);
|
343 | }
|
344 | function createFunctionExpressionFromPrivateMethod(node) {
|
345 | const {
|
346 | params,
|
347 | body,
|
348 | generator: isGenerator,
|
349 | async: isAsync
|
350 | } = node;
|
351 | return _core.types.functionExpression(undefined, params, body, isGenerator, isAsync);
|
352 | }
|
353 | function createSetFunctionNameCall(state, className) {
|
354 | return _core.types.callExpression(state.addHelper("setFunctionName"), [_core.types.thisExpression(), className]);
|
355 | }
|
356 | function createToPropertyKeyCall(state, propertyKey) {
|
357 | return _core.types.callExpression(state.addHelper("toPropertyKey"), [propertyKey]);
|
358 | }
|
359 | function createPrivateBrandCheckClosure(brandName) {
|
360 | return _core.types.arrowFunctionExpression([_core.types.identifier("_")], _core.types.binaryExpression("in", _core.types.cloneNode(brandName), _core.types.identifier("_")));
|
361 | }
|
362 | function usesPrivateField(expression) {
|
363 | try {
|
364 | _core.types.traverseFast(expression, node => {
|
365 | if (_core.types.isPrivateName(node)) {
|
366 | throw null;
|
367 | }
|
368 | });
|
369 | return false;
|
370 | } catch (_unused) {
|
371 | return true;
|
372 | }
|
373 | }
|
374 | function convertToComputedKey(path) {
|
375 | const {
|
376 | node
|
377 | } = path;
|
378 | node.computed = true;
|
379 | if (_core.types.isIdentifier(node.key)) {
|
380 | node.key = _core.types.stringLiteral(node.key.name);
|
381 | }
|
382 | }
|
383 | function hasInstancePrivateAccess(path, privateNames) {
|
384 | let containsInstancePrivateAccess = false;
|
385 | if (privateNames.length > 0) {
|
386 | const privateNameVisitor = (0, _fields.privateNameVisitorFactory)({
|
387 | PrivateName(path, state) {
|
388 | if (state.privateNamesMap.has(path.node.id.name)) {
|
389 | containsInstancePrivateAccess = true;
|
390 | path.stop();
|
391 | }
|
392 | }
|
393 | });
|
394 | const privateNamesMap = new Map();
|
395 | for (const name of privateNames) {
|
396 | privateNamesMap.set(name, null);
|
397 | }
|
398 | path.traverse(privateNameVisitor, {
|
399 | privateNamesMap: privateNamesMap
|
400 | });
|
401 | }
|
402 | return containsInstancePrivateAccess;
|
403 | }
|
404 | function checkPrivateMethodUpdateError(path, decoratedPrivateMethods) {
|
405 | const privateNameVisitor = (0, _fields.privateNameVisitorFactory)({
|
406 | PrivateName(path, state) {
|
407 | if (!state.privateNamesMap.has(path.node.id.name)) return;
|
408 | const parentPath = path.parentPath;
|
409 | const parentParentPath = parentPath.parentPath;
|
410 | if (parentParentPath.node.type === "AssignmentExpression" && parentParentPath.node.left === parentPath.node || parentParentPath.node.type === "UpdateExpression" || parentParentPath.node.type === "RestElement" || parentParentPath.node.type === "ArrayPattern" || parentParentPath.node.type === "ObjectProperty" && parentParentPath.node.value === parentPath.node && parentParentPath.parentPath.type === "ObjectPattern" || parentParentPath.node.type === "ForOfStatement" && parentParentPath.node.left === parentPath.node) {
|
411 | throw path.buildCodeFrameError(`Decorated private methods are read-only, but "#${path.node.id.name}" is updated via this expression.`);
|
412 | }
|
413 | }
|
414 | });
|
415 | const privateNamesMap = new Map();
|
416 | for (const name of decoratedPrivateMethods) {
|
417 | privateNamesMap.set(name, null);
|
418 | }
|
419 | path.traverse(privateNameVisitor, {
|
420 | privateNamesMap: privateNamesMap
|
421 | });
|
422 | }
|
423 | function transformClass(path, state, constantSuper, ignoreFunctionLength, className, propertyVisitor, version) {
|
424 | var _path$node$id, _classDecorationsId;
|
425 | const body = path.get("body.body");
|
426 | const classDecorators = path.node.decorators;
|
427 | let hasElementDecorators = false;
|
428 | let hasComputedKeysSideEffects = false;
|
429 | let elemDecsUseFnContext = false;
|
430 | const generateClassPrivateUid = createLazyPrivateUidGeneratorForClass(path);
|
431 | const classAssignments = [];
|
432 | const scopeParent = path.scope.parent;
|
433 | const memoiseExpression = (expression, hint, assignments) => {
|
434 | const localEvaluatedId = generateLetUidIdentifier(scopeParent, hint);
|
435 | assignments.push(_core.types.assignmentExpression("=", localEvaluatedId, expression));
|
436 | return _core.types.cloneNode(localEvaluatedId);
|
437 | };
|
438 | let protoInitLocal;
|
439 | let staticInitLocal;
|
440 | const classIdName = (_path$node$id = path.node.id) == null ? void 0 : _path$node$id.name;
|
441 | const setClassName = typeof className === "object" ? className : undefined;
|
442 | const usesFunctionContextOrYieldAwait = decorator => {
|
443 | try {
|
444 | _core.types.traverseFast(decorator, node => {
|
445 | if (_core.types.isThisExpression(node) || _core.types.isSuper(node) || _core.types.isYieldExpression(node) || _core.types.isAwaitExpression(node) || _core.types.isIdentifier(node, {
|
446 | name: "arguments"
|
447 | }) || classIdName && _core.types.isIdentifier(node, {
|
448 | name: classIdName
|
449 | }) || _core.types.isMetaProperty(node) && node.meta.name !== "import") {
|
450 | throw null;
|
451 | }
|
452 | });
|
453 | return false;
|
454 | } catch (_unused2) {
|
455 | return true;
|
456 | }
|
457 | };
|
458 | const instancePrivateNames = [];
|
459 | for (const element of body) {
|
460 | if (!isClassDecoratableElementPath(element)) {
|
461 | continue;
|
462 | }
|
463 | const elementNode = element.node;
|
464 | if (!elementNode.static && _core.types.isPrivateName(elementNode.key)) {
|
465 | instancePrivateNames.push(elementNode.key.id.name);
|
466 | }
|
467 | if (isDecorated(elementNode)) {
|
468 | switch (elementNode.type) {
|
469 | case "ClassProperty":
|
470 | propertyVisitor.ClassProperty(element, state);
|
471 | break;
|
472 | case "ClassPrivateProperty":
|
473 | propertyVisitor.ClassPrivateProperty(element, state);
|
474 | break;
|
475 | case "ClassAccessorProperty":
|
476 | propertyVisitor.ClassAccessorProperty(element, state);
|
477 | if (version === "2023-11") {
|
478 | break;
|
479 | }
|
480 | default:
|
481 | if (elementNode.static) {
|
482 | var _staticInitLocal;
|
483 | (_staticInitLocal = staticInitLocal) != null ? _staticInitLocal : staticInitLocal = generateLetUidIdentifier(scopeParent, "initStatic");
|
484 | } else {
|
485 | var _protoInitLocal;
|
486 | (_protoInitLocal = protoInitLocal) != null ? _protoInitLocal : protoInitLocal = generateLetUidIdentifier(scopeParent, "initProto");
|
487 | }
|
488 | break;
|
489 | }
|
490 | hasElementDecorators = true;
|
491 | elemDecsUseFnContext || (elemDecsUseFnContext = elementNode.decorators.some(usesFunctionContextOrYieldAwait));
|
492 | } else if (elementNode.type === "ClassAccessorProperty") {
|
493 | propertyVisitor.ClassAccessorProperty(element, state);
|
494 | const {
|
495 | key,
|
496 | value,
|
497 | static: isStatic,
|
498 | computed
|
499 | } = elementNode;
|
500 | const newId = generateClassPrivateUid();
|
501 | const newField = generateClassProperty(newId, value, isStatic);
|
502 | const keyPath = element.get("key");
|
503 | const [newPath] = element.replaceWith(newField);
|
504 | let getterKey, setterKey;
|
505 | if (computed && !keyPath.isConstantExpression()) {
|
506 | getterKey = (0, _misc.memoiseComputedKey)(createToPropertyKeyCall(state, key), scopeParent, scopeParent.generateUid("computedKey"));
|
507 | setterKey = _core.types.cloneNode(getterKey.left);
|
508 | } else {
|
509 | getterKey = _core.types.cloneNode(key);
|
510 | setterKey = _core.types.cloneNode(key);
|
511 | }
|
512 | assignIdForAnonymousClass(path, className);
|
513 | addProxyAccessorsFor(path.node.id, newPath, getterKey, setterKey, newId, computed, isStatic, version);
|
514 | }
|
515 | if ("computed" in element.node && element.node.computed) {
|
516 | hasComputedKeysSideEffects || (hasComputedKeysSideEffects = !scopeParent.isStatic(element.node.key));
|
517 | }
|
518 | }
|
519 | if (!classDecorators && !hasElementDecorators) {
|
520 | if (!path.node.id && typeof className === "string") {
|
521 | path.node.id = _core.types.identifier(className);
|
522 | }
|
523 | if (setClassName) {
|
524 | path.node.body.body.unshift(createStaticBlockFromExpressions([createSetFunctionNameCall(state, setClassName)]));
|
525 | }
|
526 | return;
|
527 | }
|
528 | const elementDecoratorInfo = [];
|
529 | let constructorPath;
|
530 | const decoratedPrivateMethods = new Set();
|
531 | let classInitLocal, classIdLocal;
|
532 | let decoratorReceiverId = null;
|
533 | function handleDecorators(decorators) {
|
534 | let hasSideEffects = false;
|
535 | let usesFnContext = false;
|
536 | const decoratorsThis = [];
|
537 | for (const decorator of decorators) {
|
538 | const {
|
539 | expression
|
540 | } = decorator;
|
541 | let object;
|
542 | if ((version === "2023-11" || version === "2023-05") && _core.types.isMemberExpression(expression)) {
|
543 | if (_core.types.isSuper(expression.object)) {
|
544 | object = _core.types.thisExpression();
|
545 | } else if (scopeParent.isStatic(expression.object)) {
|
546 | object = _core.types.cloneNode(expression.object);
|
547 | } else {
|
548 | var _decoratorReceiverId;
|
549 | (_decoratorReceiverId = decoratorReceiverId) != null ? _decoratorReceiverId : decoratorReceiverId = generateLetUidIdentifier(scopeParent, "obj");
|
550 | object = _core.types.assignmentExpression("=", _core.types.cloneNode(decoratorReceiverId), expression.object);
|
551 | expression.object = _core.types.cloneNode(decoratorReceiverId);
|
552 | }
|
553 | }
|
554 | decoratorsThis.push(object);
|
555 | hasSideEffects || (hasSideEffects = !scopeParent.isStatic(expression));
|
556 | usesFnContext || (usesFnContext = usesFunctionContextOrYieldAwait(decorator));
|
557 | }
|
558 | return {
|
559 | hasSideEffects,
|
560 | usesFnContext,
|
561 | decoratorsThis
|
562 | };
|
563 | }
|
564 | const willExtractSomeElemDecs = hasComputedKeysSideEffects || elemDecsUseFnContext || version !== "2023-11";
|
565 | let needsDeclaraionForClassBinding = false;
|
566 | let classDecorationsFlag = 0;
|
567 | let classDecorations = [];
|
568 | let classDecorationsId;
|
569 | let computedKeyAssignments = [];
|
570 | if (classDecorators) {
|
571 | classInitLocal = generateLetUidIdentifier(scopeParent, "initClass");
|
572 | needsDeclaraionForClassBinding = path.isClassDeclaration();
|
573 | ({
|
574 | id: classIdLocal,
|
575 | path
|
576 | } = replaceClassWithVar(path, className));
|
577 | path.node.decorators = null;
|
578 | const classDecsUsePrivateName = classDecorators.some(usesPrivateField);
|
579 | const {
|
580 | hasSideEffects,
|
581 | usesFnContext,
|
582 | decoratorsThis
|
583 | } = handleDecorators(classDecorators);
|
584 | const {
|
585 | haveThis,
|
586 | decs
|
587 | } = generateDecorationList(classDecorators, decoratorsThis, version);
|
588 | classDecorationsFlag = haveThis ? 1 : 0;
|
589 | classDecorations = decs;
|
590 | if (usesFnContext || hasSideEffects && willExtractSomeElemDecs || classDecsUsePrivateName) {
|
591 | classDecorationsId = memoiseExpression(_core.types.arrayExpression(classDecorations), "classDecs", classAssignments);
|
592 | }
|
593 | if (!hasElementDecorators) {
|
594 | for (const element of path.get("body.body")) {
|
595 | const {
|
596 | node
|
597 | } = element;
|
598 | const isComputed = "computed" in node && node.computed;
|
599 | if (isComputed) {
|
600 | if (element.isClassProperty({
|
601 | static: true
|
602 | })) {
|
603 | if (!element.get("key").isConstantExpression()) {
|
604 | const key = node.key;
|
605 | const maybeAssignment = (0, _misc.memoiseComputedKey)(key, scopeParent, scopeParent.generateUid("computedKey"));
|
606 | if (maybeAssignment != null) {
|
607 | node.key = _core.types.cloneNode(maybeAssignment.left);
|
608 | computedKeyAssignments.push(maybeAssignment);
|
609 | }
|
610 | }
|
611 | } else if (computedKeyAssignments.length > 0) {
|
612 | prependExpressionsToComputedKey(computedKeyAssignments, element);
|
613 | computedKeyAssignments = [];
|
614 | }
|
615 | }
|
616 | }
|
617 | }
|
618 | } else {
|
619 | assignIdForAnonymousClass(path, className);
|
620 | classIdLocal = _core.types.cloneNode(path.node.id);
|
621 | }
|
622 | let lastInstancePrivateName;
|
623 | let needsInstancePrivateBrandCheck = false;
|
624 | let fieldInitializerExpressions = [];
|
625 | let staticFieldInitializerExpressions = [];
|
626 | if (hasElementDecorators) {
|
627 | if (protoInitLocal) {
|
628 | const protoInitCall = _core.types.callExpression(_core.types.cloneNode(protoInitLocal), [_core.types.thisExpression()]);
|
629 | fieldInitializerExpressions.push(protoInitCall);
|
630 | }
|
631 | for (const element of body) {
|
632 | if (!isClassDecoratableElementPath(element)) {
|
633 | if (staticFieldInitializerExpressions.length > 0 && element.isStaticBlock()) {
|
634 | prependExpressionsToStaticBlock(staticFieldInitializerExpressions, element);
|
635 | staticFieldInitializerExpressions = [];
|
636 | }
|
637 | continue;
|
638 | }
|
639 | const {
|
640 | node
|
641 | } = element;
|
642 | const decorators = node.decorators;
|
643 | const hasDecorators = !!(decorators != null && decorators.length);
|
644 | const isComputed = "computed" in node && node.computed;
|
645 | let name = "computedKey";
|
646 | if (node.key.type === "PrivateName") {
|
647 | name = node.key.id.name;
|
648 | } else if (!isComputed && node.key.type === "Identifier") {
|
649 | name = node.key.name;
|
650 | }
|
651 | let decoratorsArray;
|
652 | let decoratorsHaveThis;
|
653 | if (hasDecorators) {
|
654 | const {
|
655 | hasSideEffects,
|
656 | usesFnContext,
|
657 | decoratorsThis
|
658 | } = handleDecorators(decorators);
|
659 | const {
|
660 | decs,
|
661 | haveThis
|
662 | } = generateDecorationList(decorators, decoratorsThis, version);
|
663 | decoratorsHaveThis = haveThis;
|
664 | decoratorsArray = decs.length === 1 ? decs[0] : _core.types.arrayExpression(decs);
|
665 | if (usesFnContext || hasSideEffects && willExtractSomeElemDecs) {
|
666 | decoratorsArray = memoiseExpression(decoratorsArray, name + "Decs", computedKeyAssignments);
|
667 | }
|
668 | }
|
669 | if (isComputed) {
|
670 | if (!element.get("key").isConstantExpression()) {
|
671 | const key = node.key;
|
672 | const maybeAssignment = (0, _misc.memoiseComputedKey)(hasDecorators ? createToPropertyKeyCall(state, key) : key, scopeParent, scopeParent.generateUid("computedKey"));
|
673 | if (maybeAssignment != null) {
|
674 | if (classDecorators && element.isClassProperty({
|
675 | static: true
|
676 | })) {
|
677 | node.key = _core.types.cloneNode(maybeAssignment.left);
|
678 | computedKeyAssignments.push(maybeAssignment);
|
679 | } else {
|
680 | node.key = maybeAssignment;
|
681 | }
|
682 | }
|
683 | }
|
684 | }
|
685 | const {
|
686 | key,
|
687 | static: isStatic
|
688 | } = node;
|
689 | const isPrivate = key.type === "PrivateName";
|
690 | const kind = getElementKind(element);
|
691 | if (isPrivate && !isStatic) {
|
692 | if (hasDecorators) {
|
693 | needsInstancePrivateBrandCheck = true;
|
694 | }
|
695 | if (_core.types.isClassPrivateProperty(node) || !lastInstancePrivateName) {
|
696 | lastInstancePrivateName = key;
|
697 | }
|
698 | }
|
699 | if (element.isClassMethod({
|
700 | kind: "constructor"
|
701 | })) {
|
702 | constructorPath = element;
|
703 | }
|
704 | let locals;
|
705 | if (hasDecorators) {
|
706 | let privateMethods;
|
707 | let nameExpr;
|
708 | if (isComputed) {
|
709 | nameExpr = getComputedKeyMemoiser(element.get("key"));
|
710 | } else if (key.type === "PrivateName") {
|
711 | nameExpr = _core.types.stringLiteral(key.id.name);
|
712 | } else if (key.type === "Identifier") {
|
713 | nameExpr = _core.types.stringLiteral(key.name);
|
714 | } else {
|
715 | nameExpr = _core.types.cloneNode(key);
|
716 | }
|
717 | if (kind === ACCESSOR) {
|
718 | const {
|
719 | value
|
720 | } = element.node;
|
721 | const params = version === "2023-11" && isStatic ? [] : [_core.types.thisExpression()];
|
722 | if (value) {
|
723 | params.push(_core.types.cloneNode(value));
|
724 | }
|
725 | const newId = generateClassPrivateUid();
|
726 | const newFieldInitId = generateLetUidIdentifier(scopeParent, `init_${name}`);
|
727 | const newValue = _core.types.callExpression(_core.types.cloneNode(newFieldInitId), params);
|
728 | const newField = generateClassProperty(newId, newValue, isStatic);
|
729 | const [newPath] = element.replaceWith(newField);
|
730 | if (isPrivate) {
|
731 | privateMethods = extractProxyAccessorsFor(newId, version);
|
732 | const getId = generateLetUidIdentifier(scopeParent, `get_${name}`);
|
733 | const setId = generateLetUidIdentifier(scopeParent, `set_${name}`);
|
734 | addCallAccessorsFor(version, newPath, key, getId, setId, isStatic);
|
735 | locals = [newFieldInitId, getId, setId];
|
736 | } else {
|
737 | assignIdForAnonymousClass(path, className);
|
738 | addProxyAccessorsFor(path.node.id, newPath, _core.types.cloneNode(key), _core.types.isAssignmentExpression(key) ? _core.types.cloneNode(key.left) : _core.types.cloneNode(key), newId, isComputed, isStatic, version);
|
739 | locals = [newFieldInitId];
|
740 | }
|
741 | } else if (kind === FIELD) {
|
742 | const initId = generateLetUidIdentifier(scopeParent, `init_${name}`);
|
743 | const valuePath = element.get("value");
|
744 | const args = version === "2023-11" && isStatic ? [] : [_core.types.thisExpression()];
|
745 | if (valuePath.node) args.push(valuePath.node);
|
746 | valuePath.replaceWith(_core.types.callExpression(_core.types.cloneNode(initId), args));
|
747 | locals = [initId];
|
748 | if (isPrivate) {
|
749 | privateMethods = extractProxyAccessorsFor(key, version);
|
750 | }
|
751 | } else if (isPrivate) {
|
752 | const callId = generateLetUidIdentifier(scopeParent, `call_${name}`);
|
753 | locals = [callId];
|
754 | const replaceSupers = new _helperReplaceSupers.default({
|
755 | constantSuper,
|
756 | methodPath: element,
|
757 | objectRef: classIdLocal,
|
758 | superRef: path.node.superClass,
|
759 | file: state.file,
|
760 | refToPreserve: classIdLocal
|
761 | });
|
762 | replaceSupers.replace();
|
763 | privateMethods = [createFunctionExpressionFromPrivateMethod(element.node)];
|
764 | if (kind === GETTER || kind === SETTER) {
|
765 | movePrivateAccessor(element, _core.types.cloneNode(key), _core.types.cloneNode(callId), isStatic);
|
766 | } else {
|
767 | const node = element.node;
|
768 | path.node.body.body.unshift(_core.types.classPrivateProperty(key, _core.types.cloneNode(callId), [], node.static));
|
769 | decoratedPrivateMethods.add(key.id.name);
|
770 | element.remove();
|
771 | }
|
772 | }
|
773 | elementDecoratorInfo.push({
|
774 | kind,
|
775 | decoratorsArray,
|
776 | decoratorsHaveThis,
|
777 | name: nameExpr,
|
778 | isStatic,
|
779 | privateMethods,
|
780 | locals
|
781 | });
|
782 | if (element.node) {
|
783 | element.node.decorators = null;
|
784 | }
|
785 | }
|
786 | if (isComputed && computedKeyAssignments.length > 0) {
|
787 | if (classDecorators && element.isClassProperty({
|
788 | static: true
|
789 | })) {} else {
|
790 | prependExpressionsToComputedKey(computedKeyAssignments, kind === ACCESSOR ? element.getNextSibling() : element);
|
791 | computedKeyAssignments = [];
|
792 | }
|
793 | }
|
794 | if (fieldInitializerExpressions.length > 0 && !isStatic && (kind === FIELD || kind === ACCESSOR)) {
|
795 | prependExpressionsToFieldInitializer(fieldInitializerExpressions, element);
|
796 | fieldInitializerExpressions = [];
|
797 | }
|
798 | if (staticFieldInitializerExpressions.length > 0 && isStatic && (kind === FIELD || kind === ACCESSOR)) {
|
799 | prependExpressionsToFieldInitializer(staticFieldInitializerExpressions, element);
|
800 | staticFieldInitializerExpressions = [];
|
801 | }
|
802 | if (hasDecorators && version === "2023-11") {
|
803 | if (kind === FIELD || kind === ACCESSOR) {
|
804 | const initExtraId = generateLetUidIdentifier(scopeParent, `init_extra_${name}`);
|
805 | locals.push(initExtraId);
|
806 | const initExtraCall = _core.types.callExpression(_core.types.cloneNode(initExtraId), isStatic ? [] : [_core.types.thisExpression()]);
|
807 | if (!isStatic) {
|
808 | fieldInitializerExpressions.push(initExtraCall);
|
809 | } else {
|
810 | staticFieldInitializerExpressions.push(initExtraCall);
|
811 | }
|
812 | }
|
813 | }
|
814 | }
|
815 | }
|
816 | if (computedKeyAssignments.length > 0) {
|
817 | const elements = path.get("body.body");
|
818 | let lastComputedElement;
|
819 | for (let i = elements.length - 1; i >= 0; i--) {
|
820 | const path = elements[i];
|
821 | const node = path.node;
|
822 | if (node.computed) {
|
823 | if (classDecorators && _core.types.isClassProperty(node, {
|
824 | static: true
|
825 | })) {
|
826 | continue;
|
827 | }
|
828 | lastComputedElement = path;
|
829 | break;
|
830 | }
|
831 | }
|
832 | if (lastComputedElement != null) {
|
833 | appendExpressionsToComputedKey(computedKeyAssignments, lastComputedElement);
|
834 | computedKeyAssignments = [];
|
835 | } else {}
|
836 | }
|
837 | if (fieldInitializerExpressions.length > 0) {
|
838 | const isDerivedClass = !!path.node.superClass;
|
839 | if (constructorPath) {
|
840 | if (isDerivedClass) {
|
841 | insertExpressionsAfterSuperCallAndOptimize(fieldInitializerExpressions, constructorPath, protoInitLocal);
|
842 | } else {
|
843 | prependExpressionsToConstructor(fieldInitializerExpressions, constructorPath);
|
844 | }
|
845 | } else {
|
846 | path.node.body.body.unshift(createConstructorFromExpressions(fieldInitializerExpressions, isDerivedClass));
|
847 | }
|
848 | fieldInitializerExpressions = [];
|
849 | }
|
850 | if (staticFieldInitializerExpressions.length > 0) {
|
851 | path.node.body.body.push(createStaticBlockFromExpressions(staticFieldInitializerExpressions));
|
852 | staticFieldInitializerExpressions = [];
|
853 | }
|
854 | const sortedElementDecoratorInfo = toSortedDecoratorInfo(elementDecoratorInfo);
|
855 | const elementDecorations = generateDecorationExprs(version === "2023-11" ? elementDecoratorInfo : sortedElementDecoratorInfo, version);
|
856 | const elementLocals = extractElementLocalAssignments(sortedElementDecoratorInfo);
|
857 | if (protoInitLocal) {
|
858 | elementLocals.push(protoInitLocal);
|
859 | }
|
860 | if (staticInitLocal) {
|
861 | elementLocals.push(staticInitLocal);
|
862 | }
|
863 | const classLocals = [];
|
864 | let classInitInjected = false;
|
865 | const classInitCall = classInitLocal && _core.types.callExpression(_core.types.cloneNode(classInitLocal), []);
|
866 | let originalClassPath = path;
|
867 | const originalClass = path.node;
|
868 | const staticClosures = [];
|
869 | if (classDecorators) {
|
870 | classLocals.push(classIdLocal, classInitLocal);
|
871 | const statics = [];
|
872 | path.get("body.body").forEach(element => {
|
873 | if (element.isStaticBlock()) {
|
874 | if (hasInstancePrivateAccess(element, instancePrivateNames)) {
|
875 | const staticBlockClosureId = memoiseExpression(staticBlockToFunctionClosure(element.node), "staticBlock", staticClosures);
|
876 | staticFieldInitializerExpressions.push(_core.types.callExpression(_core.types.memberExpression(staticBlockClosureId, _core.types.identifier("call")), [_core.types.thisExpression()]));
|
877 | } else {
|
878 | staticFieldInitializerExpressions.push(staticBlockToIIFE(element.node));
|
879 | }
|
880 | element.remove();
|
881 | return;
|
882 | }
|
883 | if ((element.isClassProperty() || element.isClassPrivateProperty()) && element.node.static) {
|
884 | const valuePath = element.get("value");
|
885 | if (hasInstancePrivateAccess(valuePath, instancePrivateNames)) {
|
886 | const fieldValueClosureId = memoiseExpression(fieldInitializerToClosure(valuePath.node), "fieldValue", staticClosures);
|
887 | valuePath.replaceWith(_core.types.callExpression(_core.types.memberExpression(fieldValueClosureId, _core.types.identifier("call")), [_core.types.thisExpression()]));
|
888 | }
|
889 | if (staticFieldInitializerExpressions.length > 0) {
|
890 | prependExpressionsToFieldInitializer(staticFieldInitializerExpressions, element);
|
891 | staticFieldInitializerExpressions = [];
|
892 | }
|
893 | element.node.static = false;
|
894 | statics.push(element.node);
|
895 | element.remove();
|
896 | } else if (element.isClassPrivateMethod({
|
897 | static: true
|
898 | })) {
|
899 | if (hasInstancePrivateAccess(element, instancePrivateNames)) {
|
900 | const replaceSupers = new _helperReplaceSupers.default({
|
901 | constantSuper,
|
902 | methodPath: element,
|
903 | objectRef: classIdLocal,
|
904 | superRef: path.node.superClass,
|
905 | file: state.file,
|
906 | refToPreserve: classIdLocal
|
907 | });
|
908 | replaceSupers.replace();
|
909 | const privateMethodDelegateId = memoiseExpression(createFunctionExpressionFromPrivateMethod(element.node), element.get("key.id").node.name, staticClosures);
|
910 | if (ignoreFunctionLength) {
|
911 | element.node.params = [_core.types.restElement(_core.types.identifier("arg"))];
|
912 | element.node.body = _core.types.blockStatement([_core.types.returnStatement(_core.types.callExpression(_core.types.memberExpression(privateMethodDelegateId, _core.types.identifier("apply")), [_core.types.thisExpression(), _core.types.identifier("arg")]))]);
|
913 | } else {
|
914 | element.node.params = element.node.params.map((p, i) => {
|
915 | if (_core.types.isRestElement(p)) {
|
916 | return _core.types.restElement(_core.types.identifier("arg"));
|
917 | } else {
|
918 | return _core.types.identifier("_" + i);
|
919 | }
|
920 | });
|
921 | element.node.body = _core.types.blockStatement([_core.types.returnStatement(_core.types.callExpression(_core.types.memberExpression(privateMethodDelegateId, _core.types.identifier("apply")), [_core.types.thisExpression(), _core.types.identifier("arguments")]))]);
|
922 | }
|
923 | }
|
924 | element.node.static = false;
|
925 | statics.push(element.node);
|
926 | element.remove();
|
927 | }
|
928 | });
|
929 | if (statics.length > 0 || staticFieldInitializerExpressions.length > 0) {
|
930 | const staticsClass = _core.template.expression.ast`
|
931 | class extends ${state.addHelper("identity")} {}
|
932 | `;
|
933 | staticsClass.body.body = [_core.types.classProperty(_core.types.toExpression(originalClass), undefined, undefined, undefined, true, true), ...statics];
|
934 | const constructorBody = [];
|
935 | const newExpr = _core.types.newExpression(staticsClass, []);
|
936 | if (staticFieldInitializerExpressions.length > 0) {
|
937 | constructorBody.push(...staticFieldInitializerExpressions);
|
938 | }
|
939 | if (classInitCall) {
|
940 | classInitInjected = true;
|
941 | constructorBody.push(classInitCall);
|
942 | }
|
943 | if (constructorBody.length > 0) {
|
944 | constructorBody.unshift(_core.types.callExpression(_core.types.super(), [_core.types.cloneNode(classIdLocal)]));
|
945 | staticsClass.body.body.push(createConstructorFromExpressions(constructorBody, false));
|
946 | } else {
|
947 | newExpr.arguments.push(_core.types.cloneNode(classIdLocal));
|
948 | }
|
949 | const [newPath] = path.replaceWith(newExpr);
|
950 | originalClassPath = newPath.get("callee").get("body").get("body.0.key");
|
951 | }
|
952 | }
|
953 | if (!classInitInjected && classInitCall) {
|
954 | path.node.body.body.push(_core.types.staticBlock([_core.types.expressionStatement(classInitCall)]));
|
955 | }
|
956 | let {
|
957 | superClass
|
958 | } = originalClass;
|
959 | if (superClass && (version === "2023-11" || version === "2023-05")) {
|
960 | const id = path.scope.maybeGenerateMemoised(superClass);
|
961 | if (id) {
|
962 | originalClass.superClass = _core.types.assignmentExpression("=", id, superClass);
|
963 | superClass = id;
|
964 | }
|
965 | }
|
966 | const applyDecoratorWrapper = _core.types.staticBlock([]);
|
967 | originalClass.body.body.unshift(applyDecoratorWrapper);
|
968 | const applyDecsBody = applyDecoratorWrapper.body;
|
969 | if (computedKeyAssignments.length > 0) {
|
970 | const elements = originalClassPath.get("body.body");
|
971 | let firstPublicElement;
|
972 | for (const path of elements) {
|
973 | if ((path.isClassProperty() || path.isClassMethod()) && path.node.kind !== "constructor") {
|
974 | firstPublicElement = path;
|
975 | break;
|
976 | }
|
977 | }
|
978 | if (firstPublicElement != null) {
|
979 | convertToComputedKey(firstPublicElement);
|
980 | prependExpressionsToComputedKey(computedKeyAssignments, firstPublicElement);
|
981 | } else {
|
982 | originalClass.body.body.unshift(_core.types.classProperty(_core.types.sequenceExpression([...computedKeyAssignments, _core.types.stringLiteral("_")]), undefined, undefined, undefined, true, true));
|
983 | applyDecsBody.push(_core.types.expressionStatement(_core.types.unaryExpression("delete", _core.types.memberExpression(_core.types.thisExpression(), _core.types.identifier("_")))));
|
984 | }
|
985 | computedKeyAssignments = [];
|
986 | }
|
987 | applyDecsBody.push(_core.types.expressionStatement(createLocalsAssignment(elementLocals, classLocals, elementDecorations, (_classDecorationsId = classDecorationsId) != null ? _classDecorationsId : _core.types.arrayExpression(classDecorations), _core.types.numericLiteral(classDecorationsFlag), needsInstancePrivateBrandCheck ? lastInstancePrivateName : null, setClassName, _core.types.cloneNode(superClass), state, version)));
|
988 | if (staticInitLocal) {
|
989 | applyDecsBody.push(_core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(staticInitLocal), [_core.types.thisExpression()])));
|
990 | }
|
991 | if (staticClosures.length > 0) {
|
992 | applyDecsBody.push(...staticClosures.map(expr => _core.types.expressionStatement(expr)));
|
993 | }
|
994 | path.insertBefore(classAssignments.map(expr => _core.types.expressionStatement(expr)));
|
995 | if (needsDeclaraionForClassBinding) {
|
996 | const classBindingInfo = scopeParent.getBinding(classIdLocal.name);
|
997 | if (!classBindingInfo.constantViolations.length) {
|
998 | path.insertBefore(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.cloneNode(classIdLocal))]));
|
999 | } else {
|
1000 | const classOuterBindingDelegateLocal = scopeParent.generateUidIdentifier("t" + classIdLocal.name);
|
1001 | const classOuterBindingLocal = classIdLocal;
|
1002 | path.replaceWithMultiple([_core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.cloneNode(classOuterBindingLocal)), _core.types.variableDeclarator(classOuterBindingDelegateLocal)]), _core.types.blockStatement([_core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.cloneNode(classIdLocal))]), path.node, _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(classOuterBindingDelegateLocal), _core.types.cloneNode(classIdLocal)))]), _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(classOuterBindingLocal), _core.types.cloneNode(classOuterBindingDelegateLocal)))]);
|
1003 | }
|
1004 | }
|
1005 | if (decoratedPrivateMethods.size > 0) {
|
1006 | checkPrivateMethodUpdateError(path, decoratedPrivateMethods);
|
1007 | }
|
1008 | path.scope.crawl();
|
1009 | return path;
|
1010 | }
|
1011 | function createLocalsAssignment(elementLocals, classLocals, elementDecorations, classDecorations, classDecorationsFlag, maybePrivateBrandName, setClassName, superClass, state, version) {
|
1012 | let lhs, rhs;
|
1013 | const args = [setClassName ? createSetFunctionNameCall(state, setClassName) : _core.types.thisExpression(), classDecorations, elementDecorations];
|
1014 | {
|
1015 | if (version !== "2023-11") {
|
1016 | args.splice(1, 2, elementDecorations, classDecorations);
|
1017 | }
|
1018 | if (version === "2021-12" || version === "2022-03" && !state.availableHelper("applyDecs2203R")) {
|
1019 | lhs = _core.types.arrayPattern([...elementLocals, ...classLocals]);
|
1020 | rhs = _core.types.callExpression(state.addHelper(version === "2021-12" ? "applyDecs" : "applyDecs2203"), args);
|
1021 | return _core.types.assignmentExpression("=", lhs, rhs);
|
1022 | } else if (version === "2022-03") {
|
1023 | rhs = _core.types.callExpression(state.addHelper("applyDecs2203R"), args);
|
1024 | } else if (version === "2023-01") {
|
1025 | if (maybePrivateBrandName) {
|
1026 | args.push(createPrivateBrandCheckClosure(maybePrivateBrandName));
|
1027 | }
|
1028 | rhs = _core.types.callExpression(state.addHelper("applyDecs2301"), args);
|
1029 | } else if (version === "2023-05") {
|
1030 | if (maybePrivateBrandName || superClass || classDecorationsFlag.value !== 0) {
|
1031 | args.push(classDecorationsFlag);
|
1032 | }
|
1033 | if (maybePrivateBrandName) {
|
1034 | args.push(createPrivateBrandCheckClosure(maybePrivateBrandName));
|
1035 | } else if (superClass) {
|
1036 | args.push(_core.types.unaryExpression("void", _core.types.numericLiteral(0)));
|
1037 | }
|
1038 | if (superClass) args.push(superClass);
|
1039 | rhs = _core.types.callExpression(state.addHelper("applyDecs2305"), args);
|
1040 | }
|
1041 | }
|
1042 | if (version === "2023-11") {
|
1043 | if (maybePrivateBrandName || superClass || classDecorationsFlag.value !== 0) {
|
1044 | args.push(classDecorationsFlag);
|
1045 | }
|
1046 | if (maybePrivateBrandName) {
|
1047 | args.push(createPrivateBrandCheckClosure(maybePrivateBrandName));
|
1048 | } else if (superClass) {
|
1049 | args.push(_core.types.unaryExpression("void", _core.types.numericLiteral(0)));
|
1050 | }
|
1051 | if (superClass) args.push(superClass);
|
1052 | rhs = _core.types.callExpression(state.addHelper("applyDecs2311"), args);
|
1053 | }
|
1054 | if (elementLocals.length > 0) {
|
1055 | if (classLocals.length > 0) {
|
1056 | lhs = _core.types.objectPattern([_core.types.objectProperty(_core.types.identifier("e"), _core.types.arrayPattern(elementLocals)), _core.types.objectProperty(_core.types.identifier("c"), _core.types.arrayPattern(classLocals))]);
|
1057 | } else {
|
1058 | lhs = _core.types.arrayPattern(elementLocals);
|
1059 | rhs = _core.types.memberExpression(rhs, _core.types.identifier("e"), false, false);
|
1060 | }
|
1061 | } else {
|
1062 | lhs = _core.types.arrayPattern(classLocals);
|
1063 | rhs = _core.types.memberExpression(rhs, _core.types.identifier("c"), false, false);
|
1064 | }
|
1065 | return _core.types.assignmentExpression("=", lhs, rhs);
|
1066 | }
|
1067 | function isProtoKey(node) {
|
1068 | return node.type === "Identifier" ? node.name === "__proto__" : node.value === "__proto__";
|
1069 | }
|
1070 | function isDecorated(node) {
|
1071 | return node.decorators && node.decorators.length > 0;
|
1072 | }
|
1073 | function shouldTransformElement(node) {
|
1074 | switch (node.type) {
|
1075 | case "ClassAccessorProperty":
|
1076 | return true;
|
1077 | case "ClassMethod":
|
1078 | case "ClassProperty":
|
1079 | case "ClassPrivateMethod":
|
1080 | case "ClassPrivateProperty":
|
1081 | return isDecorated(node);
|
1082 | default:
|
1083 | return false;
|
1084 | }
|
1085 | }
|
1086 | function shouldTransformClass(node) {
|
1087 | return isDecorated(node) || node.body.body.some(shouldTransformElement);
|
1088 | }
|
1089 | function NamedEvaluationVisitoryFactory(isAnonymous, visitor) {
|
1090 | function handleComputedProperty(propertyPath, key, state) {
|
1091 | switch (key.type) {
|
1092 | case "StringLiteral":
|
1093 | return _core.types.stringLiteral(key.value);
|
1094 | case "NumericLiteral":
|
1095 | case "BigIntLiteral":
|
1096 | {
|
1097 | const keyValue = key.value + "";
|
1098 | propertyPath.get("key").replaceWith(_core.types.stringLiteral(keyValue));
|
1099 | return _core.types.stringLiteral(keyValue);
|
1100 | }
|
1101 | default:
|
1102 | {
|
1103 | const ref = propertyPath.scope.maybeGenerateMemoised(key);
|
1104 | propertyPath.get("key").replaceWith(_core.types.assignmentExpression("=", ref, createToPropertyKeyCall(state, key)));
|
1105 | return _core.types.cloneNode(ref);
|
1106 | }
|
1107 | }
|
1108 | }
|
1109 | return {
|
1110 | VariableDeclarator(path, state) {
|
1111 | const id = path.node.id;
|
1112 | if (id.type === "Identifier") {
|
1113 | const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("init"));
|
1114 | if (isAnonymous(initializer)) {
|
1115 | const name = id.name;
|
1116 | visitor(initializer, state, name);
|
1117 | }
|
1118 | }
|
1119 | },
|
1120 | AssignmentExpression(path, state) {
|
1121 | const id = path.node.left;
|
1122 | if (id.type === "Identifier") {
|
1123 | const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("right"));
|
1124 | if (isAnonymous(initializer)) {
|
1125 | switch (path.node.operator) {
|
1126 | case "=":
|
1127 | case "&&=":
|
1128 | case "||=":
|
1129 | case "??=":
|
1130 | visitor(initializer, state, id.name);
|
1131 | }
|
1132 | }
|
1133 | }
|
1134 | },
|
1135 | AssignmentPattern(path, state) {
|
1136 | const id = path.node.left;
|
1137 | if (id.type === "Identifier") {
|
1138 | const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("right"));
|
1139 | if (isAnonymous(initializer)) {
|
1140 | const name = id.name;
|
1141 | visitor(initializer, state, name);
|
1142 | }
|
1143 | }
|
1144 | },
|
1145 | ObjectExpression(path, state) {
|
1146 | for (const propertyPath of path.get("properties")) {
|
1147 | if (!propertyPath.isObjectProperty()) continue;
|
1148 | const {
|
1149 | node
|
1150 | } = propertyPath;
|
1151 | const id = node.key;
|
1152 | const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(propertyPath.get("value"));
|
1153 | if (isAnonymous(initializer)) {
|
1154 | if (!node.computed) {
|
1155 | if (!isProtoKey(id)) {
|
1156 | if (id.type === "Identifier") {
|
1157 | visitor(initializer, state, id.name);
|
1158 | } else {
|
1159 | const className = _core.types.stringLiteral(id.value + "");
|
1160 | visitor(initializer, state, className);
|
1161 | }
|
1162 | }
|
1163 | } else {
|
1164 | const ref = handleComputedProperty(propertyPath, id, state);
|
1165 | visitor(initializer, state, ref);
|
1166 | }
|
1167 | }
|
1168 | }
|
1169 | },
|
1170 | ClassPrivateProperty(path, state) {
|
1171 | const {
|
1172 | node
|
1173 | } = path;
|
1174 | const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value"));
|
1175 | if (isAnonymous(initializer)) {
|
1176 | const className = _core.types.stringLiteral("#" + node.key.id.name);
|
1177 | visitor(initializer, state, className);
|
1178 | }
|
1179 | },
|
1180 | ClassAccessorProperty(path, state) {
|
1181 | const {
|
1182 | node
|
1183 | } = path;
|
1184 | const id = node.key;
|
1185 | const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value"));
|
1186 | if (isAnonymous(initializer)) {
|
1187 | if (!node.computed) {
|
1188 | if (id.type === "Identifier") {
|
1189 | visitor(initializer, state, id.name);
|
1190 | } else if (id.type === "PrivateName") {
|
1191 | const className = _core.types.stringLiteral("#" + id.id.name);
|
1192 | visitor(initializer, state, className);
|
1193 | } else {
|
1194 | const className = _core.types.stringLiteral(id.value + "");
|
1195 | visitor(initializer, state, className);
|
1196 | }
|
1197 | } else {
|
1198 | const ref = handleComputedProperty(path, id, state);
|
1199 | visitor(initializer, state, ref);
|
1200 | }
|
1201 | }
|
1202 | },
|
1203 | ClassProperty(path, state) {
|
1204 | const {
|
1205 | node
|
1206 | } = path;
|
1207 | const id = node.key;
|
1208 | const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value"));
|
1209 | if (isAnonymous(initializer)) {
|
1210 | if (!node.computed) {
|
1211 | if (id.type === "Identifier") {
|
1212 | visitor(initializer, state, id.name);
|
1213 | } else {
|
1214 | const className = _core.types.stringLiteral(id.value + "");
|
1215 | visitor(initializer, state, className);
|
1216 | }
|
1217 | } else {
|
1218 | const ref = handleComputedProperty(path, id, state);
|
1219 | visitor(initializer, state, ref);
|
1220 | }
|
1221 | }
|
1222 | }
|
1223 | };
|
1224 | }
|
1225 | function isDecoratedAnonymousClassExpression(path) {
|
1226 | return path.isClassExpression({
|
1227 | id: null
|
1228 | }) && shouldTransformClass(path.node);
|
1229 | }
|
1230 | function generateLetUidIdentifier(scope, name) {
|
1231 | const id = scope.generateUidIdentifier(name);
|
1232 | scope.push({
|
1233 | id,
|
1234 | kind: "let"
|
1235 | });
|
1236 | return _core.types.cloneNode(id);
|
1237 | }
|
1238 | function _default({
|
1239 | assertVersion,
|
1240 | assumption
|
1241 | }, {
|
1242 | loose
|
1243 | }, version, inherits) {
|
1244 | var _assumption, _assumption2;
|
1245 | {
|
1246 | if (version === "2023-11" || version === "2023-05" || version === "2023-01") {
|
1247 | assertVersion("^7.21.0");
|
1248 | } else if (version === "2021-12") {
|
1249 | assertVersion("^7.16.0");
|
1250 | } else {
|
1251 | assertVersion("^7.19.0");
|
1252 | }
|
1253 | }
|
1254 | const VISITED = new WeakSet();
|
1255 | const constantSuper = (_assumption = assumption("constantSuper")) != null ? _assumption : loose;
|
1256 | const ignoreFunctionLength = (_assumption2 = assumption("ignoreFunctionLength")) != null ? _assumption2 : loose;
|
1257 | const namedEvaluationVisitor = NamedEvaluationVisitoryFactory(isDecoratedAnonymousClassExpression, visitClass);
|
1258 | function visitClass(path, state, className) {
|
1259 | var _className, _node$id;
|
1260 | if (VISITED.has(path)) return;
|
1261 | const {
|
1262 | node
|
1263 | } = path;
|
1264 | (_className = className) != null ? _className : className = (_node$id = node.id) == null ? void 0 : _node$id.name;
|
1265 | const newPath = transformClass(path, state, constantSuper, ignoreFunctionLength, className, namedEvaluationVisitor, version);
|
1266 | if (newPath) {
|
1267 | VISITED.add(newPath);
|
1268 | return;
|
1269 | }
|
1270 | VISITED.add(path);
|
1271 | }
|
1272 | return {
|
1273 | name: "proposal-decorators",
|
1274 | inherits: inherits,
|
1275 | visitor: Object.assign({
|
1276 | ExportDefaultDeclaration(path, state) {
|
1277 | const {
|
1278 | declaration
|
1279 | } = path.node;
|
1280 | if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && isDecorated(declaration)) {
|
1281 | const isAnonymous = !declaration.id;
|
1282 | {
|
1283 | var _path$splitExportDecl;
|
1284 | (_path$splitExportDecl = path.splitExportDeclaration) != null ? _path$splitExportDecl : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
|
1285 | }
|
1286 | const updatedVarDeclarationPath = path.splitExportDeclaration();
|
1287 | if (isAnonymous) {
|
1288 | visitClass(updatedVarDeclarationPath, state, _core.types.stringLiteral("default"));
|
1289 | }
|
1290 | }
|
1291 | },
|
1292 | ExportNamedDeclaration(path) {
|
1293 | const {
|
1294 | declaration
|
1295 | } = path.node;
|
1296 | if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && isDecorated(declaration)) {
|
1297 | {
|
1298 | var _path$splitExportDecl2;
|
1299 | (_path$splitExportDecl2 = path.splitExportDeclaration) != null ? _path$splitExportDecl2 : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
|
1300 | }
|
1301 | path.splitExportDeclaration();
|
1302 | }
|
1303 | },
|
1304 | Class(path, state) {
|
1305 | visitClass(path, state, undefined);
|
1306 | }
|
1307 | }, namedEvaluationVisitor)
|
1308 | };
|
1309 | }
|
1310 |
|
1311 |
|