@@ -527,32 +527,31 @@ impl ArrayExpression {
527
527
let num_items = self . elements . len ( ) + self . non_code_meta . non_code_nodes_len ( ) ;
528
528
let mut elems = self . elements . iter ( ) ;
529
529
let mut found_line_comment = false ;
530
- let mut format_items: Vec < _ > = ( 0 ..num_items)
531
- . flat_map ( |i| {
532
- if let Some ( noncode) = self . non_code_meta . non_code_nodes . get ( & i) {
533
- noncode
534
- . iter ( )
535
- . map ( |nc| {
536
- found_line_comment |= nc. value . should_cause_array_newline ( ) ;
537
- nc. recast ( options, 0 )
538
- } )
539
- . collect :: < Vec < _ > > ( )
540
- } else {
541
- let el = elems. next ( ) . unwrap ( ) ;
542
- let s = format ! ( "{}, " , el. recast( options, 0 , ExprContext :: Other ) ) ;
543
- vec ! [ s]
544
- }
545
- } )
546
- . collect ( ) ;
530
+ let mut format_items: Vec < _ > = Vec :: with_capacity ( num_items) ;
531
+ for i in 0 ..num_items {
532
+ if let Some ( noncode) = self . non_code_meta . non_code_nodes . get ( & i) {
533
+ format_items. extend ( noncode. iter ( ) . map ( |nc| {
534
+ found_line_comment |= nc. value . should_cause_array_newline ( ) ;
535
+ nc. recast ( options, 0 )
536
+ } ) ) ;
537
+ } else {
538
+ let el = elems. next ( ) . unwrap ( ) ;
539
+ let s = format ! ( "{}, " , el. recast( options, 0 , ExprContext :: Other ) ) ;
540
+ format_items. push ( s) ;
541
+ }
542
+ }
547
543
548
544
// Format these items into a one-line array.
549
545
if let Some ( item) = format_items. last_mut ( ) {
550
546
if let Some ( norm) = item. strip_suffix ( ", " ) {
551
547
* item = norm. to_owned ( ) ;
552
548
}
553
549
}
554
- let format_items = format_items; // Remove mutability
555
- let flat_recast = format ! ( "[{}]" , format_items. join( "" ) ) ;
550
+ let mut flat_recast = "[" . to_owned ( ) ;
551
+ for fi in & format_items {
552
+ flat_recast. push_str ( fi)
553
+ }
554
+ flat_recast. push ( ']' ) ;
556
555
557
556
// We might keep the one-line representation, if it's short enough.
558
557
let max_array_length = 40 ;
@@ -562,29 +561,31 @@ impl ArrayExpression {
562
561
}
563
562
564
563
// Otherwise, we format a multi-line representation.
564
+ let mut output = "[\n " . to_owned ( ) ;
565
565
let inner_indentation = if ctxt == ExprContext :: Pipe {
566
566
options. get_indentation_offset_pipe ( indentation_level + 1 )
567
567
} else {
568
568
options. get_indentation ( indentation_level + 1 )
569
569
} ;
570
- let formatted_array_lines = format_items
571
- . iter ( )
572
- . map ( |s| {
573
- format ! (
574
- "{inner_indentation}{}{}" ,
575
- if let Some ( x) = s. strip_suffix( " " ) { x } else { s } ,
576
- if s. ends_with( '\n' ) { "" } else { "\n " }
577
- )
578
- } )
579
- . collect :: < Vec < String > > ( )
580
- . join ( "" )
581
- . to_owned ( ) ;
570
+ for format_item in format_items {
571
+ output. push_str ( & inner_indentation) ;
572
+ output. push_str ( if let Some ( x) = format_item. strip_suffix ( " " ) {
573
+ x
574
+ } else {
575
+ & format_item
576
+ } ) ;
577
+ if !format_item. ends_with ( '\n' ) {
578
+ output. push ( '\n' )
579
+ }
580
+ }
582
581
let end_indent = if ctxt == ExprContext :: Pipe {
583
582
options. get_indentation_offset_pipe ( indentation_level)
584
583
} else {
585
584
options. get_indentation ( indentation_level)
586
585
} ;
587
- format ! ( "[\n {formatted_array_lines}{end_indent}]" )
586
+ output. push_str ( & end_indent) ;
587
+ output. push ( ']' ) ;
588
+ output
588
589
}
589
590
}
590
591
0 commit comments