forked from vercel/next.js
/
font_functions_collector.rs
68 lines (64 loc) · 2.71 KB
/
font_functions_collector.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
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
use next_binding::swc::core::common::errors::HANDLER;
use next_binding::swc::core::ecma::ast::*;
use next_binding::swc::core::ecma::atoms::JsWord;
use next_binding::swc::core::ecma::visit::noop_visit_type;
use next_binding::swc::core::ecma::visit::Visit;
pub struct FontFunctionsCollector<'a> {
pub font_loaders: &'a [JsWord],
pub state: &'a mut super::State,
}
impl<'a> Visit for FontFunctionsCollector<'a> {
noop_visit_type!();
fn visit_import_decl(&mut self, import_decl: &ImportDecl) {
if self.font_loaders.contains(&import_decl.src.value) {
self.state
.removeable_module_items
.insert(import_decl.span.lo);
for specifier in &import_decl.specifiers {
match specifier {
ImportSpecifier::Named(ImportNamedSpecifier {
local, imported, ..
}) => {
self.state
.font_functions_in_allowed_scope
.insert(local.span.lo);
let function_name = if let Some(ModuleExportName::Ident(ident)) = imported {
ident.sym.clone()
} else {
local.sym.clone()
};
self.state.font_functions.insert(
local.to_id(),
super::FontFunction {
loader: import_decl.src.value.clone(),
function_name: Some(function_name),
},
);
}
ImportSpecifier::Default(ImportDefaultSpecifier { local, .. }) => {
self.state
.font_functions_in_allowed_scope
.insert(local.span.lo);
self.state.font_functions.insert(
local.to_id(),
super::FontFunction {
loader: import_decl.src.value.clone(),
function_name: None,
},
);
}
ImportSpecifier::Namespace(_) => {
HANDLER.with(|handler| {
handler
.struct_span_err(
import_decl.span,
"Font loaders can't have namespace imports",
)
.emit()
});
}
}
}
}
}
}