@@ -59,12 +59,7 @@ fn print_directives_and_statements<const MINIFY: bool>(
59
59
statements : & [ Statement < ' _ > ] ,
60
60
ctx : Context ,
61
61
) {
62
- p. print_directives_and_statements_with_semicolon_order (
63
- Some ( directives) ,
64
- statements,
65
- ctx,
66
- false ,
67
- ) ;
62
+ p. print_directives_and_statements ( Some ( directives) , statements, ctx) ;
68
63
}
69
64
70
65
impl < ' a , const MINIFY : bool > Gen < MINIFY > for Hashbang < ' a > {
@@ -77,13 +72,14 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Hashbang<'a> {
77
72
impl < ' a , const MINIFY : bool > Gen < MINIFY > for Directive < ' a > {
78
73
fn gen ( & self , p : & mut Codegen < { MINIFY } > , _ctx : Context ) {
79
74
p. add_source_mapping ( self . span . start ) ;
75
+ p. print_indent ( ) ;
80
76
// A Use Strict Directive may not contain an EscapeSequence or LineContinuation.
81
77
// So here should print original `directive` value, the `expression` value is escaped str.
82
78
// See https://github.com/babel/babel/blob/main/packages/babel-generator/src/generators/base.ts#L64
83
79
p. wrap_quote ( self . directive . as_str ( ) , |p, _| {
84
80
p. print_str ( self . directive . as_bytes ( ) ) ;
85
81
} ) ;
86
- p. print_semicolon ( ) ;
82
+ p. print_semicolon_after_statement ( ) ;
87
83
}
88
84
}
89
85
@@ -108,10 +104,41 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Statement<'a> {
108
104
Self :: TryStatement ( stmt) => stmt. gen ( p, ctx) ,
109
105
Self :: WhileStatement ( stmt) => stmt. gen ( p, ctx) ,
110
106
Self :: WithStatement ( stmt) => stmt. gen ( p, ctx) ,
111
- match_module_declaration ! ( Self ) => self . to_module_declaration ( ) . gen ( p, ctx) ,
112
- match_declaration ! ( Self ) => {
107
+
108
+ Self :: ImportDeclaration ( decl) => decl. gen ( p, ctx) ,
109
+ Self :: ExportAllDeclaration ( decl) => decl. gen ( p, ctx) ,
110
+ Self :: ExportDefaultDeclaration ( decl) => decl. gen ( p, ctx) ,
111
+ Self :: ExportNamedDeclaration ( decl) => decl. gen ( p, ctx) ,
112
+ Self :: TSExportAssignment ( decl) => decl. gen ( p, ctx) ,
113
+ Self :: TSNamespaceExportDeclaration ( decl) => decl. gen ( p, ctx) ,
114
+
115
+ Self :: VariableDeclaration ( decl) => {
116
+ p. print_indent ( ) ;
117
+ decl. gen ( p, ctx) ;
118
+ p. print_semicolon_after_statement ( ) ;
119
+ }
120
+ Self :: FunctionDeclaration ( decl) => {
113
121
p. print_indent ( ) ;
114
- self . to_declaration ( ) . gen ( p, ctx) ;
122
+ decl. gen ( p, ctx) ;
123
+ p. print_soft_newline ( ) ;
124
+ }
125
+ Self :: ClassDeclaration ( decl) => decl. gen ( p, ctx) ,
126
+ Self :: UsingDeclaration ( declaration) => declaration. gen ( p, ctx) ,
127
+ Self :: TSModuleDeclaration ( decl) => {
128
+ decl. gen ( p, ctx) ;
129
+ p. print_soft_newline ( ) ;
130
+ }
131
+ Self :: TSTypeAliasDeclaration ( decl) => {
132
+ p. print_indent ( ) ;
133
+ decl. gen ( p, ctx) ;
134
+ p. print_semicolon_after_statement ( ) ;
135
+ }
136
+ Self :: TSInterfaceDeclaration ( decl) => decl. gen ( p, ctx) ,
137
+ Self :: TSEnumDeclaration ( decl) => decl. gen ( p, ctx) ,
138
+ Self :: TSImportEqualsDeclaration ( decl) => {
139
+ p. print_indent ( ) ;
140
+ decl. gen ( p, ctx) ;
141
+ p. print_semicolon_after_statement ( ) ;
115
142
}
116
143
}
117
144
}
@@ -529,68 +556,6 @@ impl<const MINIFY: bool> Gen<MINIFY> for DebuggerStatement {
529
556
}
530
557
}
531
558
532
- impl < ' a , const MINIFY : bool > Gen < MINIFY > for ModuleDeclaration < ' a > {
533
- fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
534
- match self {
535
- Self :: ImportDeclaration ( decl) => decl. gen ( p, ctx) ,
536
- Self :: ExportAllDeclaration ( decl) => decl. gen ( p, ctx) ,
537
- Self :: ExportDefaultDeclaration ( decl) => decl. gen ( p, ctx) ,
538
- Self :: ExportNamedDeclaration ( decl) => decl. gen ( p, ctx) ,
539
- Self :: TSExportAssignment ( decl) => decl. gen ( p, ctx) ,
540
- Self :: TSNamespaceExportDeclaration ( decl) => decl. gen ( p, ctx) ,
541
- }
542
- }
543
- }
544
-
545
- impl < ' a , const MINIFY : bool > Gen < MINIFY > for Declaration < ' a > {
546
- fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
547
- match self {
548
- Self :: VariableDeclaration ( decl) => {
549
- decl. gen ( p, ctx) ;
550
- p. print_semicolon_after_statement ( ) ;
551
- }
552
- Self :: FunctionDeclaration ( decl) => {
553
- p. print_space_before_identifier ( ) ;
554
- decl. gen ( p, ctx) ;
555
- p. print_soft_newline ( ) ;
556
- }
557
- Self :: ClassDeclaration ( decl) => {
558
- p. print_space_before_identifier ( ) ;
559
- decl. gen ( p, ctx) ;
560
- p. print_soft_newline ( ) ;
561
- }
562
- Self :: UsingDeclaration ( declaration) => {
563
- p. print_space_before_identifier ( ) ;
564
- declaration. gen ( p, ctx) ;
565
- p. print_soft_newline ( ) ;
566
- }
567
- Self :: TSModuleDeclaration ( decl) => decl. gen ( p, ctx) ,
568
- Self :: TSTypeAliasDeclaration ( decl) => {
569
- if decl. modifiers . contains ( ModifierKind :: Export ) {
570
- p. print_str ( b"export " ) ;
571
- }
572
- if decl. modifiers . contains ( ModifierKind :: Declare ) {
573
- p. print_str ( b"declare " ) ;
574
- }
575
- p. print_str ( b"type" ) ;
576
- p. print_space_before_identifier ( ) ;
577
- decl. id . gen ( p, ctx) ;
578
- if let Some ( type_parameters) = & decl. type_parameters {
579
- type_parameters. gen ( p, ctx) ;
580
- }
581
- p. print_soft_space ( ) ;
582
- p. print_str ( b"=" ) ;
583
- p. print_soft_space ( ) ;
584
- decl. type_annotation . gen ( p, ctx) ;
585
- p. print_semicolon_after_statement ( ) ;
586
- }
587
- Declaration :: TSInterfaceDeclaration ( decl) => decl. gen ( p, ctx) ,
588
- Declaration :: TSEnumDeclaration ( decl) => decl. gen ( p, ctx) ,
589
- Declaration :: TSImportEqualsDeclaration ( decl) => decl. gen ( p, ctx) ,
590
- }
591
- }
592
- }
593
-
594
559
impl < ' a , const MINIFY : bool > Gen < MINIFY > for UsingDeclaration < ' a > {
595
560
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
596
561
if self . is_await {
@@ -635,6 +600,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for VariableDeclaration<'a> {
635
600
p. print_list ( & self . declarations , ctx) ;
636
601
}
637
602
}
603
+
638
604
impl < ' a , const MINIFY : bool > Gen < MINIFY > for VariableDeclarator < ' a > {
639
605
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
640
606
self . id . gen ( p, ctx) ;
@@ -692,12 +658,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Function<'a> {
692
658
impl < ' a , const MINIFY : bool > Gen < MINIFY > for FunctionBody < ' a > {
693
659
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
694
660
p. print_curly_braces ( self . span , self . is_empty ( ) , |p| {
695
- p. print_directives_and_statements_with_semicolon_order (
696
- Some ( & self . directives ) ,
697
- & self . statements ,
698
- ctx,
699
- true ,
700
- ) ;
661
+ p. print_directives_and_statements ( Some ( & self . directives ) , & self . statements , ctx) ;
701
662
} ) ;
702
663
p. needs_semicolon = false ;
703
664
}
@@ -883,7 +844,28 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ExportNamedDeclaration<'a> {
883
844
}
884
845
match & self . declaration {
885
846
Some ( decl) => {
886
- decl. gen ( p, ctx) ;
847
+ match decl {
848
+ Declaration :: VariableDeclaration ( decl) => decl. gen ( p, ctx) ,
849
+ Declaration :: FunctionDeclaration ( decl) => decl. gen ( p, ctx) ,
850
+ Declaration :: ClassDeclaration ( decl) => decl. gen ( p, ctx) ,
851
+ Declaration :: UsingDeclaration ( declaration) => declaration. gen ( p, ctx) ,
852
+ Declaration :: TSModuleDeclaration ( decl) => decl. gen ( p, ctx) ,
853
+ Declaration :: TSTypeAliasDeclaration ( decl) => decl. gen ( p, ctx) ,
854
+ Declaration :: TSInterfaceDeclaration ( decl) => decl. gen ( p, ctx) ,
855
+ Declaration :: TSEnumDeclaration ( decl) => decl. gen ( p, ctx) ,
856
+ Declaration :: TSImportEqualsDeclaration ( decl) => decl. gen ( p, ctx) ,
857
+ }
858
+ if matches ! (
859
+ decl,
860
+ Declaration :: VariableDeclaration ( _)
861
+ | Declaration :: TSTypeAliasDeclaration ( _)
862
+ | Declaration :: TSImportEqualsDeclaration ( _)
863
+ ) {
864
+ p. print_semicolon_after_statement ( ) ;
865
+ } else {
866
+ p. print_soft_newline ( ) ;
867
+ p. needs_semicolon = false ;
868
+ }
887
869
}
888
870
None => {
889
871
p. print ( b'{' ) ;
@@ -968,7 +950,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ExportAllDeclaration<'a> {
968
950
p. print_hard_space ( ) ;
969
951
}
970
952
self . with_clause . gen ( p, ctx) ;
971
-
972
953
p. print_semicolon_after_statement ( ) ;
973
954
}
974
955
}
@@ -2786,7 +2767,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSMappedType<'a> {
2786
2767
p. print_soft_space ( ) ;
2787
2768
type_annotation. gen ( p, ctx) ;
2788
2769
}
2789
- p. print_semicolon_if_needed ( ) ;
2790
2770
p. print_str ( b"}" ) ;
2791
2771
}
2792
2772
}
@@ -2961,7 +2941,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSFunctionType<'a> {
2961
2941
2962
2942
impl < ' a , const MINIFY : bool > Gen < MINIFY > for TSSignature < ' a > {
2963
2943
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
2964
- p. print_soft_space ( ) ;
2965
2944
match self {
2966
2945
Self :: TSIndexSignature ( signature) => signature. gen ( p, ctx) ,
2967
2946
Self :: TSPropertySignature ( signature) => {
@@ -3225,23 +3204,31 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleBlock<'a> {
3225
3204
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
3226
3205
p. print_curly_braces ( self . span , self . body . is_empty ( ) , |p| {
3227
3206
for item in & self . body {
3228
- item. gen ( p, ctx) ;
3229
3207
p. print_semicolon_if_needed ( ) ;
3208
+ item. gen ( p, ctx) ;
3230
3209
}
3231
3210
} ) ;
3232
3211
}
3233
3212
}
3234
3213
3235
3214
impl < ' a , const MINIFY : bool > Gen < MINIFY > for TSTypeAliasDeclaration < ' a > {
3236
3215
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
3237
- p. print_str ( b"type " ) ;
3216
+ if self . modifiers . contains ( ModifierKind :: Export ) {
3217
+ p. print_str ( b"export " ) ;
3218
+ }
3219
+ if self . modifiers . contains ( ModifierKind :: Declare ) {
3220
+ p. print_str ( b"declare " ) ;
3221
+ }
3222
+ p. print_str ( b"type" ) ;
3223
+ p. print_space_before_identifier ( ) ;
3238
3224
self . id . gen ( p, ctx) ;
3239
3225
if let Some ( type_parameters) = & self . type_parameters {
3240
3226
type_parameters. gen ( p, ctx) ;
3241
3227
}
3242
- p. print_str ( b" = " ) ;
3228
+ p. print_soft_space ( ) ;
3229
+ p. print_str ( b"=" ) ;
3230
+ p. print_soft_space ( ) ;
3243
3231
self . type_annotation . gen ( p, ctx) ;
3244
- p. print_semicolon_after_statement ( ) ;
3245
3232
}
3246
3233
}
3247
3234
@@ -3263,9 +3250,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSInterfaceDeclaration<'a> {
3263
3250
p. print_curly_braces ( self . body . span , self . body . body . is_empty ( ) , |p| {
3264
3251
for item in & self . body . body {
3265
3252
p. print_indent ( ) ;
3266
- p. print_semicolon_if_needed ( ) ;
3267
3253
item. gen ( p, ctx) ;
3268
- p. print_semicolon_after_statement ( ) ;
3254
+ p. print_semicolon ( ) ;
3255
+ p. print_soft_newline ( ) ;
3269
3256
}
3270
3257
} ) ;
3271
3258
if MINIFY {
@@ -3363,7 +3350,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSImportEqualsDeclaration<'a> {
3363
3350
self . id . gen ( p, ctx) ;
3364
3351
p. print_str ( b" = " ) ;
3365
3352
self . module_reference . gen ( p, ctx) ;
3366
- p. print_semicolon_after_statement ( ) ;
3367
3353
}
3368
3354
}
3369
3355
0 commit comments