Skip to content

Commit

Permalink
unresolved_ctxt
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Apr 27, 2024
1 parent a0726e5 commit 6a6f564
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
15 changes: 13 additions & 2 deletions crates/turbopack-ecmascript/src/tree_shake/graph.rs
Expand Up @@ -10,7 +10,7 @@ use petgraph::{
};
use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
use swc_core::{
common::{util::take::Take, DUMMY_SP},
common::{util::take::Take, SyntaxContext, DUMMY_SP},
ecma::{
ast::{
op, ClassDecl, Decl, DefaultDecl, ExportDecl, ExportNamedSpecifier, ExportSpecifier,
Expand Down Expand Up @@ -541,7 +541,11 @@ impl DepGraph {
}

/// Fills information per module items
pub(super) fn init(&mut self, module: &Module) -> (Vec<ItemId>, FxHashMap<ItemId, ItemData>) {
pub(super) fn init(
&mut self,
module: &Module,
unresolved_ctxt: SyntaxContext,
) -> (Vec<ItemId>, FxHashMap<ItemId, ItemData>) {
let mut exports = vec![];
let mut items = FxHashMap::default();
let mut ids = vec![];
Expand Down Expand Up @@ -896,12 +900,19 @@ impl DepGraph {
used_ids.write.extend(extra_ids.write);
}

let side_effects = used_ids
.read
.iter()
.chain(used_ids.write.iter())
.any(|id| id.1 == unresolved_ctxt);

let data = ItemData {
read_vars: used_ids.read,
eventual_read_vars: captured_ids.read,
write_vars: used_ids.write,
eventual_write_vars: captured_ids.write,
content: item.clone(),
side_effects,
..Default::default()
};

Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-ecmascript/src/tree_shake/mod.rs
Expand Up @@ -66,7 +66,7 @@ impl Analyzer<'_> {
unresolved_ctxt: SyntaxContext,
) -> (DepGraph, FxHashMap<ItemId, ItemData>) {
let mut g = DepGraph::default();
let (item_ids, mut items) = g.init(module);
let (item_ids, mut items) = g.init(module, unresolved_ctxt);

let mut analyzer = Analyzer {
g: &mut g,
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-ecmascript/src/tree_shake/tests.rs
Expand Up @@ -72,7 +72,7 @@ fn run(input: PathBuf) {
));

let mut g = DepGraph::default();
let (item_ids, mut items) = g.init(&module);
let (item_ids, mut items) = g.init(&module, unresolved_ctxt);

let mut s = String::new();

Expand Down

0 comments on commit 6a6f564

Please sign in to comment.