Debian Codegraph @

packages/babel-helpers/src/helpers/applyDecs.js

raw symbols SCIP
1 /* @minVersion 7.17.8 */
2
3 /**
4 * NOTE: This is an old version of the helper, used for 2021-12 decorators.
5 * Updates should be done in applyDecs2203R.js.
6 */
7
8 /**
9 Enums are used in this file, but not assigned to vars to avoid non-hoistable values
10
11 CONSTRUCTOR = 0;
12 PUBLIC = 1;
13 PRIVATE = 2;
14
15 FIELD = 0;
16 ACCESSOR = 1;
17 METHOD = 2;
18 GETTER = 3;
19 SETTER = 4;
20
21 STATIC = 5;
22
23 CLASS = 10; // only used in assertValidReturnValue
24 */
25
26 function old_createMetadataMethodsForProperty(
27 metadataMap,
28 kind,
29 property,
30 decoratorFinishedRef
31 ) {
32 return {
33 getMetadata: function (key) {
34 old_assertNotFinished(decoratorFinishedRef, "getMetadata");
35 old_assertMetadataKey(key);
36
37 var metadataForKey = metadataMap[key];
38
39 if (metadataForKey === void 0) return void 0;
40
41 if (kind === 1 /* PUBLIC */) {
42 var pub = metadataForKey.public;
43 if (pub !== void 0) {
44 return pub[property];
45 }
46 } else if (kind === 2 /* PRIVATE */) {
47 var priv = metadataForKey.private;
48 if (priv !== void 0) {
49 return priv.get(property);
50 }
51 } else if (Object.hasOwnProperty.call(metadataForKey, "constructor")) {
52 return metadataForKey.constructor;
53 }
54 },
55 setMetadata: function (key, value) {
56 old_assertNotFinished(decoratorFinishedRef, "setMetadata");
57 old_assertMetadataKey(key);
58
59 var metadataForKey = metadataMap[key];
60
61 if (metadataForKey === void 0) {
62 metadataForKey = metadataMap[key] = {};
63 }
64
65 if (kind === 1 /* PUBLIC */) {
66 var pub = metadataForKey.public;
67
68 if (pub === void 0) {
69 pub = metadataForKey.public = {};
70 }
71
72 pub[property] = value;
73 } else if (kind === 2 /* PRIVATE */) {
74 var priv = metadataForKey.priv;
75
76 if (priv === void 0) {
77 priv = metadataForKey.private = new Map();
78 }
79
80 priv.set(property, value);
81 } else {
82 metadataForKey.constructor = value;
83 }
84 },
85 };
86 }
87
88 function old_convertMetadataMapToFinal(obj, metadataMap) {
89 var parentMetadataMap = obj[Symbol.metadata || Symbol.for("Symbol.metadata")];
90 var metadataKeys = Object.getOwnPropertySymbols(metadataMap);
91
92 if (metadataKeys.length === 0) return;
93
94 for (var i = 0; i < metadataKeys.length; i++) {
95 var key = metadataKeys[i];
96 var metaForKey = metadataMap[key];
97 var parentMetaForKey = parentMetadataMap ? parentMetadataMap[key] : null;
98
99 var pub = metaForKey.public;
100 var parentPub = parentMetaForKey ? parentMetaForKey.public : null;
101
102 if (pub && parentPub) {
103 Object.setPrototypeOf(pub, parentPub);
104 }
105
106 var priv = metaForKey.private;
107
108 if (priv) {
109 var privArr = Array.from(priv.values());
110 var parentPriv = parentMetaForKey ? parentMetaForKey.private : null;
111
112 if (parentPriv) {
113 privArr = privArr.concat(parentPriv);
114 }
115
116 metaForKey.private = privArr;
117 }
118
119 if (parentMetaForKey) {
120 Object.setPrototypeOf(metaForKey, parentMetaForKey);
121 }
122 }
123
124 if (parentMetadataMap) {
125 Object.setPrototypeOf(metadataMap, parentMetadataMap);
126 }
127
128 obj[Symbol.metadata || Symbol.for("Symbol.metadata")] = metadataMap;
129 }
130
131 function old_createAddInitializerMethod(initializers, decoratorFinishedRef) {
132 return function addInitializer(initializer) {
133 old_assertNotFinished(decoratorFinishedRef, "addInitializer");
134 old_assertCallable(initializer, "An initializer");
135 initializers.push(initializer);
136 };
137 }
138
139 function old_memberDec(
140 dec,
141 name,
142 desc,
143 metadataMap,
144 initializers,
145 kind,
146 isStatic,
147 isPrivate,
148 value
149 ) {
150 var kindStr;
151
152 switch (kind) {
153 case 1 /* ACCESSOR */:
154 kindStr = "accessor";
155 break;
156 case 2 /* METHOD */:
157 kindStr = "method";
158 break;
159 case 3 /* GETTER */:
160 kindStr = "getter";
161 break;
162 case 4 /* SETTER */:
163 kindStr = "setter";
164 break;
165 default:
166 kindStr = "field";
167 }
168
169 var ctx = {
170 kind: kindStr,
171 name: isPrivate ? "#" + name : name,
172 isStatic: isStatic,
173 isPrivate: isPrivate,
174 };
175
176 var decoratorFinishedRef = { v: false };
177
178 if (kind !== 0 /* FIELD */) {
179 ctx.addInitializer = old_createAddInitializerMethod(
180 initializers,
181 decoratorFinishedRef
182 );
183 }
184
185 var metadataKind, metadataName;
186
187 if (isPrivate) {
188 metadataKind = 2 /* PRIVATE */;
189 metadataName = Symbol(name);
190
191 var access = {};
192
193 if (kind === 0 /* FIELD */) {
194 access.get = desc.get;
195 access.set = desc.set;
196 } else if (kind === 2 /* METHOD */) {
197 access.get = function () {
198 return desc.value;
199 };
200 } else {
201 // replace with values that will go through the final getter and setter
202 if (kind === 1 /* ACCESSOR */ || kind === 3 /* GETTER */) {
203 access.get = function () {
204 return desc.get.call(this);
205 };
206 }
207
208 if (kind === 1 /* ACCESSOR */ || kind === 4 /* SETTER */) {
209 access.set = function (v) {
210 desc.set.call(this, v);
211 };
212 }
213 }
214
215 ctx.access = access;
216 } else {
217 metadataKind = 1 /* PUBLIC */;
218 metadataName = name;
219 }
220
221 try {
222 return dec(
223 value,
224 Object.assign(
225 ctx,
226 old_createMetadataMethodsForProperty(
227 metadataMap,
228 metadataKind,
229 metadataName,
230 decoratorFinishedRef
231 )
232 )
233 );
234 } finally {
235 decoratorFinishedRef.v = true;
236 }
237 }
238
239 function old_assertNotFinished(decoratorFinishedRef, fnName) {
240 if (decoratorFinishedRef.v) {
241 throw new Error(
242 "attempted to call " + fnName + " after decoration was finished"
243 );
244 }
245 }
246
247 function old_assertMetadataKey(key) {
248 if (typeof key !== "symbol") {
249 throw new TypeError("Metadata keys must be symbols, received: " + key);
250 }
251 }
252
253 function old_assertCallable(fn, hint) {
254 if (typeof fn !== "function") {
255 throw new TypeError(hint + " must be a function");
256 }
257 }
258
259 function old_assertValidReturnValue(kind, value) {
260 var type = typeof value;
261
262 if (kind === 1 /* ACCESSOR */) {
263 if (type !== "object" || value === null) {
264 throw new TypeError(
265 "accessor decorators must return an object with get, set, or init properties or void 0"
266 );
267 }
268 if (value.get !== undefined) {
269 old_assertCallable(value.get, "accessor.get");
270 }
271 if (value.set !== undefined) {
272 old_assertCallable(value.set, "accessor.set");
273 }
274 if (value.init !== undefined) {
275 old_assertCallable(value.init, "accessor.init");
276 }
277 if (value.initializer !== undefined) {
278 old_assertCallable(value.initializer, "accessor.initializer");
279 }
280 } else if (type !== "function") {
281 var hint;
282 if (kind === 0 /* FIELD */) {
283 hint = "field";
284 } else if (kind === 10 /* CLASS */) {
285 hint = "class";
286 } else {
287 hint = "method";
288 }
289 throw new TypeError(hint + " decorators must return a function or void 0");
290 }
291 }
292
293 function old_getInit(desc) {
294 var initializer;
295 if (
296 (initializer = desc.init) == null &&
297 (initializer = desc.initializer) &&
298 typeof console !== "undefined"
299 ) {
300 console.warn(".initializer has been renamed to .init as of March 2022");
301 }
302 return initializer;
303 }
304
305 function old_applyMemberDec(
306 ret,
307 base,
308 decInfo,
309 name,
310 kind,
311 isStatic,
312 isPrivate,
313 metadataMap,
314 initializers
315 ) {
316 var decs = decInfo[0];
317
318 var desc, initializer, value;
319
320 if (isPrivate) {
321 if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
322 desc = {
323 get: decInfo[3],
324 set: decInfo[4],
325 };
326 } else if (kind === 3 /* GETTER */) {
327 desc = {
328 get: decInfo[3],
329 };
330 } else if (kind === 4 /* SETTER */) {
331 desc = {
332 set: decInfo[3],
333 };
334 } else {
335 desc = {
336 value: decInfo[3],
337 };
338 }
339 } else if (kind !== 0 /* FIELD */) {
340 desc = Object.getOwnPropertyDescriptor(base, name);
341 }
342
343 if (kind === 1 /* ACCESSOR */) {
344 value = {
345 get: desc.get,
346 set: desc.set,
347 };
348 } else if (kind === 2 /* METHOD */) {
349 value = desc.value;
350 } else if (kind === 3 /* GETTER */) {
351 value = desc.get;
352 } else if (kind === 4 /* SETTER */) {
353 value = desc.set;
354 }
355
356 var newValue, get, set;
357
358 if (typeof decs === "function") {
359 newValue = old_memberDec(
360 decs,
361 name,
362 desc,
363 metadataMap,
364 initializers,
365 kind,
366 isStatic,
367 isPrivate,
368 value
369 );
370
371 if (newValue !== void 0) {
372 old_assertValidReturnValue(kind, newValue);
373
374 if (kind === 0 /* FIELD */) {
375 initializer = newValue;
376 } else if (kind === 1 /* ACCESSOR */) {
377 initializer = old_getInit(newValue);
378 get = newValue.get || value.get;
379 set = newValue.set || value.set;
380
381 value = { get: get, set: set };
382 } else {
383 value = newValue;
384 }
385 }
386 } else {
387 for (var i = decs.length - 1; i >= 0; i--) {
388 var dec = decs[i];
389
390 newValue = old_memberDec(
391 dec,
392 name,
393 desc,
394 metadataMap,
395 initializers,
396 kind,
397 isStatic,
398 isPrivate,
399 value
400 );
401
402 if (newValue !== void 0) {
403 old_assertValidReturnValue(kind, newValue);
404 var newInit;
405
406 if (kind === 0 /* FIELD */) {
407 newInit = newValue;
408 } else if (kind === 1 /* ACCESSOR */) {
409 newInit = old_getInit(newValue);
410 get = newValue.get || value.get;
411 set = newValue.set || value.set;
412
413 value = { get: get, set: set };
414 } else {
415 value = newValue;
416 }
417
418 if (newInit !== void 0) {
419 if (initializer === void 0) {
420 initializer = newInit;
421 } else if (typeof initializer === "function") {
422 initializer = [initializer, newInit];
423 } else {
424 initializer.push(newInit);
425 }
426 }
427 }
428 }
429 }
430
431 if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
432 if (initializer === void 0) {
433 // If the initializer was void 0, sub in a dummy initializer
434 initializer = function (instance, init) {
435 return init;
436 };
437 } else if (typeof initializer !== "function") {
438 var ownInitializers = initializer;
439
440 initializer = function (instance, init) {
441 var value = init;
442
443 for (var i = 0; i < ownInitializers.length; i++) {
444 value = ownInitializers[i].call(instance, value);
445 }
446
447 return value;
448 };
449 } else {
450 var originalInitializer = initializer;
451
452 initializer = function (instance, init) {
453 return originalInitializer.call(instance, init);
454 };
455 }
456
457 ret.push(initializer);
458 }
459
460 if (kind !== 0 /* FIELD */) {
461 if (kind === 1 /* ACCESSOR */) {
462 desc.get = value.get;
463 desc.set = value.set;
464 } else if (kind === 2 /* METHOD */) {
465 desc.value = value;
466 } else if (kind === 3 /* GETTER */) {
467 desc.get = value;
468 } else if (kind === 4 /* SETTER */) {
469 desc.set = value;
470 }
471
472 if (isPrivate) {
473 if (kind === 1 /* ACCESSOR */) {
474 ret.push(function (instance, args) {
475 return value.get.call(instance, args);
476 });
477 ret.push(function (instance, args) {
478 return value.set.call(instance, args);
479 });
480 } else if (kind === 2 /* METHOD */) {
481 ret.push(value);
482 } else {
483 ret.push(function (instance, args) {
484 return value.call(instance, args);
485 });
486 }
487 } else {
488 Object.defineProperty(base, name, desc);
489 }
490 }
491 }
492
493 function old_applyMemberDecs(
494 ret,
495 Class,
496 protoMetadataMap,
497 staticMetadataMap,
498 decInfos
499 ) {
500 var protoInitializers;
501 var staticInitializers;
502
503 var existingProtoNonFields = new Map();
504 var existingStaticNonFields = new Map();
505
506 for (var i = 0; i < decInfos.length; i++) {
507 var decInfo = decInfos[i];
508
509 // skip computed property names
510 if (!Array.isArray(decInfo)) continue;
511
512 var kind = decInfo[1];
513 var name = decInfo[2];
514 var isPrivate = decInfo.length > 3;
515
516 var isStatic = kind >= 5; /* STATIC */
517 var base;
518 var metadataMap;
519 var initializers;
520
521 if (isStatic) {
522 base = Class;
523 metadataMap = staticMetadataMap;
524 kind = kind - 5 /* STATIC */;
525 // initialize staticInitializers when we see a non-field static member
526 if (kind !== 0 /* FIELD */) {
527 staticInitializers = staticInitializers || [];
528 initializers = staticInitializers;
529 }
530 } else {
531 base = Class.prototype;
532 metadataMap = protoMetadataMap;
533 // initialize protoInitializers when we see a non-field member
534 if (kind !== 0 /* FIELD */) {
535 protoInitializers = protoInitializers || [];
536 initializers = protoInitializers;
537 }
538 }
539
540 if (kind !== 0 /* FIELD */ && !isPrivate) {
541 var existingNonFields = isStatic
542 ? existingStaticNonFields
543 : existingProtoNonFields;
544
545 var existingKind = existingNonFields.get(name) || 0;
546
547 if (
548 existingKind === true ||
549 (existingKind === 3 /* GETTER */ && kind !== 4) /* SETTER */ ||
550 (existingKind === 4 /* SETTER */ && kind !== 3) /* GETTER */
551 ) {
552 throw new Error(
553 "Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " +
554 name
555 );
556 } else if (!existingKind && kind > 2 /* METHOD */) {
557 existingNonFields.set(name, kind);
558 } else {
559 existingNonFields.set(name, true);
560 }
561 }
562
563 old_applyMemberDec(
564 ret,
565 base,
566 decInfo,
567 name,
568 kind,
569 isStatic,
570 isPrivate,
571 metadataMap,
572 initializers
573 );
574 }
575
576 old_pushInitializers(ret, protoInitializers);
577 old_pushInitializers(ret, staticInitializers);
578 }
579
580 function old_pushInitializers(ret, initializers) {
581 if (initializers) {
582 ret.push(function (instance) {
583 for (var i = 0; i < initializers.length; i++) {
584 initializers[i].call(instance);
585 }
586 return instance;
587 });
588 }
589 }
590
591 function old_applyClassDecs(ret, targetClass, metadataMap, classDecs) {
592 if (classDecs.length > 0) {
593 var initializers = [];
594 var newClass = targetClass;
595 var name = targetClass.name;
596
597 for (var i = classDecs.length - 1; i >= 0; i--) {
598 var decoratorFinishedRef = { v: false };
599
600 try {
601 var ctx = Object.assign(
602 {
603 kind: "class",
604 name: name,
605 addInitializer: old_createAddInitializerMethod(
606 initializers,
607 decoratorFinishedRef
608 ),
609 },
610 old_createMetadataMethodsForProperty(
611 metadataMap,
612 0 /* CONSTRUCTOR */,
613 name,
614 decoratorFinishedRef
615 )
616 );
617 var nextNewClass = classDecs[i](newClass, ctx);
618 } finally {
619 decoratorFinishedRef.v = true;
620 }
621
622 if (nextNewClass !== undefined) {
623 old_assertValidReturnValue(10 /* CLASS */, nextNewClass);
624 newClass = nextNewClass;
625 }
626 }
627
628 ret.push(newClass, function () {
629 for (var i = 0; i < initializers.length; i++) {
630 initializers[i].call(newClass);
631 }
632 });
633 }
634 }
635
636 /**
637 Basic usage:
638
639 applyDecs(
640 Class,
641 [
642 // member decorators
643 [
644 dec, // dec or array of decs
645 0, // kind of value being decorated
646 'prop', // name of public prop on class containing the value being decorated,
647 '#p', // the name of the private property (if is private, void 0 otherwise),
648 ]
649 ],
650 [
651 // class decorators
652 dec1, dec2
653 ]
654 )
655 ```
656
657 Fully transpiled example:
658
659 ```js
660 @dec
661 class Class {
662 @dec
663 a = 123;
664
665 @dec
666 #a = 123;
667
668 @dec
669 @dec2
670 accessor b = 123;
671
672 @dec
673 accessor #b = 123;
674
675 @dec
676 c() { console.log('c'); }
677
678 @dec
679 #c() { console.log('privC'); }
680
681 @dec
682 get d() { console.log('d'); }
683
684 @dec
685 get #d() { console.log('privD'); }
686
687 @dec
688 set e(v) { console.log('e'); }
689
690 @dec
691 set #e(v) { console.log('privE'); }
692 }
693
694
695 // becomes
696 let initializeInstance;
697 let initializeClass;
698
699 let initA;
700 let initPrivA;
701
702 let initB;
703 let initPrivB, getPrivB, setPrivB;
704
705 let privC;
706 let privD;
707 let privE;
708
709 let Class;
710 class _Class {
711 static {
712 let ret = applyDecs(
713 this,
714 [
715 [dec, 0, 'a'],
716 [dec, 0, 'a', (i) => i.#a, (i, v) => i.#a = v],
717 [[dec, dec2], 1, 'b'],
718 [dec, 1, 'b', (i) => i.#privBData, (i, v) => i.#privBData = v],
719 [dec, 2, 'c'],
720 [dec, 2, 'c', () => console.log('privC')],
721 [dec, 3, 'd'],
722 [dec, 3, 'd', () => console.log('privD')],
723 [dec, 4, 'e'],
724 [dec, 4, 'e', () => console.log('privE')],
725 ],
726 [
727 dec
728 ]
729 )
730
731 initA = ret[0];
732
733 initPrivA = ret[1];
734
735 initB = ret[2];
736
737 initPrivB = ret[3];
738 getPrivB = ret[4];
739 setPrivB = ret[5];
740
741 privC = ret[6];
742
743 privD = ret[7];
744
745 privE = ret[8];
746
747 initializeInstance = ret[9];
748
749 Class = ret[10]
750
751 initializeClass = ret[11];
752 }
753
754 a = (initializeInstance(this), initA(this, 123));
755
756 #a = initPrivA(this, 123);
757
758 #bData = initB(this, 123);
759 get b() { return this.#bData }
760 set b(v) { this.#bData = v }
761
762 #privBData = initPrivB(this, 123);
763 get #b() { return getPrivB(this); }
764 set #b(v) { setPrivB(this, v); }
765
766 c() { console.log('c'); }
767
768 #c(...args) { return privC(this, ...args) }
769
770 get d() { console.log('d'); }
771
772 get #d() { return privD(this); }
773
774 set e(v) { console.log('e'); }
775
776 set #e(v) { privE(this, v); }
777 }
778
779 initializeClass(Class);
780 */
781 export default function applyDecs(targetClass, memberDecs, classDecs) {
782 var ret = [];
783 var staticMetadataMap = {};
784
785 var protoMetadataMap = {};
786
787 old_applyMemberDecs(
788 ret,
789 targetClass,
790 protoMetadataMap,
791 staticMetadataMap,
792 memberDecs
793 );
794
795 old_convertMetadataMapToFinal(targetClass.prototype, protoMetadataMap);
796
797 old_applyClassDecs(ret, targetClass, staticMetadataMap, classDecs);
798
799 old_convertMetadataMapToFinal(targetClass, staticMetadataMap);
800
801 return ret;
802 }