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 if let Some(ref vs) = report.vital_signs {
108 let mut parts = Vec::new();
109 parts.push(format!("avg_cyclomatic={:.1}", vs.avg_cyclomatic));
110 parts.push(format!("p90_cyclomatic={}", vs.p90_cyclomatic));
111 if let Some(v) = vs.dead_file_pct {
112 parts.push(format!("dead_file_pct={v:.1}"));
113 }
114 if let Some(v) = vs.dead_export_pct {
115 parts.push(format!("dead_export_pct={v:.1}"));
116 }
117 if let Some(v) = vs.maintainability_avg {
118 parts.push(format!("maintainability_avg={v:.1}"));
119 }
120 if let Some(v) = vs.hotspot_count {
121 parts.push(format!("hotspot_count={v}"));
122 }
123 if let Some(v) = vs.circular_dep_count {
124 parts.push(format!("circular_dep_count={v}"));
125 }
126 if let Some(v) = vs.unused_dep_count {
127 parts.push(format!("unused_dep_count={v}"));
128 }
129 println!("vital-signs:{}", parts.join(","));
130 }
131 for finding in &report.findings {
132 let relative = normalize_uri(&relative_path(&finding.path, root).display().to_string());
133 println!(
134 "high-complexity:{}:{}:{}:cyclomatic={},cognitive={}",
135 relative, finding.line, finding.name, finding.cyclomatic, finding.cognitive,
136 );
137 }
138 for score in &report.file_scores {
139 let relative = normalize_uri(&relative_path(&score.path, root).display().to_string());
140 println!(
141 "file-score:{}:mi={:.1},fan_in={},fan_out={},dead={:.2},density={:.2}",
142 relative,
143 score.maintainability_index,
144 score.fan_in,
145 score.fan_out,
146 score.dead_code_ratio,
147 score.complexity_density,
148 );
149 }
150 for entry in &report.hotspots {
151 let relative = normalize_uri(&relative_path(&entry.path, root).display().to_string());
152 println!(
153 "hotspot:{}:score={:.1},commits={},churn={},density={:.2},fan_in={},trend={}",
154 relative,
155 entry.score,
156 entry.commits,
157 entry.lines_added + entry.lines_deleted,
158 entry.complexity_density,
159 entry.fan_in,
160 entry.trend,
161 );
162 }
163 for target in &report.targets {
164 let relative = normalize_uri(&relative_path(&target.path, root).display().to_string());
165 let category = target.category.compact_label();
166 let effort = target.effort.label();
167 let confidence = target.confidence.label();
168 println!(
169 "refactoring-target:{}:priority={:.1},efficiency={:.1},category={},effort={},confidence={}:{}",
170 relative,
171 target.priority,
172 target.efficiency,
173 category,
174 effort,
175 confidence,
176 target.recommendation,
177 );
178 }
179}
180
181pub(super) fn print_duplication_compact(report: &DuplicationReport, root: &Path) {
182 for (i, group) in report.clone_groups.iter().enumerate() {
183 for instance in &group.instances {
184 let relative =
185 normalize_uri(&relative_path(&instance.file, root).display().to_string());
186 println!(
187 "clone-group-{}:{}:{}-{}:{}tokens",
188 i + 1,
189 relative,
190 instance.start_line,
191 instance.end_line,
192 group.token_count
193 );
194 }
195 }
196}
197
198#[cfg(test)]
199mod tests {
200 use super::*;
201 use fallow_core::extract::MemberKind;
202 use fallow_core::results::*;
203 use std::path::PathBuf;
204
205 fn sample_results(root: &Path) -> AnalysisResults {
207 let mut r = AnalysisResults::default();
208
209 r.unused_files.push(UnusedFile {
210 path: root.join("src/dead.ts"),
211 });
212 r.unused_exports.push(UnusedExport {
213 path: root.join("src/utils.ts"),
214 export_name: "helperFn".to_string(),
215 is_type_only: false,
216 line: 10,
217 col: 4,
218 span_start: 120,
219 is_re_export: false,
220 });
221 r.unused_types.push(UnusedExport {
222 path: root.join("src/types.ts"),
223 export_name: "OldType".to_string(),
224 is_type_only: true,
225 line: 5,
226 col: 0,
227 span_start: 60,
228 is_re_export: false,
229 });
230 r.unused_dependencies.push(UnusedDependency {
231 package_name: "lodash".to_string(),
232 location: DependencyLocation::Dependencies,
233 path: root.join("package.json"),
234 line: 5,
235 });
236 r.unused_dev_dependencies.push(UnusedDependency {
237 package_name: "jest".to_string(),
238 location: DependencyLocation::DevDependencies,
239 path: root.join("package.json"),
240 line: 5,
241 });
242 r.unused_enum_members.push(UnusedMember {
243 path: root.join("src/enums.ts"),
244 parent_name: "Status".to_string(),
245 member_name: "Deprecated".to_string(),
246 kind: MemberKind::EnumMember,
247 line: 8,
248 col: 2,
249 });
250 r.unused_class_members.push(UnusedMember {
251 path: root.join("src/service.ts"),
252 parent_name: "UserService".to_string(),
253 member_name: "legacyMethod".to_string(),
254 kind: MemberKind::ClassMethod,
255 line: 42,
256 col: 4,
257 });
258 r.unresolved_imports.push(UnresolvedImport {
259 path: root.join("src/app.ts"),
260 specifier: "./missing-module".to_string(),
261 line: 3,
262 col: 0,
263 specifier_col: 0,
264 });
265 r.unlisted_dependencies.push(UnlistedDependency {
266 package_name: "chalk".to_string(),
267 imported_from: vec![ImportSite {
268 path: root.join("src/cli.ts"),
269 line: 2,
270 col: 0,
271 }],
272 });
273 r.duplicate_exports.push(DuplicateExport {
274 export_name: "Config".to_string(),
275 locations: vec![
276 DuplicateLocation {
277 path: root.join("src/config.ts"),
278 line: 15,
279 col: 0,
280 },
281 DuplicateLocation {
282 path: root.join("src/types.ts"),
283 line: 30,
284 col: 0,
285 },
286 ],
287 });
288 r.type_only_dependencies.push(TypeOnlyDependency {
289 package_name: "zod".to_string(),
290 path: root.join("package.json"),
291 line: 8,
292 });
293
294 r
295 }
296
297 #[test]
298 fn compact_empty_results_no_lines() {
299 let root = PathBuf::from("/project");
300 let results = AnalysisResults::default();
301 let lines = build_compact_lines(&results, &root);
302 assert!(lines.is_empty());
303 }
304
305 #[test]
306 fn compact_unused_file_format() {
307 let root = PathBuf::from("/project");
308 let mut results = AnalysisResults::default();
309 results.unused_files.push(UnusedFile {
310 path: root.join("src/dead.ts"),
311 });
312
313 let lines = build_compact_lines(&results, &root);
314 assert_eq!(lines.len(), 1);
315 assert_eq!(lines[0], "unused-file:src/dead.ts");
316 }
317
318 #[test]
319 fn compact_unused_export_format() {
320 let root = PathBuf::from("/project");
321 let mut results = AnalysisResults::default();
322 results.unused_exports.push(UnusedExport {
323 path: root.join("src/utils.ts"),
324 export_name: "helperFn".to_string(),
325 is_type_only: false,
326 line: 10,
327 col: 4,
328 span_start: 120,
329 is_re_export: false,
330 });
331
332 let lines = build_compact_lines(&results, &root);
333 assert_eq!(lines[0], "unused-export:src/utils.ts:10:helperFn");
334 }
335
336 #[test]
337 fn compact_unused_type_format() {
338 let root = PathBuf::from("/project");
339 let mut results = AnalysisResults::default();
340 results.unused_types.push(UnusedExport {
341 path: root.join("src/types.ts"),
342 export_name: "OldType".to_string(),
343 is_type_only: true,
344 line: 5,
345 col: 0,
346 span_start: 60,
347 is_re_export: false,
348 });
349
350 let lines = build_compact_lines(&results, &root);
351 assert_eq!(lines[0], "unused-type:src/types.ts:5:OldType");
352 }
353
354 #[test]
355 fn compact_unused_dep_format() {
356 let root = PathBuf::from("/project");
357 let mut results = AnalysisResults::default();
358 results.unused_dependencies.push(UnusedDependency {
359 package_name: "lodash".to_string(),
360 location: DependencyLocation::Dependencies,
361 path: root.join("package.json"),
362 line: 5,
363 });
364
365 let lines = build_compact_lines(&results, &root);
366 assert_eq!(lines[0], "unused-dep:lodash");
367 }
368
369 #[test]
370 fn compact_unused_devdep_format() {
371 let root = PathBuf::from("/project");
372 let mut results = AnalysisResults::default();
373 results.unused_dev_dependencies.push(UnusedDependency {
374 package_name: "jest".to_string(),
375 location: DependencyLocation::DevDependencies,
376 path: root.join("package.json"),
377 line: 5,
378 });
379
380 let lines = build_compact_lines(&results, &root);
381 assert_eq!(lines[0], "unused-devdep:jest");
382 }
383
384 #[test]
385 fn compact_unused_enum_member_format() {
386 let root = PathBuf::from("/project");
387 let mut results = AnalysisResults::default();
388 results.unused_enum_members.push(UnusedMember {
389 path: root.join("src/enums.ts"),
390 parent_name: "Status".to_string(),
391 member_name: "Deprecated".to_string(),
392 kind: MemberKind::EnumMember,
393 line: 8,
394 col: 2,
395 });
396
397 let lines = build_compact_lines(&results, &root);
398 assert_eq!(
399 lines[0],
400 "unused-enum-member:src/enums.ts:8:Status.Deprecated"
401 );
402 }
403
404 #[test]
405 fn compact_unused_class_member_format() {
406 let root = PathBuf::from("/project");
407 let mut results = AnalysisResults::default();
408 results.unused_class_members.push(UnusedMember {
409 path: root.join("src/service.ts"),
410 parent_name: "UserService".to_string(),
411 member_name: "legacyMethod".to_string(),
412 kind: MemberKind::ClassMethod,
413 line: 42,
414 col: 4,
415 });
416
417 let lines = build_compact_lines(&results, &root);
418 assert_eq!(
419 lines[0],
420 "unused-class-member:src/service.ts:42:UserService.legacyMethod"
421 );
422 }
423
424 #[test]
425 fn compact_unresolved_import_format() {
426 let root = PathBuf::from("/project");
427 let mut results = AnalysisResults::default();
428 results.unresolved_imports.push(UnresolvedImport {
429 path: root.join("src/app.ts"),
430 specifier: "./missing-module".to_string(),
431 line: 3,
432 col: 0,
433 specifier_col: 0,
434 });
435
436 let lines = build_compact_lines(&results, &root);
437 assert_eq!(lines[0], "unresolved-import:src/app.ts:3:./missing-module");
438 }
439
440 #[test]
441 fn compact_unlisted_dep_format() {
442 let root = PathBuf::from("/project");
443 let mut results = AnalysisResults::default();
444 results.unlisted_dependencies.push(UnlistedDependency {
445 package_name: "chalk".to_string(),
446 imported_from: vec![],
447 });
448
449 let lines = build_compact_lines(&results, &root);
450 assert_eq!(lines[0], "unlisted-dep:chalk");
451 }
452
453 #[test]
454 fn compact_duplicate_export_format() {
455 let root = PathBuf::from("/project");
456 let mut results = AnalysisResults::default();
457 results.duplicate_exports.push(DuplicateExport {
458 export_name: "Config".to_string(),
459 locations: vec![
460 DuplicateLocation {
461 path: root.join("src/a.ts"),
462 line: 15,
463 col: 0,
464 },
465 DuplicateLocation {
466 path: root.join("src/b.ts"),
467 line: 30,
468 col: 0,
469 },
470 ],
471 });
472
473 let lines = build_compact_lines(&results, &root);
474 assert_eq!(lines[0], "duplicate-export:Config");
475 }
476
477 #[test]
478 fn compact_all_issue_types_produce_lines() {
479 let root = PathBuf::from("/project");
480 let results = sample_results(&root);
481 let lines = build_compact_lines(&results, &root);
482
483 assert_eq!(lines.len(), 11);
485
486 assert!(lines[0].starts_with("unused-file:"));
488 assert!(lines[1].starts_with("unused-export:"));
489 assert!(lines[2].starts_with("unused-type:"));
490 assert!(lines[3].starts_with("unused-dep:"));
491 assert!(lines[4].starts_with("unused-devdep:"));
492 assert!(lines[5].starts_with("unused-enum-member:"));
493 assert!(lines[6].starts_with("unused-class-member:"));
494 assert!(lines[7].starts_with("unresolved-import:"));
495 assert!(lines[8].starts_with("unlisted-dep:"));
496 assert!(lines[9].starts_with("duplicate-export:"));
497 assert!(lines[10].starts_with("type-only-dep:"));
498 }
499
500 #[test]
501 fn compact_strips_root_prefix_from_paths() {
502 let root = PathBuf::from("/project");
503 let mut results = AnalysisResults::default();
504 results.unused_files.push(UnusedFile {
505 path: PathBuf::from("/project/src/deep/nested/file.ts"),
506 });
507
508 let lines = build_compact_lines(&results, &root);
509 assert_eq!(lines[0], "unused-file:src/deep/nested/file.ts");
510 }
511}