Skip to main content

fallow_engine/
dead_code_config_patterns.rs

1//! The config entries that had no effect in a dead-code pass, as
2//! `workspace_diagnostics[]` entries.
3
4use fallow_config::{
5    ResolvedConfig, RulesConfig, Severity, WorkspaceDiagnostic, WorkspaceDiagnosticKind,
6};
7
8/// `workspace_diagnostics[]` entries for the config entries that had no
9/// effect in the latest dead-code pass over `config`.
10///
11/// In config order per setting:
12///
13/// - each `ignoreFindings` pattern that matched no finding;
14/// - each `ignoreDependencies` glob that matched no declared dependency, and
15///   each `ignoreDependencies` entry that hid no dependency finding;
16/// - each `entry` pattern that matched only files that are entry points
17///   anyway;
18/// - each `ignorePatterns` glob that excluded no file the run would otherwise
19///   analyze;
20/// - each `circularDependencies.allow` entry that matched no cycle;
21/// - each `ignoreExports` file glob that matched no file, and each
22///   `ignoreExports` name that no file of its glob declares;
23/// - each `duplicates.ignore` and `health.ignore` glob that matched no file;
24/// - each `overrides` rule that cannot change a result of this config;
25/// - each `overrides[].files` glob that matched no project file.
26///
27/// The `ignoreDependencies` entries need `reports_dependencies`. A surface
28/// passes `reports_dependencies = false` when its run does not report
29/// dependency findings (an issue-type filter without the dependency types, or
30/// a file scope), because a dependency entry is then not relevant to what the
31/// run shows. A `--production` run checks fewer files and dependency
32/// sections, so it reports no redundant entry (issue #3824), no unmatched
33/// `circularDependencies.allow` entry (a cycle through a test file is not
34/// in that run) and no unmatched ignore list entry (a glob for test files
35/// matches no file of that run).
36/// A normal run does not report an `ignoreDependencies` entry that a
37/// `--production` run could need, because following the note would add a
38/// finding to that run.
39///
40/// An `overrides` rule reports also in a `--production` run, because its cause
41/// does not depend on the files of the run. The same is true for an
42/// `overrides[].files` glob: its check reads every project file, so the
43/// answer does not depend on `--production`, `--changed-since` or
44/// `--workspace` (issue #5227). The `reports_dependencies` gate
45/// removes the dependency rules (issue #4065). Read `config` after CLI flags
46/// changed its rules, because a flag can start a detector.
47///
48/// The CLI, the programmatic API and the MCP typed path all build their
49/// envelope from this one function, and the human note reads the same
50/// entries, so every output states the same patterns.
51#[must_use]
52pub fn config_pattern_diagnostics(
53    config: &ResolvedConfig,
54    reports_dependencies: bool,
55) -> Vec<WorkspaceDiagnostic> {
56    let dependencies_reported = reports_dependencies && dependency_rules_on(&config.rules);
57    let dependency_globs = if dependencies_reported {
58        config.ignore_dependencies.unmatched_globs()
59    } else {
60        Vec::new()
61    };
62    let checks_redundancy = !config.production;
63    let redundant_dependencies = if dependencies_reported && checks_redundancy {
64        config.ignore_dependencies.redundant_entries()
65    } else {
66        Vec::new()
67    };
68    let (redundant_entries, redundant_ignores) = if checks_redundancy {
69        (
70            config
71                .entry_pattern_effect
72                .ineffective(&config.entry_patterns),
73            config.ignore_patterns.redundant_user_patterns(),
74        )
75    } else {
76        (Vec::new(), Vec::new())
77    };
78    let ignore_lists = if checks_redundancy {
79        unmatched_ignore_list_kinds(config)
80    } else {
81        Vec::new()
82    };
83    let finding_patterns = config.ignore_findings.unmatched_patterns();
84    let unmatched_cycles = if checks_redundancy {
85        config
86            .circular_dependencies_allow_effect
87            .ineffective(&config.circular_dependencies.allow)
88    } else {
89        Vec::new()
90    };
91    let ineffective_overrides = config
92        .ineffective_override_rules()
93        .into_iter()
94        .filter(|entry| reports_dependencies || !entry.dependency_rule);
95    let unmatched_override_globs = super::override_globs::unmatched_override_globs(config);
96    let kinds = finding_patterns
97        .into_iter()
98        .map(
99            |pattern| WorkspaceDiagnosticKind::IgnoreFindingsPatternUnmatched {
100                pattern: pattern.to_owned(),
101            },
102        )
103        .chain(dependency_globs.into_iter().map(|pattern| {
104            WorkspaceDiagnosticKind::IgnoreDependenciesGlobUnmatched {
105                pattern: pattern.to_owned(),
106            }
107        }))
108        .chain(redundant_entries.into_iter().map(|pattern| {
109            WorkspaceDiagnosticKind::EntryPatternRedundant {
110                pattern: pattern.to_owned(),
111            }
112        }))
113        .chain(redundant_dependencies.into_iter().map(|pattern| {
114            WorkspaceDiagnosticKind::IgnoreDependenciesEntryRedundant {
115                pattern: pattern.to_owned(),
116            }
117        }))
118        .chain(redundant_ignores.into_iter().map(|pattern| {
119            WorkspaceDiagnosticKind::IgnorePatternsEntryRedundant {
120                pattern: pattern.to_owned(),
121            }
122        }))
123        .chain(unmatched_cycles.into_iter().map(|files| {
124            WorkspaceDiagnosticKind::CircularDependenciesAllowUnmatched {
125                pattern: format!("[{}]", files.join(", ")),
126                files: files.clone(),
127            }
128        }))
129        .chain(ignore_lists)
130        .chain(ineffective_overrides.map(|entry| {
131            WorkspaceDiagnosticKind::OverrideRuleIneffective {
132                rule: entry.rule.to_owned(),
133                files: entry.files,
134                reason: entry.reason.to_owned(),
135            }
136        }))
137        .chain(unmatched_override_globs);
138    kinds
139        .map(|kind| {
140            WorkspaceDiagnostic::new(&config.root, config.root.clone(), kind)
141                .into_root_relative(&config.root)
142        })
143        .collect()
144}
145
146/// The `ignoreExports`, `duplicates.ignore` and `health.ignore` entries that
147/// matched no file or export of the latest dead-code pass, in that order and
148/// in config order per setting (issue #5167).
149fn unmatched_ignore_list_kinds(config: &ResolvedConfig) -> Vec<WorkspaceDiagnosticKind> {
150    let effect = &config.ignore_list_effect;
151    let unmatched_rules: Vec<&str> = effect
152        .ignore_export_files
153        .ineffective(&config.ignore_export_rules)
154        .into_iter()
155        .map(|rule| rule.file.as_str())
156        .collect();
157    let files =
158        unmatched_rules.iter().map(
159            |pattern| WorkspaceDiagnosticKind::IgnoreExportsFileUnmatched {
160                pattern: (*pattern).to_owned(),
161            },
162        );
163    // A rule whose glob matched no file already has a note; a note per name
164    // of that rule would repeat it.
165    let names = config.ignore_export_names();
166    let names = effect
167        .ignore_export_names
168        .ineffective(&names)
169        .into_iter()
170        .filter(|name| !unmatched_rules.contains(&name.file))
171        .map(|name| WorkspaceDiagnosticKind::IgnoreExportsNameUnmatched {
172            pattern: format!("{} in {}", name.export, name.file),
173            file: name.file.to_owned(),
174            export: name.export.to_owned(),
175        });
176    let duplicates = effect
177        .duplicates_ignore
178        .ineffective(&config.duplicates.ignore)
179        .into_iter()
180        .map(
181            |pattern| WorkspaceDiagnosticKind::DuplicatesIgnoreUnmatched {
182                pattern: pattern.clone(),
183            },
184        );
185    let health = effect
186        .health_ignore
187        .ineffective(&config.health.ignore)
188        .into_iter()
189        .map(|pattern| WorkspaceDiagnosticKind::HealthIgnoreUnmatched {
190            pattern: pattern.clone(),
191        });
192    files.chain(names).chain(duplicates).chain(health).collect()
193}
194
195/// Whether at least one rule that `ignoreDependencies` controls is on. With
196/// every such rule off, the run reports no dependency finding at all.
197fn dependency_rules_on(rules: &RulesConfig) -> bool {
198    [
199        rules.unused_dependencies,
200        rules.unused_dev_dependencies,
201        rules.unused_optional_dependencies,
202        rules.unlisted_dependencies,
203        rules.type_only_dependencies,
204        rules.test_only_dependencies,
205        rules.dev_dependencies_in_production,
206    ]
207    .into_iter()
208    .any(|severity| severity != Severity::Off)
209}