Skip to content

Commit 9a64250

Browse files
committed
feat: support removal of ImportDecl
1 parent e0cf962 commit 9a64250

File tree

1 file changed

+70
-9
lines changed

1 file changed

+70
-9
lines changed

src/lib.rs

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,76 @@
1-
use swc_core::ecma::{
2-
ast::Program,
3-
visit::{as_folder, FoldWith, VisitMut},
1+
use std::collections::HashSet;
2+
use swc_core::common::util::take::Take;
3+
use swc_core::ecma::atoms::JsWord;
4+
use swc_core::ecma::ast::{
5+
Id,
6+
Program,
7+
ImportDecl,
8+
ImportSpecifier,
9+
ImportDefaultSpecifier,
10+
ImportStarAsSpecifier,
11+
ImportNamedSpecifier,
12+
ModuleItem,
13+
ModuleDecl,
14+
};
15+
use swc_core::ecma::visit::{
16+
as_folder,
17+
FoldWith,
18+
VisitMut, VisitMutWith
419
};
520
use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};
621

7-
pub struct TransformVisitor;
22+
pub struct TransformVisitor {
23+
target_variables: HashSet<Id>,
24+
target_modules: HashSet<JsWord>,
25+
}
26+
27+
impl Default for TransformVisitor {
28+
fn default() -> Self {
29+
Self {
30+
target_variables: HashSet::new(),
31+
target_modules: [
32+
"node:assert",
33+
"node:assert/strict",
34+
"assert",
35+
"assert/strict",
36+
].iter().map(|s| JsWord::from(*s)).collect(),
37+
}
38+
}
39+
}
840

941
impl VisitMut for TransformVisitor {
10-
// Implement necessary visit_mut_* methods for actual custom transform.
11-
// A comprehensive list of possible visitor methods can be found here:
12-
// https://rustdoc.swc.rs/swc_ecma_visit/trait.VisitMut.html
42+
fn visit_mut_import_decl(&mut self, n: &mut ImportDecl) {
43+
if self.target_modules.contains(&n.src.value) {
44+
for s in &mut n.specifiers {
45+
match s {
46+
ImportSpecifier::Default(ImportDefaultSpecifier { local, .. }) => {
47+
self.target_variables.insert(local.to_id());
48+
},
49+
ImportSpecifier::Namespace(ImportStarAsSpecifier { local, .. }) => {
50+
self.target_variables.insert(local.to_id());
51+
},
52+
ImportSpecifier::Named(ImportNamedSpecifier { local, .. }) => {
53+
self.target_variables.insert(local.to_id());
54+
},
55+
}
56+
}
57+
n.take();
58+
} else {
59+
n.visit_mut_children_with(self);
60+
}
61+
}
62+
63+
fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
64+
n.visit_mut_children_with(self);
65+
n.retain(|s| {
66+
match s {
67+
ModuleItem::ModuleDecl(ModuleDecl::Import(decl)) => {
68+
*decl != ImportDecl::dummy()
69+
},
70+
_ => true,
71+
}
72+
});
73+
}
1374
}
1475

1576
/// An example plugin function with macro support.
@@ -29,7 +90,7 @@ impl VisitMut for TransformVisitor {
2990
/// Refer swc_plugin_macro to see how does it work internally.
3091
#[plugin_transform]
3192
pub fn process_transform(program: Program, _metadata: TransformPluginProgramMetadata) -> Program {
32-
program.fold_with(&mut as_folder(TransformVisitor))
93+
program.fold_with(&mut as_folder(TransformVisitor::default()))
3394
}
3495

3596
#[cfg(test)]
@@ -47,7 +108,7 @@ mod tests {
47108
test_fixture(
48109
Syntax::Es(EsConfig::default()),
49110
&|_t| {
50-
as_folder(TransformVisitor{})
111+
as_folder(TransformVisitor::default())
51112
},
52113
&input,
53114
&output,

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