use forge::signal::compactor;
use once_cell::sync::Lazy;
use regex::Regex;
static TIMING_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^[^\n]+ \d+ms\n?").unwrap());
pub fn compress_prettier(raw: &str, exit_code: i32) -> String {
let cleaned = compactor::normalise(raw);
if exit_code == 0 {
let s = TIMING_RE.replace_all(&cleaned, "");
return compactor::collapse_blanks(&s);
}
let kept: Vec<&str> = cleaned
.lines()
.filter(|l| {
let t = l.trim();
!t.is_empty()
&& (t.ends_with(".ts")
|| t.ends_with(".tsx")
|| t.ends_with(".js")
|| t.ends_with(".jsx")
|| t.ends_with(".json")
|| t.ends_with(".css")
|| t.ends_with(".md")
|| t.ends_with(".yml")
|| t.ends_with(".yaml")
|| t.contains("file")
|| t.contains("prettier"))
})
.collect();
if kept.is_empty() {
return compactor::collapse_blanks(&cleaned);
}
kept.join("\n")
}
pub fn compress_biome_format(raw: &str) -> String {
let cleaned = compactor::normalise(raw);
let kept: Vec<&str> = cleaned
.lines()
.filter(|l| {
let t = l.trim();
t.is_empty()
|| t.contains("file")
|| t.contains("error")
|| t.contains("warning")
|| t.contains("Fixed")
|| t.contains("Skipped")
|| t.contains("Checked")
})
.collect();
if kept.is_empty() {
return cleaned;
}
kept.join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn prettier_success_check_passthrough() {
let raw = "All matched files use Prettier code style!\n";
let out = compress_prettier(raw, 0);
assert!(out.contains("Prettier code style"), "{out}");
}
#[test]
fn prettier_failure_keeps_file_paths() {
let raw = "Checking formatting...\nsrc/components/Button.tsx\nsrc/utils/helpers.ts\nCode style issues found in 2 files. Forgot to run Prettier?\n";
let out = compress_prettier(raw, 1);
assert!(out.contains("Button.tsx"), "{out}");
assert!(out.contains("helpers.ts"), "{out}");
}
}