-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.test.js
More file actions
2651 lines (2396 loc) · 66.5 KB
/
Copy pathtransform.test.js
File metadata and controls
2651 lines (2396 loc) · 66.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-env jest */
const { prepareCode } = require('./transform.js');
const cleanString = (x) =>
x.replace(/\s+/g, ' ');
const testTransform = (name, before, after) =>
test('transform: ' + name, () => {
expect(cleanString(prepareCode(before)))
.toBe(cleanString(after));
});
const testParserError = (name, code, expectedErrorPattern) =>
test('parser error: ' + name, () => {
expect(() => prepareCode(code)).toThrow(expectedErrorPattern);
});
// ## variable declarations
testTransform('const to var', 'const x = 1;', 'var x = 1;');
testTransform('let to var', 'let x = 1;', 'var x = 1;');
testTransform('var assignment to await to two statements', 'var x = await Promise.resolve(1);', 'var x; x = await Promise.resolve(1);');
testTransform('let assignment to await to two statements', 'let x = await Promise.resolve(2);', 'var x; x = await Promise.resolve(2);');
testTransform('const assignment to await to two statements', 'const x = await Promise.resolve(3);', 'var x; x = await Promise.resolve(3);');
testTransform(
'multiple variable declarations to var',
'const x = 1, y = 2, z = 3;',
`var x = 1;
var y = 2;
var z = 3;`);
// ## function declarations
testTransform('add a layer of indirection to an async function',
'const f = async() => { await Promise.resolve(4); }',
`var f_hakk_ = async () => { await Promise.resolve(4); };
var f = (...args) => f_hakk_(...args);`);
testTransform('add a layer of indirection to an async function with a variable assignment',
'const f = async() => { var y = await Promise.resolve(4); }',
`var f_hakk_ = async () => { var y = await Promise.resolve(4); };
var f = (...args) => f_hakk_(...args);`);
// ## class declarations and expressions
testTransform('class declaration to class expression',
'class A {}', 'var A = class A {};');
testTransform(
'class method declaration to class expression',
`class A {
method1(a, b) {
return a + b;
}
}`,
`var A = class A {};
A.prototype.method1 = function (a, b) {
return a + b;
};`);
testTransform(
'class field declaration to class expression',
`class A {
field1 = 3;
}`,
`var A = class A {};
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(A.prototype, "field1", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})(3);`);
testTransform(
'class private method declaration to class expression',
`class A {
#method1(a, b) {
return a + b;
}
}`,
`var A = class A {};
A.prototype._PRIVATE_method1 = function (a, b) {
return a + b;
};`);
testTransform(
'class private field declaration to class expression',
`class A {
#field1 = 7;
}`,
`var A = class A {};
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(A.prototype, "_PRIVATE_field1", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})(7);`);
testTransform(
'class extends declaration to class expression',
`class A extends B {
constructor(b, a) {
super(a, b);
}
method1(x) {
return x;
}
}`,
`var A = class A extends B {
constructor(b, a) {
super(a, b);
}
};
A.prototype.method1 = function (x) {
return x;
};`);
testTransform(
'static block to IIFE call',
`class Counter {
static count = 0;
static {
this.count = 42;
}
}`,
`var Counter = class Counter {};
Counter.count = 0;
(function () {
this.count = 42;
}).call(Counter);`);
testTransform(
'ensure static fields and static blocks are evaluated in the correct order',
`class Config {
static a = 1;
static b = 2;
static c = 3;
static {
this.a = 4;
this.b = 5;
this.c = 6;
}
static d = 7;
}`,
`var Config = class Config {};
Config.a = 1;
Config.b = 2;
Config.c = 3;
(function () {
this.a = 4;
this.b = 5;
this.c = 6;
}).call(Config);
Config.d = 7;`);
testTransform(
'static method declaration to class expression',
`class Utils {
static helper() {
return 'help';
}
}`,
`var Utils = class Utils {};
Utils.helper = function () {
return 'help';
};`);
testTransform(
'static field declaration to class expression',
`class Config {
static version = '1.0.0';
}`,
`var Config = class Config {};
Config.version = '1.0.0';`);
testTransform(
'static getter declaration to class expression',
`class Data {
static get info() {
return { name: 'test' };
}
}`,
`var Data = class Data {};
Object.defineProperty(Data, "info", {
get: function () {
return { name: 'test' };
},
configurable: true
});`);
testTransform(
'static setter declaration to class expression',
`class Data {
static set value(v) {
this._value = v;
}
}`,
`var Data = class Data {};
Object.defineProperty(Data, "value", {
set: function (v) {
this._value = v;
},
configurable: true
});`);
testTransform(
'class with mixed static and instance members',
`class Example {
static staticField = 'static';
instanceField = 'instance';
static staticMethod() {
return 'static method';
}
instanceMethod() {
return 'instance method';
}
}`,
`var Example = class Example {};
Example.staticField = 'static';
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(Example.prototype, "instanceField", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})('instance');
Example.staticMethod = function () {
return 'static method';
};
Example.prototype.instanceMethod = function () {
return 'instance method';
};`);
testTransform(
'class with private static field',
`class Data {
static #secret = 'hidden';
}`,
`var Data = class Data {};
Data._PRIVATE_secret = 'hidden';`);
testTransform(
'class with private static method',
`class Utils {
static #helper() {
return 'private help';
}
}`,
`var Utils = class Utils {};
Utils._PRIVATE_helper = function () {
return 'private help';
};`);
testTransform(
'class with private static getter',
`class Config {
static get #value() {
return this._internal;
}
}`,
`var Config = class Config {};
Object.defineProperty(Config, "_PRIVATE_value", {
get: function () {
return this._internal;
},
configurable: true
});`);
testTransform(
'class with private static setter',
`class Config {
static set #value(v) {
this._internal = v;
}
}`,
`var Config = class Config {};
Object.defineProperty(Config, "_PRIVATE_value", {
set: function (v) {
this._internal = v;
},
configurable: true
});`);
testTransform(
'class with mixed private static and instance members',
`class Example {
static #staticPrivate = 'static private';
#instancePrivate = 'instance private';
static #staticPrivateMethod() {
return 'static private method';
}
#instancePrivateMethod() {
return 'instance private method';
}
}`,
`var Example = class Example {};
Example._PRIVATE_staticPrivate = 'static private';
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(Example.prototype, "_PRIVATE_instancePrivate", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})('instance private');
Example._PRIVATE_staticPrivateMethod = function () {
return 'static private method';
};
Example.prototype._PRIVATE_instancePrivateMethod = function () {
return 'instance private method';
};`);
testTransform(
'class with computed property names',
`const key = 'dynamic';
class Example {
[key] = 'value';
['static'] = 'static value';
[key]() {
return 'method';
}
}`,
`var key = 'dynamic';
var Example = class Example {};
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(Example.prototype, key, {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})('value');
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(Example.prototype, 'static', {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})('static value');
Example.prototype[key] = function () {
return 'method';
};`);
testTransform(
'class with computed static property names',
`const methodName = 'helper';
class Utils {
static [methodName]() {
return 'help';
}
static ['version'] = '1.0.0';
}`,
`var methodName = 'helper';
var Utils = class Utils {};
Utils[methodName] = function () {
return 'help';
};
Utils['version'] = '1.0.0';`);
// ## super keyword handling
testTransform(
'super method call in instance method',
`class Child extends Parent {
method() {
return super.parentMethod('arg');
}
}`,
`var Child = class Child extends Parent {};
Child.prototype.method = function () {
return Parent.prototype.parentMethod.call(this, 'arg');
};`);
testTransform(
'super method call in static method',
`class Child extends Parent {
static staticMethod() {
return super.parentStaticMethod('arg');
}
}`,
`var Child = class Child extends Parent {};
Child.staticMethod = function () {
return Parent.parentStaticMethod.call('arg');
};`);
testTransform(
'super property access in static method',
`class Child extends Parent {
static getValue() {
return super.staticProperty;
}
}`,
`var Child = class Child extends Parent {};
Child.getValue = function () {
return Parent.staticProperty;
};`);
testTransform(
'super property access in instance method (returns undefined for class fields)',
`class Child extends Parent {
getValue() {
return super.instanceProperty;
}
}`,
`var Child = class Child extends Parent {};
Child.prototype.getValue = function () {
return Parent.prototype.instanceProperty;
};`);
testTransform(
'super method call vs super property access (different behaviors)',
`class Child extends Parent {
testSuper() {
return {
method: super.parentMethod(),
property: super.instanceProperty
};
}
}`,
`var Child = class Child extends Parent {};
Child.prototype.testSuper = function () {
return {
method: Parent.prototype.parentMethod.call(this),
property: Parent.prototype.instanceProperty
};
};`);
testTransform(
'super method call with multiple arguments',
`class Child extends Parent {
method(a, b, c) {
return super.parentMethod(a, b, c);
}
}`,
`var Child = class Child extends Parent {};
Child.prototype.method = function (a, b, c) {
return Parent.prototype.parentMethod.call(this, a, b, c);
};`);
testTransform(
'super method call with no arguments',
`class Child extends Parent {
method() {
return super.parentMethod();
}
}`,
`var Child = class Child extends Parent {};
Child.prototype.method = function () {
return Parent.prototype.parentMethod.call(this);
};`);
testTransform(
'super method call in async method',
`class Child extends Parent {
async method() {
return await super.parentMethod();
}
}`,
`var Child = class Child extends Parent {};
Child.prototype.method = async function () {
return await Parent.prototype.parentMethod.call(this);
};`);
testTransform(
'super method call in generator method',
`class Child extends Parent {
*method() {
yield super.parentMethod();
}
}`,
`var Child = class Child extends Parent {};
Child.prototype.method = function* () {
yield Parent.prototype.parentMethod.call(this);
};`);
testTransform(
'super method call in getter',
`class Child extends Parent {
get value() {
return super.parentGetter();
}
}`,
`var Child = class Child extends Parent {};
Object.defineProperty(Child.prototype, "value", {
get: function () {
return Parent.prototype.parentGetter.call(this);
},
configurable: true
});`);
testTransform(
'super method call in setter',
`class Child extends Parent {
set value(v) {
super.parentSetter(v);
}
}`,
`var Child = class Child extends Parent {};
Object.defineProperty(Child.prototype, "value", {
set: function (v) {
Parent.prototype.parentSetter.call(this, v);
},
configurable: true
});`);
testTransform(
'super method call in private method',
`class Child extends Parent {
#privateMethod() {
return super.parentMethod();
}
}`,
`var Child = class Child extends Parent {};
Child.prototype._PRIVATE_privateMethod = function () {
return Parent.prototype.parentMethod.call(this);
};`);
testTransform(
'super method call in private static method',
`class Child extends Parent {
static #privateStaticMethod() {
return super.parentStaticMethod();
}
}`,
`var Child = class Child extends Parent {};
Child._PRIVATE_privateStaticMethod = function () {
return Parent.parentStaticMethod.call();
};`);
// ## object literals and methods
testTransform(
'object method declaration to property assignment',
`const obj = {
method() {
return 'hello';
}
};`,
`var obj = {};
obj.method = function () {
return 'hello';
};`);
testTransform(
'object getter declaration to Object.defineProperty',
`const obj = {
get value() {
return this._value;
}
};`,
`var obj = {};
Object.defineProperty(obj, "value", {
get: function () {
return this._value;
},
configurable: true
});`);
testTransform(
'object setter declaration to Object.defineProperty',
`const obj = {
set value(v) {
this._value = v;
}
};`,
`var obj = {};
Object.defineProperty(obj, "value", {
set: function (v) {
this._value = v;
},
configurable: true
});`);
testTransform(
'object with computed property names',
`const key = 'dynamic';
const obj = {
[key]: 'value',
['static']: 'static value'
};`,
`var key = 'dynamic';
var obj = {};
obj[key] = 'value';
obj['static'] = 'static value';`);
testTransform(
'object with shorthand property names',
`const x = 1, y = 2;
const obj = { x, y };`,
`var x = 1;
var y = 2;
var obj = {};
obj.x = x;
obj.y = y;`);
testTransform(
'object with mixed property types',
`const name = 'test';
const obj = {
regular: 'value',
[name]: 'computed',
method() {
return 'method';
},
get getter() {
return this._value;
},
set setter(v) {
this._value = v;
}
};`,
`var name = 'test';
var obj = {};
obj.regular = 'value';
obj[name] = 'computed';
obj.method = function () {
return 'method';
};
Object.defineProperty(obj, "getter", {
get: function () {
return this._value;
},
configurable: true
});
Object.defineProperty(obj, "setter", {
set: function (v) {
this._value = v;
},
configurable: true
});`);
// ## function types
testTransform(
'generator function declaration to function expression',
`function* generator() {
yield 1;
yield 2;
}`,
`var generator_hakk_ = function* generator() {
yield 1;
yield 2;
};
var generator = (...args) => generator_hakk_(...args);`);
testTransform(
'async generator function declaration to function expression',
`async function* asyncGenerator() {
yield await Promise.resolve(1);
}`,
`var asyncGenerator_hakk_ = async function* asyncGenerator() {
yield await Promise.resolve(1);
};
var asyncGenerator = (...args) => asyncGenerator_hakk_(...args);`);
testTransform(
'arrow function with different parameter patterns',
`const noParams = () => 'hello';
const singleParam = x => x * 2;
const multipleParams = (a, b) => a + b;
const restParams = (...args) => args.length;`,
`var noParams_hakk_ = () => 'hello';
var noParams = (...args) => noParams_hakk_(...args);
var singleParam_hakk_ = x => x * 2;
var singleParam = (...args) => singleParam_hakk_(...args);
var multipleParams_hakk_ = (a, b) => a + b;
var multipleParams = (...args) => multipleParams_hakk_(...args);
var restParams_hakk_ = (...args) => args.length;
var restParams = (...args) => restParams_hakk_(...args);`);
testTransform(
'function expression with name to wrapper',
`const namedFunc = function myFunction() {
return 'named';
};`,
`var namedFunc_hakk_ = function myFunction() {
return 'named';
};
var namedFunc = (...args) => namedFunc_hakk_(...args);`);
testTransform(
'async arrow function to wrapper',
`const asyncArrow = async () => {
return await Promise.resolve('async');
};`,
`var asyncArrow_hakk_ = async () => {
return await Promise.resolve('async');
};
var asyncArrow = (...args) => asyncArrow_hakk_(...args);`);
testTransform(
'generator arrow function to wrapper',
`const genArrow = function* () {
yield 1;
};`,
`var genArrow_hakk_ = function* () {
yield 1;
};
var genArrow = (...args) => genArrow_hakk_(...args);`);
// ## Private Field Access
testTransform(
'private field access in instance method',
`class A {
#field = 42;
getValue() {
return this.#field;
}
}`,
`var A = class A {};
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(A.prototype, "_PRIVATE_field", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})(42);
A.prototype.getValue = function () {
return this._PRIVATE_field;
};`);
testTransform(
'private field access in static method',
`class A {
static #field = 100;
static getValue() {
return this.#field;
}
}`,
`var A = class A {};
A._PRIVATE_field = 100;
A.getValue = function () {
return this._PRIVATE_field;
};`);
testTransform(
'private field access with assignment',
`class A {
#field = 0;
increment() {
this.#field++;
}
}`,
`var A = class A {};
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(A.prototype, "_PRIVATE_field", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})(0);
A.prototype.increment = function () {
this._PRIVATE_field++;
};`);
testTransform(
'private field access in getter',
`class A {
#field = 'secret';
get secret() {
return this.#field;
}
}`,
`var A = class A {};
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(A.prototype, "_PRIVATE_field", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})('secret');
Object.defineProperty(A.prototype, "secret", {
get: function () {
return this._PRIVATE_field;
},
configurable: true
});`);
testTransform(
'private field access in setter',
`class A {
#field = null;
set value(val) {
this.#field = val;
}
}`,
`var A = class A {};
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(A.prototype, "_PRIVATE_field", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})(null);
Object.defineProperty(A.prototype, "value", {
set: function (val) {
this._PRIVATE_field = val;
},
configurable: true
});`);
testTransform(
'private field access in constructor',
`class A {
#field = 10;
constructor(value) {
this.#field = value;
}
}`,
`var A = class A {
constructor(value) {
this._PRIVATE_field = value;
}
};
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(A.prototype, "_PRIVATE_field", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})(10);`);
testTransform(
'private field access with complex expressions',
`class A {
#field = [];
addItem(item) {
this.#field.push(item);
}
get length() {
return this.#field.length;
}
}`,
`var A = class A {};
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(A.prototype, "_PRIVATE_field", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})([]);
A.prototype.addItem = function (item) {
this._PRIVATE_field.push(item);
};
Object.defineProperty(A.prototype, "length", {
get: function () {
return this._PRIVATE_field.length;
},
configurable: true
});`);
testTransform(
'private field access in static block',
`class A {
static #field = 0;
static {
this.#field = 42;
}
}`,
`var A = class A {};
A._PRIVATE_field = 0;
(function () {
this._PRIVATE_field = 42;
}).call(A);`);
testTransform(
'private field access with multiple private fields',
`class A {
#field1 = 1;
#field2 = 2;
getSum() {
return this.#field1 + this.#field2;
}
}`,
`var A = class A {};
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(A.prototype, "_PRIVATE_field1", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})(1);
(function (initValue) {
const valueMap = new WeakMap();
Object.defineProperty(A.prototype, "_PRIVATE_field2", {
get() { return valueMap.has(this) ? valueMap.get(this) : initValue; },
set(newValue) { valueMap.set(this, newValue); },
configurable: true
});
})(2);
A.prototype.getSum = function () {
return this._PRIVATE_field1 + this._PRIVATE_field2;
};`);
// ## Object Literal Edge Cases
testTransform(
'nested object with methods (nested objects not transformed)',
`const config = {
database: {
host: 'localhost',
connect() {
return this.host;
}
}
};`,
`var config = {};
config.database = {
host: 'localhost',
connect() {
return this.host;
}
};`);
testTransform(
'object with method referencing this',
`const counter = {
value: 0,
increment() {
this.value++;
return this.value;
},
reset() {
this.value = 0;
}
};`,
`var counter = {};
counter.value = 0;
counter.increment = function () {
this.value++;
return this.value;
};
counter.reset = function () {
this.value = 0;
};`);
testTransform(
'object with complex computed properties (now supported)',
`const key1 = 'prop1';
const key2 = 'prop2';
const obj = {
[key1 + key2]: 'combined',
[key1.toUpperCase()]: 'uppercase',
['static_' + key2]: 'static_combined'
};`,
`var key1 = 'prop1';
var key2 = 'prop2';
var obj = {};
obj[key1 + key2] = 'combined';
obj[key1.toUpperCase()] = 'uppercase';
obj['static_' + key2] = 'static_combined';`);
testTransform(
'object with mixed property types and methods (Symbol.iterator now supported)',
`const api = {
baseUrl: 'https://api.example.com',
version: 1,
get endpoints() {
return this._endpoints || [];
},
set endpoints(value) {
this._endpoints = value;
},
async fetch(path) {
return fetch(this.baseUrl + path);
}
};`,
`var api = {};
api.baseUrl = 'https://api.example.com';
api.version = 1;
Object.defineProperty(api, "endpoints", {
get: function () {
return this._endpoints || [];
},
configurable: true
});
Object.defineProperty(api, "endpoints", {
set: function (value) {
this._endpoints = value;
},