Skip to main content

fallow_api/audit_run/
own_config.rs

1//! The evidence of a base pass under the config of the base commit.
2
3use std::path::{Path, PathBuf};
4
5use fallow_config::{ResolvedConfig, Severity};
6use fallow_types::results::AnalysisResults;
7use rustc_hash::FxHashSet;
8
9use crate::audit_keys::collection_keys;
10
11/// The path of the config file `config_path`, relative to `root`.
12///
13/// The function canonicalizes both paths. Thus a relative `-c` path, or a
14/// path through a symlink such as the macOS `/tmp`, also resolves. `None` when
15/// the config file does not exist or is not below `root`.
16#[must_use]
17pub fn config_path_under_root(config_path: &Path, root: &Path) -> Option<PathBuf> {
18    let config_path = dunce::canonicalize(config_path).ok()?;
19    let root = dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
20    config_path.strip_prefix(&root).ok().map(Path::to_path_buf)
21}
22
23/// The keys for [`super::AuditKeySnapshot::own_config_unused_code`].
24///
25/// `own` holds the results of the base pass under the config of the base
26/// commit, and `own_config` is that config. `candidates` holds the results of
27/// the base pass under the head config. The set has the unused files and
28/// unused exports that `own` reports. It also has each candidate that
29/// `own_config` keeps out of its report (an `ignorePatterns`,
30/// `ignoreFindings` or `ignoreExports` entry, or an override that turns the
31/// rule off), and each candidate export in a file that `own` reports as an
32/// unused file. The base config cannot prove that such code was in use, so a
33/// config change that only shows old unused code does not make it introduced.
34#[must_use]
35pub fn own_config_unused_code_keys(
36    own: &AnalysisResults,
37    own_config: &ResolvedConfig,
38    candidates: &AnalysisResults,
39    root: &Path,
40) -> FxHashSet<String> {
41    let mut keys: FxHashSet<String> = collection_keys(&own.unused_files, root)
42        .into_iter()
43        .chain(collection_keys(&own.unused_exports, root))
44        .collect();
45    let files = collection_keys(&candidates.unused_files, root);
46    for (finding, key) in candidates.unused_files.iter().zip(files) {
47        if hides_unused_file(own_config, relative(&finding.file.path, root)) {
48            keys.insert(key);
49        }
50    }
51    let unused_files: FxHashSet<&Path> = own
52        .unused_files
53        .iter()
54        .map(|finding| relative(&finding.file.path, root))
55        .collect();
56    let exports = collection_keys(&candidates.unused_exports, root);
57    for (finding, key) in candidates.unused_exports.iter().zip(exports) {
58        let export = &finding.export;
59        let path = relative(&export.path, root);
60        if unused_files.contains(path) || hides_unused_export(own_config, path, &export.export_name)
61        {
62            keys.insert(key);
63        }
64    }
65    keys
66}
67
68fn relative<'a>(path: &'a Path, root: &Path) -> &'a Path {
69    path.strip_prefix(root).unwrap_or(path)
70}
71
72/// Whether `config` keeps every finding in the file at `relative` out of its
73/// report.
74fn hides_path(config: &ResolvedConfig, relative: &Path) -> bool {
75    config.ignore_patterns.is_match(relative)
76        || config
77            .ignore_findings
78            .is_ignored_path(&config.root, relative)
79}
80
81fn hides_unused_file(config: &ResolvedConfig, relative: &Path) -> bool {
82    hides_path(config, relative)
83        || config.resolve_rules_for_path(relative).unused_files == Severity::Off
84}
85
86fn hides_unused_export(config: &ResolvedConfig, relative: &Path, name: &str) -> bool {
87    if hides_path(config, relative)
88        || config.resolve_rules_for_path(relative).unused_exports == Severity::Off
89    {
90        return true;
91    }
92    let file = relative.to_string_lossy();
93    config.compiled_ignore_exports.iter().any(|rule| {
94        rule.matcher.is_match(file.as_ref())
95            && rule
96                .exports
97                .iter()
98                .any(|export| export == "*" || export == name)
99    })
100}