use std::path::Path;
use globset::{Glob, GlobMatcher, GlobSet, GlobSetBuilder};
use super::FallowConfig;
use super::glob_validation::compile_path_glob;
use crate::external_plugin::{ExternalPluginDef, PluginDetection};
use crate::rule_pack::RulePackDef;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LegacyGlobGroup {
pub field: &'static str,
pub prefix: &'static str,
pub patterns: Vec<String>,
}
impl LegacyGlobGroup {
#[must_use]
pub fn counts_entry_sources(&self) -> bool {
ENTRY_SOURCE_FIELDS.contains(&self.field)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LegacyGlobNarrowing {
pub field: &'static str,
pub pattern: String,
pub legacy_only_files: Vec<String>,
pub suggestion: String,
}
const ENTRY_SOURCE_FIELDS: [&str; 2] = ["entry", "dynamicallyLoaded"];
const SINGLE_SEGMENT_RULE_VERSION: &str = "3.34.0";
const PLUGIN_FIELD_PREFIXES: [&str; 2] = ["framework[]", "rulePacks "];
const NAMED_FILES: usize = 3;
impl LegacyGlobNarrowing {
#[must_use]
pub fn counts_entry_sources(&self) -> bool {
ENTRY_SOURCE_FIELDS.contains(&self.field)
}
pub fn retain_files(&mut self, mut keep: impl FnMut(&str) -> bool) -> bool {
self.legacy_only_files.retain(|file| keep(file));
!self.legacy_only_files.is_empty()
}
#[must_use]
pub fn message(&self) -> String {
let count = self.legacy_only_files.len();
let (noun, lost) = if count == 1 {
("file", "This file does not match")
} else {
("files", "These files do not match")
};
let named = self.legacy_only_files[..count.min(NAMED_FILES)].join(", ");
let files = if count > NAMED_FILES {
format!("{named}, and {} more", count - NAMED_FILES)
} else {
named
};
let keep: &str = if PLUGIN_FIELD_PREFIXES
.iter()
.any(|prefix| self.field.starts_with(prefix))
{
"To keep the one-level match and stop this warning, write the names in place of \
\"*\" or \"?\", for example \"{name1,name2}\"."
} else {
&format!(
"If this config is written for the one-level match, set \"minimumVersion\" \
to \"{SINGLE_SEGMENT_RULE_VERSION}\" or higher to stop this warning."
)
};
format!(
"fallow: {} glob \"{}\" does not match {count} {noun} in a nested folder. A \
single \"*\" or \"?\" matches in one folder level only. {lost}: {files}. {keep} \
To match nested folders, use \"{}\".",
self.field, self.pattern, self.suggestion
)
}
}
fn written_for_single_segment_rule(config: &FallowConfig) -> bool {
config
.minimum_version
.as_deref()
.and_then(super::parsing::version_triple)
.zip(super::parsing::version_triple(SINGLE_SEGMENT_RULE_VERSION))
.is_some_and(|(declared, rule)| declared >= rule)
}
fn normalize(pattern: &str) -> &str {
super::glob_validation::normalize_path_pattern(pattern)
}
fn has_wildcard(segment: &str) -> bool {
segment != "**" && segment.contains(['*', '?'])
}
#[must_use]
pub fn has_single_segment_wildcard(pattern: &str) -> bool {
let mut after_globstar = false;
for segment in normalize(pattern).split('/') {
if has_wildcard(segment) && !after_globstar {
return true;
}
after_globstar = segment == "**";
}
false
}
#[must_use]
pub fn suggest_cross_directory_rewrite(pattern: &str) -> String {
let mut out: Vec<&str> = Vec::new();
let mut after_globstar = false;
for segment in pattern.split('/') {
if segment == "**" {
if !after_globstar {
out.push("**");
}
after_globstar = true;
continue;
}
if has_wildcard(segment) && !after_globstar {
out.push("**");
if segment == "*" {
after_globstar = true;
continue;
}
}
out.push(segment);
after_globstar = false;
}
out.join("/")
}
#[derive(Default)]
struct GroupCollector {
groups: Vec<LegacyGlobGroup>,
}
impl GroupCollector {
fn push<'a>(
&mut self,
field: &'static str,
prefix: &'static str,
patterns: impl IntoIterator<Item = &'a str>,
) {
let patterns: Vec<String> = patterns.into_iter().map(str::to_owned).collect();
if patterns.iter().any(|p| has_single_segment_wildcard(p)) {
self.groups.push(LegacyGlobGroup {
field,
prefix,
patterns,
});
}
}
fn push_list(&mut self, field: &'static str, patterns: &[String]) {
self.push(field, "", patterns.iter().map(String::as_str));
}
fn push_detection(&mut self, detection: &PluginDetection) {
match detection {
PluginDetection::Dependency { .. } => {}
PluginDetection::FileExists { pattern } => {
self.push("framework[].detection.pattern", "", [pattern.as_str()]);
}
PluginDetection::All { conditions } | PluginDetection::Any { conditions } => {
for condition in conditions {
self.push_detection(condition);
}
}
}
}
fn push_config(&mut self, config: &FallowConfig) {
self.push_list("entry", &config.entry);
self.push_list("dynamicallyLoaded", &config.dynamically_loaded);
self.push_list("production.ignore", config.production.ignore());
let positive = |p: &&String| !p.starts_with('!');
self.push(
"ignorePatterns",
"",
config
.ignore_patterns
.iter()
.filter(positive)
.map(String::as_str),
);
self.push(
"ignoreFindings",
"",
config
.ignore_findings
.iter()
.filter(positive)
.map(String::as_str),
);
self.push(
"ignoreFindings",
"!",
config
.ignore_findings
.iter()
.filter_map(|p| p.strip_prefix('!')),
);
for rule in &config.ignore_exports {
self.push("ignoreExports[].file", "", [rule.file.as_str()]);
}
for rule in &config.ignore_catalog_references {
if let Some(consumer) = &rule.consumer {
self.push(
"ignoreCatalogReferences[].consumer",
"",
[consumer.as_str()],
);
}
}
for entry in &config.overrides {
self.push_list("overrides[].files", &entry.files);
}
self.push_list("duplicates.ignore", &config.duplicates.ignore);
self.push_list("similarCode.ignore", &config.similar_code.ignore);
self.push_list("health.ignore", &config.health.ignore);
for entry in &config.health.threshold_overrides {
self.push_list("health.thresholdOverrides[].files", &entry.files);
}
for zone in &config.boundaries.zones {
self.push_list("boundaries.zones[].patterns", &zone.patterns);
}
self.push_list(
"boundaries.coverage.allowUnmatched",
&config.boundaries.coverage.allow_unmatched,
);
}
fn push_plugin(&mut self, plugin: &ExternalPluginDef) {
for used in &plugin.used_exports {
self.push(
"framework[].usedExports[].pattern",
"",
[used.pattern.as_str()],
);
}
for rule in &plugin.manifest_entries {
self.push(
"framework[].manifestEntries[].manifests",
"",
[rule.manifests.as_str()],
);
}
if let Some(detection) = &plugin.detection {
self.push_detection(detection);
}
}
fn push_rule_pack(&mut self, pack: &RulePackDef) {
for rule in &pack.rules {
self.push_list("rulePacks rules[].files", &rule.files);
self.push_list("rulePacks rules[].exclude", &rule.exclude);
self.push_list("rulePacks rules[].allowedFiles", &rule.allowed_files);
}
}
}
pub(super) fn collect_config_legacy_glob_groups(config: &FallowConfig) -> Vec<LegacyGlobGroup> {
if written_for_single_segment_rule(config) {
return Vec::new();
}
let mut collector = GroupCollector::default();
collector.push_config(config);
collector.groups
}
pub(super) fn extend_legacy_glob_groups(
groups: &mut Vec<LegacyGlobGroup>,
plugins: &[ExternalPluginDef],
rule_packs: &[RulePackDef],
) {
let mut collector = GroupCollector {
groups: std::mem::take(groups),
};
for plugin in plugins {
collector.push_plugin(plugin);
}
for pack in rule_packs {
collector.push_rule_pack(pack);
}
*groups = collector.groups;
}
struct CompiledGroup<'a> {
group: &'a LegacyGlobGroup,
strict: GlobSet,
legacy: Vec<(usize, GlobMatcher)>,
lost: Vec<Vec<String>>,
}
impl CompiledGroup<'_> {
fn record(&mut self, path: &Path) {
let mut strict_match: Option<bool> = None;
for (slot, (_, matcher)) in self.legacy.iter().enumerate() {
if !matcher.is_match(path) {
continue;
}
if !*strict_match.get_or_insert_with(|| self.strict.is_match(path)) {
self.lost[slot].push(path.to_string_lossy().replace('\\', "/"));
}
}
}
}
fn compile_group(group: &LegacyGlobGroup) -> Option<CompiledGroup<'_>> {
let mut strict = GlobSetBuilder::new();
let mut legacy = Vec::new();
for (idx, pattern) in group.patterns.iter().enumerate() {
let normalized = normalize(pattern);
if let Ok(glob) = compile_path_glob(normalized) {
strict.add(glob);
}
if has_single_segment_wildcard(normalized)
&& let Ok(glob) = Glob::new(normalized)
{
legacy.push((idx, glob.compile_matcher()));
}
}
if legacy.is_empty() {
return None;
}
let lost = vec![Vec::new(); legacy.len()];
Some(CompiledGroup {
group,
strict: strict.build().ok()?,
legacy,
lost,
})
}
#[must_use]
pub fn find_legacy_glob_narrowings(
groups: &[LegacyGlobGroup],
relative_paths: &[&Path],
) -> Vec<LegacyGlobNarrowing> {
let mut compiled: Vec<CompiledGroup<'_>> = groups.iter().filter_map(compile_group).collect();
if compiled.is_empty() {
return Vec::new();
}
for path in relative_paths {
for group in &mut compiled {
group.record(path);
}
}
let mut out = Vec::new();
for group in compiled {
let LegacyGlobGroup {
field,
prefix,
patterns,
} = group.group;
for ((idx, _), mut files) in group.legacy.iter().zip(group.lost) {
if files.is_empty() {
continue;
}
files.sort_unstable();
let pattern = &patterns[*idx];
out.push(LegacyGlobNarrowing {
field,
pattern: format!("{prefix}{pattern}"),
legacy_only_files: files,
suggestion: format!("{prefix}{}", suggest_cross_directory_rewrite(pattern)),
});
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detects_single_segment_wildcards() {
assert!(has_single_segment_wildcard("*"));
assert!(has_single_segment_wildcard("*.test.ts"));
assert!(has_single_segment_wildcard("src/x/*"));
assert!(has_single_segment_wildcard("./src/x/*"));
assert!(has_single_segment_wildcard("apps/*/docs/**"));
assert!(has_single_segment_wildcard("src/?.ts"));
assert!(has_single_segment_wildcard("src/**/foo/*.ts"));
assert!(!has_single_segment_wildcard("**/*.test.ts"));
assert!(!has_single_segment_wildcard("src/**/*.ts"));
assert!(!has_single_segment_wildcard("src/generated/**"));
assert!(!has_single_segment_wildcard("src/index.ts"));
}
#[test]
fn suggests_globstar_rewrites() {
assert_eq!(suggest_cross_directory_rewrite("src/x/*"), "src/x/**");
assert_eq!(suggest_cross_directory_rewrite("*.test.ts"), "**/*.test.ts");
assert_eq!(suggest_cross_directory_rewrite("*"), "**");
assert_eq!(
suggest_cross_directory_rewrite("apps/*/docs/**"),
"apps/**/docs/**"
);
assert_eq!(
suggest_cross_directory_rewrite("packages/cli/src/commands/*.ts"),
"packages/cli/src/commands/**/*.ts"
);
assert_eq!(suggest_cross_directory_rewrite("*/*.ts"), "**/*.ts");
}
fn group(field: &'static str, patterns: &[&str]) -> LegacyGlobGroup {
LegacyGlobGroup {
field,
prefix: "",
patterns: patterns.iter().map(|p| (*p).to_owned()).collect(),
}
}
fn paths<'a>(raw: &[&'a str]) -> Vec<&'a Path> {
raw.iter().map(|p| Path::new(*p)).collect()
}
#[test]
fn counts_files_only_the_old_rule_matched() {
let files = paths(&[
"a.test.ts",
"src/b.test.ts",
"src/deep/c.test.ts",
"src/d.ts",
]);
let found = find_legacy_glob_narrowings(&[group("health.ignore", &["*.test.ts"])], &files);
assert_eq!(found.len(), 1);
assert_eq!(
found[0].legacy_only_files,
["src/b.test.ts", "src/deep/c.test.ts"]
);
assert_eq!(found[0].suggestion, "**/*.test.ts");
assert_eq!(
found[0].message(),
"fallow: health.ignore glob \"*.test.ts\" does not match 2 files in a nested folder. A single \"*\" or \"?\" matches in one folder level only. These files do \
not match: src/b.test.ts, src/deep/c.test.ts. If this config is written for the \
one-level match, set \"minimumVersion\" to \"3.34.0\" or higher to stop this \
warning. To match nested folders, use \"**/*.test.ts\"."
);
}
#[test]
fn the_message_names_three_files_then_the_remaining_count() {
let files = paths(&[
"src/e/e.ts",
"src/a/a.ts",
"src/d/d.ts",
"src/c/c.ts",
"src/b/b.ts",
]);
let found = find_legacy_glob_narrowings(&[group("entry", &["src/*.ts"])], &files);
assert_eq!(
found[0].message(),
"fallow: entry glob \"src/*.ts\" does not match 5 files in a nested folder. A \
single \"*\" or \"?\" matches in one folder level only. These files do not \
match: src/a/a.ts, src/b/b.ts, src/c/c.ts, and 2 more. If this config is written \
for the one-level match, set \"minimumVersion\" to \"3.34.0\" or higher to stop \
this warning. To match nested folders, use \"src/**/*.ts\"."
);
}
#[test]
fn a_plugin_glob_message_names_the_names_rewrite() {
let files = paths(&["src/a/a.ts"]);
let found = find_legacy_glob_narrowings(
&[group("framework[].usedExports[].pattern", &["src/*.ts"])],
&files,
);
let message = found[0].message();
assert!(message.contains("write the names in place of"), "{message}");
assert!(!message.contains("minimumVersion"), "{message}");
}
fn config_with(minimum_version: Option<&str>) -> FallowConfig {
FallowConfig {
minimum_version: minimum_version.map(str::to_owned),
entry: vec!["scripts/*.ts".to_owned()],
..FallowConfig::default()
}
}
#[test]
fn minimum_version_at_the_single_segment_rule_drops_the_config_groups() {
for version in ["3.34.0", "3.34.1", "3.45.0", "4.0.0"] {
let groups = collect_config_legacy_glob_groups(&config_with(Some(version)));
assert!(groups.is_empty(), "{version}: {groups:?}");
}
}
#[test]
fn an_older_or_missing_minimum_version_keeps_the_config_groups() {
for version in [None, Some("3.33.9"), Some("3.0.0"), Some("2.99.99")] {
let groups = collect_config_legacy_glob_groups(&config_with(version));
assert_eq!(groups.len(), 1, "{version:?}");
}
}
#[test]
fn the_message_uses_the_singular_for_one_file() {
let files = paths(&["src/a/a.ts"]);
let found = find_legacy_glob_narrowings(&[group("entry", &["src/*.ts"])], &files);
assert!(
found[0]
.message()
.contains("does not match 1 file in a nested folder")
);
assert!(
found[0]
.message()
.contains("This file does not match: src/a/a.ts. If this config")
);
}
#[test]
fn files_that_another_source_covers_are_removed() {
let files = paths(&["src/a/a.ts", "src/b/b.ts"]);
let mut found = find_legacy_glob_narrowings(&[group("entry", &["src/*.ts"])], &files);
assert!(found[0].counts_entry_sources());
assert!(!found[0].retain_files(|_| false));
let mut partial = find_legacy_glob_narrowings(&[group("entry", &["src/*.ts"])], &files);
assert!(partial[0].retain_files(|file| file == "src/b/b.ts"));
assert_eq!(partial[0].legacy_only_files, ["src/b/b.ts"]);
let health = find_legacy_glob_narrowings(&[group("health.ignore", &["src/*.ts"])], &files);
assert!(!health[0].counts_entry_sources());
}
#[test]
fn a_sibling_pattern_that_still_matches_suppresses_the_count() {
let files = paths(&["src/deep/c.test.ts"]);
let found = find_legacy_glob_narrowings(
&[group("health.ignore", &["*.test.ts", "src/**/*.test.ts"])],
&files,
);
assert!(found.is_empty());
}
#[test]
fn no_narrowing_when_the_match_set_is_the_same() {
let files = paths(&["a.test.ts", "src/b.ts"]);
let found = find_legacy_glob_narrowings(&[group("health.ignore", &["*.test.ts"])], &files);
assert!(found.is_empty());
}
#[test]
fn exception_patterns_keep_their_prefix() {
let files = paths(&["src/deep/c.ts"]);
let mut exceptions = group("ignoreFindings", &["src/*"]);
exceptions.prefix = "!";
let found = find_legacy_glob_narrowings(&[exceptions], &files);
assert_eq!(found[0].pattern, "!src/*");
assert_eq!(found[0].suggestion, "!src/**");
}
}