-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
mod.rs
33 lines (29 loc) · 853 Bytes
/
mod.rs
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
use swc_ecmascript::{
ast::*,
visit::{Visit, VisitWith},
};
pub(crate) fn contains_cjs(m: &Module) -> bool {
let mut v = CjsFinder::default();
m.visit_with(&mut v);
v.found
}
#[derive(Copy, Clone, Default)]
struct CjsFinder {
found: bool,
}
/// This visitor implementation supports typescript, because the api of `swc`
/// does not support changing configuration based on content of the file.
impl Visit for CjsFinder {
fn visit_member_expr(&mut self, e: &MemberExpr) {
if let Expr::Ident(obj) = &*e.obj {
if let MemberProp::Ident(prop) = &e.prop {
if &*obj.sym == "module" && &*prop.sym == "exports" {
self.found = true;
return;
}
}
}
e.obj.visit_with(self);
e.prop.visit_with(self);
}
}