1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use stc_ts_utils::imports::find_imports_in_comments;
use swc_atoms::JsWord;
use swc_common::{comments::Comments, Span, Spanned};
use swc_ecma_ast::*;
use swc_ecma_visit::{Visit, VisitWith};
pub(crate) fn find_modules_and_deps<C>(comments: &C, m: &Module) -> (Vec<JsWord>, Vec<JsWord>, Vec<JsWord>)
where
C: Comments,
{
let mut v = DepFinder {
comments,
declared_modules: Default::default(),
references: Default::default(),
deps: Default::default(),
};
m.visit_with(&mut v);
(v.declared_modules, v.references, v.deps)
}
struct DepFinder<C>
where
C: Comments,
{
comments: C,
declared_modules: Vec<JsWord>,
references: Vec<JsWord>,
deps: Vec<JsWord>,
}
impl<C> DepFinder<C>
where
C: Comments,
{
fn check_comments(&mut self, span: Span) {
let deps = find_imports_in_comments(&self.comments, span);
self.references.extend(deps.into_iter().map(|i| i.to_path()));
}
}
impl<C> Visit for DepFinder<C>
where
C: Comments,
{
fn visit_module_item(&mut self, i: &ModuleItem) {
i.visit_children_with(self);
self.check_comments(i.span())
}
fn visit_module(&mut self, m: &Module) {
m.visit_children_with(self);
self.check_comments(m.span)
}
fn visit_export_all(&mut self, export: &ExportAll) {
self.deps.push(export.src.value.clone());
}
fn visit_import_decl(&mut self, import: &ImportDecl) {
self.deps.push(import.src.value.clone());
}
fn visit_named_export(&mut self, export: &NamedExport) {
if let Some(src) = &export.src {
self.deps.push(src.value.clone());
}
}
fn visit_ts_external_module_ref(&mut self, import: &TsExternalModuleRef) {
self.deps.push(import.expr.value.clone());
}
fn visit_ts_module_decl(&mut self, n: &TsModuleDecl) {
n.visit_children_with(self);
if let TsModuleName::Str(s) = &n.id {
self.declared_modules.push(s.value.clone());
}
}
}