Skip to content

Commit c53cce4

Browse files
authoredAug 4, 2024··
fix(es/typescript): Strip declare export in strip-only mode (#9374)
- Closes #9373
1 parent c2e3021 commit c53cce4

14 files changed

+117
-83
lines changed
 

‎.changeset/few-bats-battle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
swc_fast_ts_strip: patch
3+
---
4+
5+
fix(es/typescript): Strip declare export in strip-only mode

‎crates/swc_fast_ts_strip/src/lib.rs

+63-80
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ use swc_common::{
1212
};
1313
use swc_ecma_ast::{
1414
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,
1919
TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion,
20-
TsTypeParamDecl, TsTypeParamInstantiation, VarDecl, WhileStmt,
20+
TsTypeParamDecl, TsTypeParamInstantiation, WhileStmt,
2121
};
2222
use swc_ecma_parser::{
2323
lexer::Lexer,
@@ -532,16 +532,6 @@ impl Visit for TsStrip {
532532
n.visit_children_with(self);
533533
}
534534

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-
545535
fn visit_class_method(&mut self, n: &ClassMethod) {
546536
if n.function.body.is_none() || n.is_abstract {
547537
self.add_replacement(n.span);
@@ -636,19 +626,33 @@ impl Visit for TsStrip {
636626
}
637627

638628
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+
}
647634

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;
651643
}
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);
652656
}
653657

654658
fn visit_fn_decl(&mut self, n: &FnDecl) {
@@ -756,21 +760,6 @@ impl Visit for TsStrip {
756760
n.expr.visit_children_with(self);
757761
}
758762

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-
774763
fn visit_ts_export_assignment(&mut self, n: &TsExportAssignment) {
775764
HANDLER.with(|handler| {
776765
handler.span_err(
@@ -805,18 +794,16 @@ impl Visit for TsStrip {
805794
n.expr.visit_children_with(self);
806795
}
807796

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+
});
811804
}
812805

813806
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-
820807
HANDLER.with(|handler| {
821808
handler.span_err(
822809
n.span(),
@@ -826,12 +813,6 @@ impl Visit for TsStrip {
826813
}
827814

828815
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-
835816
HANDLER.with(|handler| {
836817
handler.span_err(
837818
n.span(),
@@ -904,25 +885,15 @@ impl Visit for TsStrip {
904885
self.add_replacement(span(n.span.lo, n.span.hi));
905886
}
906887

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-
917888
fn visit_if_stmt(&mut self, n: &IfStmt) {
918889
n.visit_children_with(self);
919890

920-
if n.cons.is_ts_stmt() {
891+
if n.cons.is_ts_declare() {
921892
self.add_overwrite(n.cons.span_lo(), b';');
922893
}
923894

924895
if let Some(alt) = &n.alt {
925-
if alt.is_ts_stmt() {
896+
if alt.is_ts_declare() {
926897
self.add_overwrite(alt.span_lo(), b';');
927898
}
928899
}
@@ -931,58 +902,70 @@ impl Visit for TsStrip {
931902
fn visit_for_stmt(&mut self, n: &ForStmt) {
932903
n.visit_children_with(self);
933904

934-
if n.body.is_ts_stmt() {
905+
if n.body.is_ts_declare() {
935906
self.add_overwrite(n.body.span_lo(), b';');
936907
}
937908
}
938909

939910
fn visit_for_in_stmt(&mut self, n: &ForInStmt) {
940911
n.visit_children_with(self);
941912

942-
if n.body.is_ts_stmt() {
913+
if n.body.is_ts_declare() {
943914
self.add_overwrite(n.body.span_lo(), b';');
944915
}
945916
}
946917

947918
fn visit_for_of_stmt(&mut self, n: &ForOfStmt) {
948919
n.visit_children_with(self);
949920

950-
if n.body.is_ts_stmt() {
921+
if n.body.is_ts_declare() {
951922
self.add_overwrite(n.body.span_lo(), b';');
952923
}
953924
}
954925

955926
fn visit_while_stmt(&mut self, n: &WhileStmt) {
956927
n.visit_children_with(self);
957928

958-
if n.body.is_ts_stmt() {
929+
if n.body.is_ts_declare() {
959930
self.add_overwrite(n.body.span_lo(), b';');
960931
}
961932
}
962933

963934
fn visit_do_while_stmt(&mut self, n: &DoWhileStmt) {
964935
n.visit_children_with(self);
965936

966-
if n.body.is_ts_stmt() {
937+
if n.body.is_ts_declare() {
967938
self.add_overwrite(n.body.span_lo(), b';');
968939
}
969940
}
970941
}
971942

972-
trait IsTsStmt {
973-
fn is_ts_stmt(&self) -> bool;
943+
trait IsTsDecl {
944+
fn is_ts_declare(&self) -> bool;
974945
}
975946

976-
impl IsTsStmt for Stmt {
977-
fn is_ts_stmt(&self) -> bool {
947+
impl IsTsDecl for Decl {
948+
fn is_ts_declare(&self) -> bool {
978949
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,
982958
_ => false,
983959
}
984960
}
985961
}
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+
986969
trait U8Helper {
987970
fn is_utf8_char_boundary(&self) -> bool;
988971
}

‎crates/swc_fast_ts_strip/tests/errors/enums.swc-stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33
1 | enum Foo { }
44
: ^^^^^^^^^^^^
55
`----
6+
x TypeScript enum is not supported in strip-only mode
7+
,-[3:1]
8+
2 |
9+
3 | export enum Bar { }
10+
: ^^^^^^^^^^^^
11+
`----
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
enum Foo { }
1+
enum Foo { }
2+
3+
export enum Bar { }

‎crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33
1 | module aModuleKeywordNamespace { }
44
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55
`----
6+
x TypeScript namespace declaration is not supported in strip-only mode
7+
,-[3:1]
8+
2 |
9+
3 | export module aModuleKeywordExportedNamespace { }
10+
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
`----
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
module aModuleKeywordNamespace { }
1+
module aModuleKeywordNamespace { }
2+
3+
export module aModuleKeywordExportedNamespace { }

‎crates/swc_fast_ts_strip/tests/errors/namespaces.swc-stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33
1 | namespace Foo { }
44
: ^^^^^^^^^^^^^^^^^
55
`----
6+
x TypeScript namespace declaration is not supported in strip-only mode
7+
,-[3:1]
8+
2 |
9+
3 | export namespace Bar { }
10+
: ^^^^^^^^^^^^^^^^^
11+
`----
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
namespace Foo { }
1+
namespace Foo { }
2+
3+
export namespace Bar { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
4+
5+
6+
7+
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export declare class Foo {}
2+
export declare interface Bar {}
3+
export declare type Baz = {};
4+
export declare enum Qux {}
5+
export declare const Quux = 0;
6+
export declare function Corge(): void;
7+
export declare namespace Grault {}
8+
export declare module Garply {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default interface Foo {}
2+
export interface Bar {}

0 commit comments

Comments
 (0)
Please sign in to comment.