@@ -12,12 +12,12 @@ use swc_common::{
12
12
} ;
13
13
use swc_ecma_ast:: {
14
14
ArrowExpr , BindingIdent , Class , ClassDecl , ClassMethod , ClassProp , Decl , DoWhileStmt ,
15
- EsVersion , ExportAll , ExportDecl , ExportSpecifier , FnDecl , ForInStmt , ForOfStmt , ForStmt ,
16
- IfStmt , ImportDecl , ImportSpecifier , NamedExport , Param , Pat , Program , Stmt , TsAsExpr ,
17
- TsConstAssertion , TsEnumDecl , TsExportAssignment , TsImportEqualsDecl , TsIndexSignature ,
18
- TsInstantiation , TsInterfaceDecl , TsModuleDecl , TsModuleName , TsNamespaceDecl , TsNonNullExpr ,
15
+ EsVersion , ExportAll , ExportDecl , ExportDefaultDecl , ExportSpecifier , FnDecl , ForInStmt ,
16
+ ForOfStmt , ForStmt , IfStmt , ImportDecl , ImportSpecifier , NamedExport , Param , Pat , Program ,
17
+ Stmt , TsAsExpr , TsConstAssertion , TsEnumDecl , TsExportAssignment , TsImportEqualsDecl ,
18
+ TsIndexSignature , TsInstantiation , TsModuleDecl , TsModuleName , TsNamespaceDecl , TsNonNullExpr ,
19
19
TsParamPropParam , TsSatisfiesExpr , TsTypeAliasDecl , TsTypeAnn , TsTypeAssertion ,
20
- TsTypeParamDecl , TsTypeParamInstantiation , VarDecl , WhileStmt ,
20
+ TsTypeParamDecl , TsTypeParamInstantiation , WhileStmt ,
21
21
} ;
22
22
use swc_ecma_parser:: {
23
23
lexer:: Lexer ,
@@ -532,16 +532,6 @@ impl Visit for TsStrip {
532
532
n. visit_children_with ( self ) ;
533
533
}
534
534
535
- fn visit_class_decl ( & mut self , n : & ClassDecl ) {
536
- if n. declare {
537
- self . add_replacement ( n. span ( ) ) ;
538
- self . fix_asi ( n. span ( ) ) ;
539
- return ;
540
- }
541
-
542
- n. visit_children_with ( self ) ;
543
- }
544
-
545
535
fn visit_class_method ( & mut self , n : & ClassMethod ) {
546
536
if n. function . body . is_none ( ) || n. is_abstract {
547
537
self . add_replacement ( n. span ) ;
@@ -636,19 +626,33 @@ impl Visit for TsStrip {
636
626
}
637
627
638
628
fn visit_export_decl ( & mut self , n : & ExportDecl ) {
639
- match n. decl {
640
- swc_ecma_ast:: Decl :: TsInterface ( _)
641
- | swc_ecma_ast:: Decl :: TsTypeAlias ( _)
642
- | swc_ecma_ast:: Decl :: TsEnum ( _)
643
- | swc_ecma_ast:: Decl :: TsModule ( _) => {
644
- self . add_replacement ( n. span ) ;
645
- self . fix_asi ( n. span ) ;
646
- }
629
+ if n. decl . is_ts_declare ( ) {
630
+ self . add_replacement ( n. span ) ;
631
+ self . fix_asi ( n. span ) ;
632
+ return ;
633
+ }
647
634
648
- _ => {
649
- n. visit_children_with ( self ) ;
650
- }
635
+ n. visit_children_with ( self ) ;
636
+ }
637
+
638
+ fn visit_export_default_decl ( & mut self , n : & ExportDefaultDecl ) {
639
+ if n. decl . is_ts_interface_decl ( ) {
640
+ self . add_replacement ( n. span ) ;
641
+ self . fix_asi ( n. span ) ;
642
+ return ;
651
643
}
644
+
645
+ n. visit_children_with ( self ) ;
646
+ }
647
+
648
+ fn visit_decl ( & mut self , n : & Decl ) {
649
+ if n. is_ts_declare ( ) {
650
+ self . add_replacement ( n. span ( ) ) ;
651
+ self . fix_asi ( n. span ( ) ) ;
652
+ return ;
653
+ }
654
+
655
+ n. visit_children_with ( self ) ;
652
656
}
653
657
654
658
fn visit_fn_decl ( & mut self , n : & FnDecl ) {
@@ -756,21 +760,6 @@ impl Visit for TsStrip {
756
760
n. expr . visit_children_with ( self ) ;
757
761
}
758
762
759
- fn visit_ts_enum_decl ( & mut self , e : & TsEnumDecl ) {
760
- if e. declare {
761
- self . add_replacement ( e. span ) ;
762
- self . fix_asi ( e. span ) ;
763
- return ;
764
- }
765
-
766
- HANDLER . with ( |handler| {
767
- handler. span_err (
768
- e. span ,
769
- "TypeScript enum is not supported in strip-only mode" ,
770
- ) ;
771
- } ) ;
772
- }
773
-
774
763
fn visit_ts_export_assignment ( & mut self , n : & TsExportAssignment ) {
775
764
HANDLER . with ( |handler| {
776
765
handler. span_err (
@@ -805,18 +794,16 @@ impl Visit for TsStrip {
805
794
n. expr . visit_children_with ( self ) ;
806
795
}
807
796
808
- fn visit_ts_interface_decl ( & mut self , n : & TsInterfaceDecl ) {
809
- self . add_replacement ( n. span ) ;
810
- self . fix_asi ( n. span ) ;
797
+ fn visit_ts_enum_decl ( & mut self , e : & TsEnumDecl ) {
798
+ HANDLER . with ( |handler| {
799
+ handler. span_err (
800
+ e. span ,
801
+ "TypeScript enum is not supported in strip-only mode" ,
802
+ ) ;
803
+ } ) ;
811
804
}
812
805
813
806
fn visit_ts_module_decl ( & mut self , n : & TsModuleDecl ) {
814
- if n. declare || matches ! ( n. id, TsModuleName :: Str ( ..) ) {
815
- self . add_replacement ( n. span ) ;
816
- self . fix_asi ( n. span ) ;
817
- return ;
818
- }
819
-
820
807
HANDLER . with ( |handler| {
821
808
handler. span_err (
822
809
n. span ( ) ,
@@ -826,12 +813,6 @@ impl Visit for TsStrip {
826
813
}
827
814
828
815
fn visit_ts_namespace_decl ( & mut self , n : & TsNamespaceDecl ) {
829
- if n. declare {
830
- self . add_replacement ( n. span ) ;
831
- self . fix_asi ( n. span ) ;
832
- return ;
833
- }
834
-
835
816
HANDLER . with ( |handler| {
836
817
handler. span_err (
837
818
n. span ( ) ,
@@ -904,25 +885,15 @@ impl Visit for TsStrip {
904
885
self . add_replacement ( span ( n. span . lo , n. span . hi ) ) ;
905
886
}
906
887
907
- fn visit_var_decl ( & mut self , n : & VarDecl ) {
908
- if n. declare {
909
- self . add_replacement ( n. span ) ;
910
- self . fix_asi ( n. span ) ;
911
- return ;
912
- }
913
-
914
- n. visit_children_with ( self ) ;
915
- }
916
-
917
888
fn visit_if_stmt ( & mut self , n : & IfStmt ) {
918
889
n. visit_children_with ( self ) ;
919
890
920
- if n. cons . is_ts_stmt ( ) {
891
+ if n. cons . is_ts_declare ( ) {
921
892
self . add_overwrite ( n. cons . span_lo ( ) , b';' ) ;
922
893
}
923
894
924
895
if let Some ( alt) = & n. alt {
925
- if alt. is_ts_stmt ( ) {
896
+ if alt. is_ts_declare ( ) {
926
897
self . add_overwrite ( alt. span_lo ( ) , b';' ) ;
927
898
}
928
899
}
@@ -931,58 +902,70 @@ impl Visit for TsStrip {
931
902
fn visit_for_stmt ( & mut self , n : & ForStmt ) {
932
903
n. visit_children_with ( self ) ;
933
904
934
- if n. body . is_ts_stmt ( ) {
905
+ if n. body . is_ts_declare ( ) {
935
906
self . add_overwrite ( n. body . span_lo ( ) , b';' ) ;
936
907
}
937
908
}
938
909
939
910
fn visit_for_in_stmt ( & mut self , n : & ForInStmt ) {
940
911
n. visit_children_with ( self ) ;
941
912
942
- if n. body . is_ts_stmt ( ) {
913
+ if n. body . is_ts_declare ( ) {
943
914
self . add_overwrite ( n. body . span_lo ( ) , b';' ) ;
944
915
}
945
916
}
946
917
947
918
fn visit_for_of_stmt ( & mut self , n : & ForOfStmt ) {
948
919
n. visit_children_with ( self ) ;
949
920
950
- if n. body . is_ts_stmt ( ) {
921
+ if n. body . is_ts_declare ( ) {
951
922
self . add_overwrite ( n. body . span_lo ( ) , b';' ) ;
952
923
}
953
924
}
954
925
955
926
fn visit_while_stmt ( & mut self , n : & WhileStmt ) {
956
927
n. visit_children_with ( self ) ;
957
928
958
- if n. body . is_ts_stmt ( ) {
929
+ if n. body . is_ts_declare ( ) {
959
930
self . add_overwrite ( n. body . span_lo ( ) , b';' ) ;
960
931
}
961
932
}
962
933
963
934
fn visit_do_while_stmt ( & mut self , n : & DoWhileStmt ) {
964
935
n. visit_children_with ( self ) ;
965
936
966
- if n. body . is_ts_stmt ( ) {
937
+ if n. body . is_ts_declare ( ) {
967
938
self . add_overwrite ( n. body . span_lo ( ) , b';' ) ;
968
939
}
969
940
}
970
941
}
971
942
972
- trait IsTsStmt {
973
- fn is_ts_stmt ( & self ) -> bool ;
943
+ trait IsTsDecl {
944
+ fn is_ts_declare ( & self ) -> bool ;
974
945
}
975
946
976
- impl IsTsStmt for Stmt {
977
- fn is_ts_stmt ( & self ) -> bool {
947
+ impl IsTsDecl for Decl {
948
+ fn is_ts_declare ( & self ) -> bool {
978
949
match self {
979
- Stmt :: Decl ( Decl :: TsInterface { .. } | Decl :: TsTypeAlias ( ..) ) => true ,
980
- Stmt :: Decl ( Decl :: TsModule ( n) ) => n. declare || matches ! ( n. id, TsModuleName :: Str ( ..) ) ,
981
- Stmt :: Decl ( Decl :: TsEnum ( e) ) => e. declare ,
950
+ Self :: TsInterface { .. } | Self :: TsTypeAlias ( ..) => true ,
951
+
952
+ Self :: TsModule ( module) => module. declare || matches ! ( module. id, TsModuleName :: Str ( ..) ) ,
953
+ Self :: TsEnum ( ref r#enum) => r#enum. declare ,
954
+
955
+ Self :: Var ( ref var) => var. declare ,
956
+ Self :: Fn ( FnDecl { declare : true , .. } )
957
+ | Self :: Class ( ClassDecl { declare : true , .. } ) => true ,
982
958
_ => false ,
983
959
}
984
960
}
985
961
}
962
+
963
+ impl IsTsDecl for Stmt {
964
+ fn is_ts_declare ( & self ) -> bool {
965
+ self . as_decl ( ) . map_or ( false , IsTsDecl :: is_ts_declare)
966
+ }
967
+ }
968
+
986
969
trait U8Helper {
987
970
fn is_utf8_char_boundary ( & self ) -> bool ;
988
971
}
0 commit comments