silverwolf-x's picture
download
raw
2.74 kB
use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::{
atoms::{Atom, Wtf8Atom, atom},
ecma::{
ast::{
Expr, Id, ImportDecl, ImportNamedSpecifier, ImportSpecifier, MemberExpr, MemberProp,
Module,
},
visit::{Visit, VisitWith, noop_visit_type},
},
};
#[derive(Debug, Default)]
pub(crate) struct ImportMap {
/// Map from module name to (module path, exported symbol)
imports: FxHashMap<Id, (Wtf8Atom, Atom)>,
namespace_imports: FxHashMap<Id, Wtf8Atom>,
imported_modules: FxHashSet<Wtf8Atom>,
}
#[allow(unused)]
impl ImportMap {
/// Returns true if `e` is an import of `orig_name` from `module`.
pub fn is_import(&self, e: &Expr, module: &str, orig_name: &str) -> bool {
match e {
Expr::Ident(i) => {
if let Some((i_src, i_sym)) = self.imports.get(&i.to_id()) {
i_src == module && i_sym == orig_name
} else {
false
}
}
Expr::Member(MemberExpr {
obj: box Expr::Ident(obj),
prop: MemberProp::Ident(prop),
..
}) => {
if let Some(obj_src) = self.namespace_imports.get(&obj.to_id()) {
obj_src == module && prop.sym == *orig_name
} else {
false
}
}
_ => false,
}
}
pub fn analyze(m: &Module) -> Self {
let mut data = ImportMap::default();
m.visit_with(&mut Analyzer { data: &mut data });
data
}
}
struct Analyzer<'a> {
data: &'a mut ImportMap,
}
impl Visit for Analyzer<'_> {
noop_visit_type!();
fn visit_import_decl(&mut self, import: &ImportDecl) {
self.data.imported_modules.insert(import.src.value.clone());
for s in &import.specifiers {
let (local, orig_sym) = match s {
ImportSpecifier::Named(ImportNamedSpecifier {
local, imported, ..
}) => match imported {
Some(imported) => (local.to_id(), imported.atom().into_owned()),
_ => (local.to_id(), local.sym.clone()),
},
ImportSpecifier::Default(s) => (s.local.to_id(), atom!("default")),
ImportSpecifier::Namespace(s) => {
self.data
.namespace_imports
.insert(s.local.to_id(), import.src.value.clone());
continue;
}
};
self.data
.imports
.insert(local, (import.src.value.clone(), orig_sym));
}
}
}

Xet Storage Details

Size:
2.74 kB
·
Xet hash:
969110fa1c85e601e712ca260045159f9d79358c5f067f695ac75761f88878e7

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.