@@ -15,10 +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 , SetterProp , Stmt , TsAsExpr , TsConstAssertion , TsEnumDecl , TsExportAssignment ,
19
- TsImportEqualsDecl , TsIndexSignature , TsInstantiation , TsModuleDecl , TsModuleName ,
20
- TsNamespaceDecl , TsNonNullExpr , TsParamPropParam , TsSatisfiesExpr , TsTypeAliasDecl , TsTypeAnn ,
21
- TsTypeAssertion , TsTypeParamDecl , TsTypeParamInstantiation , VarDeclarator , WhileStmt ,
18
+ Program , ReturnStmt , SetterProp , Stmt , TsAsExpr , TsConstAssertion , TsEnumDecl ,
19
+ TsExportAssignment , TsImportEqualsDecl , TsIndexSignature , TsInstantiation , TsModuleDecl ,
20
+ TsModuleName , TsNamespaceDecl , TsNonNullExpr , TsParamPropParam , TsSatisfiesExpr ,
21
+ TsTypeAliasDecl , TsTypeAnn , TsTypeAssertion , TsTypeParamDecl , TsTypeParamInstantiation ,
22
+ VarDeclarator , WhileStmt ,
22
23
} ;
23
24
use swc_ecma_parser:: {
24
25
lexer:: Lexer ,
@@ -610,11 +611,6 @@ impl Visit for TsStrip {
610
611
}
611
612
612
613
fn visit_arrow_expr ( & mut self , n : & ArrowExpr ) {
613
- #[ inline( always) ]
614
- fn is_new_line ( c : char ) -> bool {
615
- matches ! ( c, '\u{000A}' | '\u{000D}' | '\u{2028}' | '\u{2029}' )
616
- }
617
-
618
614
' type_params: {
619
615
// ```TypeScript
620
616
// let f = async <
@@ -692,6 +688,48 @@ impl Visit for TsStrip {
692
688
n. body . visit_with ( self ) ;
693
689
}
694
690
691
+ fn visit_return_stmt ( & mut self , n : & ReturnStmt ) {
692
+ let Some ( arg) = n. arg . as_deref ( ) else {
693
+ return ;
694
+ } ;
695
+
696
+ arg. visit_with ( self ) ;
697
+
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
+ }
703
+
704
+ // ```TypeScript
705
+ // return <T>
706
+ // (v: T) => v;
707
+ // ```
708
+ //
709
+ // ```TypeScript
710
+ // return (
711
+ // v ) => v;
712
+ // ```
713
+
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 ) ;
717
+
718
+ let slice = self . get_src_slice ( tp. span . with_hi ( l_paren. span . lo ) ) ;
719
+
720
+ if !slice. chars ( ) . any ( is_new_line) {
721
+ return ;
722
+ }
723
+
724
+ let l_paren_pos = l_paren. span . lo ;
725
+ let l_lt_pos = tp. span . lo ;
726
+
727
+ self . add_overwrite ( l_paren_pos, b' ' ) ;
728
+ self . add_overwrite ( l_lt_pos, b'(' ) ;
729
+ }
730
+ }
731
+ }
732
+
695
733
fn visit_binding_ident ( & mut self , n : & BindingIdent ) {
696
734
n. visit_children_with ( self ) ;
697
735
@@ -1303,6 +1341,10 @@ impl Visit for TsStrip {
1303
1341
}
1304
1342
}
1305
1343
1344
+ #[ inline( always) ]
1345
+ fn is_new_line ( c : char ) -> bool {
1346
+ matches ! ( c, '\u{000A}' | '\u{000D}' | '\u{2028}' | '\u{2029}' )
1347
+ }
1306
1348
trait IsTsDecl {
1307
1349
fn is_ts_declare ( & self ) -> bool ;
1308
1350
}
0 commit comments