-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathts_parser_test.go
1963 lines (1787 loc) · 96.3 KB
/
ts_parser_test.go
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
package js_parser
import (
"testing"
"github.com/evanw/esbuild/internal/compat"
"github.com/evanw/esbuild/internal/config"
)
func expectParseErrorTS(t *testing.T, contents string, expected string) {
t.Helper()
expectParseErrorCommon(t, contents, expected, config.Options{
TS: config.TSOptions{
Parse: true,
},
})
}
func expectParseErrorTargetTS(t *testing.T, esVersion int, contents string, expected string) {
t.Helper()
expectParseErrorCommon(t, contents, expected, config.Options{
TS: config.TSOptions{
Parse: true,
},
UnsupportedJSFeatures: compat.UnsupportedJSFeatures(map[compat.Engine][]int{
compat.ES: {esVersion},
}),
})
}
func expectPrintedTS(t *testing.T, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents, expected, config.Options{
TS: config.TSOptions{
Parse: true,
},
})
}
func expectPrintedMangleTS(t *testing.T, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents, expected, config.Options{
TS: config.TSOptions{
Parse: true,
},
MinifySyntax: true,
})
}
func expectPrintedTargetTS(t *testing.T, esVersion int, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents, expected, config.Options{
TS: config.TSOptions{
Parse: true,
},
UnsupportedJSFeatures: compat.UnsupportedJSFeatures(map[compat.Engine][]int{
compat.ES: {esVersion},
}),
})
}
func expectParseErrorTSNoAmbiguousLessThan(t *testing.T, contents string, expected string) {
t.Helper()
expectParseErrorCommon(t, contents, expected, config.Options{
TS: config.TSOptions{
Parse: true,
NoAmbiguousLessThan: true,
},
})
}
func expectPrintedTSNoAmbiguousLessThan(t *testing.T, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents, expected, config.Options{
TS: config.TSOptions{
Parse: true,
NoAmbiguousLessThan: true,
},
})
}
func expectParseErrorTSX(t *testing.T, contents string, expected string) {
t.Helper()
expectParseErrorCommon(t, contents, expected, config.Options{
TS: config.TSOptions{
Parse: true,
},
JSX: config.JSXOptions{
Parse: true,
},
})
}
func expectPrintedTSX(t *testing.T, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents, expected, config.Options{
TS: config.TSOptions{
Parse: true,
},
JSX: config.JSXOptions{
Parse: true,
},
})
}
func TestTSTypes(t *testing.T) {
expectPrintedTS(t, "let x: T extends number\n ? T\n : number", "let x;\n")
expectPrintedTS(t, "let x: {y: T extends number ? T : number}", "let x;\n")
expectPrintedTS(t, "let x: {y: T \n extends: number}", "let x;\n")
expectPrintedTS(t, "let x: {y: T \n extends?: number}", "let x;\n")
expectPrintedTS(t, "let x: (number | string)[]", "let x;\n")
expectPrintedTS(t, "let x: [string[]?]", "let x;\n")
expectPrintedTS(t, "let x: [number?, string?]", "let x;\n")
expectPrintedTS(t, "let x: [a: number, b?: string, ...c: number[]]", "let x;\n")
expectPrintedTS(t, "type x =\n A\n | B\n C", "C;\n")
expectPrintedTS(t, "type x =\n | A\n | B\n C", "C;\n")
expectPrintedTS(t, "type x =\n A\n & B\n C", "C;\n")
expectPrintedTS(t, "type x =\n & A\n & B\n C", "C;\n")
expectPrintedTS(t, "type x = [-1, 0, 1]\n[]", "[];\n")
expectPrintedTS(t, "type x = [-1n, 0n, 1n]\n[]", "[];\n")
expectPrintedTS(t, "type x = {0: number, readonly 1: boolean}\n[]", "[];\n")
expectPrintedTS(t, "type x = {'a': number, readonly 'b': boolean}\n[]", "[];\n")
expectPrintedTS(t, "type\nFoo = {}", "type;\nFoo = {};\n")
expectParseErrorTS(t, "export type\nFoo = {}", "<stdin>: ERROR: Unexpected newline after \"type\"\n")
expectPrintedTS(t, "let x: {x: 'a', y: false, z: null}", "let x;\n")
expectPrintedTS(t, "let x: {foo(): void}", "let x;\n")
expectPrintedTS(t, "let x: {['x']: number}", "let x;\n")
expectPrintedTS(t, "let x: {['x'](): void}", "let x;\n")
expectPrintedTS(t, "let x: {[key: string]: number}", "let x;\n")
expectPrintedTS(t, "let x: {[keyof: string]: number}", "let x;\n")
expectPrintedTS(t, "let x: {[readonly: string]: number}", "let x;\n")
expectPrintedTS(t, "let x: {[infer: string]: number}", "let x;\n")
expectPrintedTS(t, "let x: [keyof: string]", "let x;\n")
expectPrintedTS(t, "let x: [readonly: string]", "let x;\n")
expectPrintedTS(t, "let x: [infer: string]", "let x;\n")
expectParseErrorTS(t, "let x: A extends B ? keyof : string", "<stdin>: ERROR: Unexpected \":\"\n")
expectParseErrorTS(t, "let x: A extends B ? readonly : string", "<stdin>: ERROR: Unexpected \":\"\n")
expectParseErrorTS(t, "let x: A extends B ? infer : string", "<stdin>: ERROR: Unexpected \":\"\n")
expectParseErrorTS(t, "let x: {[new: string]: number}", "<stdin>: ERROR: Expected \"(\" but found \":\"\n")
expectParseErrorTS(t, "let x: {[import: string]: number}", "<stdin>: ERROR: Expected \"(\" but found \":\"\n")
expectParseErrorTS(t, "let x: {[typeof: string]: number}", "<stdin>: ERROR: Expected identifier but found \":\"\n")
expectPrintedTS(t, "let x: () => void = Foo", "let x = Foo;\n")
expectPrintedTS(t, "let x: new () => void = Foo", "let x = Foo;\n")
expectPrintedTS(t, "let x = 'x' as keyof T", "let x = \"x\";\n")
expectPrintedTS(t, "let x = [1] as readonly [number]", "let x = [1];\n")
expectPrintedTS(t, "let x = 'x' as keyof typeof Foo", "let x = \"x\";\n")
expectPrintedTS(t, "let fs: typeof import('fs') = require('fs')", "let fs = require(\"fs\");\n")
expectPrintedTS(t, "let fs: typeof import('fs').exists = require('fs').exists", "let fs = require(\"fs\").exists;\n")
expectPrintedTS(t, "let fs: typeof import('fs', { assert: { type: 'json' } }) = require('fs')", "let fs = require(\"fs\");\n")
expectPrintedTS(t, "let fs: typeof import('fs', { assert: { 'resolution-mode': 'import' } }) = require('fs')", "let fs = require(\"fs\");\n")
expectPrintedTS(t, "let x: <T>() => Foo<T>", "let x;\n")
expectPrintedTS(t, "let x: new <T>() => Foo<T>", "let x;\n")
expectPrintedTS(t, "let x: <T extends object>() => Foo<T>", "let x;\n")
expectPrintedTS(t, "let x: new <T extends object>() => Foo<T>", "let x;\n")
expectPrintedTS(t, "type Foo<T> = {[P in keyof T]?: T[P]}", "")
expectPrintedTS(t, "type Foo<T> = {[P in keyof T]+?: T[P]}", "")
expectPrintedTS(t, "type Foo<T> = {[P in keyof T]-?: T[P]}", "")
expectPrintedTS(t, "type Foo<T> = {readonly [P in keyof T]: T[P]}", "")
expectPrintedTS(t, "type Foo<T> = {-readonly [P in keyof T]: T[P]}", "")
expectPrintedTS(t, "type Foo<T> = {+readonly [P in keyof T]: T[P]}", "")
expectPrintedTS(t, "let x: number! = y", "let x = y;\n")
expectPrintedTS(t, "let x: number \n !y", "let x;\n!y;\n")
expectPrintedTS(t, "const x: unique = y", "const x = y;\n")
expectPrintedTS(t, "const x: unique<T> = y", "const x = y;\n")
expectPrintedTS(t, "const x: unique\nsymbol = y", "const x = y;\n")
expectPrintedTS(t, "let x: typeof a = y", "let x = y;\n")
expectPrintedTS(t, "let x: typeof a.b = y", "let x = y;\n")
expectPrintedTS(t, "let x: typeof a.if = y", "let x = y;\n")
expectPrintedTS(t, "let x: typeof if.a = y", "let x = y;\n")
expectPrintedTS(t, "let x: typeof readonly = y", "let x = y;\n")
expectParseErrorTS(t, "let x: typeof readonly Array", "<stdin>: ERROR: Expected \";\" but found \"Array\"\n")
expectPrintedTS(t, "let x: `y`", "let x;\n")
expectParseErrorTS(t, "let x: tag`y`", "<stdin>: ERROR: Expected \";\" but found \"`y`\"\n")
expectPrintedTS(t, "let x: { <A extends B>(): c.d \n <E extends F>(): g.h }", "let x;\n")
expectPrintedTSX(t, "type x = a.b \n <c></c>", "/* @__PURE__ */ React.createElement(\"c\", null);\n")
expectPrintedTS(t, "type Foo = a.b \n | c.d", "")
expectPrintedTS(t, "type Foo = a.b \n & c.d", "")
expectPrintedTS(t, "type Foo = \n | a.b \n | c.d", "")
expectPrintedTS(t, "type Foo = \n & a.b \n & c.d", "")
expectPrintedTS(t, "let x: A.B<X.Y>", "let x;\n")
expectPrintedTS(t, "let x: A.B<X.Y>=2", "let x = 2;\n")
expectPrintedTS(t, "let x: A.B<X.Y<Z>>", "let x;\n")
expectPrintedTS(t, "let x: A.B<X.Y<Z>>=2", "let x = 2;\n")
expectPrintedTS(t, "let x: A.B<X.Y<Z<T>>>", "let x;\n")
expectPrintedTS(t, "let x: A.B<X.Y<Z<T>>>=2", "let x = 2;\n")
expectPrintedTS(t, "(): A<T>=> 0", "() => 0;\n")
expectPrintedTS(t, "(): A<B<T>>=> 0", "() => 0;\n")
expectPrintedTS(t, "(): A<B<C<T>>>=> 0", "() => 0;\n")
expectPrintedTS(t, "let foo: any\n<x>y", "let foo;\ny;\n")
expectPrintedTSX(t, "let foo: any\n<x>y</x>", "let foo;\n/* @__PURE__ */ React.createElement(\"x\", null, \"y\");\n")
expectParseErrorTS(t, "let foo: (any\n<x>y)", "<stdin>: ERROR: Expected \")\" but found \"<\"\n")
expectPrintedTS(t, "let foo = bar as (null)", "let foo = bar;\n")
expectPrintedTS(t, "let foo = bar\nas (null)", "let foo = bar;\nas(null);\n")
expectParseErrorTS(t, "let foo = (bar\nas (null))", "<stdin>: ERROR: Expected \")\" but found \"as\"\n")
expectPrintedTS(t, "a as any ? b : c;", "a ? b : c;\n")
expectPrintedTS(t, "a as any ? async () => b : c;", "a ? async () => b : c;\n")
expectPrintedTS(t, "foo as number extends Object ? any : any;", "foo;\n")
expectPrintedTS(t, "foo as number extends Object ? () => void : any;", "foo;\n")
expectPrintedTS(t, "let a = b ? c : d as T extends T ? T extends T ? T : never : never ? e : f;", "let a = b ? c : d ? e : f;\n")
expectParseErrorTS(t, "type a = b extends c", "<stdin>: ERROR: Expected \"?\" but found end of file\n")
expectParseErrorTS(t, "type a = b extends c extends d", "<stdin>: ERROR: Expected \"?\" but found \"extends\"\n")
expectParseErrorTS(t, "type a = b ? c : d", "<stdin>: ERROR: Expected \";\" but found \"?\"\n")
expectPrintedTS(t, "let foo: keyof Object = 'toString'", "let foo = \"toString\";\n")
expectPrintedTS(t, "let foo: keyof\nObject = 'toString'", "let foo = \"toString\";\n")
expectPrintedTS(t, "let foo: (keyof\nObject) = 'toString'", "let foo = \"toString\";\n")
expectPrintedTS(t, "type Foo = Array<<T>(x: T) => T>\n x", "x;\n")
expectPrintedTSX(t, "<Foo<<T>(x: T) => T>/>", "/* @__PURE__ */ React.createElement(Foo, null);\n")
// Certain built-in types do not accept type parameters
expectPrintedTS(t, "x as 1 < 1", "x < 1;\n")
expectPrintedTS(t, "x as 1n < 1", "x < 1;\n")
expectPrintedTS(t, "x as -1 < 1", "x < 1;\n")
expectPrintedTS(t, "x as -1n < 1", "x < 1;\n")
expectPrintedTS(t, "x as '' < 1", "x < 1;\n")
expectPrintedTS(t, "x as `` < 1", "x < 1;\n")
expectPrintedTS(t, "x as any < 1", "x < 1;\n")
expectPrintedTS(t, "x as bigint < 1", "x < 1;\n")
expectPrintedTS(t, "x as false < 1", "x < 1;\n")
expectPrintedTS(t, "x as never < 1", "x < 1;\n")
expectPrintedTS(t, "x as null < 1", "x < 1;\n")
expectPrintedTS(t, "x as number < 1", "x < 1;\n")
expectPrintedTS(t, "x as object < 1", "x < 1;\n")
expectPrintedTS(t, "x as string < 1", "x < 1;\n")
expectPrintedTS(t, "x as symbol < 1", "x < 1;\n")
expectPrintedTS(t, "x as this < 1", "x < 1;\n")
expectPrintedTS(t, "x as true < 1", "x < 1;\n")
expectPrintedTS(t, "x as undefined < 1", "x < 1;\n")
expectPrintedTS(t, "x as unique symbol < 1", "x < 1;\n")
expectPrintedTS(t, "x as unknown < 1", "x < 1;\n")
expectPrintedTS(t, "x as void < 1", "x < 1;\n")
expectParseErrorTS(t, "x as Foo < 1", "<stdin>: ERROR: Expected \">\" but found end of file\n")
// These keywords are valid tuple labels
expectPrintedTS(t, "type _false = [false: string]", "")
expectPrintedTS(t, "type _function = [function: string]", "")
expectPrintedTS(t, "type _import = [import: string]", "")
expectPrintedTS(t, "type _new = [new: string]", "")
expectPrintedTS(t, "type _null = [null: string]", "")
expectPrintedTS(t, "type _this = [this: string]", "")
expectPrintedTS(t, "type _true = [true: string]", "")
expectPrintedTS(t, "type _typeof = [typeof: string]", "")
expectPrintedTS(t, "type _void = [void: string]", "")
// These keywords are invalid tuple labels
expectParseErrorTS(t, "type _break = [break: string]", "<stdin>: ERROR: Unexpected \"break\"\n")
expectParseErrorTS(t, "type _case = [case: string]", "<stdin>: ERROR: Unexpected \"case\"\n")
expectParseErrorTS(t, "type _catch = [catch: string]", "<stdin>: ERROR: Unexpected \"catch\"\n")
expectParseErrorTS(t, "type _class = [class: string]", "<stdin>: ERROR: Unexpected \"class\"\n")
expectParseErrorTS(t, "type _const = [const: string]", "<stdin>: ERROR: Unexpected \"const\"\n")
expectParseErrorTS(t, "type _continue = [continue: string]", "<stdin>: ERROR: Unexpected \"continue\"\n")
expectParseErrorTS(t, "type _debugger = [debugger: string]", "<stdin>: ERROR: Unexpected \"debugger\"\n")
expectParseErrorTS(t, "type _default = [default: string]", "<stdin>: ERROR: Unexpected \"default\"\n")
expectParseErrorTS(t, "type _delete = [delete: string]", "<stdin>: ERROR: Unexpected \"delete\"\n")
expectParseErrorTS(t, "type _do = [do: string]", "<stdin>: ERROR: Unexpected \"do\"\n")
expectParseErrorTS(t, "type _else = [else: string]", "<stdin>: ERROR: Unexpected \"else\"\n")
expectParseErrorTS(t, "type _enum = [enum: string]", "<stdin>: ERROR: Unexpected \"enum\"\n")
expectParseErrorTS(t, "type _export = [export: string]", "<stdin>: ERROR: Unexpected \"export\"\n")
expectParseErrorTS(t, "type _extends = [extends: string]", "<stdin>: ERROR: Unexpected \"extends\"\n")
expectParseErrorTS(t, "type _finally = [finally: string]", "<stdin>: ERROR: Unexpected \"finally\"\n")
expectParseErrorTS(t, "type _for = [for: string]", "<stdin>: ERROR: Unexpected \"for\"\n")
expectParseErrorTS(t, "type _if = [if: string]", "<stdin>: ERROR: Unexpected \"if\"\n")
expectParseErrorTS(t, "type _in = [in: string]", "<stdin>: ERROR: Unexpected \"in\"\n")
expectParseErrorTS(t, "type _instanceof = [instanceof: string]", "<stdin>: ERROR: Unexpected \"instanceof\"\n")
expectParseErrorTS(t, "type _return = [return: string]", "<stdin>: ERROR: Unexpected \"return\"\n")
expectParseErrorTS(t, "type _super = [super: string]", "<stdin>: ERROR: Unexpected \"super\"\n")
expectParseErrorTS(t, "type _switch = [switch: string]", "<stdin>: ERROR: Unexpected \"switch\"\n")
expectParseErrorTS(t, "type _throw = [throw: string]", "<stdin>: ERROR: Unexpected \"throw\"\n")
expectParseErrorTS(t, "type _try = [try: string]", "<stdin>: ERROR: Unexpected \"try\"\n")
expectParseErrorTS(t, "type _var = [var: string]", "<stdin>: ERROR: Unexpected \"var\"\n")
expectParseErrorTS(t, "type _while = [while: string]", "<stdin>: ERROR: Unexpected \"while\"\n")
expectParseErrorTS(t, "type _with = [with: string]", "<stdin>: ERROR: Unexpected \"with\"\n")
// TypeScript 4.1
expectPrintedTS(t, "let foo: `${'a' | 'b'}-${'c' | 'd'}` = 'a-c'", "let foo = \"a-c\";\n")
// TypeScript 4.2
expectPrintedTS(t, "let x: abstract new () => void = Foo", "let x = Foo;\n")
expectPrintedTS(t, "let x: abstract new <T>() => Foo<T>", "let x;\n")
expectPrintedTS(t, "let x: abstract new <T extends object>() => Foo<T>", "let x;\n")
expectParseErrorTS(t, "let x: abstract () => void = Foo", "<stdin>: ERROR: Expected \";\" but found \"(\"\n")
expectParseErrorTS(t, "let x: abstract <T>() => Foo<T>", "<stdin>: ERROR: Expected \";\" but found \"(\"\n")
expectParseErrorTS(t, "let x: abstract <T extends object>() => Foo<T>", "<stdin>: ERROR: Expected \"?\" but found \">\"\n")
}
func TestTSAsCast(t *testing.T) {
expectPrintedTS(t, "x as any\n(y);", "x;\ny;\n")
expectPrintedTS(t, "x as any\n`y`;", "x;\n`y`;\n")
expectPrintedTS(t, "x as any\n`${y}`;", "x;\n`${y}`;\n")
expectPrintedTS(t, "x as any\n--y;", "x;\n--y;\n")
expectPrintedTS(t, "x as any\n++y;", "x;\n++y;\n")
expectPrintedTS(t, "x + y as any\n(z as any) + 1;", "x + y;\nz + 1;\n")
expectPrintedTS(t, "x + y as any\n(z as any) = 1;", "x + y;\nz = 1;\n")
expectPrintedTS(t, "x = y as any\n(z as any) + 1;", "x = y;\nz + 1;\n")
expectPrintedTS(t, "x = y as any\n(z as any) = 1;", "x = y;\nz = 1;\n")
expectPrintedTS(t, "x * y as any\n['z'];", "x * y;\n[\"z\"];\n")
expectPrintedTS(t, "x * y as any\n.z;", "x * y;\n")
expectPrintedTS(t, "x as y['x'];", "x;\n")
expectPrintedTS(t, "x as y!['x'];", "x;\n")
expectPrintedTS(t, "x as y\n['x'];", "x;\n[\"x\"];\n")
expectPrintedTS(t, "x as y\n!['x'];", "x;\n![\"x\"];\n")
expectParseErrorTS(t, "x = y as any `z`;", "<stdin>: ERROR: Expected \";\" but found \"`z`\"\n")
expectParseErrorTS(t, "x = y as any `${z}`;", "<stdin>: ERROR: Expected \";\" but found \"`${\"\n")
expectParseErrorTS(t, "x = y as any?.z;", "<stdin>: ERROR: Expected \";\" but found \"?.\"\n")
expectParseErrorTS(t, "x = y as any--;", "<stdin>: ERROR: Expected \";\" but found \"--\"\n")
expectParseErrorTS(t, "x = y as any++;", "<stdin>: ERROR: Expected \";\" but found \"++\"\n")
expectParseErrorTS(t, "x = y as any(z);", "<stdin>: ERROR: Expected \";\" but found \"(\"\n")
expectParseErrorTS(t, "x = y as any\n= z;", "<stdin>: ERROR: Unexpected \"=\"\n")
expectParseErrorTS(t, "a, x as y `z`;", "<stdin>: ERROR: Expected \";\" but found \"`z`\"\n")
expectParseErrorTS(t, "a ? b : x as y `z`;", "<stdin>: ERROR: Expected \";\" but found \"`z`\"\n")
expectParseErrorTS(t, "x as any = y;", "<stdin>: ERROR: Expected \";\" but found \"=\"\n")
expectParseErrorTS(t, "(x as any = y);", "<stdin>: ERROR: Expected \")\" but found \"=\"\n")
expectParseErrorTS(t, "(x = y as any(z));", "<stdin>: ERROR: Expected \")\" but found \"(\"\n")
}
func TestTSClass(t *testing.T) {
expectPrintedTS(t, "export default class Foo {}", "export default class Foo {\n}\n")
expectPrintedTS(t, "export default class Foo extends Bar<T> {}", "export default class Foo extends Bar {\n}\n")
expectPrintedTS(t, "export default class Foo extends Bar<T>() {}", "export default class Foo extends Bar() {\n}\n")
expectPrintedTS(t, "export default class Foo implements Bar<T> {}", "export default class Foo {\n}\n")
expectPrintedTS(t, "export default class Foo<T> {}", "export default class Foo {\n}\n")
expectPrintedTS(t, "export default class Foo<T> extends Bar<T> {}", "export default class Foo extends Bar {\n}\n")
expectPrintedTS(t, "export default class Foo<T> extends Bar<T>() {}", "export default class Foo extends Bar() {\n}\n")
expectPrintedTS(t, "export default class Foo<T> implements Bar<T> {}", "export default class Foo {\n}\n")
expectPrintedTS(t, "(class Foo<T> {})", "(class Foo {\n});\n")
expectPrintedTS(t, "(class Foo<T> extends Bar<T> {})", "(class Foo extends Bar {\n});\n")
expectPrintedTS(t, "(class Foo<T> extends Bar<T>() {})", "(class Foo extends Bar() {\n});\n")
expectPrintedTS(t, "(class Foo<T> implements Bar<T> {})", "(class Foo {\n});\n")
expectPrintedTS(t, "export default class {}", "export default class {\n}\n")
expectPrintedTS(t, "export default class extends Foo<T> {}", "export default class extends Foo {\n}\n")
expectPrintedTS(t, "export default class implements Foo<T> {}", "export default class {\n}\n")
expectPrintedTS(t, "export default class <T> {}", "export default class {\n}\n")
expectPrintedTS(t, "export default class <T> extends Foo<T> {}", "export default class extends Foo {\n}\n")
expectPrintedTS(t, "export default class <T> implements Foo<T> {}", "export default class {\n}\n")
expectPrintedTS(t, "(class <T> {})", "(class {\n});\n")
expectPrintedTS(t, "(class extends Foo<T> {})", "(class extends Foo {\n});\n")
expectPrintedTS(t, "(class extends Foo<T>() {})", "(class extends Foo() {\n});\n")
expectPrintedTS(t, "(class implements Foo<T> {})", "(class {\n});\n")
expectPrintedTS(t, "(class <T> extends Foo<T> {})", "(class extends Foo {\n});\n")
expectPrintedTS(t, "(class <T> extends Foo<T>() {})", "(class extends Foo() {\n});\n")
expectPrintedTS(t, "(class <T> implements Foo<T> {})", "(class {\n});\n")
expectPrintedTS(t, "abstract class A { abstract foo(): void; bar(): void {} }", "class A {\n bar() {\n }\n}\n")
expectPrintedTS(t, "export abstract class A { abstract foo(): void; bar(): void {} }", "export class A {\n bar() {\n }\n}\n")
expectPrintedTS(t, "export default abstract", "export default abstract;\n")
expectPrintedTS(t, "export default abstract - after", "export default abstract - after;\n")
expectPrintedTS(t, "export default abstract class { abstract foo(): void; bar(): void {} } - after", "export default class {\n bar() {\n }\n}\n-after;\n")
expectPrintedTS(t, "export default abstract class A { abstract foo(): void; bar(): void {} } - after", "export default class A {\n bar() {\n }\n}\n-after;\n")
expectPrintedTS(t, "class A<T extends number> extends B.C<D, E> {}", "class A extends B.C {\n}\n")
expectPrintedTS(t, "class A<T extends number> implements B.C<D, E>, F.G<H, I> {}", "class A {\n}\n")
expectPrintedTS(t, "class A<T extends number> extends X implements B.C<D, E>, F.G<H, I> {}", "class A extends X {\n}\n")
reservedWordError :=
" is a reserved word and cannot be used in strict mode\n" +
"<stdin>: NOTE: All code inside a class is implicitly in strict mode\n"
expectParseErrorTS(t, "class Foo { constructor(public) {} }", "<stdin>: ERROR: \"public\""+reservedWordError)
expectParseErrorTS(t, "class Foo { constructor(protected) {} }", "<stdin>: ERROR: \"protected\""+reservedWordError)
expectParseErrorTS(t, "class Foo { constructor(private) {} }", "<stdin>: ERROR: \"private\""+reservedWordError)
expectPrintedTS(t, "class Foo { constructor(readonly) {} }", "class Foo {\n constructor(readonly) {\n }\n}\n")
expectPrintedTS(t, "class Foo { constructor(override) {} }", "class Foo {\n constructor(override) {\n }\n}\n")
expectPrintedTS(t, "class Foo { constructor(public x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n")
expectPrintedTS(t, "class Foo { constructor(protected x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n")
expectPrintedTS(t, "class Foo { constructor(private x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n")
expectPrintedTS(t, "class Foo { constructor(readonly x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n")
expectPrintedTS(t, "class Foo { constructor(override x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n")
expectPrintedTS(t, "class Foo { constructor(public readonly x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n")
expectPrintedTS(t, "class Foo { constructor(protected readonly x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n")
expectPrintedTS(t, "class Foo { constructor(private readonly x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n")
expectPrintedTS(t, "class Foo { constructor(override readonly x) {} }", "class Foo {\n constructor(x) {\n this.x = x;\n }\n}\n")
expectParseErrorTS(t, "class Foo { constructor(public {x}) {} }", "<stdin>: ERROR: Expected identifier but found \"{\"\n")
expectParseErrorTS(t, "class Foo { constructor(protected {x}) {} }", "<stdin>: ERROR: Expected identifier but found \"{\"\n")
expectParseErrorTS(t, "class Foo { constructor(private {x}) {} }", "<stdin>: ERROR: Expected identifier but found \"{\"\n")
expectParseErrorTS(t, "class Foo { constructor(readonly {x}) {} }", "<stdin>: ERROR: Expected identifier but found \"{\"\n")
expectParseErrorTS(t, "class Foo { constructor(override {x}) {} }", "<stdin>: ERROR: Expected identifier but found \"{\"\n")
expectParseErrorTS(t, "class Foo { constructor(public [x]) {} }", "<stdin>: ERROR: Expected identifier but found \"[\"\n")
expectParseErrorTS(t, "class Foo { constructor(protected [x]) {} }", "<stdin>: ERROR: Expected identifier but found \"[\"\n")
expectParseErrorTS(t, "class Foo { constructor(private [x]) {} }", "<stdin>: ERROR: Expected identifier but found \"[\"\n")
expectParseErrorTS(t, "class Foo { constructor(readonly [x]) {} }", "<stdin>: ERROR: Expected identifier but found \"[\"\n")
expectParseErrorTS(t, "class Foo { constructor(override [x]) {} }", "<stdin>: ERROR: Expected identifier but found \"[\"\n")
expectPrintedTS(t, "class Foo { foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { foo: number = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n")
expectPrintedTS(t, "class Foo { foo(): void {} }", "class Foo {\n foo() {\n }\n}\n")
expectPrintedTS(t, "class Foo { foo(): void; foo(): void {} }", "class Foo {\n foo() {\n }\n}\n")
expectParseErrorTS(t, "class Foo { foo(): void foo(): void {} }", "<stdin>: ERROR: Expected \";\" but found \"foo\"\n")
expectPrintedTS(t, "class Foo { foo?: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { foo?: number = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n")
expectPrintedTS(t, "class Foo { foo?(): void {} }", "class Foo {\n foo() {\n }\n}\n")
expectPrintedTS(t, "class Foo { foo?(): void; foo(): void {} }", "class Foo {\n foo() {\n }\n}\n")
expectParseErrorTS(t, "class Foo { foo?(): void foo(): void {} }", "<stdin>: ERROR: Expected \";\" but found \"foo\"\n")
expectPrintedTS(t, "class Foo { foo!: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { foo!: number = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n")
expectPrintedTS(t, "class Foo { foo!(): void {} }", "class Foo {\n foo() {\n }\n}\n")
expectPrintedTS(t, "class Foo { foo!(): void; foo(): void {} }", "class Foo {\n foo() {\n }\n}\n")
expectParseErrorTS(t, "class Foo { foo!(): void foo(): void {} }", "<stdin>: ERROR: Expected \";\" but found \"foo\"\n")
expectPrintedTS(t, "class Foo { foo \n ?: number }", "class Foo {\n}\n")
expectParseErrorTS(t, "class Foo { foo \n !: number }", "<stdin>: ERROR: Expected identifier but found \"!\"\n")
expectPrintedTS(t, "class Foo { public foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { private foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { protected foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { declare foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { declare public foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { public declare foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { override foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { override public foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { public override foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { declare override public foo: number }", "class Foo {\n}\n")
expectParseErrorTS(t, "class Foo { declare foo = 123 }", "<stdin>: ERROR: Class fields that use \"declare\" cannot be initialized\n")
expectPrintedTS(t, "class Foo { public static foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { private static foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { protected static foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { declare static foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { declare public static foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { public declare static foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { public static declare foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { override static foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { override public static foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { public override static foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { public static override foo: number }", "class Foo {\n}\n")
expectPrintedTS(t, "class Foo { declare override public static foo: number }", "class Foo {\n}\n")
expectParseErrorTS(t, "class Foo { declare static foo = 123 }", "<stdin>: ERROR: Class fields that use \"declare\" cannot be initialized\n")
expectParseErrorTS(t, "class Foo { static declare foo = 123 }", "<stdin>: ERROR: Class fields that use \"declare\" cannot be initialized\n")
expectParseErrorTS(t, "class Foo { declare #foo }", "<stdin>: ERROR: \"declare\" cannot be used with a private identifier\n")
expectParseErrorTS(t, "class Foo { declare [foo: string]: number }", "<stdin>: ERROR: \"declare\" cannot be used with an index signature\n")
expectParseErrorTS(t, "class Foo { declare foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a method\n")
expectParseErrorTS(t, "class Foo { declare get foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a getter\n")
expectParseErrorTS(t, "class Foo { declare set foo(x) }", "<stdin>: ERROR: \"declare\" cannot be used with a setter\n")
expectParseErrorTS(t, "class Foo { declare static #foo }", "<stdin>: ERROR: \"declare\" cannot be used with a private identifier\n")
expectParseErrorTS(t, "class Foo { declare static [foo: string]: number }", "<stdin>: ERROR: \"declare\" cannot be used with an index signature\n")
expectParseErrorTS(t, "class Foo { declare static foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a method\n")
expectParseErrorTS(t, "class Foo { declare static get foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a getter\n")
expectParseErrorTS(t, "class Foo { declare static set foo(x) }", "<stdin>: ERROR: \"declare\" cannot be used with a setter\n")
expectParseErrorTS(t, "class Foo { static declare #foo }", "<stdin>: ERROR: \"declare\" cannot be used with a private identifier\n")
expectParseErrorTS(t, "class Foo { static declare [foo: string]: number }", "<stdin>: ERROR: \"declare\" cannot be used with an index signature\n")
expectParseErrorTS(t, "class Foo { static declare foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a method\n")
expectParseErrorTS(t, "class Foo { static declare get foo() }", "<stdin>: ERROR: \"declare\" cannot be used with a getter\n")
expectParseErrorTS(t, "class Foo { static declare set foo(x) }", "<stdin>: ERROR: \"declare\" cannot be used with a setter\n")
expectPrintedTS(t, "class Foo { [key: string]: any\nfoo = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n")
expectPrintedTS(t, "class Foo { [key: string]: any; foo = 0 }", "class Foo {\n constructor() {\n this.foo = 0;\n }\n}\n")
expectParseErrorTS(t, "class Foo<> {}", "<stdin>: ERROR: Expected identifier but found \">\"\n")
expectParseErrorTS(t, "class Foo<,> {}", "<stdin>: ERROR: Expected identifier but found \",\"\n")
expectParseErrorTS(t, "class Foo<T><T> {}", "<stdin>: ERROR: Expected \"{\" but found \"<\"\n")
}
func TestTSPrivateIdentifiers(t *testing.T) {
// The TypeScript compiler still moves private field initializers into the
// constructor, but it has to leave the private field declaration in place so
// the private field is still declared.
expectPrintedTS(t, "class Foo { #foo }", "class Foo {\n #foo;\n}\n")
expectPrintedTS(t, "class Foo { #foo = 1 }", "class Foo {\n #foo = 1;\n}\n")
expectPrintedTS(t, "class Foo { #foo() {} }", "class Foo {\n #foo() {\n }\n}\n")
expectPrintedTS(t, "class Foo { get #foo() {} }", "class Foo {\n get #foo() {\n }\n}\n")
expectPrintedTS(t, "class Foo { set #foo(x) {} }", "class Foo {\n set #foo(x) {\n }\n}\n")
// The TypeScript compiler doesn't currently support static private fields
// because it moves static field initializers to after the class body and
// private fields can't be used outside the class body. It remains to be seen
// how the TypeScript compiler will transform private static fields once it
// finally does support them. For now just leave the initializer in place.
expectPrintedTS(t, "class Foo { static #foo }", "class Foo {\n static #foo;\n}\n")
expectPrintedTS(t, "class Foo { static #foo = 1 }", "class Foo {\n static #foo = 1;\n}\n")
expectPrintedTS(t, "class Foo { static #foo() {} }", "class Foo {\n static #foo() {\n }\n}\n")
expectPrintedTS(t, "class Foo { static get #foo() {} }", "class Foo {\n static get #foo() {\n }\n}\n")
expectPrintedTS(t, "class Foo { static set #foo(x) {} }", "class Foo {\n static set #foo(x) {\n }\n}\n")
}
func TestTSInterface(t *testing.T) {
expectPrintedTS(t, "interface A { a } x", "x;\n")
expectPrintedTS(t, "interface A { a; b } x", "x;\n")
expectPrintedTS(t, "interface A { a() } x", "x;\n")
expectPrintedTS(t, "interface A { a(); b } x", "x;\n")
expectPrintedTS(t, "interface Foo { foo(): Foo \n is: Bar } x", "x;\n")
expectPrintedTS(t, "interface A<T extends number> extends B.C<D, E>, F.G<H, I> {} x", "x;\n")
expectPrintedTS(t, "export interface A<T extends number> extends B.C<D, E>, F.G<H, I> {} x", "x;\n")
expectPrintedTS(t, "export default interface Foo {} x", "x;\n")
}
func TestTSNamespace(t *testing.T) {
// Check ES5 emit
expectPrintedTargetTS(t, 5, "namespace x { export var y = 1 }", "var x;\n(function(x) {\n x.y = 1;\n})(x || (x = {}));\n")
expectPrintedTargetTS(t, 2015, "namespace x { export var y = 1 }", "var x;\n((x) => {\n x.y = 1;\n})(x || (x = {}));\n")
// Certain syntax isn't allowed inside a namespace block
expectParseErrorTS(t, "namespace x { return }", "<stdin>: ERROR: A return statement cannot be used here:\n")
expectParseErrorTS(t, "namespace x { await 1 }", "<stdin>: ERROR: \"await\" can only be used inside an \"async\" function\n")
expectParseErrorTS(t, "namespace x { if (y) return }", "<stdin>: ERROR: A return statement cannot be used here:\n")
expectParseErrorTS(t, "namespace x { if (y) await 1 }", "<stdin>: ERROR: \"await\" can only be used inside an \"async\" function\n")
expectParseErrorTS(t, "namespace x { this }", "<stdin>: ERROR: Cannot use \"this\" here:\n")
expectParseErrorTS(t, "namespace x { () => this }", "<stdin>: ERROR: Cannot use \"this\" here:\n")
expectParseErrorTS(t, "namespace x { class y { [this] } }", "<stdin>: ERROR: Cannot use \"this\" here:\n")
expectParseErrorTS(t, "namespace x { (function() { this }) }", "")
expectParseErrorTS(t, "namespace x { function y() { this } }", "")
expectParseErrorTS(t, "namespace x { class y { x = this } }", "")
expectParseErrorTS(t, "export namespace x { export let yield = 1 }",
"<stdin>: ERROR: \"yield\" is a reserved word and cannot be used in an ECMAScript module\n"+
"<stdin>: NOTE: This file is considered to be an ECMAScript module because of the \"export\" keyword here:\n")
expectPrintedTS(t, "namespace x { export let await = 1, y = await }", `var x;
((x) => {
x.await = 1;
x.y = x.await;
})(x || (x = {}));
`)
expectPrintedTS(t, "namespace x { export let yield = 1, y = yield }", `var x;
((x) => {
x.yield = 1;
x.y = x.yield;
})(x || (x = {}));
`)
expectPrintedTS(t, "namespace Foo { 0 }", `var Foo;
((Foo) => {
0;
})(Foo || (Foo = {}));
`)
expectPrintedTS(t, "export namespace Foo { 0 }", `export var Foo;
((Foo) => {
0;
})(Foo || (Foo = {}));
`)
// Namespaces should introduce a scope that prevents name collisions
expectPrintedTS(t, "namespace Foo { let x } let x", `var Foo;
((Foo) => {
let x;
})(Foo || (Foo = {}));
let x;
`)
// Exports in namespaces shouldn't collide with module exports
expectPrintedTS(t, "namespace Foo { export let x } export let x", `var Foo;
((Foo) => {
})(Foo || (Foo = {}));
export let x;
`)
expectPrintedTS(t, "declare namespace Foo { export let x } namespace x { 0 }", `var x;
((x) => {
0;
})(x || (x = {}));
`)
errorText := `<stdin>: ERROR: The symbol "foo" has already been declared
<stdin>: NOTE: The symbol "foo" was originally declared here:
`
// Namespaces with values are not allowed to merge
expectParseErrorTS(t, "var foo; namespace foo { 0 }", errorText)
expectParseErrorTS(t, "let foo; namespace foo { 0 }", errorText)
expectParseErrorTS(t, "const foo = 0; namespace foo { 0 }", errorText)
expectParseErrorTS(t, "namespace foo { 0 } var foo", errorText)
expectParseErrorTS(t, "namespace foo { 0 } let foo", errorText)
expectParseErrorTS(t, "namespace foo { 0 } const foo = 0", errorText)
// Namespaces without values are allowed to merge
expectPrintedTS(t, "var foo; namespace foo {}", "var foo;\n")
expectPrintedTS(t, "let foo; namespace foo {}", "let foo;\n")
expectPrintedTS(t, "const foo = 0; namespace foo {}", "const foo = 0;\n")
expectPrintedTS(t, "namespace foo {} var foo", "var foo;\n")
expectPrintedTS(t, "namespace foo {} let foo", "let foo;\n")
expectPrintedTS(t, "namespace foo {} const foo = 0", "const foo = 0;\n")
// Namespaces with types but no values are allowed to merge
expectPrintedTS(t, "var foo; namespace foo { export type bar = number }", "var foo;\n")
expectPrintedTS(t, "let foo; namespace foo { export type bar = number }", "let foo;\n")
expectPrintedTS(t, "const foo = 0; namespace foo { export type bar = number }", "const foo = 0;\n")
expectPrintedTS(t, "namespace foo { export type bar = number } var foo", "var foo;\n")
expectPrintedTS(t, "namespace foo { export type bar = number } let foo", "let foo;\n")
expectPrintedTS(t, "namespace foo { export type bar = number } const foo = 0", "const foo = 0;\n")
// Namespaces are allowed to merge with certain symbols
expectPrintedTS(t, "function foo() {} namespace foo { 0 }", `function foo() {
}
((foo) => {
0;
})(foo || (foo = {}));
`)
expectPrintedTS(t, "function* foo() {} namespace foo { 0 }", `function* foo() {
}
((foo) => {
0;
})(foo || (foo = {}));
`)
expectPrintedTS(t, "async function foo() {} namespace foo { 0 }", `async function foo() {
}
((foo) => {
0;
})(foo || (foo = {}));
`)
expectPrintedTS(t, "class foo {} namespace foo { 0 }", `class foo {
}
((foo) => {
0;
})(foo || (foo = {}));
`)
expectPrintedTS(t, "enum foo { a } namespace foo { 0 }", `var foo = /* @__PURE__ */ ((foo) => {
foo[foo["a"] = 0] = "a";
return foo;
})(foo || {});
((foo) => {
0;
})(foo || (foo = {}));
`)
expectPrintedTS(t, "namespace foo {} namespace foo { 0 }", `var foo;
((foo) => {
0;
})(foo || (foo = {}));
`)
expectParseErrorTS(t, "namespace foo { 0 } function foo() {}", errorText)
expectParseErrorTS(t, "namespace foo { 0 } function* foo() {}", errorText)
expectParseErrorTS(t, "namespace foo { 0 } async function foo() {}", errorText)
expectParseErrorTS(t, "namespace foo { 0 } class foo {}", errorText)
expectPrintedTS(t, "namespace foo { 0 } enum foo { a }", `var foo;
((foo) => {
0;
})(foo || (foo = {}));
var foo = /* @__PURE__ */ ((foo) => {
foo[foo["a"] = 0] = "a";
return foo;
})(foo || {});
`)
expectPrintedTS(t, "namespace foo { 0 } namespace foo {}", `var foo;
((foo) => {
0;
})(foo || (foo = {}));
`)
expectPrintedTS(t, "namespace foo { 0 } namespace foo { 0 }", `var foo;
((foo) => {
0;
})(foo || (foo = {}));
((foo) => {
0;
})(foo || (foo = {}));
`)
expectPrintedTS(t, "function foo() {} namespace foo { 0 } function foo() {}", `function foo() {
}
((foo) => {
0;
})(foo || (foo = {}));
function foo() {
}
`)
expectPrintedTS(t, "function* foo() {} namespace foo { 0 } function* foo() {}", `function* foo() {
}
((foo) => {
0;
})(foo || (foo = {}));
function* foo() {
}
`)
expectPrintedTS(t, "async function foo() {} namespace foo { 0 } async function foo() {}", `async function foo() {
}
((foo) => {
0;
})(foo || (foo = {}));
async function foo() {
}
`)
// Namespace merging shouldn't allow for other merging
expectParseErrorTS(t, "class foo {} namespace foo { 0 } class foo {}", errorText)
expectParseErrorTS(t, "class foo {} namespace foo { 0 } enum foo {}", errorText)
expectParseErrorTS(t, "enum foo {} namespace foo { 0 } class foo {}", errorText)
expectParseErrorTS(t, "namespace foo { 0 } namespace foo { 0 } let foo", errorText)
expectParseErrorTS(t, "namespace foo { 0 } enum foo {} class foo {}", errorText)
// Test dot nested namespace syntax
expectPrintedTS(t, "namespace foo.bar { foo(bar) }", `var foo;
((foo) => {
let bar;
((bar) => {
foo(bar);
})(bar = foo.bar || (foo.bar = {}));
})(foo || (foo = {}));
`)
// "module" is a deprecated alias for "namespace"
expectPrintedTS(t, "module foo { export namespace bar { foo(bar) } }", `var foo;
((foo) => {
let bar;
((bar) => {
foo(bar);
})(bar = foo.bar || (foo.bar = {}));
})(foo || (foo = {}));
`)
expectPrintedTS(t, "namespace foo { export module bar { foo(bar) } }", `var foo;
((foo) => {
let bar;
((bar) => {
foo(bar);
})(bar = foo.bar || (foo.bar = {}));
})(foo || (foo = {}));
`)
expectPrintedTS(t, "module foo.bar { foo(bar) }", `var foo;
((foo) => {
let bar;
((bar) => {
foo(bar);
})(bar = foo.bar || (foo.bar = {}));
})(foo || (foo = {}));
`)
}
func TestTSNamespaceExports(t *testing.T) {
expectPrintedTS(t, `
namespace A {
export namespace B {
export function fn() {}
}
namespace C {
export function fn() {}
}
namespace D {
function fn() {}
}
}
`, `var A;
((A) => {
let B;
((B) => {
function fn() {
}
B.fn = fn;
})(B = A.B || (A.B = {}));
let C;
((C) => {
function fn() {
}
C.fn = fn;
})(C || (C = {}));
let D;
((D) => {
function fn() {
}
})(D || (D = {}));
})(A || (A = {}));
`)
expectPrintedTS(t, `
namespace A {
export namespace B {
export class Class {}
}
namespace C {
export class Class {}
}
namespace D {
class Class {}
}
}
`, `var A;
((A) => {
let B;
((B) => {
class Class {
}
B.Class = Class;
})(B = A.B || (A.B = {}));
let C;
((C) => {
class Class {
}
C.Class = Class;
})(C || (C = {}));
let D;
((D) => {
class Class {
}
})(D || (D = {}));
})(A || (A = {}));
`)
expectPrintedTS(t, `
namespace A {
export namespace B {
export enum Enum {}
}
namespace C {
export enum Enum {}
}
namespace D {
enum Enum {}
}
}
`, `var A;
((A) => {
let B;
((B) => {
let Enum;
((Enum) => {
})(Enum = B.Enum || (B.Enum = {}));
})(B = A.B || (A.B = {}));
let C;
((C) => {
let Enum;
((Enum) => {
})(Enum = C.Enum || (C.Enum = {}));
})(C || (C = {}));
let D;
((D) => {
let Enum;
((Enum) => {
})(Enum || (Enum = {}));
})(D || (D = {}));
})(A || (A = {}));
`)
expectPrintedTS(t, `
namespace A {
export namespace B {
export let foo = 1
foo += foo
}
namespace C {
export let foo = 1
foo += foo
}
namespace D {
let foo = 1
foo += foo
}
}
`, `var A;
((A) => {
let B;
((B) => {
B.foo = 1;
B.foo += B.foo;
})(B = A.B || (A.B = {}));
let C;
((C) => {
C.foo = 1;
C.foo += C.foo;
})(C || (C = {}));
let D;
((D) => {
let foo = 1;
foo += foo;
})(D || (D = {}));
})(A || (A = {}));
`)
expectPrintedTS(t, `
namespace A {
export namespace B {
export const foo = 1
}
namespace C {
export const foo = 1
}
namespace D {
const foo = 1
}
}
`, `var A;
((A) => {
let B;
((B) => {
B.foo = 1;
})(B = A.B || (A.B = {}));
let C;
((C) => {
C.foo = 1;
})(C || (C = {}));
let D;
((D) => {
const foo = 1;
})(D || (D = {}));
})(A || (A = {}));
`)
expectPrintedTS(t, `
namespace A {
export namespace B {
export var foo = 1
foo += foo
}
namespace C {
export var foo = 1
foo += foo
}
namespace D {
var foo = 1
foo += foo
}
}
`, `var A;
((A) => {
let B;
((B) => {
B.foo = 1;
B.foo += B.foo;
})(B = A.B || (A.B = {}));
let C;
((C) => {
C.foo = 1;
C.foo += C.foo;
})(C || (C = {}));
let D;
((D) => {
var foo = 1;
foo += foo;
})(D || (D = {}));
})(A || (A = {}));
`)
expectPrintedTS(t, `
namespace ns {
export declare const L1
console.log(L1)
export declare let [[L2 = x, { [y]: L3 }]]
console.log(L2, L3)
export declare function F()
console.log(F)
export declare function F2() { }
console.log(F2)
export declare class C { }
console.log(C)
export declare enum E { }
console.log(E)
export declare namespace N { }
console.log(N)
}
`, `var ns;
((ns) => {
console.log(ns.L1);
console.log(ns.L2, ns.L3);
console.log(F);
console.log(F2);
console.log(C);
console.log(E);
console.log(N);
})(ns || (ns = {}));
`)
expectPrintedTS(t, `
namespace a { export var a = 123; log(a) }
namespace b { export let b = 123; log(b) }
namespace c { export enum c {} log(c) }
namespace d { export class d {} log(d) }
namespace e { export namespace e {} log(e) }
namespace f { export function f() {} log(f) }
`, `var a;
((_a) => {
_a.a = 123;
log(_a.a);
})(a || (a = {}));
var b;
((_b) => {
_b.b = 123;
log(_b.b);
})(b || (b = {}));
var c;
((_c) => {
let c;
((c) => {
})(c = _c.c || (_c.c = {}));
log(c);
})(c || (c = {}));
var d;
((_d) => {
class d {
}
_d.d = d;
log(d);
})(d || (d = {}));
var e;
((e) => {
log(e);
})(e || (e = {}));
var f;
((_f) => {
function f() {
}
_f.f = f;