1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::path::PathBuf;
use anyhow::{Context, Result};
use caixa_core::LAYOUT_DIR_LIB;
use caixa_fmt::{FmtConfig, format_source};
use caixa_lint::{FixSafety, Severity, apply_fixes, lint_source};
use caixa_theme::Theme;
use clap::Args;
/// Run caixa-lint — Ruby+Rust distilled best practices. Prints Nord-themed
/// diagnostics; exits non-zero if any error-level rule fires.
///
/// `--fix` writes mechanically-safe corrections back to the source. Loops
/// until no more safe fixes apply (so cascading rules converge in one
/// invocation). `--fix-unsafe` additionally applies heuristic fixes that
/// might change behavior.
#[derive(Args)]
pub struct Lint {
/// Paths to lint. Defaults to `./caixa.lisp` + every `.lisp` under `lib/`.
#[arg(value_name = "PATH")]
pub paths: Vec<PathBuf>,
/// Max severity to report (errors-only if true).
#[arg(long)]
pub errors_only: bool,
/// Disable color even on a TTY.
#[arg(long)]
pub no_color: bool,
/// Apply mechanically-safe autofixes back to disk.
#[arg(long)]
pub fix: bool,
/// Also apply heuristic (potentially behavior-changing) autofixes.
/// Implies `--fix`.
#[arg(long)]
pub fix_unsafe: bool,
/// With `--fix`, print the diff/result instead of writing back.
#[arg(long)]
pub fix_dry_run: bool,
}
impl Lint {
pub fn run(mut self) -> Result<()> {
if self.fix_unsafe {
self.fix = true;
}
let targets = self.resolve_targets()?;
let theme = Theme::blackmatter_dark();
let mut error_count = 0usize;
let mut total_fixes = 0usize;
for path in &targets {
let mut src = std::fs::read_to_string(path)
.with_context(|| format!("reading {}", path.display()))?;
// If --fix is on, loop until no more safe fixes apply.
// Each pass re-lints since rules may produce new fixes
// after a previous one rewrote the form.
if self.fix {
let safety = if self.fix_unsafe {
FixSafety::Unsafe
} else {
FixSafety::Safe
};
let mut applied_in_path = 0usize;
loop {
let diags =
lint_source(&src).with_context(|| format!("linting {}", path.display()))?;
let result = apply_fixes(&src, &diags, safety);
if result.applied == 0 {
break;
}
applied_in_path += result.applied;
src = result.source;
}
if applied_in_path > 0 {
// Re-fmt after autofix: edits might shift keywords
// around in ways the previous fmt didn't anticipate.
// Running fmt again converges on the canonical layout.
let cfg = FmtConfig::default();
if let Ok(reformatted) = format_source(&src, &cfg) {
src = reformatted;
}
if self.fix_dry_run {
println!(
"=== {} ({} fix{}) ===",
path.display(),
applied_in_path,
if applied_in_path == 1 { "" } else { "es" }
);
println!("{src}");
} else {
std::fs::write(path, &src)
.with_context(|| format!("writing {}", path.display()))?;
}
}
total_fixes += applied_in_path;
}
// Final lint pass for reporting (any leftover diagnostics
// that weren't autofixable, or all diagnostics if --fix is off).
let mut diags =
lint_source(&src).with_context(|| format!("linting {}", path.display()))?;
if self.errors_only {
diags.retain(|d| d.severity == Severity::Error);
}
for d in &diags {
if d.severity == Severity::Error {
error_count += 1;
}
let rendered = if self.no_color {
let plain = Theme::blackmatter_light();
d.render(&src, &plain)
} else {
d.render(&src, &theme)
};
eprintln!("{}: {rendered}", path.display());
}
}
if self.fix {
eprintln!(
"caixa-lint: {} file(s) checked, {total_fixes} fix(es) applied, {error_count} remaining error(s)",
targets.len()
);
} else {
eprintln!(
"caixa-lint: {} file(s) checked, {error_count} error(s)",
targets.len()
);
}
if error_count > 0 {
std::process::exit(1);
}
Ok(())
}
fn resolve_targets(&self) -> Result<Vec<PathBuf>> {
if !self.paths.is_empty() {
return Ok(self.paths.clone());
}
let mut out = Vec::new();
let root = PathBuf::from(".");
let manifest = root.join("caixa.lisp");
if manifest.exists() {
out.push(manifest);
}
if let Ok(dir) = std::fs::read_dir(root.join(LAYOUT_DIR_LIB)) {
for entry in dir.flatten() {
let p = entry.path();
if p.extension().is_some_and(|e| e == "lisp") {
out.push(p);
}
}
}
Ok(out)
}
}