Unverified Commit de62a966 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: refactor export analysis (#92781)

Remove `ModuleReferencesVisitor` and move everything into the `ImportMap` `Analyzer`
parent f8de120d
...@@ -33,7 +33,9 @@ use crate::{ ...@@ -33,7 +33,9 @@ use crate::{
analyzer::{WellKnownObjectKind, is_unresolved}, analyzer::{WellKnownObjectKind, is_unresolved},
code_gen::CodeGen, code_gen::CodeGen,
references::{ references::{
constant_value::parse_single_expr_lit, esm::EsmModuleItem, for_each_ident_in_pat, constant_value::parse_single_expr_lit,
esm::{EsmModuleItem, Liveness},
for_each_ident_in_pat,
}, },
utils::{AstPathRange, unparen}, utils::{AstPathRange, unparen},
}; };
...@@ -387,6 +389,29 @@ impl VarGraph { ...@@ -387,6 +389,29 @@ impl VarGraph {
effect.normalize(); effect.normalize();
} }
} }
/// Returns the liveness of a given export identifier. An export is live if it might
/// change values after module evaluation.
pub fn get_export_ident_liveness(&self, id: Id) -> Liveness {
if let Some(VarMeta {
value: _,
assignment_scopes: assignment_kinds,
}) = self.values.get(&id)
{
// If all assignments are in module scope, the export is not live.
if *assignment_kinds != AssignmentScopes::AllInModuleEvalScope {
Liveness::Live
} else {
Liveness::Constant
}
} else {
// If we haven't computed a value for it, that means it might be
// - A free variable or
// - an imported variable
// In those cases, we just assume that the value is live since we don't know anything
Liveness::Live
}
}
} }
pub fn create_graph( pub fn create_graph(
......
...@@ -1747,8 +1747,8 @@ async fn process_parse_result( ...@@ -1747,8 +1747,8 @@ async fn process_parse_result(
let export_contexts = eval_context let export_contexts = eval_context
.map_either( .map_either(
|e| Cow::Owned(e.imports.exports), |e| Cow::Owned(e.imports.exports_ids),
|e| Cow::Borrowed(&e.imports.exports), |e| Cow::Borrowed(&e.imports.exports_ids),
) )
.into_inner(); .into_inner();
let preserved_exports = let preserved_exports =
......
...@@ -706,16 +706,17 @@ impl EsmExports { ...@@ -706,16 +706,17 @@ impl EsmExports {
// TODO ideally, this information would just be stored in // TODO ideally, this information would just be stored in
// EsmExport::LocalBinding and we wouldn't have to re-correlated this // EsmExport::LocalBinding and we wouldn't have to re-correlated this
// information with eval_context.imports.exports to get the syntax context. // information with eval_context.imports.exports to get the syntax context.
let binding = let binding = if let Some((local, ctxt)) =
if let Some((local, ctxt)) = eval_context.imports.exports.get(exported) { eval_context.imports.exports_ids.get(exported)
Some((Cow::Borrowed(local.as_str()), *ctxt)) {
} else { Some((Cow::Borrowed(local.as_str()), *ctxt))
bail!( } else {
"Expected export to be in eval context {:?} {:?}", bail!(
exported, "Expected export to be in eval context {:?} {:?}",
eval_context.imports, exported,
) eval_context.imports,
}; )
};
let (local, ctxt) = binding.unwrap_or_else(|| { let (local, ctxt) = binding.unwrap_or_else(|| {
// Fallback, shouldn't happen in practice // Fallback, shouldn't happen in practice
( (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment