use super::*;
impl ModuleInfoExtractor {
fn forwardable_import(&self, local_name: &str) -> Option<ForwardableImport> {
if self.local_declaration_names.contains(local_name) {
return None;
}
self.imports.iter().find_map(|import| {
if import.local_name != local_name {
return None;
}
let imported_name = match &import.imported_name {
ImportedName::Named(name) => name.clone(),
ImportedName::Default => "default".to_string(),
ImportedName::Namespace | ImportedName::SideEffect => return None,
};
Some(ForwardableImport {
source: import.source.clone(),
imported_name,
is_type_only: import.is_type_only,
})
})
}
fn push_import_forward(
&mut self,
import: ForwardableImport,
exported_name: String,
is_type_only: bool,
span: Span,
) {
self.re_exports.push(ReExportInfo {
source: import.source,
imported_name: import.imported_name,
exported_name,
is_type_only: is_type_only || import.is_type_only,
span,
..Default::default()
});
}
pub(super) fn record_module_value_name(&mut self, name: &str) -> String {
self.module_value_names.insert(name.to_string());
name.to_string()
}
fn is_module_type_only_name(&self, name: &str) -> bool {
self.module_type_names.contains(name) && !self.module_value_names.contains(name)
}
pub(crate) fn resolve_pending_local_export_specifiers(&mut self) {
self.resolve_pending_binding_forwards();
let pending = std::mem::take(&mut self.pending_local_export_specifiers);
for spec in pending {
if let Some(import) = self.forwardable_import(&spec.local_name) {
self.push_import_forward(import, spec.exported_name, spec.is_type_only, spec.span);
} else {
let is_type_only =
spec.is_type_only || self.is_module_type_only_name(&spec.local_name);
self.exports.push(ExportInfo {
name: ExportName::Named(spec.exported_name),
local_name: Some(spec.local_name),
is_type_only,
visibility: VisibilityTag::None,
expected_unused_reason: None,
span: spec.span,
members: vec![],
is_side_effect_used: false,
super_class: None,
deprecated: false,
deprecated_reason: None,
});
}
}
}
fn resolve_pending_binding_forwards(&mut self) {
let pending = std::mem::take(&mut self.pending_binding_forwards);
for forward in pending {
match forward {
PendingBindingForward::ExportDefault { local_name } => {
let Some(import) = self.forwardable_import(&local_name) else {
continue;
};
let Some(index) = self.exports.iter().position(|export| {
matches!(export.name, ExportName::Default)
&& export.local_name.as_deref() == Some(local_name.as_str())
}) else {
continue;
};
let export = self.exports.remove(index);
self.push_import_forward(
import,
"default".to_string(),
export.is_type_only,
export.span,
);
}
PendingBindingForward::ExportAssignment { local_name, span } => {
let Some(import) = self.forwardable_import(&local_name) else {
continue;
};
self.has_cjs_exports = true;
self.push_import_forward(import, "default".to_string(), false, span);
}
}
}
}
fn is_lit_custom_element_decorator(&self, decorator: &LitCustomElementDecorator) -> bool {
const LIT_DECORATOR_SOURCES: &[&str] =
&["lit/decorators.js", "lit/decorators/custom-element.js"];
self.imports.iter().any(|import| {
LIT_DECORATOR_SOURCES.contains(&import.source.as_str())
&& match decorator {
LitCustomElementDecorator::Named { local_name } => {
import.local_name == *local_name
&& matches!(
&import.imported_name,
ImportedName::Named(name) if name == "customElement"
)
}
LitCustomElementDecorator::Namespace { local_name } => {
import.local_name == *local_name
&& matches!(import.imported_name, ImportedName::Namespace)
}
}
})
}
pub(super) fn is_node_module_register(&self, local_name: &str, via_namespace: bool) -> bool {
const NODE_MODULE_SOURCES: &[&str] = &["node:module", "module"];
self.imports.iter().any(|import| {
NODE_MODULE_SOURCES.contains(&import.source.as_str())
&& import.local_name == local_name
&& if via_namespace {
matches!(import.imported_name, ImportedName::Namespace)
} else {
matches!(
&import.imported_name,
ImportedName::Named(name) if name == "register"
)
}
})
}
fn apply_lit_custom_element_candidates(&mut self) {
if self.lit_custom_element_candidates.is_empty() {
return;
}
let mut class_names = Vec::new();
let mut anonymous_default_indices = Vec::new();
let mut registrations = Vec::new();
for candidate in &self.lit_custom_element_candidates {
if !self.is_lit_custom_element_decorator(&candidate.decorator) {
continue;
}
if let Some(tag) = &candidate.tag {
let class_local_name = match &candidate.target {
SideEffectRegistrationTarget::LocalClass(name) => name.clone(),
SideEffectRegistrationTarget::AnonymousDefaultExport(_) => String::new(),
};
registrations.push(fallow_types::extract::RegisteredCustomElement {
tag: tag.clone(),
class_local_name,
span_start: candidate.span_start,
});
}
match &candidate.target {
SideEffectRegistrationTarget::LocalClass(class_name) => {
class_names.push(class_name.clone());
}
SideEffectRegistrationTarget::AnonymousDefaultExport(index) => {
anonymous_default_indices.push(*index);
}
}
}
self.registered_custom_elements.extend(registrations);
self.side_effect_registered_class_names.extend(class_names);
for index in anonymous_default_indices {
if let Some(export) = self.exports.get_mut(index) {
export.is_side_effect_used = true;
}
}
}
pub(super) fn record_lit_custom_element_candidate(
&mut self,
decorator: LitCustomElementDecorator,
target: SideEffectRegistrationTarget,
tag: Option<String>,
span_start: u32,
) {
self.lit_custom_element_candidates
.push(LitCustomElementCandidate {
decorator,
target,
tag,
span_start,
});
}
pub(super) fn apply_side_effect_registrations(&mut self) {
self.apply_lit_custom_element_candidates();
if self.side_effect_registered_class_names.is_empty() {
return;
}
for export in &mut self.exports {
let Some(local_name) = export.local_name.as_deref() else {
continue;
};
if self.side_effect_registered_class_names.contains(local_name) {
export.is_side_effect_used = true;
}
}
}
}