Debian Codegraph @

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

raw symbols SCIP
1 /* @minVersion 7.20.0 */
2
3 /**
4 Enums are used in this file, but not assigned to vars to avoid non-hoistable values
5
6 CONSTRUCTOR = 0;
7 PUBLIC = 1;
8 PRIVATE = 2;
9
10 FIELD = 0;
11 ACCESSOR = 1;
12 METHOD = 2;
13 GETTER = 3;
14 SETTER = 4;
15
16 STATIC = 5;
17
18 CLASS = 10; // only used in assertValidReturnValue
19 */
20
21 function createAddInitializerMethod(initializers, decoratorFinishedRef) {
22 return function addInitializer(initializer) {
23 assertNotFinished(decoratorFinishedRef, "addInitializer");
24 assertCallable(initializer, "An initializer");
25 initializers.push(initializer);
26 };
27 }
28
29 function memberDec(
30 dec,
31 name,
32 desc,
33 initializers,
34 kind,
35 isStatic,
36 isPrivate,
37 value
38 ) {
39 var kindStr;
40
41 switch (kind) {
42 case 1 /* ACCESSOR */:
43 kindStr = "accessor";
44 break;
45 case 2 /* METHOD */:
46 kindStr = "method";
47 break;
48 case 3 /* GETTER */:
49 kindStr = "getter";
50 break;
51 case 4 /* SETTER */:
52 kindStr = "setter";
53 break;
54 default:
55 kindStr = "field";
56 }
57
58 var ctx = {
59 kind: kindStr,
60 name: isPrivate ? "#" + name : name,
61 static: isStatic,
62 private: isPrivate,
63 };
64
65 var decoratorFinishedRef = { v: false };
66
67 if (kind !== 0 /* FIELD */) {
68 ctx.addInitializer = createAddInitializerMethod(
69 initializers,
70 decoratorFinishedRef
71 );
72 }
73
74 var get, set;
75 if (kind === 0 /* FIELD */) {
76 if (isPrivate) {
77 get = desc.get;
78 set = desc.set;
79 } else {
80 get = function () {
81 return this[name];
82 };
83 set = function (v) {
84 this[name] = v;
85 };
86 }
87 } else if (kind === 2 /* METHOD */) {
88 get = function () {
89 return desc.value;
90 };
91 } else {
92 // replace with values that will go through the final getter and setter
93 if (kind === 1 /* ACCESSOR */ || kind === 3 /* GETTER */) {
94 get = function () {
95 return desc.get.call(this);
96 };
97 }
98
99 if (kind === 1 /* ACCESSOR */ || kind === 4 /* SETTER */) {
100 set = function (v) {
101 desc.set.call(this, v);
102 };
103 }
104 }
105 ctx.access =
106 get && set ? { get: get, set: set } : get ? { get: get } : { set: set };
107
108 try {
109 return dec(value, ctx);
110 } finally {
111 decoratorFinishedRef.v = true;
112 }
113 }
114
115 function assertNotFinished(decoratorFinishedRef, fnName) {
116 if (decoratorFinishedRef.v) {
117 throw new Error(
118 "attempted to call " + fnName + " after decoration was finished"
119 );
120 }
121 }
122
123 function assertCallable(fn, hint) {
124 if (typeof fn !== "function") {
125 throw new TypeError(hint + " must be a function");
126 }
127 }
128
129 function assertValidReturnValue(kind, value) {
130 var type = typeof value;
131
132 if (kind === 1 /* ACCESSOR */) {
133 if (type !== "object" || value === null) {
134 throw new TypeError(
135 "accessor decorators must return an object with get, set, or init properties or void 0"
136 );
137 }
138 if (value.get !== undefined) {
139 assertCallable(value.get, "accessor.get");
140 }
141 if (value.set !== undefined) {
142 assertCallable(value.set, "accessor.set");
143 }
144 if (value.init !== undefined) {
145 assertCallable(value.init, "accessor.init");
146 }
147 } else if (type !== "function") {
148 var hint;
149 if (kind === 0 /* FIELD */) {
150 hint = "field";
151 } else if (kind === 10 /* CLASS */) {
152 hint = "class";
153 } else {
154 hint = "method";
155 }
156 throw new TypeError(hint + " decorators must return a function or void 0");
157 }
158 }
159
160 function applyMemberDec(
161 ret,
162 base,
163 decInfo,
164 name,
165 kind,
166 isStatic,
167 isPrivate,
168 initializers
169 ) {
170 var decs = decInfo[0];
171
172 var desc, init, value;
173
174 if (isPrivate) {
175 if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
176 desc = {
177 get: decInfo[3],
178 set: decInfo[4],
179 };
180 } else if (kind === 3 /* GETTER */) {
181 desc = {
182 get: decInfo[3],
183 };
184 } else if (kind === 4 /* SETTER */) {
185 desc = {
186 set: decInfo[3],
187 };
188 } else {
189 desc = {
190 value: decInfo[3],
191 };
192 }
193 } else if (kind !== 0 /* FIELD */) {
194 desc = Object.getOwnPropertyDescriptor(base, name);
195 }
196
197 if (kind === 1 /* ACCESSOR */) {
198 value = {
199 get: desc.get,
200 set: desc.set,
201 };
202 } else if (kind === 2 /* METHOD */) {
203 value = desc.value;
204 } else if (kind === 3 /* GETTER */) {
205 value = desc.get;
206 } else if (kind === 4 /* SETTER */) {
207 value = desc.set;
208 }
209
210 var newValue, get, set;
211
212 if (typeof decs === "function") {
213 newValue = memberDec(
214 decs,
215 name,
216 desc,
217 initializers,
218 kind,
219 isStatic,
220 isPrivate,
221 value
222 );
223
224 if (newValue !== void 0) {
225 assertValidReturnValue(kind, newValue);
226
227 if (kind === 0 /* FIELD */) {
228 init = newValue;
229 } else if (kind === 1 /* ACCESSOR */) {
230 init = newValue.init;
231 get = newValue.get || value.get;
232 set = newValue.set || value.set;
233
234 value = { get: get, set: set };
235 } else {
236 value = newValue;
237 }
238 }
239 } else {
240 for (var i = decs.length - 1; i >= 0; i--) {
241 var dec = decs[i];
242
243 newValue = memberDec(
244 dec,
245 name,
246 desc,
247 initializers,
248 kind,
249 isStatic,
250 isPrivate,
251 value
252 );
253
254 if (newValue !== void 0) {
255 assertValidReturnValue(kind, newValue);
256 var newInit;
257
258 if (kind === 0 /* FIELD */) {
259 newInit = newValue;
260 } else if (kind === 1 /* ACCESSOR */) {
261 newInit = newValue.init;
262 get = newValue.get || value.get;
263 set = newValue.set || value.set;
264
265 value = { get: get, set: set };
266 } else {
267 value = newValue;
268 }
269
270 if (newInit !== void 0) {
271 if (init === void 0) {
272 init = newInit;
273 } else if (typeof init === "function") {
274 init = [init, newInit];
275 } else {
276 init.push(newInit);
277 }
278 }
279 }
280 }
281 }
282
283 if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
284 if (init === void 0) {
285 // If the initializer was void 0, sub in a dummy initializer
286 init = function (instance, init) {
287 return init;
288 };
289 } else if (typeof init !== "function") {
290 var ownInitializers = init;
291
292 init = function (instance, init) {
293 var value = init;
294
295 for (var i = 0; i < ownInitializers.length; i++) {
296 value = ownInitializers[i].call(instance, value);
297 }
298
299 return value;
300 };
301 } else {
302 var originalInitializer = init;
303
304 init = function (instance, init) {
305 return originalInitializer.call(instance, init);
306 };
307 }
308
309 ret.push(init);
310 }
311
312 if (kind !== 0 /* FIELD */) {
313 if (kind === 1 /* ACCESSOR */) {
314 desc.get = value.get;
315 desc.set = value.set;
316 } else if (kind === 2 /* METHOD */) {
317 desc.value = value;
318 } else if (kind === 3 /* GETTER */) {
319 desc.get = value;
320 } else if (kind === 4 /* SETTER */) {
321 desc.set = value;
322 }
323
324 if (isPrivate) {
325 if (kind === 1 /* ACCESSOR */) {
326 ret.push(function (instance, args) {
327 return value.get.call(instance, args);
328 });
329 ret.push(function (instance, args) {
330 return value.set.call(instance, args);
331 });
332 } else if (kind === 2 /* METHOD */) {
333 ret.push(value);
334 } else {
335 ret.push(function (instance, args) {
336 return value.call(instance, args);
337 });
338 }
339 } else {
340 Object.defineProperty(base, name, desc);
341 }
342 }
343 }
344
345 function applyMemberDecs(Class, decInfos) {
346 var ret = [];
347 var protoInitializers;
348 var staticInitializers;
349
350 var existingProtoNonFields = new Map();
351 var existingStaticNonFields = new Map();
352
353 for (var i = 0; i < decInfos.length; i++) {
354 var decInfo = decInfos[i];
355
356 // skip computed property names
357 if (!Array.isArray(decInfo)) continue;
358
359 var kind = decInfo[1];
360 var name = decInfo[2];
361 var isPrivate = decInfo.length > 3;
362
363 var isStatic = kind >= 5; /* STATIC */
364 var base;
365 var initializers;
366
367 if (isStatic) {
368 base = Class;
369 kind = kind - 5 /* STATIC */;
370 // initialize staticInitializers when we see a non-field static member
371 if (kind !== 0 /* FIELD */) {
372 staticInitializers = staticInitializers || [];
373 initializers = staticInitializers;
374 }
375 } else {
376 base = Class.prototype;
377 // initialize protoInitializers when we see a non-field member
378 if (kind !== 0 /* FIELD */) {
379 protoInitializers = protoInitializers || [];
380 initializers = protoInitializers;
381 }
382 }
383
384 if (kind !== 0 /* FIELD */ && !isPrivate) {
385 var existingNonFields = isStatic
386 ? existingStaticNonFields
387 : existingProtoNonFields;
388
389 var existingKind = existingNonFields.get(name) || 0;
390
391 if (
392 existingKind === true ||
393 (existingKind === 3 /* GETTER */ && kind !== 4) /* SETTER */ ||
394 (existingKind === 4 /* SETTER */ && kind !== 3) /* GETTER */
395 ) {
396 throw new Error(
397 "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: " +
398 name
399 );
400 } else if (!existingKind && kind > 2 /* METHOD */) {
401 existingNonFields.set(name, kind);
402 } else {
403 existingNonFields.set(name, true);
404 }
405 }
406
407 applyMemberDec(
408 ret,
409 base,
410 decInfo,
411 name,
412 kind,
413 isStatic,
414 isPrivate,
415 initializers
416 );
417 }
418
419 pushInitializers(ret, protoInitializers);
420 pushInitializers(ret, staticInitializers);
421 return ret;
422 }
423
424 function pushInitializers(ret, initializers) {
425 if (initializers) {
426 ret.push(function (instance) {
427 for (var i = 0; i < initializers.length; i++) {
428 initializers[i].call(instance);
429 }
430 return instance;
431 });
432 }
433 }
434
435 function applyClassDecs(targetClass, classDecs) {
436 if (classDecs.length > 0) {
437 var initializers = [];
438 var newClass = targetClass;
439 var name = targetClass.name;
440
441 for (var i = classDecs.length - 1; i >= 0; i--) {
442 var decoratorFinishedRef = { v: false };
443
444 try {
445 var nextNewClass = classDecs[i](newClass, {
446 kind: "class",
447 name: name,
448 addInitializer: createAddInitializerMethod(
449 initializers,
450 decoratorFinishedRef
451 ),
452 });
453 } finally {
454 decoratorFinishedRef.v = true;
455 }
456
457 if (nextNewClass !== undefined) {
458 assertValidReturnValue(10 /* CLASS */, nextNewClass);
459 newClass = nextNewClass;
460 }
461 }
462
463 return [
464 newClass,
465 function () {
466 for (var i = 0; i < initializers.length; i++) {
467 initializers[i].call(newClass);
468 }
469 },
470 ];
471 }
472 // The transformer will not emit assignment when there are no class decorators,
473 // so we don't have to return an empty array here.
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 export default function applyDecs2203R(targetClass, memberDecs, classDecs) {
622 return {
623 e: applyMemberDecs(targetClass, memberDecs),
624 // Lazily apply class decorations so that member init locals can be properly bound.
625 get c() {
626 return applyClassDecs(targetClass, classDecs);
627 },
628 };
629 }