Skip to main content

fallow_core/
test_side.rs

1//! The files of a project that production mode leaves out.
2//!
3//! The `test-only-export` rule and the test importers of a production
4//! `unused-export` finding use this one definition.
5
6use std::path::PathBuf;
7
8use fallow_config::ResolvedConfig;
9use fixedbitset::FixedBitSet;
10use globset::GlobSet;
11use rustc_hash::FxHashSet;
12
13use crate::discover::{FileId, production_exclude_globs};
14use crate::graph::{ModuleGraph, ModuleNode};
15
16/// Decides which files of a graph without production mode are test-side.
17///
18/// A file is test-side when an entry point reaches it and one of these is
19/// true:
20///
21/// - a production exclude pattern or a `production.ignore` glob matches its
22///   root-relative path (test, story, fixture, bench and config files);
23/// - no runtime entry point reaches it, as for a test helper below `src/`
24///   that only tests import, or a file that only a support entry seeds.
25///
26/// An unreachable file is never test-side: an unused test helper is not an
27/// importer.
28///
29/// [`Self::with_support_reach`] narrows the second rule to the files that
30/// no support entry reaches either. Production mode analyzes most files that
31/// a support entry reaches, for example a source file that a bundler config
32/// names, so the `test-only-export` rule treats them as production code.
33pub struct TestSideMatcher {
34    root: PathBuf,
35    excludes: Option<GlobSet>,
36    support_reachable: Option<FixedBitSet>,
37}
38
39impl TestSideMatcher {
40    /// Build the matcher for the project of `config`, in any mode. A file
41    /// that only a support entry reaches is test-side.
42    ///
43    /// The test importers of a production finding use this rule. The
44    /// export of such a finding is unused in production mode, so a file that
45    /// production mode analyzes cannot import it.
46    #[must_use]
47    pub fn new(config: &ResolvedConfig) -> Self {
48        Self {
49            root: config.root.clone(),
50            excludes: production_exclude_globs(config),
51            support_reachable: None,
52        }
53    }
54
55    /// Build the matcher for `graph`, where a file that a support entry
56    /// reaches is not test-side unless a production exclude glob matches it.
57    #[must_use]
58    pub fn with_support_reach(config: &ResolvedConfig, graph: &ModuleGraph) -> Self {
59        Self {
60            support_reachable: Some(graph.support_reachable()),
61            ..Self::new(config)
62        }
63    }
64
65    /// Whether `node` is a test-side file. See [`TestSideMatcher`].
66    #[must_use]
67    pub fn is_test_side(&self, node: &ModuleNode) -> bool {
68        if !node.is_reachable() {
69            return false;
70        }
71        let support_reached = self
72            .support_reachable
73            .as_ref()
74            .is_some_and(|reach| reach.contains(node.file_id.0 as usize));
75        if !node.is_runtime_reachable() && !support_reached {
76            return true;
77        }
78        let relative = node.path.strip_prefix(&self.root).unwrap_or(&node.path);
79        self.excludes
80            .as_ref()
81            .is_some_and(|excludes| excludes.is_match(relative))
82    }
83}
84
85/// The test-side files of `graph` for the `test-only-export` rule. See
86/// [`TestSideMatcher::with_support_reach`].
87pub(crate) fn test_side_files(graph: &ModuleGraph, config: &ResolvedConfig) -> FxHashSet<FileId> {
88    let matcher = TestSideMatcher::with_support_reach(config, graph);
89    graph
90        .modules()
91        .iter()
92        .filter(|module| matcher.is_test_side(module))
93        .map(|module| module.file_id)
94        .collect()
95}