use std::path::{Path, PathBuf};
use fallow_config::ResolvedConfig;
use fallow_types::discover::{DiscoveredFile, EntryPointSource};
use fallow_types::path_util::display_relative;
use rustc_hash::FxHashSet;
use super::CategorizedEntryPoints;
use super::walk::should_emit_note_once;
fn find_legacy_glob_narrowings(
config: &ResolvedConfig,
files: &[DiscoveredFile],
entry_fields: bool,
) -> Vec<fallow_config::LegacyGlobNarrowing> {
if config.quiet {
return Vec::new();
}
let groups: Vec<fallow_config::LegacyGlobGroup> = config
.legacy_glob_groups
.iter()
.filter(|group| group.counts_entry_sources() == entry_fields)
.cloned()
.collect();
if groups.is_empty() {
return Vec::new();
}
let relative: Vec<&Path> = files
.iter()
.map(|file| file.path.strip_prefix(&config.root).unwrap_or(&file.path))
.collect();
fallow_config::find_legacy_glob_narrowings(&groups, &relative)
}
fn warn_legacy_glob_narrowing(
config: &ResolvedConfig,
narrowing: &fallow_config::LegacyGlobNarrowing,
) {
if should_emit_note_once(format!(
"legacy-glob::{}::{}::{}",
config.root.display(),
narrowing.field,
narrowing.pattern
)) {
tracing::warn!("{}", narrowing.message());
}
}
pub(super) fn warn_legacy_glob_narrowings(config: &ResolvedConfig, files: &[DiscoveredFile]) {
for narrowing in find_legacy_glob_narrowings(config, files, false) {
warn_legacy_glob_narrowing(config, &narrowing);
}
}
pub(super) fn warn_entry_legacy_glob_narrowings(
config: &ResolvedConfig,
files: &[DiscoveredFile],
entry_points: &CategorizedEntryPoints,
) {
for mut narrowing in find_legacy_glob_narrowings(config, files, true) {
let is_entry_field = narrowing.field == "entry";
let declared: FxHashSet<String> = entry_points
.all
.iter()
.filter(|entry| {
if is_entry_field {
!matches!(entry.source, EntryPointSource::ManualEntry)
} else {
!matches!(entry.source, EntryPointSource::DynamicallyLoaded)
}
})
.map(|entry| {
display_relative(&config.root, &entry.path.components().collect::<PathBuf>())
})
.collect();
if narrowing.retain_files(|file| !declared.contains(file)) {
warn_legacy_glob_narrowing(config, &narrowing);
}
}
}