@@ -15,11 +15,11 @@ use swc_ecma_ast::{
15
15
Constructor , Decl , DefaultDecl , DoWhileStmt , EsVersion , ExportAll , ExportDecl ,
16
16
ExportDefaultDecl , ExportSpecifier , FnDecl , ForInStmt , ForOfStmt , ForStmt , GetterProp , IfStmt ,
17
17
ImportDecl , ImportSpecifier , NamedExport , ObjectPat , Param , Pat , PrivateMethod , PrivateProp ,
18
- Program , ReturnStmt , SetterProp , Stmt , TsAsExpr , TsConstAssertion , TsEnumDecl ,
18
+ Program , ReturnStmt , SetterProp , Stmt , ThrowStmt , TsAsExpr , TsConstAssertion , TsEnumDecl ,
19
19
TsExportAssignment , TsImportEqualsDecl , TsIndexSignature , TsInstantiation , TsModuleDecl ,
20
20
TsModuleName , TsNamespaceDecl , TsNonNullExpr , TsParamPropParam , TsSatisfiesExpr ,
21
21
TsTypeAliasDecl , TsTypeAnn , TsTypeAssertion , TsTypeParamDecl , TsTypeParamInstantiation ,
22
- VarDeclarator , WhileStmt ,
22
+ VarDeclarator , WhileStmt , YieldExpr ,
23
23
} ;
24
24
use swc_ecma_parser:: {
25
25
lexer:: Lexer ,
@@ -596,6 +596,34 @@ impl TsStrip {
596
596
597
597
self . add_replacement ( * span) ;
598
598
}
599
+
600
+ // ```TypeScript
601
+ // return/yield/throw <T>
602
+ // (v: T) => v;
603
+ // ```
604
+ //
605
+ // ```TypeScript
606
+ // return/yield/throw (
607
+ // v ) => v;
608
+ // ```
609
+ fn fix_asi_in_arrow_expr ( & mut self , arrow_expr : & ArrowExpr ) {
610
+ if let Some ( tp) = & arrow_expr. type_params {
611
+ let l_paren = self . get_next_token ( tp. span . hi ) ;
612
+ debug_assert_eq ! ( l_paren. token, Token :: LParen ) ;
613
+
614
+ let slice = self . get_src_slice ( tp. span . with_hi ( l_paren. span . lo ) ) ;
615
+
616
+ if !slice. chars ( ) . any ( is_new_line) {
617
+ return ;
618
+ }
619
+
620
+ let l_paren_pos = l_paren. span . lo ;
621
+ let l_lt_pos = tp. span . lo ;
622
+
623
+ self . add_overwrite ( l_paren_pos, b' ' ) ;
624
+ self . add_overwrite ( l_lt_pos, b'(' ) ;
625
+ }
626
+ }
599
627
}
600
628
601
629
impl Visit for TsStrip {
@@ -695,39 +723,52 @@ impl Visit for TsStrip {
695
723
696
724
arg. visit_with ( self ) ;
697
725
698
- if let Some ( arrow_expr) = arg. as_arrow ( ) {
699
- if arrow_expr. is_async {
700
- // We have already handled type parameters in `visit_arrow_expr`.
701
- return ;
702
- }
726
+ let Some ( arrow_expr) = arg. as_arrow ( ) else {
727
+ return ;
728
+ } ;
703
729
704
- // ```TypeScript
705
- // return <T>
706
- // (v: T) => v;
707
- // ```
708
- //
709
- // ```TypeScript
710
- // return (
711
- // v ) => v;
712
- // ```
730
+ if arrow_expr. is_async {
731
+ // We have already handled type parameters in `visit_arrow_expr`.
732
+ return ;
733
+ }
713
734
714
- if let Some ( tp) = & arrow_expr. type_params {
715
- let l_paren = self . get_next_token ( tp. span . hi ) ;
716
- debug_assert_eq ! ( l_paren. token, Token :: LParen ) ;
735
+ self . fix_asi_in_arrow_expr ( arrow_expr) ;
736
+ }
717
737
718
- let slice = self . get_src_slice ( tp. span . with_hi ( l_paren. span . lo ) ) ;
738
+ fn visit_yield_expr ( & mut self , n : & YieldExpr ) {
739
+ let Some ( arg) = & n. arg else {
740
+ return ;
741
+ } ;
719
742
720
- if !slice. chars ( ) . any ( is_new_line) {
721
- return ;
722
- }
743
+ arg. visit_with ( self ) ;
723
744
724
- let l_paren_pos = l_paren. span . lo ;
725
- let l_lt_pos = tp. span . lo ;
745
+ let Some ( arrow_expr) = arg. as_arrow ( ) else {
746
+ return ;
747
+ } ;
726
748
727
- self . add_overwrite ( l_paren_pos , b' ' ) ;
728
- self . add_overwrite ( l_lt_pos , b'(' ) ;
729
- }
749
+ if arrow_expr . is_async {
750
+ // We have already handled type parameters in `visit_arrow_expr`.
751
+ return ;
730
752
}
753
+
754
+ self . fix_asi_in_arrow_expr ( arrow_expr) ;
755
+ }
756
+
757
+ fn visit_throw_stmt ( & mut self , n : & ThrowStmt ) {
758
+ let arg = & n. arg ;
759
+
760
+ arg. visit_with ( self ) ;
761
+
762
+ let Some ( arrow_expr) = arg. as_arrow ( ) else {
763
+ return ;
764
+ } ;
765
+
766
+ if arrow_expr. is_async {
767
+ // We have already handled type parameters in `visit_arrow_expr`.
768
+ return ;
769
+ }
770
+
771
+ self . fix_asi_in_arrow_expr ( arrow_expr) ;
731
772
}
732
773
733
774
fn visit_binding_ident ( & mut self , n : & BindingIdent ) {
0 commit comments