use farmfe_core::module::meta_data::script::statement::SwcId;
use farmfe_core::plugin::ResolveKind;
use farmfe_core::{HashMap, HashSet};
use crate::module::{UsedExports, UsedExportsIdent};
use super::{
ExportInfo, ExportSpecifierInfo, ImportInfo, ImportSpecifierInfo, StatementId,
UsedStatementIdent, analyze_used_import_all_fields::UsedImportAllFields,
};
#[derive(Debug)]
pub struct TracedUsedImportStatement {
pub stmt_id: StatementId,
pub source: String,
pub used_stmt_idents: UsedExports,
pub kind: ResolveKind,
}
impl TracedUsedImportStatement {
pub fn from_import_info_and_used_idents(
stmt_id: StatementId,
import_info: &ImportInfo,
used_defined_idents: &HashSet<UsedStatementIdent>,
mut used_import_all_fields: HashMap<SwcId, HashSet<UsedImportAllFields>>,
) -> Self {
let mut used_stmt_idents = HashSet::default();
let used_defined_idents = used_defined_idents
.iter()
.filter_map(|i| match i {
UsedStatementIdent::SwcIdent(i) => Some(i),
_ => None,
})
.collect::<HashSet<_>>();
for specifier in &import_info.specifiers {
match specifier {
ImportSpecifierInfo::Namespace(id) => {
if used_defined_idents.contains(id) {
if let Some(used_import_all_fields) = used_import_all_fields.remove(id) {
if !used_import_all_fields.contains(&UsedImportAllFields::All) {
for used_import_all_field in used_import_all_fields {
match used_import_all_field {
UsedImportAllFields::All => unreachable!(),
UsedImportAllFields::Ident(field)
| UsedImportAllFields::LiteralComputed(field) => {
if &field == "default" {
used_stmt_idents.insert(UsedExportsIdent::Default);
} else {
used_stmt_idents.insert(UsedExportsIdent::SwcIdent(field));
}
}
}
}
} else {
used_stmt_idents.insert(UsedExportsIdent::ImportAll);
}
} else {
used_stmt_idents.insert(UsedExportsIdent::ImportAll);
}
}
}
ImportSpecifierInfo::Named { local, imported } => {
if used_defined_idents.contains(local) {
if let Some(imported) = imported {
if imported.sym == "default" {
used_stmt_idents.insert(UsedExportsIdent::Default);
} else {
used_stmt_idents.insert(UsedExportsIdent::SwcIdent(imported.sym.to_string()));
}
} else if used_defined_idents.contains(local) {
used_stmt_idents.insert(UsedExportsIdent::SwcIdent(local.sym.to_string()));
}
}
}
ImportSpecifierInfo::Default(id) => {
if used_defined_idents.contains(id) {
used_stmt_idents.insert(UsedExportsIdent::Default);
}
}
}
}
Self {
stmt_id,
source: import_info.source.clone(),
used_stmt_idents: UsedExports::Partial(used_stmt_idents),
kind: ResolveKind::Import,
}
}
pub fn from_export_info_and_used_idents(
stmt_id: StatementId,
export_info: &ExportInfo,
used_defined_idents: &HashSet<UsedStatementIdent>,
) -> Option<Self> {
if let Some(source) = &export_info.source {
let mut used_stmt_idents = HashSet::default();
for specifier in &export_info.specifiers {
match specifier {
ExportSpecifierInfo::Namespace(i) => {
if used_defined_idents.contains(&UsedStatementIdent::SwcIdent(i.clone())) {
used_stmt_idents.insert(UsedExportsIdent::ImportAll);
}
}
ExportSpecifierInfo::Named { local, .. } => {
if used_defined_idents.contains(&UsedStatementIdent::SwcIdent(local.clone())) {
if local.sym == "default" {
used_stmt_idents.insert(UsedExportsIdent::Default);
} else {
used_stmt_idents.insert(UsedExportsIdent::SwcIdent(local.sym.to_string()));
}
}
}
ExportSpecifierInfo::Default => {
unreachable!("export v from 'foo'; is not valid in ES module.");
}
ExportSpecifierInfo::All => {
if used_defined_idents.contains(&UsedStatementIdent::ExportAll) {
used_stmt_idents.insert(UsedExportsIdent::ExportAll);
} else {
for used_ident in used_defined_idents {
if let UsedStatementIdent::InExportAll(i) = used_ident {
used_stmt_idents.insert(UsedExportsIdent::SwcIdent(i.to_string()));
}
}
}
}
}
}
return Some(Self {
stmt_id,
source: source.clone(),
used_stmt_idents: UsedExports::Partial(used_stmt_idents),
kind: ResolveKind::ExportFrom,
});
}
None
}
}