Skip to content

Commit 2217730

Browse files
authoredAug 1, 2024··
fix(es/typescript): Handle single type statement in if/for/while (#9364)
- Closes: #9363
1 parent 9fba319 commit 2217730

File tree

6 files changed

+146
-7
lines changed

6 files changed

+146
-7
lines changed
 

‎.changeset/lucky-panthers-cough.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_transforms_typescript: patch
3+
swc_fast_ts_strip: patch
4+
---
5+
6+
Fix (TypeScript): Handle single type statement in if/for/while

‎crates/swc_ecma_transforms_typescript/src/strip_type.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,16 @@ impl VisitMut for StripType {
205205
}
206206

207207
fn visit_mut_stmts(&mut self, n: &mut Vec<Stmt>) {
208-
n.retain(should_retain_stmt);
209208
n.visit_mut_children_with(self);
209+
n.retain(|s| !s.is_empty());
210+
}
211+
212+
fn visit_mut_stmt(&mut self, n: &mut Stmt) {
213+
if should_retain_stmt(n) {
214+
n.visit_mut_children_with(self);
215+
} else if !n.is_empty() {
216+
n.take();
217+
}
210218
}
211219

212220
fn visit_mut_ts_import_equals_decl(&mut self, _: &mut TsImportEqualsDecl) {

‎crates/swc_fast_ts_strip/src/lib.rs

+75-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ use swc_common::{
1111
BytePos, FileName, Mark, SourceMap, Span, Spanned,
1212
};
1313
use swc_ecma_ast::{
14-
ArrowExpr, BindingIdent, Class, ClassDecl, ClassMethod, ClassProp, EsVersion, ExportAll,
15-
ExportDecl, ExportSpecifier, FnDecl, ImportDecl, ImportSpecifier, NamedExport, Param, Pat,
16-
Program, TsAsExpr, TsConstAssertion, TsEnumDecl, TsExportAssignment, TsImportEqualsDecl,
17-
TsIndexSignature, TsInstantiation, TsInterfaceDecl, TsModuleDecl, TsModuleName,
18-
TsNamespaceDecl, TsNonNullExpr, TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn,
19-
TsTypeAssertion, TsTypeParamDecl, TsTypeParamInstantiation, VarDecl,
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,
19+
TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion,
20+
TsTypeParamDecl, TsTypeParamInstantiation, VarDecl, WhileStmt,
2021
};
2122
use swc_ecma_parser::{
2223
lexer::Lexer,
@@ -910,8 +911,76 @@ impl Visit for TsStrip {
910911

911912
n.visit_children_with(self);
912913
}
914+
915+
fn visit_if_stmt(&mut self, n: &IfStmt) {
916+
n.visit_children_with(self);
917+
918+
if n.cons.is_ts_stmt() {
919+
self.add_overwrite(n.cons.span_lo(), b';');
920+
}
921+
922+
if let Some(alt) = &n.alt {
923+
if alt.is_ts_stmt() {
924+
self.add_overwrite(alt.span_lo(), b';');
925+
}
926+
}
927+
}
928+
929+
fn visit_for_stmt(&mut self, n: &ForStmt) {
930+
n.visit_children_with(self);
931+
932+
if n.body.is_ts_stmt() {
933+
self.add_overwrite(n.body.span_lo(), b';');
934+
}
935+
}
936+
937+
fn visit_for_in_stmt(&mut self, n: &ForInStmt) {
938+
n.visit_children_with(self);
939+
940+
if n.body.is_ts_stmt() {
941+
self.add_overwrite(n.body.span_lo(), b';');
942+
}
943+
}
944+
945+
fn visit_for_of_stmt(&mut self, n: &ForOfStmt) {
946+
n.visit_children_with(self);
947+
948+
if n.body.is_ts_stmt() {
949+
self.add_overwrite(n.body.span_lo(), b';');
950+
}
951+
}
952+
953+
fn visit_while_stmt(&mut self, n: &WhileStmt) {
954+
n.visit_children_with(self);
955+
956+
if n.body.is_ts_stmt() {
957+
self.add_overwrite(n.body.span_lo(), b';');
958+
}
959+
}
960+
961+
fn visit_do_while_stmt(&mut self, n: &DoWhileStmt) {
962+
n.visit_children_with(self);
963+
964+
if n.body.is_ts_stmt() {
965+
self.add_overwrite(n.body.span_lo(), b';');
966+
}
967+
}
913968
}
914969

970+
trait IsTsStmt {
971+
fn is_ts_stmt(&self) -> bool;
972+
}
973+
974+
impl IsTsStmt for Stmt {
975+
fn is_ts_stmt(&self) -> bool {
976+
match self {
977+
Stmt::Decl(Decl::TsInterface { .. } | Decl::TsTypeAlias(..)) => true,
978+
Stmt::Decl(Decl::TsModule(n)) => n.declare || matches!(n.id, TsModuleName::Str(..)),
979+
Stmt::Decl(Decl::TsEnum(e)) => e.declare,
980+
_ => false,
981+
}
982+
}
983+
}
915984
trait U8Helper {
916985
fn is_utf8_char_boundary(&self) -> bool;
917986
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
if (false) ;
2+
console.log("Hello, World!");
3+
4+
if (false) { }
5+
else ;
6+
console.log("Hello, World!");
7+
8+
for (; false;)
9+
;
10+
console.log("Hello, World!");
11+
12+
for (; false;)
13+
;
14+
console.log("Hello, World!");
15+
16+
while (false)
17+
;
18+
console.log("Hello, World!");
19+
20+
do
21+
;
22+
while (false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
if (false) ;
2+
console.log("Hello, World!");
3+
if (false) {} else ;
4+
console.log("Hello, World!");
5+
for(; false;);
6+
console.log("Hello, World!");
7+
for(; false;);
8+
console.log("Hello, World!");
9+
while(false);
10+
console.log("Hello, World!");
11+
do ;
12+
while (false)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
if (false) type Foo = string
2+
console.log("Hello, World!");
3+
4+
if (false) { }
5+
else type Bar = string
6+
console.log("Hello, World!");
7+
8+
for (; false;)
9+
interface X { }
10+
console.log("Hello, World!");
11+
12+
for (; false;)
13+
interface X { }
14+
console.log("Hello, World!");
15+
16+
while (false)
17+
type Baz = string
18+
console.log("Hello, World!");
19+
20+
do
21+
interface X { }
22+
while (false);

0 commit comments

Comments
 (0)
Please sign in to comment.