Skip to content

Commit 825d747

Browse files
committed
feat: support removal of simple form of assertion
1 parent 9a64250 commit 825d747

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ use swc_core::ecma::ast::{
1111
ImportNamedSpecifier,
1212
ModuleItem,
1313
ModuleDecl,
14+
CallExpr,
15+
Callee,
16+
Expr,
17+
Stmt,
18+
ExprStmt,
1419
};
1520
use swc_core::ecma::visit::{
1621
as_folder,
@@ -71,6 +76,44 @@ impl VisitMut for TransformVisitor {
7176
}
7277
});
7378
}
79+
80+
fn visit_mut_call_expr(&mut self, n: &mut CallExpr) {
81+
if self.target_variables.is_empty() {
82+
n.visit_mut_children_with(self);
83+
return;
84+
}
85+
let matched = match n.callee {
86+
Callee::Expr(ref expr) => {
87+
match expr.as_ref() {
88+
Expr::Ident(ref ident) => self.target_variables.contains(&ident.to_id()),
89+
_ => false
90+
}
91+
},
92+
_ => false
93+
};
94+
if matched {
95+
n.take();
96+
} else {
97+
n.visit_mut_children_with(self);
98+
}
99+
}
100+
101+
fn visit_mut_stmt(&mut self, n: &mut Stmt) {
102+
n.visit_mut_children_with(self);
103+
match n {
104+
Stmt::Expr(ExprStmt{ expr, ..}) => {
105+
match expr.as_ref() {
106+
Expr::Call(call_expr) => {
107+
if *call_expr == CallExpr::dummy() {
108+
n.take();
109+
}
110+
},
111+
_ => {}
112+
}
113+
},
114+
_ => {}
115+
}
116+
}
74117
}
75118

76119
/// An example plugin function with macro support.

tests/fixtures/simple/expected.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
const foo = 1;
22
const bar = 2;
3+
;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy