@@ -454,18 +454,27 @@ impl VisitMut for Fixer<'_> {
454
454
455
455
impl Fixer < ' _ > {
456
456
fn wrap_with_paren_if_required ( & mut self , e : & mut Expr ) {
457
+ let mut has_padding_value = false ;
457
458
match e {
458
459
// Flatten seq expr
459
460
Expr :: Seq ( SeqExpr { span, exprs } ) => {
460
461
let len = exprs
461
462
. iter ( )
462
463
. map ( |expr| match * * expr {
464
+ Expr :: Paren ( ParenExpr { ref expr, .. } ) => {
465
+ if let Expr :: Seq ( SeqExpr { exprs, .. } ) = expr. as_ref ( ) {
466
+ exprs. len ( )
467
+ } else {
468
+ 1
469
+ }
470
+ }
463
471
Expr :: Seq ( SeqExpr { ref exprs, .. } ) => exprs. len ( ) ,
464
472
_ => 1 ,
465
473
} )
466
474
. sum ( ) ;
467
475
468
476
let exprs_len = exprs. len ( ) ;
477
+ // don't has child seq
469
478
let expr = if len == exprs_len {
470
479
let mut exprs = exprs
471
480
. into_iter ( )
@@ -475,14 +484,15 @@ impl Fixer<'_> {
475
484
if is_last {
476
485
Some ( e. take ( ) )
477
486
} else {
478
- ignore_return_value ( e. take ( ) )
487
+ ignore_return_value ( e. take ( ) , & mut has_padding_value )
479
488
}
480
489
} )
481
490
. collect :: < Vec < _ > > ( ) ;
482
491
if exprs. len ( ) == 1 {
483
492
* e = * exprs. pop ( ) . unwrap ( ) ;
484
493
return ;
485
494
}
495
+ let exprs = ignore_padding_value ( exprs) ;
486
496
Expr :: Seq ( SeqExpr { span : * span, exprs } )
487
497
} else {
488
498
let mut buf = Vec :: with_capacity ( len) ;
@@ -493,15 +503,20 @@ impl Fixer<'_> {
493
503
Expr :: Seq ( SeqExpr { ref mut exprs, .. } ) => {
494
504
let exprs = exprs. take ( ) ;
495
505
if !is_last {
496
- buf. extend ( exprs. into_iter ( ) . filter_map ( ignore_return_value) ) ;
506
+ buf. extend ( exprs. into_iter ( ) . filter_map ( |expr| {
507
+ ignore_return_value ( expr, & mut has_padding_value)
508
+ } ) ) ;
497
509
} else {
498
510
let exprs_len = exprs. len ( ) ;
499
511
for ( i, expr) in exprs. into_iter ( ) . enumerate ( ) {
500
512
let is_last = i + 1 == exprs_len;
501
513
if is_last {
502
514
buf. push ( expr) ;
503
515
} else {
504
- buf. extend ( ignore_return_value ( expr) ) ;
516
+ buf. extend ( ignore_return_value (
517
+ expr,
518
+ & mut has_padding_value,
519
+ ) ) ;
505
520
}
506
521
}
507
522
}
@@ -510,7 +525,10 @@ impl Fixer<'_> {
510
525
if is_last {
511
526
buf. push ( expr. take ( ) ) ;
512
527
} else {
513
- buf. extend ( ignore_return_value ( expr. take ( ) ) ) ;
528
+ buf. extend ( ignore_return_value (
529
+ expr. take ( ) ,
530
+ & mut has_padding_value,
531
+ ) ) ;
514
532
}
515
533
}
516
534
}
@@ -520,16 +538,14 @@ impl Fixer<'_> {
520
538
* e = * buf. pop ( ) . unwrap ( ) ;
521
539
return ;
522
540
}
523
- buf. shrink_to_fit ( ) ;
524
541
525
- Expr :: Seq ( SeqExpr {
526
- span : * span,
527
- exprs : buf,
528
- } )
542
+ let exprs = ignore_padding_value ( buf) ;
543
+
544
+ Expr :: Seq ( SeqExpr { span : * span, exprs } )
529
545
} ;
530
546
531
547
match self . ctx {
532
- Context :: ForcedExpr { .. } | Context :: Callee { .. } => {
548
+ Context :: ForcedExpr { .. } => {
533
549
* e = Expr :: Paren ( ParenExpr {
534
550
span : * span,
535
551
expr : Box :: new ( expr) ,
@@ -569,6 +585,16 @@ impl Fixer<'_> {
569
585
}
570
586
}
571
587
588
+ Expr :: Call ( CallExpr {
589
+ callee : ExprOrSuper :: Expr ( ref mut callee) ,
590
+ ..
591
+ } ) if callee. is_seq ( ) => {
592
+ * callee = Box :: new ( Expr :: Paren ( ParenExpr {
593
+ span : callee. span ( ) ,
594
+ expr : callee. take ( ) ,
595
+ } ) )
596
+ }
597
+
572
598
Expr :: Call ( CallExpr {
573
599
callee : ExprOrSuper :: Expr ( ref mut callee) ,
574
600
..
@@ -676,9 +702,16 @@ impl Fixer<'_> {
676
702
}
677
703
}
678
704
679
- fn ignore_return_value ( expr : Box < Expr > ) -> Option < Box < Expr > > {
705
+ fn ignore_return_value ( expr : Box < Expr > , has_padding_value : & mut bool ) -> Option < Box < Expr > > {
680
706
match * expr {
681
- Expr :: Ident ( ..) | Expr :: Fn ( ..) | Expr :: Lit ( ..) => None ,
707
+ Expr :: Fn ( ..) | Expr :: Arrow ( ..) | Expr :: Lit ( ..) => {
708
+ if * has_padding_value {
709
+ None
710
+ } else {
711
+ * has_padding_value = true ;
712
+ Some ( expr)
713
+ }
714
+ }
682
715
Expr :: Seq ( SeqExpr { span, exprs } ) => {
683
716
let len = exprs. len ( ) ;
684
717
let mut exprs: Vec < _ > = exprs
@@ -688,7 +721,7 @@ fn ignore_return_value(expr: Box<Expr>) -> Option<Box<Expr>> {
688
721
if i + 1 == len {
689
722
Some ( expr)
690
723
} else {
691
- ignore_return_value ( expr)
724
+ ignore_return_value ( expr, has_padding_value )
692
725
}
693
726
} )
694
727
. collect ( ) ;
@@ -702,11 +735,30 @@ fn ignore_return_value(expr: Box<Expr>) -> Option<Box<Expr>> {
702
735
op : op ! ( "void" ) ,
703
736
arg,
704
737
..
705
- } ) => ignore_return_value ( arg) ,
738
+ } ) => ignore_return_value ( arg, has_padding_value ) ,
706
739
_ => Some ( expr) ,
707
740
}
708
741
}
709
742
743
+ // at least 3 element in seq, which means we can safely
744
+ // remove that padding, if not at last position
745
+ fn ignore_padding_value ( exprs : Vec < Box < Expr > > ) -> Vec < Box < Expr > > {
746
+ let len = exprs. len ( ) ;
747
+
748
+ if len > 2 {
749
+ exprs
750
+ . into_iter ( )
751
+ . enumerate ( )
752
+ . filter_map ( |( i, e) | match e. as_ref ( ) {
753
+ Expr :: Fn ( ..) | Expr :: Arrow ( ..) | Expr :: Lit ( ..) if i + 1 != len => None ,
754
+ _ => Some ( e) ,
755
+ } )
756
+ . collect ( )
757
+ } else {
758
+ exprs
759
+ }
760
+ }
761
+
710
762
#[ cfg( test) ]
711
763
mod tests {
712
764
use super :: fixer;
@@ -821,36 +873,44 @@ const _ref = {}, { c =( _tmp = {}, d = _extends({}, _tmp), _tmp) } = _ref;"
821
873
((a, b), (c())) + ((d, e), (f()));
822
874
" ,
823
875
"var a, b, c, d, e, f;
824
- c() + f( )"
876
+ (a, b, c()) + (d, e, f() )"
825
877
) ;
826
878
827
- test_fixer ! ( fixer_02, "(b, c), d;" , "d;" ) ;
879
+ test_fixer ! ( fixer_02, "(b, c), d;" , "b, c, d;" ) ;
828
880
829
- test_fixer ! ( fixer_03, "((a, b), (c && d)) && e;" , "c && d && e;" ) ;
881
+ test_fixer ! ( fixer_03, "((a, b), (c && d)) && e;" , "(a, b, c && d) && e;" ) ;
830
882
831
- test_fixer ! ( fixer_04, "for ((a, b), c;;) ;" , "for(c;;);" ) ;
883
+ test_fixer ! ( fixer_04, "for ((a, b), c;;) ;" , "for(a, b, c;;);" ) ;
832
884
833
885
test_fixer ! (
834
886
fixer_05,
835
887
"var a, b, c = (1), d, e, f = (2);
836
888
((a, b), c) + ((d, e), f);" ,
837
889
"var a, b, c = 1, d, e, f = 2;
838
- c + f ;"
890
+ (a, b, c) + (d, e, f) ;"
839
891
) ;
840
892
841
893
test_fixer ! (
842
894
fixer_06,
843
895
"var a, b, c, d;
844
896
a = ((b, c), d);" ,
845
897
"var a, b, c, d;
846
- a = d ;"
898
+ a = (b, c, d) ;"
847
899
) ;
848
900
849
- test_fixer ! ( fixer_07, "a => ((b, c) => ((a, b), c));" , "(a)=>(b, c)=>c;" ) ;
901
+ test_fixer ! (
902
+ fixer_07,
903
+ "a => ((b, c) => ((a, b), c));" ,
904
+ "(a)=>(b, c)=>(a, b, c);"
905
+ ) ;
850
906
851
- test_fixer ! ( fixer_08, "typeof (((1), a), (2));" , "typeof 2 " ) ;
907
+ test_fixer ! ( fixer_08, "typeof (((1), a), (2));" , "typeof (a, 2) " ) ;
852
908
853
- test_fixer ! ( fixer_09, "(((a, b), c), d) ? e : f;" , "d ? e : f;" ) ;
909
+ test_fixer ! (
910
+ fixer_09,
911
+ "(((a, b), c), d) ? e : f;" ,
912
+ "(a, b, c, d) ? e : f;"
913
+ ) ;
854
914
855
915
test_fixer ! (
856
916
fixer_10,
@@ -861,16 +921,18 @@ function a() {
861
921
" ,
862
922
"
863
923
function a() {
864
- return void 3;
924
+ return a, void 3;
865
925
}
866
926
"
867
927
) ;
868
928
869
- test_fixer ! ( fixer_11, "c && ((((2), (3)), d), b);" , "c && b" ) ;
929
+ test_fixer ! ( fixer_11, "c && ((((2), (3)), d), b);" , "c && (d, b)" ) ;
930
+
931
+ test_fixer ! ( fixer_12, "(((a, b), c), d) + e;" , "(a, b, c, d) + e;" ) ;
870
932
871
- test_fixer ! ( fixer_12 , "(((a, b ), c ), d) + e ;" , "d + e; " ) ;
933
+ test_fixer ! ( fixer_13 , "delete (((1 ), a ), (2)) ;" , "delete (a, 2) " ) ;
872
934
873
- test_fixer ! ( fixer_13 , "delete (((1), a), (2)); " , "delete 2 " ) ;
935
+ test_fixer ! ( fixer_14 , "(1, 2, a) " , "1, a " ) ;
874
936
875
937
identical ! ( issue_231, "'' + (truthy && '?') + truthy;" ) ;
876
938
@@ -950,7 +1012,7 @@ var store = global[SHARED] || (global[SHARED] = {});
950
1012
951
1013
identical ! ( bin_seq_expr_1, "(foo(), op) || (seq(), foo)" ) ;
952
1014
953
- test_fixer ! ( bin_seq_expr_2, "(foo, op) || (seq, foo)" , "op || foo ") ;
1015
+ identical ! ( bin_seq_expr_2, "(foo, op) || (seq, foo)" ) ;
954
1016
955
1017
identical ! ( cond_object_1, "let foo = {} ? 1 : 2;" ) ;
956
1018
@@ -1088,6 +1150,12 @@ var store = global[SHARED] || (global[SHARED] = {});
1088
1150
identical ! ( issue_1493, "('a' ?? 'b') || ''" ) ;
1089
1151
identical ! ( call_seq, "let x = ({}, () => 2)();" ) ;
1090
1152
1153
+ test_fixer ! (
1154
+ call_seq_with_padding,
1155
+ "let x = ({}, (1, 2), () => 2)();" ,
1156
+ "let x = ({}, () => 2)();"
1157
+ ) ;
1158
+
1091
1159
identical ! (
1092
1160
param_seq,
1093
1161
"function t(x = ({}, 2)) {
0 commit comments