1use std::path::Path;
2
3use fallow_core::duplicates::DuplicationReport;
4use fallow_core::results::{AnalysisResults, UnusedExport, UnusedMember};
5
6use super::{normalize_uri, relative_path};
7
8pub(super) fn print_compact(results: &AnalysisResults, root: &Path) {
9 for line in build_compact_lines(results, root) {
10 println!("{line}");
11 }
12}
13
14pub fn build_compact_lines(results: &AnalysisResults, root: &Path) -> Vec<String> {
17 let rel = |p: &Path| normalize_uri(&relative_path(p, root).display().to_string());
18
19 let compact_export = |export: &UnusedExport, kind: &str, re_kind: &str| -> String {
20 let tag = if export.is_re_export { re_kind } else { kind };
21 format!(
22 "{}:{}:{}:{}",
23 tag,
24 rel(&export.path),
25 export.line,
26 export.export_name
27 )
28 };
29
30 let compact_member = |member: &UnusedMember, kind: &str| -> String {
31 format!(
32 "{}:{}:{}:{}.{}",
33 kind,
34 rel(&member.path),
35 member.line,
36 member.parent_name,
37 member.member_name
38 )
39 };
40
41 let mut lines = Vec::new();
42
43 for file in &results.unused_files {
44 lines.push(format!("unused-file:{}", rel(&file.path)));
45 }
46 for export in &results.unused_exports {
47 lines.push(compact_export(export, "unused-export", "unused-re-export"));
48 }
49 for export in &results.unused_types {
50 lines.push(compact_export(
51 export,
52 "unused-type",
53 "unused-re-export-type",
54 ));
55 }
56 for dep in &results.unused_dependencies {
57 lines.push(format!("unused-dep:{}", dep.package_name));
58 }
59 for dep in &results.unused_dev_dependencies {
60 lines.push(format!("unused-devdep:{}", dep.package_name));
61 }
62 for dep in &results.unused_optional_dependencies {
63 lines.push(format!("unused-optionaldep:{}", dep.package_name));
64 }
65 for member in &results.unused_enum_members {
66 lines.push(compact_member(member, "unused-enum-member"));
67 }
68 for member in &results.unused_class_members {
69 lines.push(compact_member(member, "unused-class-member"));
70 }
71 for import in &results.unresolved_imports {
72 lines.push(format!(
73 "unresolved-import:{}:{}:{}",
74 rel(&import.path),
75 import.line,
76 import.specifier
77 ));
78 }
79 for dep in &results.unlisted_dependencies {
80 lines.push(format!("unlisted-dep:{}", dep.package_name));
81 }
82 for dup in &results.duplicate_exports {
83 lines.push(format!("duplicate-export:{}", dup.export_name));
84 }
85 for dep in &results.type_only_dependencies {
86 lines.push(format!("type-only-dep:{}", dep.package_name));
87 }
88 for cycle in &results.circular_dependencies {
89 let chain: Vec<String> = cycle.files.iter().map(|p| rel(p)).collect();
90 let mut display_chain = chain.clone();
91 if let Some(first) = chain.first() {
92 display_chain.push(first.clone());
93 }
94 let first_file = chain.first().map_or_else(String::new, Clone::clone);
95 lines.push(format!(
96 "circular-dependency:{}:{}:{}",
97 first_file,
98 cycle.line,
99 display_chain.join(" \u{2192} ")
100 ));
101 }
102
103 lines
104}
105
106pub(super) fn print_health_compact(report: &crate::health_types::HealthReport, root: &Path) {
107 for finding in &report.findings {
108 let relative = normalize_uri(&relative_path(&finding.path, root).display().to_string());
109 println!(
110 "high-complexity:{}:{}:{}:cyclomatic={},cognitive={}",
111 relative, finding.line, finding.name, finding.cyclomatic, finding.cognitive,
112 );
113 }
114 for score in &report.file_scores {
115 let relative = normalize_uri(&relative_path(&score.path, root).display().to_string());
116 println!(
117 "file-score:{}:mi={:.1},fan_in={},fan_out={},dead={:.2},density={:.2}",
118 relative,
119 score.maintainability_index,
120 score.fan_in,
121 score.fan_out,
122 score.dead_code_ratio,
123 score.complexity_density,
124 );
125 }
126 for entry in &report.hotspots {
127 let relative = normalize_uri(&relative_path(&entry.path, root).display().to_string());
128 println!(
129 "hotspot:{}:score={:.1},commits={},churn={},density={:.2},fan_in={},trend={}",
130 relative,
131 entry.score,
132 entry.commits,
133 entry.lines_added + entry.lines_deleted,
134 entry.complexity_density,
135 entry.fan_in,
136 entry.trend,
137 );
138 }
139}
140
141pub(super) fn print_duplication_compact(report: &DuplicationReport, root: &Path) {
142 for (i, group) in report.clone_groups.iter().enumerate() {
143 for instance in &group.instances {
144 let relative =
145 normalize_uri(&relative_path(&instance.file, root).display().to_string());
146 println!(
147 "clone-group-{}:{}:{}-{}:{}tokens",
148 i + 1,
149 relative,
150 instance.start_line,
151 instance.end_line,
152 group.token_count
153 );
154 }
155 }
156}
157
158#[cfg(test)]
159mod tests {
160 use super::*;
161 use fallow_core::extract::MemberKind;
162 use fallow_core::results::*;
163 use std::path::PathBuf;
164
165 fn sample_results(root: &Path) -> AnalysisResults {
167 let mut r = AnalysisResults::default();
168
169 r.unused_files.push(UnusedFile {
170 path: root.join("src/dead.ts"),
171 });
172 r.unused_exports.push(UnusedExport {
173 path: root.join("src/utils.ts"),
174 export_name: "helperFn".to_string(),
175 is_type_only: false,
176 line: 10,
177 col: 4,
178 span_start: 120,
179 is_re_export: false,
180 });
181 r.unused_types.push(UnusedExport {
182 path: root.join("src/types.ts"),
183 export_name: "OldType".to_string(),
184 is_type_only: true,
185 line: 5,
186 col: 0,
187 span_start: 60,
188 is_re_export: false,
189 });
190 r.unused_dependencies.push(UnusedDependency {
191 package_name: "lodash".to_string(),
192 location: DependencyLocation::Dependencies,
193 path: root.join("package.json"),
194 line: 5,
195 });
196 r.unused_dev_dependencies.push(UnusedDependency {
197 package_name: "jest".to_string(),
198 location: DependencyLocation::DevDependencies,
199 path: root.join("package.json"),
200 line: 5,
201 });
202 r.unused_enum_members.push(UnusedMember {
203 path: root.join("src/enums.ts"),
204 parent_name: "Status".to_string(),
205 member_name: "Deprecated".to_string(),
206 kind: MemberKind::EnumMember,
207 line: 8,
208 col: 2,
209 });
210 r.unused_class_members.push(UnusedMember {
211 path: root.join("src/service.ts"),
212 parent_name: "UserService".to_string(),
213 member_name: "legacyMethod".to_string(),
214 kind: MemberKind::ClassMethod,
215 line: 42,
216 col: 4,
217 });
218 r.unresolved_imports.push(UnresolvedImport {
219 path: root.join("src/app.ts"),
220 specifier: "./missing-module".to_string(),
221 line: 3,
222 col: 0,
223 specifier_col: 0,
224 });
225 r.unlisted_dependencies.push(UnlistedDependency {
226 package_name: "chalk".to_string(),
227 imported_from: vec![ImportSite {
228 path: root.join("src/cli.ts"),
229 line: 2,
230 col: 0,
231 }],
232 });
233 r.duplicate_exports.push(DuplicateExport {
234 export_name: "Config".to_string(),
235 locations: vec![
236 DuplicateLocation {
237 path: root.join("src/config.ts"),
238 line: 15,
239 col: 0,
240 },
241 DuplicateLocation {
242 path: root.join("src/types.ts"),
243 line: 30,
244 col: 0,
245 },
246 ],
247 });
248 r.type_only_dependencies.push(TypeOnlyDependency {
249 package_name: "zod".to_string(),
250 path: root.join("package.json"),
251 line: 8,
252 });
253
254 r
255 }
256
257 #[test]
258 fn compact_empty_results_no_lines() {
259 let root = PathBuf::from("/project");
260 let results = AnalysisResults::default();
261 let lines = build_compact_lines(&results, &root);
262 assert!(lines.is_empty());
263 }
264
265 #[test]
266 fn compact_unused_file_format() {
267 let root = PathBuf::from("/project");
268 let mut results = AnalysisResults::default();
269 results.unused_files.push(UnusedFile {
270 path: root.join("src/dead.ts"),
271 });
272
273 let lines = build_compact_lines(&results, &root);
274 assert_eq!(lines.len(), 1);
275 assert_eq!(lines[0], "unused-file:src/dead.ts");
276 }
277
278 #[test]
279 fn compact_unused_export_format() {
280 let root = PathBuf::from("/project");
281 let mut results = AnalysisResults::default();
282 results.unused_exports.push(UnusedExport {
283 path: root.join("src/utils.ts"),
284 export_name: "helperFn".to_string(),
285 is_type_only: false,
286 line: 10,
287 col: 4,
288 span_start: 120,
289 is_re_export: false,
290 });
291
292 let lines = build_compact_lines(&results, &root);
293 assert_eq!(lines[0], "unused-export:src/utils.ts:10:helperFn");
294 }
295
296 #[test]
297 fn compact_unused_type_format() {
298 let root = PathBuf::from("/project");
299 let mut results = AnalysisResults::default();
300 results.unused_types.push(UnusedExport {
301 path: root.join("src/types.ts"),
302 export_name: "OldType".to_string(),
303 is_type_only: true,
304 line: 5,
305 col: 0,
306 span_start: 60,
307 is_re_export: false,
308 });
309
310 let lines = build_compact_lines(&results, &root);
311 assert_eq!(lines[0], "unused-type:src/types.ts:5:OldType");
312 }
313
314 #[test]
315 fn compact_unused_dep_format() {
316 let root = PathBuf::from("/project");
317 let mut results = AnalysisResults::default();
318 results.unused_dependencies.push(UnusedDependency {
319 package_name: "lodash".to_string(),
320 location: DependencyLocation::Dependencies,
321 path: root.join("package.json"),
322 line: 5,
323 });
324
325 let lines = build_compact_lines(&results, &root);
326 assert_eq!(lines[0], "unused-dep:lodash");
327 }
328
329 #[test]
330 fn compact_unused_devdep_format() {
331 let root = PathBuf::from("/project");
332 let mut results = AnalysisResults::default();
333 results.unused_dev_dependencies.push(UnusedDependency {
334 package_name: "jest".to_string(),
335 location: DependencyLocation::DevDependencies,
336 path: root.join("package.json"),
337 line: 5,
338 });
339
340 let lines = build_compact_lines(&results, &root);
341 assert_eq!(lines[0], "unused-devdep:jest");
342 }
343
344 #[test]
345 fn compact_unused_enum_member_format() {
346 let root = PathBuf::from("/project");
347 let mut results = AnalysisResults::default();
348 results.unused_enum_members.push(UnusedMember {
349 path: root.join("src/enums.ts"),
350 parent_name: "Status".to_string(),
351 member_name: "Deprecated".to_string(),
352 kind: MemberKind::EnumMember,
353 line: 8,
354 col: 2,
355 });
356
357 let lines = build_compact_lines(&results, &root);
358 assert_eq!(
359 lines[0],
360 "unused-enum-member:src/enums.ts:8:Status.Deprecated"
361 );
362 }
363
364 #[test]
365 fn compact_unused_class_member_format() {
366 let root = PathBuf::from("/project");
367 let mut results = AnalysisResults::default();
368 results.unused_class_members.push(UnusedMember {
369 path: root.join("src/service.ts"),
370 parent_name: "UserService".to_string(),
371 member_name: "legacyMethod".to_string(),
372 kind: MemberKind::ClassMethod,
373 line: 42,
374 col: 4,
375 });
376
377 let lines = build_compact_lines(&results, &root);
378 assert_eq!(
379 lines[0],
380 "unused-class-member:src/service.ts:42:UserService.legacyMethod"
381 );
382 }
383
384 #[test]
385 fn compact_unresolved_import_format() {
386 let root = PathBuf::from("/project");
387 let mut results = AnalysisResults::default();
388 results.unresolved_imports.push(UnresolvedImport {
389 path: root.join("src/app.ts"),
390 specifier: "./missing-module".to_string(),
391 line: 3,
392 col: 0,
393 specifier_col: 0,
394 });
395
396 let lines = build_compact_lines(&results, &root);
397 assert_eq!(lines[0], "unresolved-import:src/app.ts:3:./missing-module");
398 }
399
400 #[test]
401 fn compact_unlisted_dep_format() {
402 let root = PathBuf::from("/project");
403 let mut results = AnalysisResults::default();
404 results.unlisted_dependencies.push(UnlistedDependency {
405 package_name: "chalk".to_string(),
406 imported_from: vec![],
407 });
408
409 let lines = build_compact_lines(&results, &root);
410 assert_eq!(lines[0], "unlisted-dep:chalk");
411 }
412
413 #[test]
414 fn compact_duplicate_export_format() {
415 let root = PathBuf::from("/project");
416 let mut results = AnalysisResults::default();
417 results.duplicate_exports.push(DuplicateExport {
418 export_name: "Config".to_string(),
419 locations: vec![
420 DuplicateLocation {
421 path: root.join("src/a.ts"),
422 line: 15,
423 col: 0,
424 },
425 DuplicateLocation {
426 path: root.join("src/b.ts"),
427 line: 30,
428 col: 0,
429 },
430 ],
431 });
432
433 let lines = build_compact_lines(&results, &root);
434 assert_eq!(lines[0], "duplicate-export:Config");
435 }
436
437 #[test]
438 fn compact_all_issue_types_produce_lines() {
439 let root = PathBuf::from("/project");
440 let results = sample_results(&root);
441 let lines = build_compact_lines(&results, &root);
442
443 assert_eq!(lines.len(), 11);
445
446 assert!(lines[0].starts_with("unused-file:"));
448 assert!(lines[1].starts_with("unused-export:"));
449 assert!(lines[2].starts_with("unused-type:"));
450 assert!(lines[3].starts_with("unused-dep:"));
451 assert!(lines[4].starts_with("unused-devdep:"));
452 assert!(lines[5].starts_with("unused-enum-member:"));
453 assert!(lines[6].starts_with("unused-class-member:"));
454 assert!(lines[7].starts_with("unresolved-import:"));
455 assert!(lines[8].starts_with("unlisted-dep:"));
456 assert!(lines[9].starts_with("duplicate-export:"));
457 assert!(lines[10].starts_with("type-only-dep:"));
458 }
459
460 #[test]
461 fn compact_strips_root_prefix_from_paths() {
462 let root = PathBuf::from("/project");
463 let mut results = AnalysisResults::default();
464 results.unused_files.push(UnusedFile {
465 path: PathBuf::from("/project/src/deep/nested/file.ts"),
466 });
467
468 let lines = build_compact_lines(&results, &root);
469 assert_eq!(lines[0], "unused-file:src/deep/nested/file.ts");
470 }
471}