Skip to main content

fallow_core/
results.rs

1// Re-export all result types from fallow-types
2pub use fallow_types::results::{
3    AnalysisResults, CircularDependency, DependencyLocation, DuplicateExport, DuplicateLocation,
4    ExportUsage, ImportSite, ReferenceLocation, TypeOnlyDependency, UnlistedDependency,
5    UnresolvedImport, UnusedDependency, UnusedExport, UnusedFile, UnusedMember,
6};
7
8#[cfg(test)]
9mod tests {
10    use std::path::PathBuf;
11
12    use super::*;
13    use crate::extract::MemberKind;
14
15    #[test]
16    fn empty_results_no_issues() {
17        let results = AnalysisResults::default();
18        assert_eq!(results.total_issues(), 0);
19        assert!(!results.has_issues());
20    }
21
22    #[test]
23    fn results_with_unused_file() {
24        let mut results = AnalysisResults::default();
25        results.unused_files.push(UnusedFile {
26            path: PathBuf::from("test.ts"),
27        });
28        assert_eq!(results.total_issues(), 1);
29        assert!(results.has_issues());
30    }
31
32    #[test]
33    fn results_with_unused_export() {
34        let mut results = AnalysisResults::default();
35        results.unused_exports.push(UnusedExport {
36            path: PathBuf::from("test.ts"),
37            export_name: "foo".to_string(),
38            is_type_only: false,
39            line: 1,
40            col: 0,
41            span_start: 0,
42            is_re_export: false,
43        });
44        assert_eq!(results.total_issues(), 1);
45        assert!(results.has_issues());
46    }
47
48    #[test]
49    fn results_total_counts_all_types() {
50        let mut results = AnalysisResults::default();
51        results.unused_files.push(UnusedFile {
52            path: PathBuf::from("a.ts"),
53        });
54        results.unused_exports.push(UnusedExport {
55            path: PathBuf::from("b.ts"),
56            export_name: "x".to_string(),
57            is_type_only: false,
58            line: 1,
59            col: 0,
60            span_start: 0,
61            is_re_export: false,
62        });
63        results.unused_types.push(UnusedExport {
64            path: PathBuf::from("c.ts"),
65            export_name: "T".to_string(),
66            is_type_only: true,
67            line: 1,
68            col: 0,
69            span_start: 0,
70            is_re_export: false,
71        });
72        results.unused_dependencies.push(UnusedDependency {
73            package_name: "dep".to_string(),
74            location: DependencyLocation::Dependencies,
75            path: PathBuf::from("package.json"),
76            line: 5,
77        });
78        results.unused_dev_dependencies.push(UnusedDependency {
79            package_name: "dev".to_string(),
80            location: DependencyLocation::DevDependencies,
81            path: PathBuf::from("package.json"),
82            line: 5,
83        });
84        results.unused_enum_members.push(UnusedMember {
85            path: PathBuf::from("d.ts"),
86            parent_name: "E".to_string(),
87            member_name: "A".to_string(),
88            kind: MemberKind::EnumMember,
89            line: 1,
90            col: 0,
91        });
92        results.unused_class_members.push(UnusedMember {
93            path: PathBuf::from("e.ts"),
94            parent_name: "C".to_string(),
95            member_name: "m".to_string(),
96            kind: MemberKind::ClassMethod,
97            line: 1,
98            col: 0,
99        });
100        results.unresolved_imports.push(UnresolvedImport {
101            path: PathBuf::from("f.ts"),
102            specifier: "./missing".to_string(),
103            line: 1,
104            col: 0,
105            specifier_col: 0,
106        });
107        results.unlisted_dependencies.push(UnlistedDependency {
108            package_name: "unlisted".to_string(),
109            imported_from: vec![ImportSite {
110                path: PathBuf::from("g.ts"),
111                line: 1,
112                col: 0,
113            }],
114        });
115        results.duplicate_exports.push(DuplicateExport {
116            export_name: "dup".to_string(),
117            locations: vec![
118                DuplicateLocation {
119                    path: PathBuf::from("h.ts"),
120                    line: 15,
121                    col: 0,
122                },
123                DuplicateLocation {
124                    path: PathBuf::from("i.ts"),
125                    line: 30,
126                    col: 0,
127                },
128            ],
129        });
130
131        assert_eq!(results.total_issues(), 10);
132        assert!(results.has_issues());
133    }
134}