use rustc_hash::FxHashMap;
use fallow_types::discover::FileId;
use fallow_types::extract::{ExportName, ImportedName};
use super::ModuleGraph;
use super::types::{RoutedReference, SymbolReference};
use crate::resolve::{ResolvedModule, platform_family_key};
enum ForwardedNames {
One(String),
AllButDefault,
All,
}
impl ForwardedNames {
fn new(imported_name: &str, exported_name: &str) -> Self {
match (imported_name, exported_name) {
("*", "*") => Self::AllButDefault,
("*", _) => Self::All,
(name, _) => Self::One(name.to_string()),
}
}
fn contains(&self, name: &ExportName) -> bool {
match self {
Self::One(forwarded) => name.matches_str(forwarded),
Self::AllButDefault => *name != ExportName::Default,
Self::All => true,
}
}
}
struct SiblingCredit {
target: FileId,
sibling: FileId,
names: ForwardedNames,
}
fn same_platform_family(graph: &ModuleGraph, a: FileId, b: FileId) -> bool {
let (Some(a), Some(b)) = (
graph.modules.get(a.0 as usize),
graph.modules.get(b.0 as usize),
) else {
return false;
};
match (platform_family_key(&a.path), platform_family_key(&b.path)) {
(Some(a), Some(b)) => a.parent == b.parent && a.base == b.base,
_ => false,
}
}
fn collect_sibling_credits(
graph: &ModuleGraph,
module_by_id: &FxHashMap<FileId, &ResolvedModule>,
) -> Vec<SiblingCredit> {
let mut credits = Vec::new();
for module in module_by_id.values() {
for re_export in &module.re_exports {
let Some(target) = re_export.target.internal_file_id() else {
continue;
};
for import in &module.resolved_imports {
if !matches!(import.info.imported_name, ImportedName::SideEffect)
|| import.info.source != re_export.info.source
{
continue;
}
let Some(sibling) = import.target.internal_file_id() else {
continue;
};
if sibling == target || !same_platform_family(graph, target, sibling) {
continue;
}
credits.push(SiblingCredit {
target,
sibling,
names: ForwardedNames::new(
&re_export.info.imported_name,
&re_export.info.exported_name,
),
});
}
}
}
credits.sort_by_key(|credit| (credit.sibling.0, credit.target.0));
credits
}
fn same_reference(a: &SymbolReference, b: &SymbolReference) -> bool {
a.from_file == b.from_file
&& a.kind == b.kind
&& a.namespace == b.namespace
&& a.import_span == b.import_span
}
impl ModuleGraph {
pub(super) fn credit_platform_family_re_exports(
&mut self,
module_by_id: &FxHashMap<FileId, &ResolvedModule>,
) {
for credit in collect_sibling_credits(self, module_by_id) {
let target = &self.modules[credit.target.0 as usize];
let copied: Vec<(ExportName, Vec<RoutedReference>)> = target
.exports
.iter()
.filter(|export| credit.names.contains(&export.name))
.filter(|export| !export.references.is_empty())
.map(|export| (export.name.clone(), export.routed_references().collect()))
.collect();
let sibling = &mut self.modules[credit.sibling.0 as usize];
for (name, refs) in copied {
let Some(export) = sibling.exports.iter_mut().find(|e| e.name == name) else {
continue;
};
for routed in refs {
if export
.references
.iter()
.any(|existing| same_reference(existing, &routed.reference))
{
continue;
}
export.push_reference(routed.reference, routed.path);
}
}
}
}
}