use std::path::Path;
use rustc_hash::{FxHashMap, FxHashSet};
use super::relative_key_path;
pub fn styling_keys(report: &fallow_output::HealthReport, root: &Path) -> FxHashSet<String> {
styling_ledger_keys(&report.styling_findings, root)
.into_iter()
.collect()
}
pub fn styling_finding_key(finding: &fallow_output::StylingFinding, root: &Path) -> String {
let path = if finding.internal.group_paths.is_empty() {
relative_key_path(Path::new(&finding.path), root)
} else {
finding
.internal
.group_paths
.iter()
.map(|path| relative_key_path(Path::new(path), root))
.collect::<Vec<_>>()
.join("|")
};
let base = format!(
"styling:{}:{}:{path}:{}",
finding.code, finding.sub_kind, finding.value
);
match finding.internal.rule_selector.as_deref() {
Some(selector) if !selector.is_empty() => {
format!(
"{base}:{:016x}",
xxhash_rust::xxh3::xxh3_64(selector.as_bytes())
)
}
_ => base,
}
}
pub fn styling_ledger_keys(findings: &[fallow_output::StylingFinding], root: &Path) -> Vec<String> {
styling_ledger_keys_with_diff(findings, root, None)
}
pub fn styling_ledger_keys_with_diff(
findings: &[fallow_output::StylingFinding],
root: &Path,
diff: Option<&fallow_output::DiffIndex>,
) -> Vec<String> {
let mut keys: Vec<String> = findings
.iter()
.map(|finding| styling_finding_key(finding, root))
.collect();
let on_added_line = |finding: &fallow_output::StylingFinding| -> bool {
diff.is_some_and(|diff| {
diff.key_for(&root.join(&finding.path), root)
.is_some_and(|path| diff.line_is_added(&path, u64::from(finding.line)))
})
};
let mut order: Vec<usize> = (0..findings.len()).collect();
order.sort_by_cached_key(|&index| (on_added_line(&findings[index]), findings[index].line));
let mut seen: FxHashMap<String, usize> = FxHashMap::default();
for index in order {
let occurrence = seen.entry(keys[index].clone()).or_default();
*occurrence += 1;
if *occurrence > 1 {
keys[index] = format!("{}#{occurrence}", keys[index]);
}
}
keys
}
pub(super) fn annotate_styling_json(
json: &mut serde_json::Value,
findings: &[fallow_output::StylingFinding],
root: &Path,
base: &FxHashSet<String>,
) {
let Some(items) = json
.get_mut("styling_findings")
.and_then(serde_json::Value::as_array_mut)
else {
return;
};
let keys = styling_ledger_keys(findings, root);
for ((item, key), finding) in items.iter_mut().zip(&keys).zip(findings) {
if let serde_json::Value::Object(map) = item {
let introduced = finding
.internal
.introduced
.unwrap_or_else(|| !base.contains(key));
map.insert("introduced".to_string(), serde_json::json!(introduced));
}
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::{styling_finding_key, styling_ledger_keys, styling_ledger_keys_with_diff};
fn styling_finding(path: &str, line: u32, value: &str) -> fallow_output::StylingFinding {
fallow_output::StylingFinding {
internal: fallow_output::StylingFindingInternal::default(),
code: "css-selector-complexity".to_string(),
sub_kind: "high-specificity".to_string(),
path: path.to_string(),
line,
value: value.to_string(),
effective_severity: fallow_output::StylingFindingSeverity::Warn,
blast_radius: None,
confidence: None,
agent_disposition: None,
nearest_token: None,
fix_hint: None,
actions: Vec::new(),
}
}
#[test]
fn styling_ledger_keys_ignore_line_shifts_and_count_repeats() {
let root = PathBuf::from("/project");
let base = [
styling_finding("src/a.css", 1, "specificity 1-2-0"),
styling_finding("src/a.css", 4, "specificity 1-2-0"),
styling_finding("src/b.css", 2, "specificity 1-2-0"),
];
let head = [
styling_finding("src/b.css", 2, "specificity 1-2-0"),
styling_finding("src/a.css", 7, "specificity 1-2-0"),
styling_finding("src/a.css", 3, "specificity 1-2-0"),
];
let base_keys = styling_ledger_keys(&base, &root);
let head_keys = styling_ledger_keys(&head, &root);
assert_eq!(
base_keys,
vec![
"styling:css-selector-complexity:high-specificity:src/a.css:specificity 1-2-0",
"styling:css-selector-complexity:high-specificity:src/a.css:specificity 1-2-0#2",
"styling:css-selector-complexity:high-specificity:src/b.css:specificity 1-2-0",
]
);
assert_eq!(head_keys[1], base_keys[1]);
assert_eq!(head_keys[2], base_keys[0]);
assert_eq!(head_keys[0], base_keys[2]);
}
#[test]
fn styling_ledger_keys_with_diff_give_added_lines_the_last_occurrence() {
let root = PathBuf::from("/project");
let base = [styling_finding("src/a.css", 4, "specificity 1-2-0")];
let head = [
styling_finding("src/a.css", 1, "specificity 1-2-0"),
styling_finding("src/a.css", 5, "specificity 1-2-0"),
];
let diff = fallow_output::DiffIndex::from_unified_diff(
"diff --git a/src/a.css b/src/a.css\n--- a/src/a.css\n+++ b/src/a.css\n@@ -0,0 +1,1 @@\n+#nav .menu .item { color: green; }\n",
);
let base_keys = styling_ledger_keys(&base, &root);
let head_keys = styling_ledger_keys_with_diff(&head, &root, Some(&diff));
assert_eq!(head_keys[1], base_keys[0]);
assert_eq!(head_keys[0], format!("{}#2", base_keys[0]));
}
#[test]
fn duplicate_block_key_ignores_the_named_occurrence() {
let root = PathBuf::from("/project");
let block = |named: &str, paths: &[&str]| {
let mut finding = styling_finding(named, 10, "4-declaration block repeated 2 times");
finding.code = "css-duplicate-block".to_string();
finding.sub_kind = "duplicate-declaration-block".to_string();
finding.internal.group_paths = paths.iter().map(ToString::to_string).collect();
finding
};
let base = styling_finding_key(&block("src/A.vue", &["src/A.vue", "src/B.vue"]), &root);
let scoped = styling_finding_key(&block("src/B.vue", &["src/A.vue", "src/B.vue"]), &root);
let other = styling_finding_key(&block("src/A.vue", &["src/A.vue", "src/C.vue"]), &root);
assert_eq!(base, scoped);
assert_ne!(base, other);
}
#[test]
fn styling_finding_key_uses_the_rule_selector() {
let root = PathBuf::from("/project");
let with_selector = |line: u32, selector: &str| {
let mut finding = styling_finding("src/a.css", line, "specificity 1-2-0");
finding.internal.rule_selector = Some(selector.to_string());
finding
};
let legacy = styling_finding_key(&with_selector(4, "#app .legacy .title"), &root);
let moved = styling_finding_key(&with_selector(9, "#app .legacy .title"), &root);
let nav = styling_finding_key(&with_selector(6, "#nav .menu .item"), &root);
assert_eq!(legacy, moved);
assert_ne!(legacy, nav);
assert!(!legacy.contains("#app"), "{legacy}");
}
}