Debian Codegraph @

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

raw symbols SCIP
1 /* @minVersion 7.19.0 */
2
3 /**
4 * NOTE: This is an old version of the helper, used for 2022-03 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 function applyDecs2203Factory() {
26 function createAddInitializerMethod(initializers, decoratorFinishedRef) {
27 return function addInitializer(initializer) {
28 assertNotFinished(decoratorFinishedRef, "addInitializer");
29 assertCallable(initializer, "An initializer");
30 initializers.push(initializer);
31 };
32 }
33
34 function memberDec(
35 dec,
36 name,
37 desc,
38 initializers,
39 kind,
40 isStatic,
41 isPrivate,
42 value
43 ) {
44 var kindStr;
45
46 switch (kind) {
47 case 1 /* ACCESSOR */:
48 kindStr = "accessor";
49 break;
50 case 2 /* METHOD */:
51 kindStr = "method";
52 break;
53 case 3 /* GETTER */:
54 kindStr = "getter";
55 break;
56 case 4 /* SETTER */:
57 kindStr = "setter";
58 break;
59 default:
60 kindStr = "field";
61 }
62
63 var ctx = {
64 kind: kindStr,
65 name: isPrivate ? "#" + name : name,
66 static: isStatic,
67 private: isPrivate,
68 };
69
70 var decoratorFinishedRef = { v: false };
71
72 if (kind !== 0 /* FIELD */) {
73 ctx.addInitializer = createAddInitializerMethod(
74 initializers,
75 decoratorFinishedRef
76 );
77 }
78
79 var get, set;
80 if (kind === 0 /* FIELD */) {
81 if (isPrivate) {
82 get = desc.get;
83 set = desc.set;
84 } else {
85 get = function () {
86 return this[name];
87 };
88 set = function (v) {
89 this[name] = v;
90 };
91 }
92 } else if (kind === 2 /* METHOD */) {
93 get = function () {
94 return desc.value;
95 };
96 } else {
97 // replace with values that will go through the final getter and setter
98 if (kind === 1 /* ACCESSOR */ || kind === 3 /* GETTER */) {
99 get = function () {
100 return desc.get.call(this);
101 };
102 }
103
104 if (kind === 1 /* ACCESSOR */ || kind === 4 /* SETTER */) {
105 set = function (v) {
106 desc.set.call(this, v);
107 };
108 }
109 }
110 ctx.access =
111 get && set ? { get: get, set: set } : get ? { get: get } : { set: set };
112
113 try {
114 return dec(value, ctx);
115 } finally {
116 decoratorFinishedRef.v = true;
117 }
118 }
119
120 function assertNotFinished(decoratorFinishedRef, fnName) {
121 if (decoratorFinishedRef.v) {
122 throw new Error(
123 "attempted to call " + fnName + " after decoration was finished"
124 );
125 }
126 }
127
128 function assertCallable(fn, hint) {
129 if (typeof fn !== "function") {
130 throw new TypeError(hint + " must be a function");
131 }
132 }
133
134 function assertValidReturnValue(kind, value) {
135 var type = typeof value;
136
137 if (kind === 1 /* ACCESSOR */) {
138 if (type !== "object" || value === null) {
139 throw new TypeError(
140 "accessor decorators must return an object with get, set, or init properties or void 0"
141 );
142 }
143 if (value.get !== undefined) {
144 assertCallable(value.get, "accessor.get");
145 }
146 if (value.set !== undefined) {
147 assertCallable(value.set, "accessor.set");
148 }
149 if (value.init !== undefined) {
150 assertCallable(value.init, "accessor.init");
151 }
152 } else if (type !== "function") {
153 var hint;
154 if (kind === 0 /* FIELD */) {
155 hint = "field";
156 } else if (kind === 10 /* CLASS */) {
157 hint = "class";
158 } else {
159 hint = "method";
160 }
161 throw new TypeError(
162 hint + " decorators must return a function or void 0"
163 );
164 }
165 }
166
167 function applyMemberDec(
168 ret,
169 base,
170 decInfo,
171 name,
172 kind,
173 isStatic,
174 isPrivate,
175 initializers
176 ) {
177 var decs = decInfo[0];
178
179 var desc, init, value;
180
181 if (isPrivate) {
182 if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
183 desc = {
184 get: decInfo[3],
185 set: decInfo[4],
186 };
187 } else if (kind === 3 /* GETTER */) {
188 desc = {
189 get: decInfo[3],
190 };
191 } else if (kind === 4 /* SETTER */) {
192 desc = {
193 set: decInfo[3],
194 };
195 } else {
196 desc = {
197 value: decInfo[3],
198 };
199 }
200 } else if (kind !== 0 /* FIELD */) {
201 desc = Object.getOwnPropertyDescriptor(base, name);
202 }
203
204 if (kind === 1 /* ACCESSOR */) {
205 value = {
206 get: desc.get,
207 set: desc.set,
208 };
209 } else if (kind === 2 /* METHOD */) {
210 value = desc.value;
211 } else if (kind === 3 /* GETTER */) {
212 value = desc.get;
213 } else if (kind === 4 /* SETTER */) {
214 value = desc.set;
215 }
216
217 var newValue, get, set;
218
219 if (typeof decs === "function") {
220 newValue = memberDec(
221 decs,
222 name,
223 desc,
224 initializers,
225 kind,
226 isStatic,
227 isPrivate,
228 value
229 );
230
231 if (newValue !== void 0) {
232 assertValidReturnValue(kind, newValue);
233
234 if (kind === 0 /* FIELD */) {
235 init = newValue;
236 } else if (kind === 1 /* ACCESSOR */) {
237 init = newValue.init;
238 get = newValue.get || value.get;
239 set = newValue.set || value.set;
240
241 value = { get: get, set: set };
242 } else {
243 value = newValue;
244 }
245 }
246 } else {
247 for (var i = decs.length - 1; i >= 0; i--) {
248 var dec = decs[i];
249
250 newValue = memberDec(
251 dec,
252 name,
253 desc,
254 initializers,
255 kind,
256 isStatic,
257 isPrivate,
258 value
259 );
260
261 if (newValue !== void 0) {
262 assertValidReturnValue(kind, newValue);
263 var newInit;
264
265 if (kind === 0 /* FIELD */) {
266 newInit = newValue;
267 } else if (kind === 1 /* ACCESSOR */) {
268 newInit = newValue.init;
269 get = newValue.get || value.get;
270 set = newValue.set || value.set;
271
272 value = { get: get, set: set };
273 } else {
274 value = newValue;
275 }
276
277 if (newInit !== void 0) {
278 if (init === void 0) {
279 init = newInit;
280 } else if (typeof init === "function") {
281 init = [init, newInit];
282 } else {
283 init.push(newInit);
284 }
285 }
286 }
287 }
288 }
289
290 if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
291 if (init === void 0) {
292 // If the initializer was void 0, sub in a dummy initializer
293 init = function (instance, init) {
294 return init;
295 };
296 } else if (typeof init !== "function") {
297 var ownInitializers = init;
298
299 init = function (instance, init) {
300 var value = init;
301
302 for (var i = 0; i < ownInitializers.length; i++) {
303 value = ownInitializers[i].call(instance, value);
304 }
305
306 return value;
307 };
308 } else {
309 var originalInitializer = init;
310
311 init = function (instance, init) {
312 return originalInitializer.call(instance, init);
313 };
314 }
315
316 ret.push(init);
317 }
318
319 if (kind !== 0 /* FIELD */) {
320 if (kind === 1 /* ACCESSOR */) {
321 desc.get = value.get;
322 desc.set = value.set;
323 } else if (kind === 2 /* METHOD */) {
324 desc.value = value;
325 } else if (kind === 3 /* GETTER */) {
326 desc.get = value;
327 } else if (kind === 4 /* SETTER */) {
328 desc.set = value;
329 }
330
331 if (isPrivate) {
332 if (kind === 1 /* ACCESSOR */) {
333 ret.push(function (instance, args) {
334 return value.get.call(instance, args);
335 });
336 ret.push(function (instance, args) {
337 return value.set.call(instance, args);
338 });
339 } else if (kind === 2 /* METHOD */) {
340 ret.push(value);
341 } else {
342 ret.push(function (instance, args) {
343 return value.call(instance, args);
344 });
345 }
346 } else {
347 Object.defineProperty(base, name, desc);
348 }
349 }
350 }
351
352 function applyMemberDecs(ret, Class, decInfos) {
353 var protoInitializers;
354 var staticInitializers;
355
356 var existingProtoNonFields = new Map();
357 var existingStaticNonFields = new Map();
358
359 for (var i = 0; i < decInfos.length; i++) {
360 var decInfo = decInfos[i];
361
362 // skip computed property names
363 if (!Array.isArray(decInfo)) continue;
364
365 var kind = decInfo[1];
366 var name = decInfo[2];
367 var isPrivate = decInfo.length > 3;
368
369 var isStatic = kind >= 5; /* STATIC */
370 var base;
371 var initializers;
372
373 if (isStatic) {
374 base = Class;
375 kind = kind - 5 /* STATIC */;
376 // initialize staticInitializers when we see a non-field static member
377 if (kind !== 0 /* FIELD */) {
378 staticInitializers = staticInitializers || [];
379 initializers = staticInitializers;
380 }
381 } else {
382 base = Class.prototype;
383 // initialize protoInitializers when we see a non-field member
384 if (kind !== 0 /* FIELD */) {
385 protoInitializers = protoInitializers || [];
386 initializers = protoInitializers;
387 }
388 }
389
390 if (kind !== 0 /* FIELD */ && !isPrivate) {
391 var existingNonFields = isStatic
392 ? existingStaticNonFields
393 : existingProtoNonFields;
394
395 var existingKind = existingNonFields.get(name) || 0;
396
397 if (
398 existingKind === true ||
399 (existingKind === 3 /* GETTER */ && kind !== 4) /* SETTER */ ||
400 (existingKind === 4 /* SETTER */ && kind !== 3) /* GETTER */
401 ) {
402 throw new Error(
403 "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: " +
404 name
405 );
406 } else if (!existingKind && kind > 2 /* METHOD */) {
407 existingNonFields.set(name, kind);
408 } else {
409 existingNonFields.set(name, true);
410 }
411 }
412
413 applyMemberDec(
414 ret,
415 base,
416 decInfo,
417 name,
418 kind,
419 isStatic,
420 isPrivate,
421 initializers
422 );
423 }
424
425 pushInitializers(ret, protoInitializers);
426 pushInitializers(ret, staticInitializers);
427 }
428
429 function pushInitializers(ret, initializers) {
430 if (initializers) {
431 ret.push(function (instance) {
432 for (var i = 0; i < initializers.length; i++) {
433 initializers[i].call(instance);
434 }
435 return instance;
436 });
437 }
438 }
439
440 function applyClassDecs(ret, targetClass, classDecs) {
441 if (classDecs.length > 0) {
442 var initializers = [];
443 var newClass = targetClass;
444 var name = targetClass.name;
445
446 for (var i = classDecs.length - 1; i >= 0; i--) {
447 var decoratorFinishedRef = { v: false };
448
449 try {
450 var nextNewClass = classDecs[i](newClass, {
451 kind: "class",
452 name: name,
453 addInitializer: createAddInitializerMethod(
454 initializers,
455 decoratorFinishedRef
456 ),
457 });
458 } finally {
459 decoratorFinishedRef.v = true;
460 }
461
462 if (nextNewClass !== undefined) {
463 assertValidReturnValue(10 /* CLASS */, nextNewClass);
464 newClass = nextNewClass;
465 }
466 }
467
468 ret.push(newClass, function () {
469 for (var i = 0; i < initializers.length; i++) {
470 initializers[i].call(newClass);
471 }
472 });
473 }
474 }
475
476 /**
477 Basic usage:
478
479 applyDecs(
480 Class,
481 [
482 // member decorators
483 [
484 dec, // dec or array of decs
485 0, // kind of value being decorated
486 'prop', // name of public prop on class containing the value being decorated,
487 '#p', // the name of the private property (if is private, void 0 otherwise),
488 ]
489 ],
490 [
491 // class decorators
492 dec1, dec2
493 ]
494 )
495 ```
496
497 Fully transpiled example:
498
499 ```js
500 @dec
501 class Class {
502 @dec
503 a = 123;
504
505 @dec
506 #a = 123;
507
508 @dec
509 @dec2
510 accessor b = 123;
511
512 @dec
513 accessor #b = 123;
514
515 @dec
516 c() { console.log('c'); }
517
518 @dec
519 #c() { console.log('privC'); }
520
521 @dec
522 get d() { console.log('d'); }
523
524 @dec
525 get #d() { console.log('privD'); }
526
527 @dec
528 set e(v) { console.log('e'); }
529
530 @dec
531 set #e(v) { console.log('privE'); }
532 }
533
534
535 // becomes
536 let initializeInstance;
537 let initializeClass;
538
539 let initA;
540 let initPrivA;
541
542 let initB;
543 let initPrivB, getPrivB, setPrivB;
544
545 let privC;
546 let privD;
547 let privE;
548
549 let Class;
550 class _Class {
551 static {
552 let ret = applyDecs(
553 this,
554 [
555 [dec, 0, 'a'],
556 [dec, 0, 'a', (i) => i.#a, (i, v) => i.#a = v],
557 [[dec, dec2], 1, 'b'],
558 [dec, 1, 'b', (i) => i.#privBData, (i, v) => i.#privBData = v],
559 [dec, 2, 'c'],
560 [dec, 2, 'c', () => console.log('privC')],
561 [dec, 3, 'd'],
562 [dec, 3, 'd', () => console.log('privD')],
563 [dec, 4, 'e'],
564 [dec, 4, 'e', () => console.log('privE')],
565 ],
566 [
567 dec
568 ]
569 )
570
571 initA = ret[0];
572
573 initPrivA = ret[1];
574
575 initB = ret[2];
576
577 initPrivB = ret[3];
578 getPrivB = ret[4];
579 setPrivB = ret[5];
580
581 privC = ret[6];
582
583 privD = ret[7];
584
585 privE = ret[8];
586
587 initializeInstance = ret[9];
588
589 Class = ret[10]
590
591 initializeClass = ret[11];
592 }
593
594 a = (initializeInstance(this), initA(this, 123));
595
596 #a = initPrivA(this, 123);
597
598 #bData = initB(this, 123);
599 get b() { return this.#bData }
600 set b(v) { this.#bData = v }
601
602 #privBData = initPrivB(this, 123);
603 get #b() { return getPrivB(this); }
604 set #b(v) { setPrivB(this, v); }
605
606 c() { console.log('c'); }
607
608 #c(...args) { return privC(this, ...args) }
609
610 get d() { console.log('d'); }
611
612 get #d() { return privD(this); }
613
614 set e(v) { console.log('e'); }
615
616 set #e(v) { privE(this, v); }
617 }
618
619 initializeClass(Class);
620 */
621
622 return function applyDecs2203Impl(targetClass, memberDecs, classDecs) {
623 var ret = [];
624 applyMemberDecs(ret, targetClass, memberDecs);
625 applyClassDecs(ret, targetClass, classDecs);
626 return ret;
627 };
628 }
629
630 var applyDecs2203Impl;
631
632 export default function applyDecs2203(targetClass, memberDecs, classDecs) {
633 applyDecs2203Impl = applyDecs2203Impl || applyDecs2203Factory();
634 return applyDecs2203Impl(targetClass, memberDecs, classDecs);
635 }