Skip to main content

fallow_types/
results_dependency_findings.rs

1//! Unused, unlisted and type-only dependencies, unused members, unresolved
2//! imports, and duplicate exports.
3
4use super::*;
5
6/// A dependency that is listed in package.json but never imported.
7#[derive(Debug, Clone, Serialize, Deserialize)]
8#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
9pub struct UnusedDependency {
10    /// Package name, including internal workspace package names.
11    pub package_name: String,
12    /// Whether this is in `dependencies`, `devDependencies`, or `optionalDependencies`.
13    pub location: DependencyLocation,
14    /// Path to the package.json where this dependency is listed.
15    /// For root deps this is `<root>/package.json`, for workspace deps it is `<ws>/package.json`.
16    #[serde(serialize_with = "serde_path::serialize")]
17    pub path: PathBuf,
18    /// 1-based line number of the dependency entry in package.json.
19    pub line: u32,
20    /// Workspace roots that import this package even though the declaring workspace does not.
21    #[serde(
22        default,
23        serialize_with = "serde_path::serialize_vec",
24        skip_serializing_if = "Vec::is_empty"
25    )]
26    #[cfg_attr(feature = "schema", schemars(default))]
27    pub used_in_workspaces: Vec<PathBuf>,
28    /// Workspace roots whose package.json declares this package for the files
29    /// that import it. Only a root finding fills this field: these imports use
30    /// the nearer workspace declaration, so the root declaration stays unused.
31    #[serde(
32        default,
33        serialize_with = "serde_path::serialize_vec",
34        skip_serializing_if = "Vec::is_empty"
35    )]
36    #[cfg_attr(feature = "schema", schemars(default))]
37    pub declared_and_imported_in: Vec<PathBuf>,
38}
39
40/// Where in package.json a dependency is listed.
41///
42/// # Examples
43///
44/// ```
45/// use fallow_types::results::DependencyLocation;
46///
47/// // All three variants are constructible
48/// let loc = DependencyLocation::Dependencies;
49/// let dev = DependencyLocation::DevDependencies;
50/// let opt = DependencyLocation::OptionalDependencies;
51/// // Debug output includes the variant name
52/// assert!(format!("{loc:?}").contains("Dependencies"));
53/// assert!(format!("{dev:?}").contains("DevDependencies"));
54/// assert!(format!("{opt:?}").contains("OptionalDependencies"));
55/// ```
56#[derive(Debug, Clone, Serialize, Deserialize)]
57#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
58#[serde(rename_all = "camelCase")]
59pub enum DependencyLocation {
60    /// Listed in `dependencies`.
61    Dependencies,
62    /// Listed in `devDependencies`.
63    DevDependencies,
64    /// Listed in `optionalDependencies`.
65    OptionalDependencies,
66}
67
68/// An unused enum, class, store, or namespace member.
69#[derive(Debug, Clone, Serialize, Deserialize)]
70#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
71pub struct UnusedMember {
72    /// File containing the unused member.
73    #[serde(serialize_with = "serde_path::serialize")]
74    pub path: PathBuf,
75    /// Name of the parent enum, class, store, or exported namespace.
76    pub parent_name: String,
77    /// Name of the unused member. A member of a nested namespace has the dotted
78    /// path from the exported namespace (`Inner.x`).
79    pub member_name: String,
80    /// The kind of member: enum member, class method, class property, store
81    /// member, or namespace member.
82    pub kind: MemberKind,
83    /// 1-based line number.
84    pub line: u32,
85    /// 0-based byte column offset.
86    pub col: u32,
87}
88
89/// An import that could not be resolved.
90#[derive(Debug, Clone, Serialize, Deserialize)]
91#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
92pub struct UnresolvedImport {
93    /// File containing the unresolved import.
94    #[serde(serialize_with = "serde_path::serialize")]
95    pub path: PathBuf,
96    /// The import specifier that could not be resolved.
97    pub specifier: String,
98    /// 1-based line number.
99    pub line: u32,
100    /// 0-based byte column offset of the import statement.
101    pub col: u32,
102    /// 0-based byte column offset of the source string literal (the specifier in quotes).
103    /// Used by the LSP to underline just the specifier, not the entire import line.
104    pub specifier_col: u32,
105}
106
107/// A dependency used in code but not listed in package.json.
108#[derive(Debug, Clone, Serialize, Deserialize)]
109#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
110pub struct UnlistedDependency {
111    /// Package name, including internal workspace package names, that is
112    /// imported but not listed in package.json.
113    pub package_name: String,
114    /// Import sites where this unlisted dependency is used (file path, line, column).
115    pub imported_from: Vec<ImportSite>,
116}
117
118/// A binary that a package.json script or a process call runs, but that no
119/// installed or listed package provides.
120#[derive(Debug, Clone, Serialize, Deserialize)]
121#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
122pub struct UnlistedBinary {
123    /// The binary name as the command runs it (for example `ffmpeg`).
124    pub binary_name: String,
125    /// The sites that run this binary.
126    pub used_in: Vec<BinaryUseSite>,
127}
128
129/// A location that runs a binary.
130#[derive(Debug, Clone, Serialize, Deserialize)]
131#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
132pub struct BinaryUseSite {
133    /// The `package.json` that declares the script, or the source file that
134    /// makes the process call.
135    #[serde(serialize_with = "serde_path::serialize")]
136    pub path: PathBuf,
137    /// 1-based line number.
138    pub line: u32,
139    /// 0-based byte column offset.
140    pub col: u32,
141    /// The name of the package.json script that runs the binary. Absent for
142    /// a process call in source.
143    #[serde(default, skip_serializing_if = "Option::is_none")]
144    pub script_name: Option<String>,
145}
146
147/// A location where an import occurs.
148#[derive(Debug, Clone, Serialize, Deserialize)]
149#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
150pub struct ImportSite {
151    /// File containing the import.
152    #[serde(serialize_with = "serde_path::serialize")]
153    pub path: PathBuf,
154    /// 1-based line number.
155    pub line: u32,
156    /// 0-based byte column offset.
157    pub col: u32,
158}
159
160/// An export that appears multiple times across the project.
161#[derive(Debug, Clone, Serialize, Deserialize)]
162#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
163pub struct DuplicateExport {
164    /// The duplicated export name.
165    pub export_name: String,
166    /// Locations where this export name appears.
167    pub locations: Vec<DuplicateLocation>,
168}
169
170/// A location where a duplicate export appears.
171#[derive(Debug, Clone, Serialize, Deserialize)]
172#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
173pub struct DuplicateLocation {
174    /// File containing the duplicate export.
175    #[serde(serialize_with = "serde_path::serialize")]
176    pub path: PathBuf,
177    /// 1-based line number.
178    pub line: u32,
179    /// 0-based byte column offset.
180    pub col: u32,
181}
182
183/// One module that exports one binding under two or more names, for example
184/// `export const a = 1; export default a;` or `export { a, a as b }`. The
185/// finding is about the export surface of one file, not about usage, so an
186/// entry file can have it too. Value and type-only exports of one name are
187/// separate bindings. Two bindings with the same initializer are not one
188/// binding.
189#[derive(Debug, Clone, Serialize, Deserialize)]
190#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
191pub struct DuplicateExportAlias {
192    /// The module that exports the binding.
193    #[serde(serialize_with = "serde_path::serialize")]
194    pub path: PathBuf,
195    /// The local name of the binding. For a re-export, the name that the
196    /// source module exports.
197    pub local_name: String,
198    /// The export names of the binding, in source order.
199    pub export_names: Vec<String>,
200    /// 1-based line number of the first export of the binding.
201    pub line: u32,
202    /// 0-based byte column offset of the first export of the binding.
203    pub col: u32,
204    /// One location for each export name, in source order.
205    pub locations: Vec<DuplicateExportAliasLocation>,
206}
207
208/// One export name of a [`DuplicateExportAlias`].
209#[derive(Debug, Clone, Serialize, Deserialize)]
210#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
211pub struct DuplicateExportAliasLocation {
212    /// The export name at this location (`default` for a default export).
213    pub export_name: String,
214    /// 1-based line number.
215    pub line: u32,
216    /// 0-based byte column offset.
217    pub col: u32,
218}
219
220/// A production dependency that is only used via type-only imports.
221/// In production builds, type imports are erased, so this dependency
222/// is not needed at runtime and could be moved to devDependencies.
223#[derive(Debug, Clone, Serialize, Deserialize)]
224#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
225pub struct TypeOnlyDependency {
226    /// Production dependency that is only used via type-only imports.
227    pub package_name: String,
228    /// Path to the package.json where the dependency is listed.
229    #[serde(serialize_with = "serde_path::serialize")]
230    pub path: PathBuf,
231    /// 1-based line number of the dependency entry in package.json.
232    pub line: u32,
233}