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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! Fix JSON output contract.
use serde::Serialize;
use serde_json::Value;
/// Inputs for building `fallow fix --format json`.
#[derive(Clone, Copy)]
pub struct FixJsonOutputInput<'a> {
/// True when `--dry-run` was passed and no files were modified.
pub dry_run: bool,
/// Per-fix entries as prepared by the fix planner.
pub fixes: &'a [Value],
/// Fixes skipped because file content changed since analysis.
pub skipped_content_changed: usize,
/// Fixes skipped because the file mixes line endings.
pub skipped_mixed_line_endings: usize,
/// Export removals skipped because confidence was below the threshold.
pub skipped_low_confidence_exports: usize,
/// Dependency removals withheld because the finding carried a
/// reachability caveat.
pub skipped_low_confidence_dependencies: usize,
/// Enum- and class-member removals withheld because the finding carried a
/// reachability caveat.
pub skipped_low_confidence_members: usize,
/// Export removals withheld because a re-export that the run keeps still
/// names the export.
pub skipped_kept_re_exports: usize,
/// Export removals withheld because the same `export` keyword also
/// exports a name that the run keeps.
pub skipped_shared_declarations: usize,
}
/// JSON root emitted by `fallow fix --format json`.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "schema", schemars(title = "fallow fix --format json"))]
pub struct FixJsonOutput<'a> {
/// True when `--dry-run` was passed and no files were modified.
pub dry_run: bool,
/// Per-fix entries, applied and skipped alike.
pub fixes: &'a [Value],
/// Number of entries in `fixes` whose `applied` flag is true.
pub total_fixed: usize,
/// User-facing skipped entries; excludes plan-level skip diagnostics,
/// which are reported through the dedicated `skipped_*` counters.
pub skipped: usize,
/// Fixes skipped because file content changed since analysis.
pub skipped_content_changed: usize,
/// Fixes skipped because the file mixes line endings.
pub skipped_mixed_line_endings: usize,
/// Export removals skipped because confidence was below the threshold.
pub skipped_low_confidence_exports: usize,
/// Dependency removals withheld because the finding carried a
/// reachability caveat. Counted separately from the export withholding
/// so the two names stay honest; both are intentional and neither moves
/// the exit code.
pub skipped_low_confidence_dependencies: usize,
/// Enum- and class-member removals withheld because the finding carried a
/// reachability caveat. A third counter for the same reason the second
/// one exists: a member is neither an export nor a dependency, and
/// folding it into either name would make that name lie. Intentional,
/// exit-code-neutral, and `0` on a run that read every file it found.
pub skipped_low_confidence_members: usize,
/// Export removals withheld because a re-export that the run keeps still
/// names the export: the removal would leave that re-export pointing at a
/// declaration that is no longer exported. Each withheld export is a
/// `remove_export` entry with `skip_reason: "kept_re_export"`, so this
/// counts exports, not files. Intentional and exit-code-neutral.
pub skipped_kept_re_exports: usize,
/// Export removals withheld because the same `export` keyword also
/// exports a name that the run keeps, as in `export const { a, b } = x;`
/// with only `b` unused: the removal of the keyword would remove the
/// export of `a` too. Each withheld export is a `remove_export` entry
/// with `skip_reason: "shared_declaration_in_use"`, so this counts
/// exports, not files. Intentional and exit-code-neutral.
pub skipped_shared_declarations: usize,
}
/// Count fix entries whose `applied` flag is true.
#[must_use]
pub fn count_applied_fixes(fixes: &[Value]) -> usize {
fixes
.iter()
.filter(|fix| fix.get("applied").and_then(Value::as_bool).unwrap_or(false))
.count()
}
/// Count user-facing skipped entries, excluding plan-level skip diagnostics.
#[must_use]
pub fn count_reported_fix_skips(fixes: &[Value]) -> usize {
fixes
.iter()
.filter(|fix| {
let is_skipped = fix.get("skipped").and_then(Value::as_bool).unwrap_or(false);
let reason = fix.get("skip_reason").and_then(Value::as_str);
let is_plan_skip = matches!(
reason,
Some(
"content_changed"
| "mixed_line_endings"
| "low_confidence_off_graph"
| "low_confidence_unresolved_imports"
| "low_confidence_incomplete_analysis"
| "kept_re_export"
| "shared_declaration_in_use"
| "generated_source"
)
);
is_skipped && !is_plan_skip
})
.count()
}
/// Build the typed fix JSON root.
#[must_use]
pub fn build_fix_json_output(input: FixJsonOutputInput<'_>) -> FixJsonOutput<'_> {
FixJsonOutput {
dry_run: input.dry_run,
fixes: input.fixes,
total_fixed: count_applied_fixes(input.fixes),
skipped: count_reported_fix_skips(input.fixes),
skipped_content_changed: input.skipped_content_changed,
skipped_mixed_line_endings: input.skipped_mixed_line_endings,
skipped_low_confidence_exports: input.skipped_low_confidence_exports,
skipped_low_confidence_dependencies: input.skipped_low_confidence_dependencies,
skipped_low_confidence_members: input.skipped_low_confidence_members,
skipped_kept_re_exports: input.skipped_kept_re_exports,
skipped_shared_declarations: input.skipped_shared_declarations,
}
}
/// Serialize the typed fix JSON root.
///
/// # Errors
///
/// Returns a serde error when a fix entry cannot be converted to JSON.
pub fn serialize_fix_json_output(
input: FixJsonOutputInput<'_>,
) -> Result<Value, serde_json::Error> {
serde_json::to_value(build_fix_json_output(input))
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn fix_output_counts_applied_and_user_skips() {
let fixes = vec![
json!({"applied": true}),
json!({"applied": false, "skipped": true, "skip_reason": "manual"}),
json!({"skipped": true, "skip_reason": "content_changed"}),
json!({"skipped": true, "skip_reason": "low_confidence_unresolved_imports"}),
json!({
"type": "remove_dependency",
"skipped": true,
"skip_reason": "low_confidence_incomplete_analysis",
}),
json!({
"type": "remove_enum_member",
"skipped": true,
"skip_reason": "low_confidence_incomplete_analysis",
}),
json!({
"type": "remove_export",
"applied": false,
"skipped": true,
"skip_reason": "kept_re_export",
}),
json!({
"type": "remove_export",
"applied": false,
"skipped": true,
"skip_reason": "shared_declaration_in_use",
}),
];
let output = build_fix_json_output(FixJsonOutputInput {
dry_run: true,
fixes: &fixes,
skipped_content_changed: 1,
skipped_mixed_line_endings: 2,
skipped_low_confidence_exports: 3,
skipped_low_confidence_dependencies: 4,
skipped_low_confidence_members: 5,
skipped_kept_re_exports: 6,
skipped_shared_declarations: 7,
});
assert!(output.dry_run);
assert_eq!(output.total_fixed, 1);
assert_eq!(output.skipped, 1);
assert_eq!(output.skipped_content_changed, 1);
assert_eq!(output.skipped_mixed_line_endings, 2);
assert_eq!(output.skipped_low_confidence_exports, 3);
assert_eq!(
output.skipped, 1,
"a withheld dependency belongs to its own counter, not the generic skip count"
);
assert_eq!(output.skipped_low_confidence_dependencies, 4);
assert_eq!(
output.skipped, 1,
"a withheld enum member belongs to its own counter too"
);
assert_eq!(output.skipped_low_confidence_members, 5);
assert_eq!(
output.skipped, 1,
"an export held back by a kept re-export belongs to its own counter"
);
assert_eq!(output.skipped_kept_re_exports, 6);
assert_eq!(
output.skipped, 1,
"an export held back by its shared declaration belongs to its own counter"
);
assert_eq!(output.skipped_shared_declarations, 7);
}
#[test]
fn fix_output_serializes_expected_root_keys() {
let fixes = vec![json!({"type": "unused-export", "applied": true})];
let value = serialize_fix_json_output(FixJsonOutputInput {
dry_run: false,
fixes: &fixes,
skipped_content_changed: 0,
skipped_mixed_line_endings: 0,
skipped_low_confidence_exports: 0,
skipped_low_confidence_dependencies: 0,
skipped_low_confidence_members: 0,
skipped_kept_re_exports: 0,
skipped_shared_declarations: 0,
})
.expect("fix output serializes");
assert_eq!(value["dry_run"], false);
assert_eq!(value["total_fixed"], 1);
assert_eq!(value["skipped"], 0);
assert_eq!(value["skipped_kept_re_exports"], 0);
assert_eq!(value["skipped_shared_declarations"], 0);
assert_eq!(value["fixes"][0]["type"], "unused-export");
}
}