Skip to main content

fallow_graph/graph/
entry_marks.rs

1//! Marks on entry modules that depend on how a file is loaded: the files that
2//! the graph cannot follow member reads into, and the fixture entry points.
3
4use rustc_hash::FxHashSet;
5
6use crate::resolve::ResolvedModule;
7use fallow_types::discover::FileId;
8
9use super::ModuleGraph;
10
11/// The files that a dynamic import, an import pattern or a `require()` loads.
12#[must_use]
13pub fn untracked_load_targets(resolved_modules: &[ResolvedModule]) -> FxHashSet<FileId> {
14    let mut targets = FxHashSet::default();
15    for module in resolved_modules {
16        let required = module
17            .resolved_imports
18            .iter()
19            .filter(|import| import.target.is_commonjs_require());
20        for import in module.resolved_dynamic_imports.iter().chain(required) {
21            if let Some(target) = import.target.internal_file_id() {
22                targets.insert(target);
23            }
24        }
25        for (_, files) in &module.resolved_dynamic_patterns {
26            targets.extend(files.iter().copied());
27        }
28    }
29    targets
30}
31
32impl ModuleGraph {
33    /// Mark the consumer-checked modules in `file_ids` as fixture entry
34    /// points. See [`ModuleNode::is_fixture_entry`](super::ModuleNode::is_fixture_entry).
35    pub fn set_fixture_entries(&mut self, file_ids: &FxHashSet<FileId>) {
36        for file_id in file_ids {
37            if let Some(module) = self.modules.get_mut(file_id.0 as usize)
38                && module.is_consumer_checked()
39            {
40                module.set_fixture_entry();
41            }
42        }
43    }
44}