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