use rustc_hash::FxHashSet;
use fallow_config::ExternalPluginDef;
use super::super::Plugin;
use super::should_warn;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum PluginDiagnostic {
PatternCollision {
pattern: String,
owners: Vec<String>,
},
EnablerTypo {
plugin: String,
enabler: String,
suggestion: String,
},
}
pub(super) fn detect_pattern_collisions(
builtin_active: &[&dyn Plugin],
external_active: &[&ExternalPluginDef],
) -> Vec<PluginDiagnostic> {
use rustc_hash::FxHashMap;
let mut pattern_owners: FxHashMap<String, (Vec<String>, FxHashSet<String>)> =
FxHashMap::default();
let record = |pattern_owners: &mut FxHashMap<_, (Vec<String>, FxHashSet<String>)>,
pattern: String,
name: String| {
let (list, seen) = pattern_owners.entry(pattern).or_default();
if seen.insert(name.clone()) {
list.push(name);
}
};
for plugin in builtin_active {
for pat in plugin.config_patterns() {
record(
&mut pattern_owners,
(*pat).to_string(),
plugin.name().to_string(),
);
}
}
for ext in external_active {
for pat in &ext.config_patterns {
record(&mut pattern_owners, pat.clone(), ext.name.clone());
}
}
let builtin_names: FxHashSet<&str> = builtin_active.iter().map(|p| p.name()).collect();
let mut findings: Vec<PluginDiagnostic> = pattern_owners
.into_iter()
.filter_map(|(pattern, (owners, _seen))| {
if owners.len() < 2 || owners.iter().all(|o| builtin_names.contains(o.as_str())) {
None
} else {
Some(PluginDiagnostic::PatternCollision { pattern, owners })
}
})
.collect();
findings.sort_unstable_by(|a, b| match (a, b) {
(
PluginDiagnostic::PatternCollision { pattern: ap, .. },
PluginDiagnostic::PatternCollision { pattern: bp, .. },
) => ap.cmp(bp),
_ => std::cmp::Ordering::Equal,
});
findings
}
pub(super) fn detect_enabler_typos(
external_plugins: &[ExternalPluginDef],
all_deps: &[String],
) -> Vec<PluginDiagnostic> {
let mut findings = Vec::new();
for ext in external_plugins {
if ext.detection.is_some() || ext.enablers.is_empty() {
continue;
}
let any_match = ext.enablers.iter().any(|enabler| {
if enabler.ends_with('/') {
all_deps.iter().any(|d| d.starts_with(enabler))
} else {
all_deps.iter().any(|d| d == enabler)
}
});
if any_match {
continue;
}
for enabler in &ext.enablers {
let candidates = all_deps.iter().map(String::as_str);
let Some(suggestion) = fallow_config::levenshtein::closest_match(enabler, candidates)
else {
continue;
};
findings.push(PluginDiagnostic::EnablerTypo {
plugin: ext.name.clone(),
enabler: enabler.clone(),
suggestion: suggestion.to_string(),
});
}
}
findings
}
pub(super) fn emit_plugin_diagnostics(findings: &[PluginDiagnostic]) {
for finding in findings {
match finding {
PluginDiagnostic::PatternCollision { pattern, owners } => {
let key = format!("collision::{pattern}::{owners:?}");
if !should_warn(key) {
continue;
}
let winner = &owners[0];
let others = owners[1..].join(", ");
tracing::warn!(
"plugin config_patterns collision: identical pattern \
'{pattern}' is claimed by plugins [{joined}]; '{winner}' \
runs first (registration order), others ({others}) \
follow. Rename one of the patterns or remove the \
duplicate plugin to make resolution explicit. A future \
release may reject identical-pattern collisions.",
joined = owners.join(", "),
);
}
PluginDiagnostic::EnablerTypo {
plugin,
enabler,
suggestion,
} => {
let key = format!("enabler::{plugin}::{enabler}");
if !should_warn(key) {
continue;
}
tracing::warn!(
"plugin '{plugin}' enabler '{enabler}' does not match any \
dependency in package.json; did you mean '{suggestion}'? \
The plugin will not activate. A future release may reject \
unmatched enablers.",
);
}
}
}
}