@@ -193,45 +193,46 @@ fn print_if<const MINIFY: bool>(
193
193
p. print ( b'(' ) ;
194
194
p. print_expression ( & if_stmt. test ) ;
195
195
p. print ( b')' ) ;
196
- p. print_soft_space ( ) ;
197
196
198
197
match & if_stmt. consequent {
199
198
Statement :: BlockStatement ( block) => {
199
+ p. print_soft_space ( ) ;
200
200
p. print_block_statement ( block, ctx) ;
201
+ if if_stmt. alternate . is_some ( ) {
202
+ p. print_soft_space ( ) ;
203
+ } else {
204
+ p. print_soft_newline ( ) ;
205
+ }
201
206
}
202
207
stmt if wrap_to_avoid_ambiguous_else ( stmt) => {
208
+ p. print_soft_space ( ) ;
203
209
p. print_block_start ( stmt. span ( ) . start ) ;
204
210
stmt. gen ( p, ctx) ;
205
211
p. needs_semicolon = false ;
206
212
p. print_block_end ( stmt. span ( ) . end ) ;
213
+ if if_stmt. alternate . is_some ( ) {
214
+ p. print_soft_space ( ) ;
215
+ } else {
216
+ p. print_soft_newline ( ) ;
217
+ }
207
218
}
208
- stmt => {
209
- stmt. gen ( p, ctx) ;
210
- }
211
- }
212
- if if_stmt. alternate . is_some ( ) {
213
- p. print_soft_space ( ) ;
214
- } else {
215
- p. print_soft_newline ( ) ;
219
+ stmt => p. print_body ( stmt, false , ctx) ,
216
220
}
217
221
if let Some ( alternate) = if_stmt. alternate . as_ref ( ) {
218
222
p. print_semicolon_if_needed ( ) ;
219
223
p. print_space_before_identifier ( ) ;
220
- p. print_str ( b"else " ) ;
224
+ p. print_str ( b"else" ) ;
221
225
match alternate {
222
226
Statement :: BlockStatement ( block) => {
227
+ p. print_soft_space ( ) ;
223
228
p. print_block_statement ( block, ctx) ;
224
229
p. print_soft_newline ( ) ;
225
230
}
226
231
Statement :: IfStatement ( if_stmt) => {
232
+ p. print_hard_space ( ) ;
227
233
print_if ( if_stmt, p, ctx) ;
228
234
}
229
- _ => {
230
- p. print_soft_newline ( ) ;
231
- p. indent ( ) ;
232
- alternate. gen ( p, ctx) ;
233
- p. dedent ( ) ;
234
- }
235
+ stmt => p. print_body ( stmt, true , ctx) ,
235
236
}
236
237
}
237
238
}
@@ -296,12 +297,12 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ForStatement<'a> {
296
297
p. print_semicolon ( ) ;
297
298
298
299
if let Some ( update) = self . update . as_ref ( ) {
300
+ p. print_soft_space ( ) ;
299
301
p. print_expression ( update) ;
300
302
}
301
303
302
304
p. print ( b')' ) ;
303
- p. print_soft_space ( ) ;
304
- p. print_body ( & self . body , ctx) ;
305
+ p. print_body ( & self . body , false , ctx) ;
305
306
}
306
307
}
307
308
@@ -319,8 +320,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ForInStatement<'a> {
319
320
p. print_hard_space ( ) ;
320
321
p. print_expression ( & self . right ) ;
321
322
p. print ( b')' ) ;
322
- p. print_soft_space ( ) ;
323
- p. print_body ( & self . body , ctx) ;
323
+ p. print_body ( & self . body , false , ctx) ;
324
324
}
325
325
}
326
326
@@ -340,8 +340,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ForOfStatement<'a> {
340
340
p. print_str ( b"of " ) ;
341
341
self . right . gen_expr ( p, Precedence :: Assign , Context :: default ( ) ) ;
342
342
p. print ( b')' ) ;
343
- p. print_soft_space ( ) ;
344
- p. print_body ( & self . body , ctx) ;
343
+ p. print_body ( & self . body , false , ctx) ;
345
344
}
346
345
}
347
346
@@ -370,8 +369,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for WhileStatement<'a> {
370
369
p. print ( b'(' ) ;
371
370
p. print_expression ( & self . test ) ;
372
371
p. print ( b')' ) ;
373
- p. print_soft_space ( ) ;
374
- p. print_body ( & self . body , ctx) ;
372
+ p. print_body ( & self . body , false , ctx) ;
375
373
}
376
374
}
377
375
@@ -470,12 +468,8 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for SwitchCase<'a> {
470
468
p. print_colon ( ) ;
471
469
472
470
if self . consequent . len ( ) == 1 {
473
- if let Statement :: BlockStatement ( block) = & self . consequent [ 0 ] {
474
- p. print_soft_space ( ) ;
475
- p. print_block_statement ( block, ctx) ;
476
- p. print_soft_newline ( ) ;
477
- return ;
478
- }
471
+ p. print_body ( & self . consequent [ 0 ] , false , ctx) ;
472
+ return ;
479
473
}
480
474
481
475
p. print_soft_newline ( ) ;
@@ -503,12 +497,14 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ReturnStatement<'a> {
503
497
504
498
impl < ' a , const MINIFY : bool > Gen < MINIFY > for LabeledStatement < ' a > {
505
499
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
506
- p. add_source_mapping ( self . span . start ) ;
507
- p. print_indent ( ) ;
500
+ if !MINIFY && ( p. indent > 0 || p. print_next_indent_as_space ) {
501
+ p. add_source_mapping ( self . span . start ) ;
502
+ p. print_indent ( ) ;
503
+ }
504
+ p. print_space_before_identifier ( ) ;
508
505
self . label . gen ( p, ctx) ;
509
506
p. print_colon ( ) ;
510
- p. print_soft_space ( ) ;
511
- p. print_body ( & self . body , ctx) ;
507
+ p. print_body ( & self . body , false , ctx) ;
512
508
}
513
509
}
514
510
@@ -531,6 +527,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TryStatement<'a> {
531
527
}
532
528
p. print_soft_space ( ) ;
533
529
p. print_block_statement ( & handler. body , ctx) ;
530
+ if self . finalizer . is_some ( ) {
531
+ p. print_soft_newline ( ) ;
532
+ }
534
533
}
535
534
if let Some ( finalizer) = & self . finalizer {
536
535
p. print_soft_space ( ) ;
@@ -560,7 +559,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for WithStatement<'a> {
560
559
p. print ( b'(' ) ;
561
560
p. print_expression ( & self . object ) ;
562
561
p. print ( b')' ) ;
563
- p. print_body ( & self . body , ctx) ;
562
+ p. print_body ( & self . body , false , ctx) ;
564
563
}
565
564
}
566
565
@@ -716,6 +715,10 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ImportDeclaration<'a> {
716
715
}
717
716
if let Some ( specifiers) = & self . specifiers {
718
717
if specifiers. is_empty ( ) {
718
+ p. print_str ( b"{}" ) ;
719
+ p. print_soft_space ( ) ;
720
+ p. print_str ( b"from" ) ;
721
+ p. print_soft_space ( ) ;
719
722
p. print ( b'\'' ) ;
720
723
p. print_str ( self . source . value . as_bytes ( ) ) ;
721
724
p. print ( b'\'' ) ;
@@ -732,32 +735,39 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ImportDeclaration<'a> {
732
735
match specifier {
733
736
ImportDeclarationSpecifier :: ImportDefaultSpecifier ( spec) => {
734
737
if in_block {
738
+ p. print_soft_space ( ) ;
735
739
p. print_str ( b"}," ) ;
736
740
in_block = false ;
737
741
} else if index != 0 {
738
742
p. print_comma ( ) ;
743
+ p. print_soft_space ( ) ;
739
744
}
740
745
spec. local . gen ( p, ctx) ;
741
746
}
742
747
ImportDeclarationSpecifier :: ImportNamespaceSpecifier ( spec) => {
743
748
if in_block {
749
+ p. print_soft_space ( ) ;
744
750
p. print_str ( b"}," ) ;
745
751
in_block = false ;
746
752
} else if index != 0 {
747
753
p. print_comma ( ) ;
754
+ p. print_soft_space ( ) ;
748
755
}
749
756
p. print_str ( b"* as " ) ;
750
757
spec. local . gen ( p, ctx) ;
751
758
}
752
759
ImportDeclarationSpecifier :: ImportSpecifier ( spec) => {
753
760
if in_block {
754
761
p. print_comma ( ) ;
762
+ p. print_soft_space ( ) ;
755
763
} else {
756
764
if index != 0 {
757
765
p. print_comma ( ) ;
766
+ p. print_soft_space ( ) ;
758
767
}
759
768
in_block = true ;
760
769
p. print ( b'{' ) ;
770
+ p. print_soft_space ( ) ;
761
771
}
762
772
763
773
if spec. import_kind . is_type ( ) {
@@ -785,6 +795,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ImportDeclaration<'a> {
785
795
}
786
796
}
787
797
if in_block {
798
+ p. print_soft_space ( ) ;
788
799
p. print ( b'}' ) ;
789
800
}
790
801
p. print_str ( b" from " ) ;
@@ -825,6 +836,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ImportAttribute<'a> {
825
836
ImportAttributeKey :: StringLiteral ( literal) => literal. gen ( p, ctx) ,
826
837
} ;
827
838
p. print_colon ( ) ;
839
+ p. print_soft_space ( ) ;
828
840
self . value . gen ( p, ctx) ;
829
841
}
830
842
}
@@ -1477,14 +1489,16 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for ObjectExpression<'a> {
1477
1489
fn gen_expr ( & self , p : & mut Codegen < { MINIFY } > , _precedence : Precedence , ctx : Context ) {
1478
1490
let n = p. code_len ( ) ;
1479
1491
p. wrap ( p. start_of_stmt == n || p. start_of_arrow_expr == n, |p| {
1480
- let single_line = self . properties . is_empty ( ) ;
1492
+ let single_line = self . properties . len ( ) <= 1 ;
1481
1493
p. print_curly_braces ( self . span , single_line, |p| {
1482
1494
for ( index, item) in self . properties . iter ( ) . enumerate ( ) {
1483
1495
if index != 0 {
1484
1496
p. print_comma ( ) ;
1485
1497
p. print_soft_newline ( ) ;
1486
1498
}
1487
- p. print_indent ( ) ;
1499
+ if !single_line {
1500
+ p. print_indent ( ) ;
1501
+ }
1488
1502
item. gen ( p, ctx) ;
1489
1503
}
1490
1504
if !single_line {
@@ -1691,9 +1705,10 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for BinaryExpression<'a> {
1691
1705
self . left . gen_expr ( p, left_precedence, ctx) ;
1692
1706
if self . operator . is_keyword ( ) {
1693
1707
p. print_space_before_identifier ( ) ;
1708
+ } else {
1709
+ p. print_soft_space ( ) ;
1694
1710
}
1695
1711
self . operator . gen ( p, ctx) ;
1696
- p. print_soft_space ( ) ;
1697
1712
let right_precedence = if self . precedence ( ) . is_left_associative ( ) {
1698
1713
self . precedence ( )
1699
1714
} else {
@@ -1714,6 +1729,7 @@ impl<const MINIFY: bool> Gen<MINIFY> for BinaryOperator {
1714
1729
let op: Operator = ( * self ) . into ( ) ;
1715
1730
p. print_space_before_operator ( op) ;
1716
1731
p. print_str ( operator) ;
1732
+ p. print_soft_space ( ) ;
1717
1733
p. prev_op = Some ( op) ;
1718
1734
p. prev_op_end = p. code ( ) . len ( ) ;
1719
1735
}
@@ -1755,7 +1771,7 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for ConditionalExpression<'a> {
1755
1771
p. print_soft_space ( ) ;
1756
1772
self . consequent . gen_expr ( p, Precedence :: Assign , ctx. and_in ( true ) ) ;
1757
1773
p. print_soft_space ( ) ;
1758
- p. print ( b':' ) ;
1774
+ p. print_colon ( ) ;
1759
1775
p. print_soft_space ( ) ;
1760
1776
self . alternate . gen_expr ( p, Precedence :: Assign , ctx. union_in_if ( wrap) ) ;
1761
1777
} ) ;
@@ -1898,7 +1914,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for AssignmentTargetMaybeDefault<'a> {
1898
1914
impl < ' a , const MINIFY : bool > Gen < MINIFY > for AssignmentTargetWithDefault < ' a > {
1899
1915
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
1900
1916
self . binding . gen ( p, ctx) ;
1917
+ p. print_soft_space ( ) ;
1901
1918
p. print_equal ( ) ;
1919
+ p. print_soft_space ( ) ;
1902
1920
self . init . gen_expr ( p, Precedence :: Assign , Context :: default ( ) ) ;
1903
1921
}
1904
1922
}
@@ -1916,7 +1934,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for AssignmentTargetPropertyIdentifier<
1916
1934
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
1917
1935
self . binding . gen ( p, ctx) ;
1918
1936
if let Some ( expr) = & self . init {
1937
+ p. print_soft_space ( ) ;
1919
1938
p. print_equal ( ) ;
1939
+ p. print_soft_space ( ) ;
1920
1940
expr. gen_expr ( p, Precedence :: Assign , Context :: default ( ) ) ;
1921
1941
}
1922
1942
}
@@ -1938,6 +1958,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for AssignmentTargetPropertyProperty<'a
1938
1958
}
1939
1959
}
1940
1960
p. print_colon ( ) ;
1961
+ p. print_soft_space ( ) ;
1941
1962
self . binding . gen ( p, ctx) ;
1942
1963
}
1943
1964
}
@@ -2157,7 +2178,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for JSXElementName<'a> {
2157
2178
impl < ' a , const MINIFY : bool > Gen < MINIFY > for JSXNamespacedName < ' a > {
2158
2179
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
2159
2180
self . namespace . gen ( p, ctx) ;
2160
- p. print ( b':' ) ;
2181
+ p. print_colon ( ) ;
2161
2182
self . property . gen ( p, ctx) ;
2162
2183
}
2163
2184
}
@@ -2175,7 +2196,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for JSXAttribute<'a> {
2175
2196
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
2176
2197
self . name . gen ( p, ctx) ;
2177
2198
if let Some ( value) = & self . value {
2178
- p. print ( b'=' ) ;
2199
+ p. print_equal ( ) ;
2179
2200
value. gen ( p, ctx) ;
2180
2201
}
2181
2202
}
@@ -2326,6 +2347,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for StaticBlock<'a> {
2326
2347
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
2327
2348
p. add_source_mapping ( self . span . start ) ;
2328
2349
p. print_str ( b"static" ) ;
2350
+ p. print_soft_space ( ) ;
2329
2351
p. print_curly_braces ( self . span , self . body . is_empty ( ) , |p| {
2330
2352
for stmt in & self . body {
2331
2353
p. print_semicolon_if_needed ( ) ;
@@ -2487,13 +2509,15 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ObjectPattern<'a> {
2487
2509
fn gen ( & self , p : & mut Codegen < MINIFY > , ctx : Context ) {
2488
2510
p. add_source_mapping ( self . span . start ) ;
2489
2511
p. print ( b'{' ) ;
2512
+ p. print_soft_space ( ) ;
2490
2513
p. print_list ( & self . properties , ctx) ;
2491
2514
if let Some ( rest) = & self . rest {
2492
2515
if !self . properties . is_empty ( ) {
2493
2516
p. print_comma ( ) ;
2494
2517
}
2495
2518
rest. gen ( p, ctx) ;
2496
2519
}
2520
+ p. print_soft_space ( ) ;
2497
2521
p. print ( b'}' ) ;
2498
2522
p. add_source_mapping ( self . span . end ) ;
2499
2523
}
@@ -2513,6 +2537,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for BindingProperty<'a> {
2513
2537
}
2514
2538
if !self . shorthand {
2515
2539
p. print_colon ( ) ;
2540
+ p. print_soft_space ( ) ;
2516
2541
}
2517
2542
self . value . gen ( p, ctx) ;
2518
2543
}
@@ -2876,12 +2901,15 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSTemplateLiteralType<'a> {
2876
2901
2877
2902
impl < ' a , const MINIFY : bool > Gen < MINIFY > for TSTypeLiteral < ' a > {
2878
2903
fn gen ( & self , p : & mut Codegen < { MINIFY } > , ctx : Context ) {
2879
- p. print_curly_braces ( self . span , self . members . is_empty ( ) , |p| {
2904
+ let single_line = self . members . len ( ) <= 1 ;
2905
+ p. print_curly_braces ( self . span , single_line, |p| {
2880
2906
for item in & self . members {
2881
2907
p. print_indent ( ) ;
2882
2908
item. gen ( p, ctx) ;
2883
- p. print_semicolon ( ) ;
2884
- p. print_soft_newline ( ) ;
2909
+ if !single_line {
2910
+ p. print_semicolon ( ) ;
2911
+ p. print_soft_newline ( ) ;
2912
+ }
2885
2913
}
2886
2914
} ) ;
2887
2915
}
0 commit comments