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