Debian Codegraph @

packages/babel-traverse/test/arrow-transform.js

raw symbols SCIP
1 import { NodePath } from "../lib/index.js";
2 import { parse } from "@babel/parser";
3 import * as t from "@babel/types";
4
5 import _generate from "@babel/generator";
6 const generate = _generate.default || _generate;
7
8 function assertConversion(
9 input,
10 output,
11 { methodName = "method", extend = false, arrowOpts } = {},
12 ) {
13 const inputAst = wrapMethod(input, methodName, extend);
14 const outputAst = wrapMethod(output, methodName, extend);
15
16 const rootPath = NodePath.get({
17 hub: {
18 addHelper(helperName) {
19 return t.memberExpression(
20 t.identifier("babelHelpers"),
21 t.identifier(helperName),
22 );
23 },
24 },
25 parentPath: null,
26 parent: inputAst,
27 container: inputAst,
28 key: "program",
29 }).setContext();
30
31 rootPath.traverse({
32 ClassMethod(path) {
33 path.get("body.body.0.expression").arrowFunctionToExpression(arrowOpts);
34 },
35 });
36
37 expect(generate(inputAst).code).toBe(generate(outputAst).code);
38 }
39
40 function wrapMethod(body, methodName, extend) {
41 return parse(
42 `
43 class Example ${extend ? "extends class {}" : ""} {
44 ${methodName}() {${body} }
45 }
46 `,
47 { plugins: ["jsx"] },
48 );
49 }
50
51 describe("arrow function conversion", () => {
52 it("should convert super calls in constructors", () => {
53 assertConversion(
54 `
55 () => {
56 super(345);
57 };
58 super();
59 () => super();
60 `,
61 `
62 var _supercall = (..._args) => super(..._args);
63
64 (function () {
65 _supercall(345);
66 });
67 _supercall();
68 () => _supercall();
69 `,
70 { methodName: "constructor", extend: true },
71 );
72 });
73
74 it("should convert super calls in constructors used in computed method keys", () => {
75 assertConversion(
76 `
77 () => ({
78 [super()]: 123,
79 get [super() + "1"]() {},
80 [super()]() {},
81 });
82 `,
83 `
84 var _supercall = (..._args) => super(..._args);
85
86 (function () {
87 return {
88 [_supercall()]: 123,
89 get [_supercall() + "1"]() {},
90 [_supercall()]() {},
91 };
92 });
93 `,
94 { methodName: "constructor", extend: true },
95 );
96 });
97
98 it("should convert super calls and this references in constructors", () => {
99 assertConversion(
100 `
101 () => {
102 super(345);
103 this;
104 };
105 super();
106 this;
107 () => super();
108 () => this;
109 `,
110 `
111 var _supercall = (..._args) => (super(..._args), _this = this),
112 _this;
113
114 (function () {
115 _supercall(345);
116 _this;
117 });
118 _supercall();
119 this;
120 () => _supercall();
121 () => this;
122 `,
123 { methodName: "constructor", extend: true },
124 );
125 });
126
127 it("should convert this references in constructors", () => {
128 assertConversion(
129 `
130 () => {
131 this;
132 };
133 super();
134 this;
135 () => super();
136 () => this;
137 `,
138 `
139 var _this;
140
141 (function () {
142 _this;
143 });
144 super();
145 _this = this;
146 this;
147 () => (super(), _this = this);
148 () => this;
149 `,
150 { methodName: "constructor", extend: true },
151 );
152 });
153
154 it("should convert this references in constructors with super() used in computed method keys", () => {
155 assertConversion(
156 `
157 () => this;
158
159 ({
160 [super()]: 123,
161 get [super() + "1"]() {},
162 [super()]() {},
163 });
164 `,
165 `
166 var _this;
167
168 (function () { return _this; });
169
170 ({
171 [(super(), _this = this)]: 123,
172 get [(super(), _this = this) + "1"]() {},
173 [(super(), _this = this)]() {},
174 });
175 `,
176 { methodName: "constructor", extend: true },
177 );
178 });
179
180 it("should convert this references in constructors with spec compliance", () => {
181 assertConversion(
182 `
183 () => {
184 this;
185 };
186 super();
187 this;
188 () => super();
189 () => this;
190 `,
191 `
192 var _this,
193 _arrowCheckId = {};
194
195 (function () {
196 babelHelpers.newArrowCheck(this, _arrowCheckId);
197
198 _this;
199 }).bind(_arrowCheckId);
200 super();
201 _this = this;
202 this;
203 () => (super(), _this = this);
204 () => this;
205 `,
206 {
207 methodName: "constructor",
208 extend: true,
209 arrowOpts: { specCompliant: true },
210 },
211 );
212 });
213
214 it("should convert this references in constructors without extension", () => {
215 assertConversion(
216 `
217 () => {
218 this;
219 };
220 this;
221 () => this;
222 `,
223 `
224 var _this = this;
225
226 (function () {
227 _this;
228 });
229 this;
230 () => this;
231 `,
232 { methodName: "constructor" },
233 );
234 });
235
236 it("should convert this references in constructors with spec compliance without extension", () => {
237 assertConversion(
238 `
239 () => {
240 this;
241 };
242 this;
243 () => this;
244 `,
245 `
246 var _this = this;
247
248 (function () {
249 babelHelpers.newArrowCheck(this, _this);
250
251 this;
252 }).bind(this);
253 this;
254 () => this;
255 `,
256 { methodName: "constructor", arrowOpts: { specCompliant: true } },
257 );
258 });
259
260 it("should convert this references in methods", () => {
261 assertConversion(
262 `
263 () => {
264 this;
265 };
266 this;
267 () => this;
268 `,
269 `
270 var _this = this;
271
272 (function () {
273 _this;
274 });
275 this;
276 () => this;
277 `,
278 );
279 });
280
281 it("should convert this references in methods with spec compliance", () => {
282 assertConversion(
283 `
284 () => {
285 this;
286 };
287 this;
288 () => this;
289 `,
290 `
291 var _this = this;
292
293 (function () {
294 babelHelpers.newArrowCheck(this, _this);
295
296 this;
297 }).bind(this);
298 this;
299 () => this;
300 `,
301 { arrowOpts: { specCompliant: true } },
302 );
303 });
304
305 it("should convert this references inside JSX in methods", () => {
306 assertConversion(
307 `
308 () => {
309 <this.this this="" />;
310 };
311 <this.this this="" />;
312 () => <this.this this="" />;
313 `,
314 `
315 var _this = this;
316
317 (function () {
318 <_this.this this="" />;
319 });
320 <this.this this="" />;
321 () => <this.this this="" />;
322 `,
323 );
324 });
325
326 it("should convert this references used in computed method keys", () => {
327 assertConversion(
328 `
329 () => ({
330 [this]: 123,
331 get [this + "1"]() {},
332 [this]() {},
333 });
334 `,
335 `
336 var _this = this;
337
338 (function () {
339 return {
340 [_this]: 123,
341 get [_this + "1"]() {},
342 [_this]() {},
343 };
344 });
345 `,
346 );
347 });
348
349 it("should convert arguments references", () => {
350 assertConversion(
351 `
352 () => {
353 arguments;
354 };
355 arguments;
356 () => arguments;
357 `,
358 `
359 var _arguments = arguments;
360
361 (function () {
362 _arguments;
363 });
364 arguments;
365 () => arguments;
366 `,
367 );
368 });
369
370 it("should convert new.target references", () => {
371 assertConversion(
372 `
373 () => {
374 new.target;
375 };
376 new.target;
377 () => new.target;
378 `,
379 `
380 var _newtarget = new.target;
381
382 (function () {
383 _newtarget;
384 });
385 new.target;
386 () => new.target;
387 `,
388 );
389 });
390
391 it("should convert super.prop references", () => {
392 assertConversion(
393 `
394 () => {
395 var tmp = super.foo;
396 };
397 super.foo;
398 () => super.foo;
399 `,
400 `
401 var _superprop_getFoo = () => super.foo;
402
403 (function () {
404 var tmp = _superprop_getFoo();
405 });
406 super.foo;
407 () => super.foo;
408 `,
409 );
410 });
411
412 it("should convert super[prop] references", () => {
413 assertConversion(
414 `
415 () => {
416 var tmp = super[foo];
417 };
418 super[foo];
419 () => super[foo];
420 `,
421 `
422 var _superprop_get = _prop => super[_prop];
423
424 (function () {
425 var tmp = _superprop_get(foo);
426 });
427 super[foo];
428 () => super[foo];
429 `,
430 );
431 });
432
433 it("should convert super.prop assignment", () => {
434 assertConversion(
435 `
436 () => {
437 super.foo = 4;
438 };
439 super.foo = 4;
440 () => super.foo = 4;
441 `,
442 `
443 var _superprop_setFoo = _value => super.foo = _value;
444
445 (function () {
446 _superprop_setFoo(4);
447 });
448 super.foo = 4;
449 () => super.foo = 4;
450 `,
451 );
452 });
453
454 it("should convert super[prop] assignment", () => {
455 assertConversion(
456 `
457 () => {
458 super[foo] = 4;
459 };
460 super[foo] = 4;
461 () => super[foo] = 4;
462 `,
463 `
464 var _superprop_set = (_prop, _value) => super[_prop] = _value;
465
466 (function () {
467 _superprop_set(foo, 4);
468 });
469 super[foo] = 4;
470 () => super[foo] = 4;
471 `,
472 );
473 });
474
475 it("should convert super.prop operator assign", () => {
476 assertConversion(
477 `
478 () => {
479 super.foo **= 4;
480 };
481 super.foo **= 4;
482 () => super.foo **= 4;
483 `,
484 `
485 var _superprop_setFoo = _value => super.foo = _value,
486 _superprop_getFoo = () => super.foo;
487
488 (function () {
489 _superprop_setFoo(_superprop_getFoo() ** 4);
490 });
491 super.foo **= 4;
492 () => super.foo **= 4;
493 `,
494 );
495 });
496
497 it("should convert super[prop] operator assign", () => {
498 assertConversion(
499 `
500 () => {
501 super[foo] **= 4;
502 };
503 super[foo] **= 4;
504 () => super[foo] **= 4;
505 `,
506 `
507 var _superprop_set = (_prop, _value) => super[_prop] = _value,
508 _superprop_get = _prop2 => super[_prop2];
509
510 (function () {
511 var _tmp;
512
513 _superprop_set(_tmp = foo, _superprop_get(_tmp) ** 4);
514 });
515 super[foo] **= 4;
516 () => super[foo] **= 4;
517 `,
518 );
519 });
520
521 it("should convert super.prop operator logical assign `??=`", () => {
522 assertConversion(
523 `
524 () => {
525 super.foo ??= 4;
526 };
527 super.foo ??= 4;
528 () => super.foo ??= 4;
529 `,
530 `
531 var _superprop_getFoo = () => super.foo,
532 _superprop_setFoo = _value => super.foo = _value;
533
534 (function () {
535 _superprop_getFoo() ?? _superprop_setFoo(4);
536 });
537
538 super.foo ??= 4;
539
540 () => super.foo ??= 4;
541 `,
542 );
543 });
544
545 it("should convert super.prop operator logical assign `&&=`", () => {
546 assertConversion(
547 `
548 () => {
549 super.foo &&= true;
550 };
551 super.foo &&= true;
552 () => super.foo &&= true;
553 `,
554 `
555 var _superprop_getFoo = () => super.foo,
556 _superprop_setFoo = _value => super.foo = _value;
557
558 (function () {
559 _superprop_getFoo() && _superprop_setFoo(true);
560 });
561
562 super.foo &&= true;
563
564 () => super.foo &&= true;
565 `,
566 );
567 });
568
569 it("should convert super.prop operator logical assign `||=`", () => {
570 assertConversion(
571 `
572 () => {
573 super.foo ||= true;
574 };
575 super.foo ||= true;
576 () => super.foo ||= true;
577 `,
578 `
579 var _superprop_getFoo = () => super.foo,
580 _superprop_setFoo = _value => super.foo = _value;
581
582 (function () {
583 _superprop_getFoo() || _superprop_setFoo(true);
584 });
585
586 super.foo ||= true;
587
588 () => super.foo ||= true;
589 `,
590 );
591 });
592
593 it("should convert super[prop] operator logical assign `??=`", () => {
594 assertConversion(
595 `
596 () => {
597 super[foo] ??= 4;
598 };
599 super[foo] ??= 4;
600 () => super[foo] ??= 4;
601 `,
602 `
603 var _superprop_get = _prop => super[_prop],
604 _superprop_set = (_prop2, _value) => super[_prop2] = _value;
605
606 (function () {
607 var _tmp;
608
609 _superprop_get(_tmp = foo) ?? _superprop_set(_tmp, 4);
610 });
611
612 super[foo] ??= 4;
613
614 () => super[foo] ??= 4;
615 `,
616 );
617 });
618
619 it("should convert super[prop] operator logical assign `&&=`", () => {
620 assertConversion(
621 `
622 () => {
623 super[foo] &&= true;
624 };
625 super[foo] &&= true;
626 () => super[foo] &&= true;
627 `,
628 `
629 var _superprop_get = _prop => super[_prop],
630 _superprop_set = (_prop2, _value) => super[_prop2] = _value;
631
632 (function () {
633 var _tmp;
634
635 _superprop_get(_tmp = foo) && _superprop_set(_tmp, true);
636 });
637
638 super[foo] &&= true;
639
640 () => super[foo] &&= true;
641 `,
642 );
643 });
644
645 it("should convert super[prop] operator logical assign `||=`", () => {
646 assertConversion(
647 `
648 () => {
649 super[foo] ||= true;
650 };
651 super[foo] ||= true;
652 () => super[foo] ||= true;
653 `,
654 `
655 var _superprop_get = _prop => super[_prop],
656 _superprop_set = (_prop2, _value) => super[_prop2] = _value;
657
658 (function () {
659 var _tmp;
660
661 _superprop_get(_tmp = foo) || _superprop_set(_tmp, true);
662 });
663
664 super[foo] ||= true;
665
666 () => super[foo] ||= true;
667 `,
668 );
669 });
670
671 it("should convert `++super.prop` prefix update", () => {
672 assertConversion(
673 `
674 () => {
675 ++super.foo;
676 };
677 ++super.foo;
678 () => ++super.foo;
679 `,
680 `
681 var _superprop_getFoo = () => super.foo,
682 _superprop_setFoo = _value => super.foo = _value;
683
684 (function () {
685 var _tmp;
686
687 _tmp = _superprop_getFoo(), _superprop_setFoo(_tmp + 1);
688 });
689 ++super.foo;
690 () => ++super.foo;
691 `,
692 );
693 });
694
695 it("should convert `--super.prop` prefix update", () => {
696 assertConversion(
697 `
698 () => {
699 --super.foo;
700 };
701 --super.foo;
702 () => --super.foo;
703 `,
704 `
705 var _superprop_getFoo = () => super.foo,
706 _superprop_setFoo = _value => super.foo = _value;
707
708 (function () {
709 var _tmp;
710
711 _tmp = _superprop_getFoo(), _superprop_setFoo(_tmp - 1);
712 });
713 --super.foo;
714 () => --super.foo;
715 `,
716 );
717 });
718
719 it("should convert `++super[prop]` prefix update", () => {
720 assertConversion(
721 `
722 () => {
723 ++super[foo];
724 };
725 ++super[foo];
726 () => ++super[foo];
727 `,
728 `
729 var _superprop_get = _prop2 => super[_prop2],
730 _superprop_set = (_prop3, _value) => super[_prop3] = _value;
731
732 (function () {
733 var _tmp, _prop;
734
735 _tmp = _superprop_get(_prop = foo), _superprop_set(_prop, _tmp + 1);
736 });
737 ++super[foo];
738 () => ++super[foo];
739 `,
740 );
741 });
742
743 it("should convert `--super[prop]` prefix update", () => {
744 assertConversion(
745 `
746 () => {
747 --super[foo];
748 };
749 --super[foo];
750 () => --super[foo];
751 `,
752 `
753 var _superprop_get = _prop2 => super[_prop2],
754 _superprop_set = (_prop3, _value) => super[_prop3] = _value;
755
756 (function () {
757 var _tmp, _prop;
758
759 _tmp = _superprop_get(_prop = foo), _superprop_set(_prop, _tmp - 1);
760 });
761 --super[foo];
762 () => --super[foo];
763 `,
764 );
765 });
766
767 it("should convert `super.prop++` suffix update", () => {
768 assertConversion(
769 `
770 () => {
771 super.foo++;
772 };
773 super.foo++;
774 () => super.foo++;
775 `,
776 `
777 var _superprop_getFoo = () => super.foo,
778 _superprop_setFoo = _value => super.foo = _value;
779
780 (function () {
781 var _tmp;
782
783 _tmp = _superprop_getFoo(), _superprop_setFoo(_tmp + 1), _tmp;
784 });
785 super.foo++;
786 () => super.foo++;
787 `,
788 );
789 });
790
791 it("should convert `super.prop--` suffix update", () => {
792 assertConversion(
793 `
794 () => {
795 super.foo--;
796 };
797 super.foo--;
798 () => super.foo--;
799 `,
800 `
801 var _superprop_getFoo = () => super.foo,
802 _superprop_setFoo = _value => super.foo = _value;
803
804 (function () {
805 var _tmp;
806
807 _tmp = _superprop_getFoo(), _superprop_setFoo(_tmp - 1), _tmp;
808 });
809 super.foo--;
810 () => super.foo--;
811 `,
812 );
813 });
814
815 it("should convert `super[prop]++` suffix update", () => {
816 assertConversion(
817 `
818 () => {
819 super[foo]++;
820 };
821 super[foo]++;
822 () => super[foo]++;
823 `,
824 `
825 var _superprop_get = _prop2 => super[_prop2],
826 _superprop_set = (_prop3, _value) => super[_prop3] = _value;
827
828 (function () {
829 var _tmp, _prop;
830
831 _tmp = _superprop_get(_prop = foo), _superprop_set(_prop, _tmp + 1), _tmp;
832 });
833 super[foo]++;
834 () => super[foo]++;
835 `,
836 );
837 });
838
839 it("should convert `super[prop]--` suffix update", () => {
840 assertConversion(
841 `
842 () => {
843 super[foo]--;
844 };
845 super[foo]--;
846 () => super[foo]--;
847 `,
848 `
849 var _superprop_get = _prop2 => super[_prop2],
850 _superprop_set = (_prop3, _value) => super[_prop3] = _value;
851
852 (function () {
853 var _tmp, _prop;
854
855 _tmp = _superprop_get(_prop = foo), _superprop_set(_prop, _tmp - 1), _tmp;
856 });
857 super[foo]--;
858 () => super[foo]--;
859 `,
860 );
861 });
862
863 it("should convert super.prop() calls without params", () => {
864 assertConversion(
865 `
866 () => {
867 super.foo();
868 };
869 super.foo();
870 () => super.foo();
871 `,
872 `
873 var _superprop_getFoo = () => super.foo,
874 _this = this;
875
876 (function () {
877 _superprop_getFoo().call(_this);
878 });
879 super.foo();
880 () => super.foo();
881 `,
882 );
883 });
884
885 it("should convert super.prop() calls with params", () => {
886 assertConversion(
887 `
888 () => {
889 super.foo(a, b, ...c);
890 };
891 super.foo(a, b, ...c);
892 () => super.foo(a, b, ...c);
893 `,
894 `
895 var _superprop_getFoo = () => super.foo,
896 _this = this;
897
898 (function () {
899 _superprop_getFoo().call(_this, a, b, ...c);
900 });
901 super.foo(a, b, ...c);
902 () => super.foo(a, b, ...c);
903 `,
904 );
905 });
906
907 it("should convert super[prop]() calls", () => {
908 assertConversion(
909 `
910 () => {
911 super[foo]();
912 };
913 super[foo]();
914 () => super[foo]();
915 `,
916 `
917 var _superprop_get = _prop => super[_prop],
918 _this = this;
919
920 (function () {
921 _superprop_get(foo).call(_this);
922 });
923 super[foo]();
924 () => super[foo]();
925 `,
926 );
927 });
928 });