fallow_types/results.rs
1//! Analysis result types for all issue categories.
2
3use std::path::PathBuf;
4
5use serde::{Deserialize, Serialize};
6
7use crate::extract::{
8 MemberKind, SecurityControlKind, SecurityUrlShape, SkippedSecurityCalleeExpressionKind,
9 SkippedSecurityCalleeReason,
10};
11use crate::output::{
12 FixAction, FixActionType, IssueAction, SuppressLineAction, SuppressLineKind, SuppressLineScope,
13};
14use crate::output_dead_code::{
15 BoundaryCallViolationFinding, BoundaryCoverageViolationFinding, BoundaryViolationFinding,
16 CircularDependencyFinding, DuplicateExportFinding, DuplicatePropShapeFinding,
17 DynamicSegmentNameConflictFinding, EmptyCatalogGroupFinding, InvalidClientExportFinding,
18 MisconfiguredDependencyOverrideFinding, MisplacedDirectiveFinding,
19 MixedClientServerBarrelFinding, PolicyViolationFinding, PrivateTypeLeakFinding,
20 PropDrillingChainFinding, ReExportCycleFinding, RouteCollisionFinding,
21 TestOnlyDependencyFinding, ThinWrapperFinding, TypeOnlyDependencyFinding,
22 UnlistedDependencyFinding, UnprovidedInjectFinding, UnrenderedComponentFinding,
23 UnresolvedCatalogReferenceFinding, UnresolvedImportFinding, UnusedCatalogEntryFinding,
24 UnusedClassMemberFinding, UnusedComponentEmitFinding, UnusedComponentInputFinding,
25 UnusedComponentOutputFinding, UnusedComponentPropFinding, UnusedDependencyFinding,
26 UnusedDependencyOverrideFinding, UnusedDevDependencyFinding, UnusedEnumMemberFinding,
27 UnusedExportFinding, UnusedFileFinding, UnusedLoadDataKeyFinding,
28 UnusedOptionalDependencyFinding, UnusedServerActionFinding, UnusedStoreMemberFinding,
29 UnusedSvelteEventFinding, UnusedTypeFinding,
30};
31use crate::serde_path;
32use crate::suppress::{IssueKind, closest_known_kind_name};
33
34/// Summary of detected entry points, grouped by discovery source.
35///
36/// Used to surface entry-point detection status in human and JSON output,
37/// so library authors can verify that fallow found the right entry points.
38#[derive(Debug, Clone, Default)]
39#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
40pub struct EntryPointSummary {
41 /// Total number of entry points detected.
42 pub total: usize,
43 /// Breakdown by source category (e.g., "package.json" -> 3, "plugin" -> 12).
44 /// Sorted by key for deterministic output.
45 pub by_source: Vec<(String, usize)>,
46}
47
48/// Per-component render fan-in counts plus the precomputed concentration
49/// aggregates.
50///
51/// DESCRIPTIVE blast-radius signal (NOT a rule, finding, or threshold): the
52/// component-graph analogue of module-level fan-in. Module fan-in counts
53/// importing MODULES; render fan-in counts JSX render CALL SITES (a shared
54/// `<Button>` is rendered in far more places than it is imported).
55///
56/// `per_component` is the internal carrier (keyed for hotspot path annotation),
57/// `#[serde(skip)]` on [`AnalysisResults`] so it never appears under bare
58/// `fallow` / `audit`; the aggregates feed the descriptive `VitalSigns` block
59/// (`p95_render_fan_in` / `render_fan_in_high_pct` / `max_render_fan_in`).
60///
61/// UNDERCOUNT is the documented safe direction: a child rendered via a JSX
62/// spread, a dynamic / `createElement(var)` form, or a member-expression tag
63/// (`<Lib.Button/>`) is not resolved by the shared `ChildResolver` and so
64/// increments no component's fan-in. A true high-fan-in component can only be
65/// undersold, never falsely flagged. A rare name-collision over-credit is
66/// possible via the default-import sole-component fallback (inherited verbatim
67/// from the prop-drilling / thin-wrapper resolver); low-harm for a descriptive,
68/// non-gating metric.
69#[derive(Debug, Clone, Default)]
70pub struct RenderFanInMetric {
71 /// Per-component render-site + distinct-parent counts. Keyed by
72 /// `(component file path, component name)` so the hotspot surface can map a
73 /// file back to its top component's fan-in. Components rendered nowhere ARE
74 /// included as a real `0` so the percentile distribution is not skewed.
75 pub per_component: Vec<RenderFanInComponent>,
76 /// 95th-percentile DISTINCT-PARENTS render fan-in across components (the
77 /// per-component distribution analogue of the module-fan-in p95). `None` on
78 /// an empty population. Mirrors `compute_coupling_concentration`.
79 pub p95_distinct_parents: Option<u32>,
80 /// Percentage of components whose distinct-parents render fan-in exceeds the
81 /// `max(p95, 10)` threshold (the same floor coupling concentration uses).
82 /// `None` on an empty population.
83 pub high_pct: Option<f64>,
84 /// The single highest DISTINCT-PARENTS count across all components (the
85 /// headline blast-radius number: the most distinct render LOCATIONS any one
86 /// component is rendered from, the honest edit-ripple count). `None` on an
87 /// empty population. `render_sites` (incl. repeats) is secondary per-component
88 /// context, never the headline.
89 pub max_distinct_parents: Option<u32>,
90}
91
92/// One component's render fan-in detail: how many JSX render SITES target it and
93/// how many DISTINCT parent components render it.
94#[derive(Debug, Clone)]
95pub struct RenderFanInComponent {
96 /// Absolute path of the file declaring the component.
97 pub file: PathBuf,
98 /// The component name.
99 pub component: String,
100 /// Total JSX render SITES that resolve to this component across the project
101 /// (each capitalized / member JSX tag is one site). SECONDARY context ("incl.
102 /// repeats"): a single parent rendering one child five times is five sites but
103 /// one distinct parent, so render_sites overcounts blast radius.
104 pub render_sites: u32,
105 /// Distinct `(parent_file, parent_component)` keys that render this
106 /// component. The HEADLINE blast-radius axis: the honest count of distinct
107 /// render LOCATIONS, the percentiled distribution analogue of "distinct
108 /// importers".
109 pub distinct_parents: u32,
110}
111
112/// Per-kind hook counts for a React component, summarized from `hook_uses`.
113/// DESCRIPTIVE editor context (the LSP code-lens hook breakdown), never a
114/// finding, severity, or `total_issues` input. `custom` collects every
115/// `use*`-named call that is not one of the four built-ins.
116#[derive(Debug, Clone, Default, PartialEq, Eq)]
117pub struct ReactHookSummary {
118 /// `useState(...)` call count.
119 pub state: u16,
120 /// `useEffect(...)` call count.
121 pub effect: u16,
122 /// `useMemo(...)` call count.
123 pub memo: u16,
124 /// `useCallback(...)` call count.
125 pub callback: u16,
126 /// Count of any other `use*`-named call (a custom hook).
127 pub custom: u16,
128}
129
130/// A prop-drilling trace for a prop at the ROOT of a forwarding chain.
131/// DESCRIPTIVE ambient editor context (the LSP per-prop hover): the prop is
132/// forwarded unchanged through `depth` components before a component
133/// substantively consumes it. Reuses the `prop-drilling` chain machinery's
134/// abstain ladder (spread / `cloneElement` / dynamic / provider-in-subtree drop
135/// the whole chain), so the trace is honest. NOT a finding (the opt-in
136/// `prop-drilling` rule owns the finding); this rides the `#[serde(skip)]`
137/// `ReactComponentIntel` carrier.
138#[derive(Debug, Clone, PartialEq, Eq)]
139pub struct ReactPropDrill {
140 /// The chain depth = number of components the prop is forwarded THROUGH
141 /// (source + intermediates + consumer), matching `PropDrillingChain.depth`.
142 pub depth: u32,
143 /// The ordered component names from source to consumer (`hops[0]` owns the
144 /// prop, the last consumes it).
145 pub hops: Vec<String>,
146}
147
148/// Per-prop usage intelligence for one React component prop. DESCRIPTIVE editor
149/// context (the LSP per-prop hover): whether the prop is read in the component
150/// body and how many render sites pass it. NOT a finding (the
151/// `unused-component-prop` React arm owns the deadness rule); this is ambient
152/// signal. `anchor_line` / `anchor_col` follow the same convention the React
153/// `unused-component-prop` findings use (1-based line, byte-derived col from
154/// `byte_offset_to_line_col`).
155#[derive(Debug, Clone, PartialEq, Eq)]
156pub struct ReactPropIntel {
157 /// The declared prop name.
158 pub name: String,
159 /// 1-based line of the prop declaration (anchors the hover).
160 pub anchor_line: u32,
161 /// Column of the prop declaration (byte-derived, matching the React
162 /// `unused-component-prop` finding convention).
163 pub anchor_col: u32,
164 /// Whether the prop is referenced in the component body (`used_in_script`
165 /// for the React arm: a resolved reference to the destructured local).
166 pub used_in_body: bool,
167 /// Count of render sites (test/spec/story/fixture files excluded) whose
168 /// passed-attribute set contains this prop name.
169 pub passed_from_sites: u32,
170 /// A prop-drilling trace, present only when this prop is the ROOT of a
171 /// forwarding chain that reaches a consumer through `>= N` pass-through
172 /// components. `None` for an ordinary prop. Test/spec/story/fixture source
173 /// components never carry a drill trace.
174 pub drill: Option<ReactPropDrill>,
175}
176
177/// Per-component render + prop + hook intelligence for one React component.
178/// DESCRIPTIVE ambient editor context surfaced by the LSP (a component summary
179/// code lens plus per-prop hovers), NOT a finding, IssueKind, severity, or
180/// `total_issues` input. Carried in-process on the `#[serde(skip)]`
181/// `AnalysisResults::react_component_intel` field (like
182/// [`RenderFanInMetric`]); never serialized, so bare `fallow` / `audit` and the
183/// JSON / schema surface are untouched.
184///
185/// Counts are HONEST: test/spec/story/fixture render sites are excluded from
186/// `render_sites`, `distinct_parents`, and per-prop `passed_from_sites`, and
187/// `distinct_parents` (not the repeat-inflated `render_sites`) is the headline,
188/// mirroring the render-fan-in metric's discipline.
189#[derive(Debug, Clone, PartialEq, Eq)]
190pub struct ReactComponentIntel {
191 /// Absolute path of the file declaring the component.
192 pub path: PathBuf,
193 /// The component name.
194 pub component_name: String,
195 /// 1-based line of the component definition (anchors the code lens).
196 pub anchor_line: u32,
197 /// Column of the component definition (byte-derived).
198 pub anchor_col: u32,
199 /// Total JSX render SITES that resolve to this component (each capitalized /
200 /// member JSX tag is one site). SECONDARY context: a single parent rendering
201 /// the child five times is five sites but one distinct parent.
202 pub render_sites: u32,
203 /// Distinct `(parent_file, parent_component)` keys rendering this component.
204 /// The HEADLINE blast-radius count (never the repeat-inflated site count).
205 pub distinct_parents: u32,
206 /// Number of declared props on this component.
207 pub prop_count: u16,
208 /// Per-kind hook counts.
209 pub hooks: ReactHookSummary,
210 /// Per-prop usage intelligence (one entry per declared prop).
211 pub props: Vec<ReactPropIntel>,
212}
213
214/// Complete analysis results.
215///
216/// # Examples
217///
218/// ```
219/// use fallow_types::output_dead_code::UnusedFileFinding;
220/// use fallow_types::results::{AnalysisResults, UnusedFile};
221/// use std::path::PathBuf;
222///
223/// let mut results = AnalysisResults::default();
224/// assert_eq!(results.total_issues(), 0);
225/// assert!(!results.has_issues());
226///
227/// results
228/// .unused_files
229/// .push(UnusedFileFinding::with_actions(UnusedFile {
230/// path: PathBuf::from("src/dead.ts"),
231/// }));
232/// assert_eq!(results.total_issues(), 1);
233/// assert!(results.has_issues());
234/// ```
235#[derive(Debug, Default, Clone, Serialize)]
236#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
237pub struct AnalysisResults {
238 /// Files not reachable from any entry point. Wrapped in
239 /// [`UnusedFileFinding`] so each entry carries a typed `actions` array
240 /// natively, replacing the pre-2.76 post-pass injection.
241 pub unused_files: Vec<UnusedFileFinding>,
242 /// Exports never imported by other modules. Wrapped in
243 /// [`UnusedExportFinding`] so each entry carries a typed `actions`
244 /// array natively.
245 pub unused_exports: Vec<UnusedExportFinding>,
246 /// Type exports never imported by other modules. Wrapped in
247 /// [`UnusedTypeFinding`]: the inner [`UnusedExport`] struct is shared
248 /// with `unused_exports` but the wrapper emits a type-targeted fix
249 /// description.
250 pub unused_types: Vec<UnusedTypeFinding>,
251 /// Exported symbols whose public signature references same-file private
252 /// types. Wrapped in [`PrivateTypeLeakFinding`] so each entry carries a
253 /// typed `actions` array natively.
254 pub private_type_leaks: Vec<PrivateTypeLeakFinding>,
255 /// Dependencies listed in package.json but never imported. Wrapped in
256 /// [`UnusedDependencyFinding`] so each entry carries a typed `actions`
257 /// array natively. The fix action swaps from `remove-dependency` to
258 /// `move-dependency` when `used_in_workspaces` is non-empty.
259 pub unused_dependencies: Vec<UnusedDependencyFinding>,
260 /// Dev dependencies listed in package.json but never imported. Wrapped
261 /// in [`UnusedDevDependencyFinding`]: same bare struct as
262 /// `unused_dependencies` with a `devDependencies`-targeted fix
263 /// description.
264 pub unused_dev_dependencies: Vec<UnusedDevDependencyFinding>,
265 /// Optional dependencies listed in package.json but never imported.
266 /// Wrapped in [`UnusedOptionalDependencyFinding`] with an
267 /// `optionalDependencies`-targeted fix description.
268 pub unused_optional_dependencies: Vec<UnusedOptionalDependencyFinding>,
269 /// Enum members never accessed. Wrapped in
270 /// [`UnusedEnumMemberFinding`] so each entry carries a typed `actions`
271 /// array natively.
272 pub unused_enum_members: Vec<UnusedEnumMemberFinding>,
273 /// Class members never accessed. Wrapped in
274 /// [`UnusedClassMemberFinding`]: same inner [`UnusedMember`] struct as
275 /// `unused_enum_members`, with a class-targeted fix description and the
276 /// `auto_fixable: false` default to reflect dependency-injection
277 /// patterns.
278 pub unused_class_members: Vec<UnusedClassMemberFinding>,
279 /// Store members (Pinia `state` / `getters` / `actions` key, or a
280 /// setup-store returned key) declared but never accessed by any consumer
281 /// project-wide. Wrapped in [`UnusedStoreMemberFinding`]: same inner
282 /// [`UnusedMember`] struct as `unused_class_members`, with a
283 /// store-targeted fix description. Cross-graph: the store binding is
284 /// imported (the module is reachable) yet a specific member is dead.
285 #[serde(default, skip_serializing_if = "Vec::is_empty")]
286 pub unused_store_members: Vec<UnusedStoreMemberFinding>,
287 /// Import specifiers that could not be resolved. Wrapped in
288 /// [`UnresolvedImportFinding`] so each entry carries a typed `actions`
289 /// array natively.
290 pub unresolved_imports: Vec<UnresolvedImportFinding>,
291 /// Dependencies used in code but not listed in package.json. Wrapped in
292 /// [`UnlistedDependencyFinding`].
293 pub unlisted_dependencies: Vec<UnlistedDependencyFinding>,
294 /// Exports with the same name across multiple modules. Wrapped in
295 /// [`DuplicateExportFinding`] so each entry carries a typed `actions`
296 /// array natively, with the position-0 `add-to-config` `ignoreExports`
297 /// snippet wired in at wrapper construction.
298 pub duplicate_exports: Vec<DuplicateExportFinding>,
299 /// Production dependencies only used via type-only imports (could be
300 /// devDependencies). Only populated in production mode. Wrapped in
301 /// [`TypeOnlyDependencyFinding`].
302 pub type_only_dependencies: Vec<TypeOnlyDependencyFinding>,
303 /// Production dependencies only imported by test files (could be
304 /// devDependencies). Wrapped in [`TestOnlyDependencyFinding`].
305 #[serde(default)]
306 pub test_only_dependencies: Vec<TestOnlyDependencyFinding>,
307 /// Circular dependency chains detected in the module graph. Wrapped in
308 /// [`CircularDependencyFinding`] so each entry carries a typed `actions`
309 /// array natively.
310 pub circular_dependencies: Vec<CircularDependencyFinding>,
311 /// Cycles or self-loops in the re-export edge subgraph (barrel files
312 /// re-exporting from each other in a loop). Wrapped in
313 /// [`ReExportCycleFinding`] so each entry carries a typed `actions`
314 /// array natively (a `refactor-re-export-cycle` informational primary
315 /// plus a `suppress-file` secondary; cycles are file-scoped so a single
316 /// suppression breaks the cycle).
317 #[serde(default)]
318 pub re_export_cycles: Vec<ReExportCycleFinding>,
319 /// Imports that cross architecture boundary rules. Wrapped in
320 /// [`BoundaryViolationFinding`] so each entry carries a typed `actions`
321 /// array natively.
322 #[serde(default)]
323 pub boundary_violations: Vec<BoundaryViolationFinding>,
324 /// Files that matched no architecture boundary zone while
325 /// `boundaries.coverage.requireAllFiles` was enabled.
326 #[serde(default)]
327 pub boundary_coverage_violations: Vec<BoundaryCoverageViolationFinding>,
328 /// Calls from zoned files to callees forbidden for that zone via
329 /// `boundaries.calls.forbidden`. Wrapped in
330 /// [`BoundaryCallViolationFinding`] so each entry carries a typed
331 /// `actions` array natively.
332 #[serde(default)]
333 pub boundary_call_violations: Vec<BoundaryCallViolationFinding>,
334 /// Banned calls, imports, and catalogue-derived effects matched by
335 /// declarative rule packs
336 /// (`rulePacks` config). Wrapped in [`PolicyViolationFinding`] so each
337 /// entry carries a typed `actions` array natively. Each finding carries
338 /// its effective per-rule severity.
339 #[serde(default)]
340 pub policy_violations: Vec<PolicyViolationFinding>,
341 /// Suppression comments or JSDoc tags that no longer match any issue.
342 #[serde(default)]
343 pub stale_suppressions: Vec<StaleSuppression>,
344 /// Entries in package manager catalog sections not referenced by any
345 /// workspace package via the catalog: protocol. Supports
346 /// `pnpm-workspace.yaml` catalogs and Bun root `package.json` catalogs.
347 /// Wrapped in [`UnusedCatalogEntryFinding`] so each entry carries a typed
348 /// `actions` array natively, with per-instance `auto_fixable` derived
349 /// from `hardcoded_consumers` and the catalog source file.
350 #[serde(default)]
351 pub unused_catalog_entries: Vec<UnusedCatalogEntryFinding>,
352 /// Named groups under package manager catalogs sections that declare no
353 /// package entries. The top-level catalog: map is not reported. Wrapped in
354 /// [`EmptyCatalogGroupFinding`].
355 #[serde(default)]
356 pub empty_catalog_groups: Vec<EmptyCatalogGroupFinding>,
357 /// Workspace package.json references to catalogs (`catalog:` or
358 /// `catalog:<name>`) that do not declare the consumed package. The package
359 /// manager install will error until the named catalog grows to include the
360 /// package or the reference is switched / removed. Wrapped in
361 /// [`UnresolvedCatalogReferenceFinding`] with the discriminated
362 /// `add-catalog-entry` / `update-catalog-reference` primary at position 0.
363 #[serde(default)]
364 pub unresolved_catalog_references: Vec<UnresolvedCatalogReferenceFinding>,
365 /// Entries in pnpm-workspace.yaml's overrides: section, or package.json's
366 /// pnpm.overrides block, whose target package is not declared by any
367 /// workspace package and is not present in pnpm-lock.yaml. Default severity
368 /// is warn because projects without a readable lockfile fall back to
369 /// manifest-only checks; the hint field flags those conservative cases.
370 /// Wrapped in [`UnusedDependencyOverrideFinding`].
371 #[serde(default)]
372 pub unused_dependency_overrides: Vec<UnusedDependencyOverrideFinding>,
373 /// pnpm.overrides entries whose key or value does not parse as a valid
374 /// override spec (empty key, empty value, malformed selector, unbalanced
375 /// parent matcher). pnpm install will reject these. Default severity is
376 /// error. Wrapped in [`MisconfiguredDependencyOverrideFinding`].
377 #[serde(default)]
378 pub misconfigured_dependency_overrides: Vec<MisconfiguredDependencyOverrideFinding>,
379 /// `"use client"` files that export a Next.js server-only / route-segment
380 /// config name (e.g. `metadata`, `revalidate`, `GET`). Next.js rejects this
381 /// at build time. Wrapped in [`InvalidClientExportFinding`] so each entry
382 /// carries a typed `actions` array natively. Default severity is `warn`.
383 #[serde(default)]
384 pub invalid_client_exports: Vec<InvalidClientExportFinding>,
385 /// Barrel files that re-export BOTH a `"use client"` origin module AND a
386 /// server-only origin module (the Next.js App Router footgun). Wrapped in
387 /// [`MixedClientServerBarrelFinding`] so each entry carries a typed
388 /// `actions` array natively. Default severity is `warn`.
389 #[serde(default)]
390 pub mixed_client_server_barrels: Vec<MixedClientServerBarrelFinding>,
391 /// `"use client"` / `"use server"` directives written as expression
392 /// statements after a non-directive statement, so the RSC bundler parses
393 /// them as ordinary strings and silently ignores them. Wrapped in
394 /// [`MisplacedDirectiveFinding`] so each entry carries a typed `actions`
395 /// array natively. Default severity is `warn`.
396 #[serde(default)]
397 pub misplaced_directives: Vec<MisplacedDirectiveFinding>,
398 /// Vue `inject(KEY)` / Svelte `getContext(KEY)` calls whose symbol KEY is
399 /// provided nowhere in the project (the injected-never-provided dead-half).
400 /// Wrapped in [`UnprovidedInjectFinding`] so each entry carries a typed
401 /// `actions` array natively. Default severity is `warn`.
402 #[serde(default, skip_serializing_if = "Vec::is_empty")]
403 pub unprovided_injects: Vec<UnprovidedInjectFinding>,
404 /// Vue/Svelte single-file components that are reachable but rendered nowhere
405 /// (the imported-but-never-rendered dead-half). Wrapped in
406 /// [`UnrenderedComponentFinding`] so each entry carries a typed `actions`
407 /// array natively. Default severity is `warn`.
408 #[serde(default, skip_serializing_if = "Vec::is_empty")]
409 pub unrendered_components: Vec<UnrenderedComponentFinding>,
410 /// Next.js App Router route files that resolve to the same URL within one
411 /// app-root (a guaranteed `next build` failure). Wrapped in
412 /// [`RouteCollisionFinding`] so each entry carries a typed `actions` array
413 /// natively. One finding per colliding file. Default severity is `warn`.
414 #[serde(default)]
415 pub route_collisions: Vec<RouteCollisionFinding>,
416 /// Sibling Next.js dynamic route segments at one tree position using
417 /// different param spellings (a dev / runtime error; `next build` does NOT
418 /// catch it). Wrapped in [`DynamicSegmentNameConflictFinding`] so each entry
419 /// carries a typed `actions` array natively. Default severity is `warn`.
420 #[serde(default)]
421 pub dynamic_segment_name_conflicts: Vec<DynamicSegmentNameConflictFinding>,
422 /// Vue `<script setup>` `defineProps`, Svelte 5 `$props()`, and React props
423 /// referenced nowhere in their own component. Wrapped in
424 /// [`UnusedComponentPropFinding`] so each entry carries a typed `actions`
425 /// array natively. Default severity is `warn`.
426 #[serde(default, skip_serializing_if = "Vec::is_empty")]
427 pub unused_component_props: Vec<UnusedComponentPropFinding>,
428 /// Vue `<script setup>` `defineEmits` events emitted nowhere in their own SFC
429 /// (no `emit('<name>')` call). Wrapped in [`UnusedComponentEmitFinding`] so
430 /// each entry carries a typed `actions` array natively. Default severity is
431 /// `warn`.
432 #[serde(default, skip_serializing_if = "Vec::is_empty")]
433 pub unused_component_emits: Vec<UnusedComponentEmitFinding>,
434 /// Angular `@Input()` / signal `input()` / `model()` inputs read nowhere in
435 /// their own component (neither the template nor the class body). Wrapped in
436 /// [`UnusedComponentInputFinding`] so each entry carries a typed `actions`
437 /// array natively. Default severity is `warn`.
438 #[serde(default, skip_serializing_if = "Vec::is_empty")]
439 pub unused_component_inputs: Vec<UnusedComponentInputFinding>,
440 /// Angular `@Output()` / signal `output()` outputs emitted nowhere in their
441 /// own component (no `this.<output>.emit(...)`). Wrapped in
442 /// [`UnusedComponentOutputFinding`] so each entry carries a typed `actions`
443 /// array natively. Default severity is `warn`.
444 #[serde(default, skip_serializing_if = "Vec::is_empty")]
445 pub unused_component_outputs: Vec<UnusedComponentOutputFinding>,
446 /// Svelte components dispatching a custom event via `createEventDispatcher()`
447 /// whose event name is listened to nowhere project-wide (cross-file
448 /// dead-output direction). Wrapped in [`UnusedSvelteEventFinding`] so each
449 /// entry carries a typed `actions` array natively. Default severity is
450 /// `warn`.
451 #[serde(default, skip_serializing_if = "Vec::is_empty")]
452 pub unused_svelte_events: Vec<UnusedSvelteEventFinding>,
453 /// Next.js Server Actions (exports of `"use server"` files) that no code in
454 /// the project references. Reclassified out of `unused_exports` for
455 /// `"use server"` files. Wrapped in [`UnusedServerActionFinding`] so each
456 /// entry carries a typed `actions` array natively. Default severity is
457 /// `warn`.
458 #[serde(default, skip_serializing_if = "Vec::is_empty")]
459 pub unused_server_actions: Vec<UnusedServerActionFinding>,
460 /// SvelteKit `+page.{ts,server.ts,js,server.js}` `load()` return-object keys
461 /// read by no consumer. Wrapped in [`UnusedLoadDataKeyFinding`] so each entry
462 /// carries a typed `actions` array natively. Default severity is `warn`.
463 #[serde(default, skip_serializing_if = "Vec::is_empty")]
464 pub unused_load_data_keys: Vec<UnusedLoadDataKeyFinding>,
465 /// `true` when the `unused-load-data-key` detector abstained project-wide
466 /// because a whole-object use of `page.data` / `$page.data` was seen
467 /// somewhere (S1 observability: an empty `unused_load_data_keys` with this
468 /// flag set is NOT a clean bill, it means the rule could not run safely).
469 /// Serialized only when `true` so the default JSON contract is unchanged.
470 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
471 pub unused_load_data_keys_global_abstain: bool,
472 /// React/Preact props forwarded unchanged through `>= N` intermediate
473 /// pass-through components until a consumer (located per-chain records).
474 /// Wrapped in [`PropDrillingChainFinding`] so each entry carries a typed
475 /// `actions` array natively. Health signal: the rule defaults to `off`
476 /// (opt-in), so this is dormant and populated ONLY when the user enables it.
477 #[serde(default, skip_serializing_if = "Vec::is_empty")]
478 pub prop_drilling_chains: Vec<PropDrillingChainFinding>,
479 /// React/Preact components whose entire body is a single spread-forwarded
480 /// child render (`return <Child {...props}/>`): pure structural indirection,
481 /// a candidate for inlining at call sites. Wrapped in [`ThinWrapperFinding`]
482 /// so each entry carries a typed `actions` array natively. Health signal: the
483 /// rule defaults to `off` (opt-in), so this is dormant and populated ONLY
484 /// when the user enables it.
485 #[serde(default, skip_serializing_if = "Vec::is_empty")]
486 pub thin_wrappers: Vec<ThinWrapperFinding>,
487 /// React/Preact components that participate in a duplicate-prop-shape group:
488 /// three or more components across two or more files whose statically-known
489 /// prop NAME set is identical after stripping ubiquitous DOM / passthrough
490 /// names (a missing shared `Props` type / base component). Wrapped in
491 /// [`DuplicatePropShapeFinding`] so each entry carries a typed `actions`
492 /// array and its sibling roster natively. Health signal: the rule defaults to
493 /// `off` (opt-in), so this is dormant and populated ONLY when the user
494 /// enables it.
495 #[serde(default, skip_serializing_if = "Vec::is_empty")]
496 pub duplicate_prop_shapes: Vec<DuplicatePropShapeFinding>,
497 /// Number of suppression entries that matched an issue during analysis.
498 /// Human output uses this for the suppression footer; it is skipped in
499 /// machine output to avoid changing the public JSON issue contract.
500 #[serde(skip)]
501 pub suppression_count: usize,
502 /// Number of component props exempted from `unused-component-props` this run
503 /// because their local destructure binding name matched
504 /// `unusedComponentProps.ignorePattern`. Drives a human-output note so a
505 /// typo'd pattern (matching nothing) is not a silent no-op; skipped in
506 /// machine output, like [`Self::suppression_count`].
507 #[serde(skip)]
508 pub unused_component_props_exempted: usize,
509 /// Suppression comments present in analyzed files this run (every present
510 /// marker, all kinds, not only consumed ones). Internal: read in-process by
511 /// `fallow impact` to distinguish a genuinely resolved finding from one
512 /// silenced by a `fallow-ignore`. Skipped during serialization, like
513 /// [`Self::suppression_count`], so the public JSON output contract is
514 /// unchanged.
515 #[serde(skip)]
516 pub active_suppressions: Vec<ActiveSuppression>,
517 /// Detected feature flag patterns. Advisory output, not included in issue counts.
518 /// Skipped during default serialization: injected separately in JSON output when enabled.
519 #[serde(skip)]
520 pub feature_flags: Vec<FeatureFlag>,
521 /// Local security candidates (e.g. `client-server-leak`). CANDIDATES for
522 /// downstream agent verification, NOT verified vulnerabilities. Off by
523 /// default; populated only when the corresponding `security_*` rule is
524 /// enabled (forced on by `fallow security`). Excluded from `total_issues`
525 /// and skipped during serialization so they never surface under bare
526 /// `fallow` or the `audit` gate; the `fallow security` command reads this
527 /// field and emits its own envelope. Mirrors [`Self::feature_flags`].
528 #[serde(skip)]
529 pub security_findings: Vec<SecurityFinding>,
530 /// In-band blind-spot count: number of `"use client"` files whose transitive
531 /// import cone contains a dynamic `import()` the reachability BFS cannot
532 /// follow. Surfaced by `fallow security` so a leak hidden behind an
533 /// unresolved edge is never silently reported as "clean". Skipped during
534 /// serialization like [`Self::security_findings`].
535 #[serde(skip)]
536 pub security_unresolved_edge_files: usize,
537 /// In-band blind-spot count: number of sink-shaped nodes the catalogue
538 /// detector could not flatten to a static callee path (dynamic dispatch,
539 /// computed members, aliased bindings). Surfaced by `fallow security` so an
540 /// empty catalogue result with a non-zero count is not reported as "clean".
541 /// Skipped during serialization like [`Self::security_findings`].
542 #[serde(skip)]
543 pub security_unresolved_callee_sites: usize,
544 /// Location samples for sink-shaped nodes the catalogue detector could not
545 /// flatten to a static callee path. Skipped during default serialization;
546 /// `fallow security` summarizes this metadata in its own envelope.
547 #[serde(skip)]
548 pub security_unresolved_callee_diagnostics: Vec<SecurityUnresolvedCalleeDiagnostic>,
549 /// Usage counts for all exports across the project. Used by the LSP for Code Lens.
550 /// Not included in issue counts -- this is metadata, not an issue type.
551 /// Skipped during serialization: this is internal LSP data, not part of the JSON output schema.
552 #[serde(skip)]
553 pub export_usages: Vec<ExportUsage>,
554 /// Summary of detected entry points, grouped by discovery source.
555 /// Not included in issue counts -- this is informational metadata.
556 /// Skipped during serialization: rendered separately in JSON output.
557 #[serde(skip)]
558 pub entry_point_summary: Option<EntryPointSummary>,
559 /// Per-component render fan-in (JSX render SITES + distinct parents) plus the
560 /// precomputed concentration aggregates. DESCRIPTIVE blast-radius signal, not
561 /// an issue type: the component-graph analogue of module fan-in. `None` on
562 /// non-React projects (the dep gate fails and `render_edges` is empty).
563 /// Skipped during serialization (internal carrier, like
564 /// [`Self::export_usages`]); the public surface is the `VitalSigns`
565 /// aggregate, so bare `fallow` / `audit` never serialize it. See
566 /// [`RenderFanInMetric`].
567 #[serde(skip)]
568 pub render_fan_in: Option<RenderFanInMetric>,
569 /// Per-component React render/prop/hook intelligence. DESCRIPTIVE ambient
570 /// editor context (LSP code lens + per-prop hover), NOT an issue type: it is
571 /// never in `total_issues`. Empty on non-React projects (the dep gate fails
572 /// and `component_functions` is empty). Skipped during serialization
573 /// (in-process LSP carrier, like [`Self::render_fan_in`]); bare `fallow` /
574 /// `audit` never serialize it and the JSON / schema surface is unchanged.
575 /// See [`ReactComponentIntel`].
576 #[serde(skip)]
577 pub react_component_intel: Vec<ReactComponentIntel>,
578}
579
580struct AnalysisResultsCoreMergeParts {
581 unused_files: Vec<UnusedFileFinding>,
582 unused_exports: Vec<UnusedExportFinding>,
583 unused_types: Vec<UnusedTypeFinding>,
584 private_type_leaks: Vec<PrivateTypeLeakFinding>,
585 unused_enum_members: Vec<UnusedEnumMemberFinding>,
586 unused_class_members: Vec<UnusedClassMemberFinding>,
587 unused_store_members: Vec<UnusedStoreMemberFinding>,
588 unresolved_imports: Vec<UnresolvedImportFinding>,
589 boundary_violations: Vec<BoundaryViolationFinding>,
590 boundary_coverage_violations: Vec<BoundaryCoverageViolationFinding>,
591 boundary_call_violations: Vec<BoundaryCallViolationFinding>,
592 policy_violations: Vec<PolicyViolationFinding>,
593 stale_suppressions: Vec<StaleSuppression>,
594}
595
596struct AnalysisResultsGraphMergeParts {
597 unused_dependencies: Vec<UnusedDependencyFinding>,
598 unused_dev_dependencies: Vec<UnusedDevDependencyFinding>,
599 unused_optional_dependencies: Vec<UnusedOptionalDependencyFinding>,
600 unlisted_dependencies: Vec<UnlistedDependencyFinding>,
601 duplicate_exports: Vec<DuplicateExportFinding>,
602 type_only_dependencies: Vec<TypeOnlyDependencyFinding>,
603 test_only_dependencies: Vec<TestOnlyDependencyFinding>,
604 circular_dependencies: Vec<CircularDependencyFinding>,
605 re_export_cycles: Vec<ReExportCycleFinding>,
606}
607
608struct AnalysisResultsWorkspaceMergeParts {
609 unused_catalog_entries: Vec<UnusedCatalogEntryFinding>,
610 empty_catalog_groups: Vec<EmptyCatalogGroupFinding>,
611 unresolved_catalog_references: Vec<UnresolvedCatalogReferenceFinding>,
612 unused_dependency_overrides: Vec<UnusedDependencyOverrideFinding>,
613 misconfigured_dependency_overrides: Vec<MisconfiguredDependencyOverrideFinding>,
614}
615
616struct AnalysisResultsFrameworkMergeParts {
617 invalid_client_exports: Vec<InvalidClientExportFinding>,
618 mixed_client_server_barrels: Vec<MixedClientServerBarrelFinding>,
619 misplaced_directives: Vec<MisplacedDirectiveFinding>,
620 unprovided_injects: Vec<UnprovidedInjectFinding>,
621 unrendered_components: Vec<UnrenderedComponentFinding>,
622 route_collisions: Vec<RouteCollisionFinding>,
623 dynamic_segment_name_conflicts: Vec<DynamicSegmentNameConflictFinding>,
624 unused_component_props: Vec<UnusedComponentPropFinding>,
625 unused_component_emits: Vec<UnusedComponentEmitFinding>,
626 unused_component_inputs: Vec<UnusedComponentInputFinding>,
627 unused_component_outputs: Vec<UnusedComponentOutputFinding>,
628 unused_svelte_events: Vec<UnusedSvelteEventFinding>,
629 unused_server_actions: Vec<UnusedServerActionFinding>,
630 unused_load_data_keys: Vec<UnusedLoadDataKeyFinding>,
631 unused_load_data_keys_global_abstain: bool,
632 prop_drilling_chains: Vec<PropDrillingChainFinding>,
633 thin_wrappers: Vec<ThinWrapperFinding>,
634 duplicate_prop_shapes: Vec<DuplicatePropShapeFinding>,
635}
636
637struct AnalysisResultsMetadataMergeParts {
638 suppression_count: usize,
639 unused_component_props_exempted: usize,
640 active_suppressions: Vec<ActiveSuppression>,
641 feature_flags: Vec<FeatureFlag>,
642 security_findings: Vec<SecurityFinding>,
643 security_unresolved_edge_files: usize,
644 security_unresolved_callee_sites: usize,
645 security_unresolved_callee_diagnostics: Vec<SecurityUnresolvedCalleeDiagnostic>,
646 export_usages: Vec<ExportUsage>,
647 entry_point_summary: Option<EntryPointSummary>,
648 render_fan_in: Option<RenderFanInMetric>,
649 react_component_intel: Vec<ReactComponentIntel>,
650}
651
652/// Exhaustively destructure `other` into the five grouped merge-part structs.
653///
654/// The single exhaustive `let Self { .. }` lives here so that adding a field to
655/// [`AnalysisResults`] becomes a compile error (a field must be routed into one
656/// of the part structs) instead of being silently dropped during a merge. See
657/// issue #444.
658#[expect(
659 clippy::too_many_lines,
660 reason = "irreducible single exhaustive field-routing: the one `let Self { .. }` destructure must name every field so a newly added field is a compile error (issue #444); splitting it would defeat that exhaustiveness guarantee"
661)]
662fn split_merge_parts(
663 other: AnalysisResults,
664) -> (
665 AnalysisResultsCoreMergeParts,
666 AnalysisResultsGraphMergeParts,
667 AnalysisResultsWorkspaceMergeParts,
668 AnalysisResultsFrameworkMergeParts,
669 AnalysisResultsMetadataMergeParts,
670) {
671 let AnalysisResults {
672 unused_files,
673 unused_exports,
674 unused_types,
675 private_type_leaks,
676 unused_dependencies,
677 unused_dev_dependencies,
678 unused_optional_dependencies,
679 unused_enum_members,
680 unused_class_members,
681 unused_store_members,
682 unresolved_imports,
683 unlisted_dependencies,
684 duplicate_exports,
685 type_only_dependencies,
686 test_only_dependencies,
687 circular_dependencies,
688 re_export_cycles,
689 boundary_violations,
690 boundary_coverage_violations,
691 boundary_call_violations,
692 policy_violations,
693 stale_suppressions,
694 unused_catalog_entries,
695 empty_catalog_groups,
696 unresolved_catalog_references,
697 unused_dependency_overrides,
698 misconfigured_dependency_overrides,
699 invalid_client_exports,
700 mixed_client_server_barrels,
701 misplaced_directives,
702 unprovided_injects,
703 unrendered_components,
704 route_collisions,
705 dynamic_segment_name_conflicts,
706 unused_component_props,
707 unused_component_emits,
708 unused_component_inputs,
709 unused_component_outputs,
710 unused_svelte_events,
711 unused_server_actions,
712 unused_load_data_keys,
713 unused_load_data_keys_global_abstain,
714 prop_drilling_chains,
715 thin_wrappers,
716 duplicate_prop_shapes,
717 suppression_count,
718 unused_component_props_exempted,
719 active_suppressions,
720 feature_flags,
721 security_findings,
722 security_unresolved_edge_files,
723 security_unresolved_callee_sites,
724 security_unresolved_callee_diagnostics,
725 export_usages,
726 entry_point_summary,
727 render_fan_in,
728 react_component_intel,
729 } = other;
730
731 (
732 AnalysisResultsCoreMergeParts {
733 unused_files,
734 unused_exports,
735 unused_types,
736 private_type_leaks,
737 unused_enum_members,
738 unused_class_members,
739 unused_store_members,
740 unresolved_imports,
741 boundary_violations,
742 boundary_coverage_violations,
743 boundary_call_violations,
744 policy_violations,
745 stale_suppressions,
746 },
747 AnalysisResultsGraphMergeParts {
748 unused_dependencies,
749 unused_dev_dependencies,
750 unused_optional_dependencies,
751 unlisted_dependencies,
752 duplicate_exports,
753 type_only_dependencies,
754 test_only_dependencies,
755 circular_dependencies,
756 re_export_cycles,
757 },
758 AnalysisResultsWorkspaceMergeParts {
759 unused_catalog_entries,
760 empty_catalog_groups,
761 unresolved_catalog_references,
762 unused_dependency_overrides,
763 misconfigured_dependency_overrides,
764 },
765 AnalysisResultsFrameworkMergeParts {
766 invalid_client_exports,
767 mixed_client_server_barrels,
768 misplaced_directives,
769 unprovided_injects,
770 unrendered_components,
771 route_collisions,
772 dynamic_segment_name_conflicts,
773 unused_component_props,
774 unused_component_emits,
775 unused_component_inputs,
776 unused_component_outputs,
777 unused_svelte_events,
778 unused_server_actions,
779 unused_load_data_keys,
780 unused_load_data_keys_global_abstain,
781 prop_drilling_chains,
782 thin_wrappers,
783 duplicate_prop_shapes,
784 },
785 AnalysisResultsMetadataMergeParts {
786 suppression_count,
787 unused_component_props_exempted,
788 active_suppressions,
789 feature_flags,
790 security_findings,
791 security_unresolved_edge_files,
792 security_unresolved_callee_sites,
793 security_unresolved_callee_diagnostics,
794 export_usages,
795 entry_point_summary,
796 render_fan_in,
797 react_component_intel,
798 },
799 )
800}
801
802macro_rules! counted_analysis_result_fields {
803 ($callback:ident $(, $arg:expr)? ) => {
804 $callback! {
805 $($arg,)?
806 unused_files => "unused_files",
807 unused_exports => "unused_exports",
808 unused_types => "unused_types",
809 private_type_leaks => "private_type_leaks",
810 unused_dependencies => "unused_dependencies",
811 unused_dev_dependencies => "unused_dev_dependencies",
812 unused_optional_dependencies => "unused_optional_dependencies",
813 unused_enum_members => "unused_enum_members",
814 unused_class_members => "unused_class_members",
815 unused_store_members => "unused_store_members",
816 unresolved_imports => "unresolved_imports",
817 unlisted_dependencies => "unlisted_dependencies",
818 duplicate_exports => "duplicate_exports",
819 type_only_dependencies => "type_only_dependencies",
820 test_only_dependencies => "test_only_dependencies",
821 circular_dependencies => "circular_dependencies",
822 re_export_cycles => "re_export_cycles",
823 boundary_violations => "boundary_violations",
824 boundary_coverage_violations => "boundary_coverage_violations",
825 boundary_call_violations => "boundary_call_violations",
826 policy_violations => "policy_violations",
827 stale_suppressions => "stale_suppressions",
828 unused_catalog_entries => "unused_catalog_entries",
829 empty_catalog_groups => "empty_catalog_groups",
830 unresolved_catalog_references => "unresolved_catalog_references",
831 unused_dependency_overrides => "unused_dependency_overrides",
832 misconfigured_dependency_overrides => "misconfigured_dependency_overrides",
833 invalid_client_exports => "invalid_client_exports",
834 mixed_client_server_barrels => "mixed_client_server_barrels",
835 misplaced_directives => "misplaced_directives",
836 unprovided_injects => "unprovided_injects",
837 unrendered_components => "unrendered_components",
838 route_collisions => "route_collisions",
839 dynamic_segment_name_conflicts => "dynamic_segment_name_conflicts",
840 unused_component_props => "unused_component_props",
841 unused_component_emits => "unused_component_emits",
842 unused_component_inputs => "unused_component_inputs",
843 unused_component_outputs => "unused_component_outputs",
844 unused_svelte_events => "unused_svelte_events",
845 unused_server_actions => "unused_server_actions",
846 unused_load_data_keys => "unused_load_data_keys",
847 }
848 };
849}
850
851macro_rules! counted_result_key_slice {
852 ($($field:ident => $key:literal,)+) => {
853 &[$($key),+]
854 };
855}
856
857macro_rules! counted_result_field_sum {
858 ($results:expr, $($field:ident => $key:literal,)+) => {
859 0 $(+ ($results).$field.len())+
860 };
861}
862
863/// Serialized `AnalysisResults` arrays that contribute to [`AnalysisResults::total_issues`].
864pub const TOTAL_ISSUE_RESULT_KEYS: &[&str] =
865 counted_analysis_result_fields!(counted_result_key_slice);
866
867impl AnalysisResults {
868 /// Total number of issues found.
869 ///
870 /// Sums across all issue categories (unused files, exports, types,
871 /// dependencies, members, unresolved imports, unlisted deps, duplicates,
872 /// type-only deps, circular deps, and boundary violations).
873 ///
874 /// # Examples
875 ///
876 /// ```
877 /// use fallow_types::output_dead_code::{UnresolvedImportFinding, UnusedFileFinding};
878 /// use fallow_types::results::{AnalysisResults, UnresolvedImport, UnusedFile};
879 /// use std::path::PathBuf;
880 ///
881 /// let mut results = AnalysisResults::default();
882 /// results
883 /// .unused_files
884 /// .push(UnusedFileFinding::with_actions(UnusedFile {
885 /// path: PathBuf::from("a.ts"),
886 /// }));
887 /// results
888 /// .unresolved_imports
889 /// .push(UnresolvedImportFinding::with_actions(UnresolvedImport {
890 /// path: PathBuf::from("b.ts"),
891 /// specifier: "./missing".to_string(),
892 /// line: 1,
893 /// col: 0,
894 /// specifier_col: 0,
895 /// }));
896 /// assert_eq!(results.total_issues(), 2);
897 /// ```
898 #[must_use]
899 pub const fn total_issues(&self) -> usize {
900 counted_analysis_result_fields!(counted_result_field_sum, self)
901 }
902
903 /// Whether any issues were found.
904 #[must_use]
905 pub const fn has_issues(&self) -> bool {
906 self.total_issues() > 0
907 }
908
909 /// Merge `other` into `self`, taking the union of every field.
910 ///
911 /// This is the single canonical way to combine two [`AnalysisResults`]
912 /// (the LSP merges per-project-root results through it). The method
913 /// exhaustively destructures `Self`, so adding a field to the struct
914 /// becomes a compile error here instead of a silently-dropped field. See
915 /// issue #444.
916 ///
917 /// Every `Vec` field is appended (callers dedup downstream where needed,
918 /// e.g. the LSP's identity-keyed `dedup_results`). `suppression_count`
919 /// sums; `entry_point_summary` keeps `self`'s value when present and
920 /// otherwise adopts `other`'s.
921 pub fn merge_into(&mut self, other: Self) {
922 let (core, graph, workspace, framework, metadata) = split_merge_parts(other);
923 self.merge_core_findings(core);
924 self.merge_dependency_and_graph_findings(graph);
925 self.merge_workspace_findings(workspace);
926 self.merge_framework_findings(framework);
927 self.merge_metadata_and_security(metadata);
928 }
929
930 fn merge_core_findings(&mut self, parts: AnalysisResultsCoreMergeParts) {
931 self.unused_files.extend(parts.unused_files);
932 self.unused_exports.extend(parts.unused_exports);
933 self.unused_types.extend(parts.unused_types);
934 self.private_type_leaks.extend(parts.private_type_leaks);
935 self.unused_enum_members.extend(parts.unused_enum_members);
936 self.unused_class_members.extend(parts.unused_class_members);
937 self.unused_store_members.extend(parts.unused_store_members);
938 self.unresolved_imports.extend(parts.unresolved_imports);
939 self.boundary_violations.extend(parts.boundary_violations);
940 self.boundary_coverage_violations
941 .extend(parts.boundary_coverage_violations);
942 self.boundary_call_violations
943 .extend(parts.boundary_call_violations);
944 self.policy_violations.extend(parts.policy_violations);
945 self.stale_suppressions.extend(parts.stale_suppressions);
946 }
947
948 fn merge_dependency_and_graph_findings(&mut self, parts: AnalysisResultsGraphMergeParts) {
949 self.unused_dependencies.extend(parts.unused_dependencies);
950 self.unused_dev_dependencies
951 .extend(parts.unused_dev_dependencies);
952 self.unused_optional_dependencies
953 .extend(parts.unused_optional_dependencies);
954 self.unlisted_dependencies
955 .extend(parts.unlisted_dependencies);
956 self.duplicate_exports.extend(parts.duplicate_exports);
957 self.type_only_dependencies
958 .extend(parts.type_only_dependencies);
959 self.test_only_dependencies
960 .extend(parts.test_only_dependencies);
961 self.circular_dependencies
962 .extend(parts.circular_dependencies);
963 self.re_export_cycles.extend(parts.re_export_cycles);
964 }
965
966 fn merge_workspace_findings(&mut self, parts: AnalysisResultsWorkspaceMergeParts) {
967 self.unused_catalog_entries
968 .extend(parts.unused_catalog_entries);
969 self.empty_catalog_groups.extend(parts.empty_catalog_groups);
970 self.unresolved_catalog_references
971 .extend(parts.unresolved_catalog_references);
972 self.unused_dependency_overrides
973 .extend(parts.unused_dependency_overrides);
974 self.misconfigured_dependency_overrides
975 .extend(parts.misconfigured_dependency_overrides);
976 }
977
978 fn merge_framework_findings(&mut self, parts: AnalysisResultsFrameworkMergeParts) {
979 self.invalid_client_exports
980 .extend(parts.invalid_client_exports);
981 self.mixed_client_server_barrels
982 .extend(parts.mixed_client_server_barrels);
983 self.misplaced_directives.extend(parts.misplaced_directives);
984 self.unprovided_injects.extend(parts.unprovided_injects);
985 self.unrendered_components
986 .extend(parts.unrendered_components);
987 self.route_collisions.extend(parts.route_collisions);
988 self.dynamic_segment_name_conflicts
989 .extend(parts.dynamic_segment_name_conflicts);
990 self.unused_component_props
991 .extend(parts.unused_component_props);
992 self.unused_component_emits
993 .extend(parts.unused_component_emits);
994 self.unused_component_inputs
995 .extend(parts.unused_component_inputs);
996 self.unused_component_outputs
997 .extend(parts.unused_component_outputs);
998 self.unused_svelte_events.extend(parts.unused_svelte_events);
999 self.unused_server_actions
1000 .extend(parts.unused_server_actions);
1001 self.unused_load_data_keys
1002 .extend(parts.unused_load_data_keys);
1003 self.unused_load_data_keys_global_abstain |= parts.unused_load_data_keys_global_abstain;
1004 self.prop_drilling_chains.extend(parts.prop_drilling_chains);
1005 self.thin_wrappers.extend(parts.thin_wrappers);
1006 self.duplicate_prop_shapes
1007 .extend(parts.duplicate_prop_shapes);
1008 }
1009
1010 fn merge_metadata_and_security(&mut self, parts: AnalysisResultsMetadataMergeParts) {
1011 self.feature_flags.extend(parts.feature_flags);
1012 self.security_findings.extend(parts.security_findings);
1013 self.security_unresolved_edge_files += parts.security_unresolved_edge_files;
1014 self.security_unresolved_callee_sites += parts.security_unresolved_callee_sites;
1015 self.security_unresolved_callee_diagnostics
1016 .extend(parts.security_unresolved_callee_diagnostics);
1017 self.export_usages.extend(parts.export_usages);
1018 self.active_suppressions.extend(parts.active_suppressions);
1019 self.suppression_count += parts.suppression_count;
1020 self.unused_component_props_exempted += parts.unused_component_props_exempted;
1021 if self.entry_point_summary.is_none() {
1022 self.entry_point_summary = parts.entry_point_summary;
1023 }
1024 if self.render_fan_in.is_none() {
1025 self.render_fan_in = parts.render_fan_in;
1026 }
1027 self.react_component_intel
1028 .extend(parts.react_component_intel);
1029 }
1030
1031 /// Sort all result arrays for deterministic output ordering.
1032 ///
1033 /// Parallel collection (rayon, `FxHashMap` iteration) does not guarantee
1034 /// insertion order, so the same project can produce different orderings
1035 /// across runs. This method canonicalises every result list by sorting on
1036 /// (path, line, col, name) so that JSON/SARIF/human output is stable.
1037 pub fn sort(&mut self) {
1038 self.sort_core_findings();
1039 self.sort_dependency_findings();
1040 self.sort_graph_findings();
1041 self.sort_catalog_findings();
1042 self.sort_metadata_findings();
1043 self.sort_export_usages();
1044 }
1045
1046 fn sort_core_findings(&mut self) {
1047 self.sort_core_declaration_findings();
1048 self.sort_core_member_findings();
1049 self.sort_core_framework_findings();
1050 self.sort_core_route_and_load_findings();
1051 }
1052
1053 fn sort_core_declaration_findings(&mut self) {
1054 self.unused_files
1055 .sort_by(|a, b| a.file.path.cmp(&b.file.path));
1056
1057 self.unused_exports.sort_by(|a, b| {
1058 a.export
1059 .path
1060 .cmp(&b.export.path)
1061 .then(a.export.line.cmp(&b.export.line))
1062 .then(a.export.export_name.cmp(&b.export.export_name))
1063 });
1064
1065 self.unused_types.sort_by(|a, b| {
1066 a.export
1067 .path
1068 .cmp(&b.export.path)
1069 .then(a.export.line.cmp(&b.export.line))
1070 .then(a.export.export_name.cmp(&b.export.export_name))
1071 });
1072
1073 self.private_type_leaks.sort_by(|a, b| {
1074 a.leak
1075 .path
1076 .cmp(&b.leak.path)
1077 .then(a.leak.line.cmp(&b.leak.line))
1078 .then(a.leak.export_name.cmp(&b.leak.export_name))
1079 .then(a.leak.type_name.cmp(&b.leak.type_name))
1080 });
1081
1082 self.unused_dependencies.sort_by(|a, b| {
1083 a.dep
1084 .path
1085 .cmp(&b.dep.path)
1086 .then(a.dep.line.cmp(&b.dep.line))
1087 .then(a.dep.package_name.cmp(&b.dep.package_name))
1088 });
1089
1090 self.unused_dev_dependencies.sort_by(|a, b| {
1091 a.dep
1092 .path
1093 .cmp(&b.dep.path)
1094 .then(a.dep.line.cmp(&b.dep.line))
1095 .then(a.dep.package_name.cmp(&b.dep.package_name))
1096 });
1097
1098 self.unused_optional_dependencies.sort_by(|a, b| {
1099 a.dep
1100 .path
1101 .cmp(&b.dep.path)
1102 .then(a.dep.line.cmp(&b.dep.line))
1103 .then(a.dep.package_name.cmp(&b.dep.package_name))
1104 });
1105 }
1106
1107 fn sort_core_member_findings(&mut self) {
1108 self.unused_enum_members.sort_by(|a, b| {
1109 a.member
1110 .path
1111 .cmp(&b.member.path)
1112 .then(a.member.line.cmp(&b.member.line))
1113 .then(a.member.parent_name.cmp(&b.member.parent_name))
1114 .then(a.member.member_name.cmp(&b.member.member_name))
1115 });
1116
1117 self.unused_class_members.sort_by(|a, b| {
1118 a.member
1119 .path
1120 .cmp(&b.member.path)
1121 .then(a.member.line.cmp(&b.member.line))
1122 .then(a.member.parent_name.cmp(&b.member.parent_name))
1123 .then(a.member.member_name.cmp(&b.member.member_name))
1124 });
1125
1126 self.unused_store_members.sort_by(|a, b| {
1127 a.member
1128 .path
1129 .cmp(&b.member.path)
1130 .then(a.member.line.cmp(&b.member.line))
1131 .then(a.member.parent_name.cmp(&b.member.parent_name))
1132 .then(a.member.member_name.cmp(&b.member.member_name))
1133 });
1134
1135 self.unresolved_imports.sort_by(|a, b| {
1136 a.import
1137 .path
1138 .cmp(&b.import.path)
1139 .then(a.import.line.cmp(&b.import.line))
1140 .then(a.import.col.cmp(&b.import.col))
1141 .then(a.import.specifier.cmp(&b.import.specifier))
1142 });
1143 }
1144
1145 fn sort_core_framework_findings(&mut self) {
1146 self.invalid_client_exports.sort_by(|a, b| {
1147 a.export
1148 .path
1149 .cmp(&b.export.path)
1150 .then(a.export.line.cmp(&b.export.line))
1151 .then(a.export.export_name.cmp(&b.export.export_name))
1152 });
1153
1154 self.mixed_client_server_barrels.sort_by(|a, b| {
1155 a.barrel
1156 .path
1157 .cmp(&b.barrel.path)
1158 .then(a.barrel.line.cmp(&b.barrel.line))
1159 .then(a.barrel.client_origin.cmp(&b.barrel.client_origin))
1160 .then(a.barrel.server_origin.cmp(&b.barrel.server_origin))
1161 });
1162
1163 self.misplaced_directives.sort_by(|a, b| {
1164 a.directive_site
1165 .path
1166 .cmp(&b.directive_site.path)
1167 .then(a.directive_site.line.cmp(&b.directive_site.line))
1168 .then(a.directive_site.col.cmp(&b.directive_site.col))
1169 .then(a.directive_site.directive.cmp(&b.directive_site.directive))
1170 });
1171
1172 self.unprovided_injects.sort_by(|a, b| {
1173 a.inject
1174 .path
1175 .cmp(&b.inject.path)
1176 .then(a.inject.line.cmp(&b.inject.line))
1177 .then(a.inject.col.cmp(&b.inject.col))
1178 .then(a.inject.key_name.cmp(&b.inject.key_name))
1179 });
1180
1181 self.unrendered_components.sort_by(|a, b| {
1182 a.component
1183 .path
1184 .cmp(&b.component.path)
1185 .then(a.component.line.cmp(&b.component.line))
1186 .then(a.component.col.cmp(&b.component.col))
1187 .then(a.component.component_name.cmp(&b.component.component_name))
1188 });
1189 }
1190
1191 fn sort_core_route_and_load_findings(&mut self) {
1192 self.route_collisions.sort_by(|a, b| {
1193 a.collision
1194 .path
1195 .cmp(&b.collision.path)
1196 .then(a.collision.url.cmp(&b.collision.url))
1197 });
1198
1199 self.dynamic_segment_name_conflicts.sort_by(|a, b| {
1200 a.conflict
1201 .path
1202 .cmp(&b.conflict.path)
1203 .then(a.conflict.position.cmp(&b.conflict.position))
1204 });
1205
1206 self.unused_component_props.sort_by(|a, b| {
1207 a.prop
1208 .path
1209 .cmp(&b.prop.path)
1210 .then(a.prop.line.cmp(&b.prop.line))
1211 .then(a.prop.prop_name.cmp(&b.prop.prop_name))
1212 });
1213
1214 self.unused_component_emits.sort_by(|a, b| {
1215 a.emit
1216 .path
1217 .cmp(&b.emit.path)
1218 .then(a.emit.line.cmp(&b.emit.line))
1219 .then(a.emit.emit_name.cmp(&b.emit.emit_name))
1220 });
1221
1222 self.unused_component_inputs.sort_by(|a, b| {
1223 a.input
1224 .path
1225 .cmp(&b.input.path)
1226 .then(a.input.line.cmp(&b.input.line))
1227 .then(a.input.input_name.cmp(&b.input.input_name))
1228 });
1229
1230 self.unused_component_outputs.sort_by(|a, b| {
1231 a.output
1232 .path
1233 .cmp(&b.output.path)
1234 .then(a.output.line.cmp(&b.output.line))
1235 .then(a.output.output_name.cmp(&b.output.output_name))
1236 });
1237
1238 self.unused_svelte_events.sort_by(|a, b| {
1239 a.event
1240 .path
1241 .cmp(&b.event.path)
1242 .then(a.event.line.cmp(&b.event.line))
1243 .then(a.event.event_name.cmp(&b.event.event_name))
1244 });
1245
1246 self.unused_server_actions.sort_by(|a, b| {
1247 a.action
1248 .path
1249 .cmp(&b.action.path)
1250 .then(a.action.line.cmp(&b.action.line))
1251 .then(a.action.col.cmp(&b.action.col))
1252 .then(a.action.action_name.cmp(&b.action.action_name))
1253 });
1254
1255 self.unused_load_data_keys.sort_by(|a, b| {
1256 a.key
1257 .path
1258 .cmp(&b.key.path)
1259 .then(a.key.line.cmp(&b.key.line))
1260 .then(a.key.col.cmp(&b.key.col))
1261 .then(a.key.key_name.cmp(&b.key.key_name))
1262 });
1263 }
1264
1265 /// Sort prop-drilling chains by their source hop (first hop): file, line,
1266 /// prop, depth, for deterministic output. Split out of `sort_core_findings`
1267 /// to keep that function under the unit-size ceiling.
1268 fn sort_prop_drilling_chains(&mut self) {
1269 self.prop_drilling_chains.sort_by(|a, b| {
1270 let a_src = a.chain.hops.first();
1271 let b_src = b.chain.hops.first();
1272 let a_file = a_src.map(|h| &h.file);
1273 let b_file = b_src.map(|h| &h.file);
1274 a_file
1275 .cmp(&b_file)
1276 .then_with(|| a_src.map(|h| h.line).cmp(&b_src.map(|h| h.line)))
1277 .then(a.chain.prop.cmp(&b.chain.prop))
1278 .then(a.chain.depth.cmp(&b.chain.depth))
1279 });
1280 }
1281
1282 /// Sort thin-wrapper findings by file, line, then component for
1283 /// deterministic output.
1284 fn sort_thin_wrappers(&mut self) {
1285 self.thin_wrappers.sort_by(|a, b| {
1286 a.wrapper
1287 .file
1288 .cmp(&b.wrapper.file)
1289 .then(a.wrapper.line.cmp(&b.wrapper.line))
1290 .then(a.wrapper.component.cmp(&b.wrapper.component))
1291 });
1292 }
1293
1294 /// Sort duplicate-prop-shape findings by the shared shape first (so a
1295 /// group's members stay adjacent), then file, line, and component, for
1296 /// deterministic output.
1297 fn sort_duplicate_prop_shapes(&mut self) {
1298 self.duplicate_prop_shapes.sort_by(|a, b| {
1299 a.shape
1300 .shape
1301 .cmp(&b.shape.shape)
1302 .then(a.shape.file.cmp(&b.shape.file))
1303 .then(a.shape.line.cmp(&b.shape.line))
1304 .then(a.shape.component.cmp(&b.shape.component))
1305 });
1306 }
1307
1308 fn sort_dependency_findings(&mut self) {
1309 self.unlisted_dependencies
1310 .sort_by(|a, b| a.dep.package_name.cmp(&b.dep.package_name));
1311 for dep in &mut self.unlisted_dependencies {
1312 dep.dep
1313 .imported_from
1314 .sort_by(|a, b| a.path.cmp(&b.path).then(a.line.cmp(&b.line)));
1315 }
1316
1317 self.duplicate_exports
1318 .sort_by(|a, b| a.export.export_name.cmp(&b.export.export_name));
1319 for dup in &mut self.duplicate_exports {
1320 dup.export
1321 .locations
1322 .sort_by(|a, b| a.path.cmp(&b.path).then(a.line.cmp(&b.line)));
1323 }
1324
1325 self.type_only_dependencies.sort_by(|a, b| {
1326 a.dep
1327 .path
1328 .cmp(&b.dep.path)
1329 .then(a.dep.line.cmp(&b.dep.line))
1330 .then(a.dep.package_name.cmp(&b.dep.package_name))
1331 });
1332
1333 self.test_only_dependencies.sort_by(|a, b| {
1334 a.dep
1335 .path
1336 .cmp(&b.dep.path)
1337 .then(a.dep.line.cmp(&b.dep.line))
1338 .then(a.dep.package_name.cmp(&b.dep.package_name))
1339 });
1340 }
1341
1342 fn sort_graph_findings(&mut self) {
1343 self.circular_dependencies.sort_by(|a, b| {
1344 a.cycle
1345 .files
1346 .cmp(&b.cycle.files)
1347 .then(a.cycle.length.cmp(&b.cycle.length))
1348 });
1349
1350 self.re_export_cycles
1351 .sort_by(|a, b| a.cycle.files.cmp(&b.cycle.files));
1352
1353 self.boundary_violations.sort_by(|a, b| {
1354 a.violation
1355 .from_path
1356 .cmp(&b.violation.from_path)
1357 .then(a.violation.line.cmp(&b.violation.line))
1358 .then(a.violation.col.cmp(&b.violation.col))
1359 .then(a.violation.to_path.cmp(&b.violation.to_path))
1360 });
1361
1362 self.boundary_coverage_violations.sort_by(|a, b| {
1363 a.violation
1364 .path
1365 .cmp(&b.violation.path)
1366 .then(a.violation.line.cmp(&b.violation.line))
1367 .then(a.violation.col.cmp(&b.violation.col))
1368 });
1369
1370 self.boundary_call_violations.sort_by(|a, b| {
1371 a.violation
1372 .path
1373 .cmp(&b.violation.path)
1374 .then(a.violation.line.cmp(&b.violation.line))
1375 .then(a.violation.col.cmp(&b.violation.col))
1376 .then(a.violation.callee.cmp(&b.violation.callee))
1377 });
1378
1379 self.policy_violations.sort_by(|a, b| {
1380 a.violation
1381 .path
1382 .cmp(&b.violation.path)
1383 .then(a.violation.line.cmp(&b.violation.line))
1384 .then(a.violation.col.cmp(&b.violation.col))
1385 .then(a.violation.rule_id.cmp(&b.violation.rule_id))
1386 });
1387 }
1388
1389 fn sort_catalog_findings(&mut self) {
1390 self.stale_suppressions.sort_by(|a, b| {
1391 a.path
1392 .cmp(&b.path)
1393 .then(a.line.cmp(&b.line))
1394 .then(a.col.cmp(&b.col))
1395 });
1396
1397 self.unused_catalog_entries.sort_by(|a, b| {
1398 a.entry
1399 .path
1400 .cmp(&b.entry.path)
1401 .then_with(|| {
1402 catalog_sort_key(&a.entry.catalog_name)
1403 .cmp(&catalog_sort_key(&b.entry.catalog_name))
1404 })
1405 .then(a.entry.catalog_name.cmp(&b.entry.catalog_name))
1406 .then(a.entry.entry_name.cmp(&b.entry.entry_name))
1407 });
1408 for finding in &mut self.unused_catalog_entries {
1409 finding.entry.hardcoded_consumers.sort();
1410 finding.entry.hardcoded_consumers.dedup();
1411 }
1412
1413 self.empty_catalog_groups.sort_by(|a, b| {
1414 a.group
1415 .path
1416 .cmp(&b.group.path)
1417 .then_with(|| {
1418 catalog_sort_key(&a.group.catalog_name)
1419 .cmp(&catalog_sort_key(&b.group.catalog_name))
1420 })
1421 .then(a.group.catalog_name.cmp(&b.group.catalog_name))
1422 .then(a.group.line.cmp(&b.group.line))
1423 });
1424
1425 self.unresolved_catalog_references.sort_by(|a, b| {
1426 a.reference
1427 .path
1428 .cmp(&b.reference.path)
1429 .then(a.reference.line.cmp(&b.reference.line))
1430 .then_with(|| {
1431 catalog_sort_key(&a.reference.catalog_name)
1432 .cmp(&catalog_sort_key(&b.reference.catalog_name))
1433 })
1434 .then(a.reference.catalog_name.cmp(&b.reference.catalog_name))
1435 .then(a.reference.entry_name.cmp(&b.reference.entry_name))
1436 });
1437 for finding in &mut self.unresolved_catalog_references {
1438 finding.reference.available_in_catalogs.sort();
1439 finding.reference.available_in_catalogs.dedup();
1440 }
1441
1442 self.unused_dependency_overrides.sort_by(|a, b| {
1443 a.entry
1444 .path
1445 .cmp(&b.entry.path)
1446 .then(a.entry.line.cmp(&b.entry.line))
1447 .then(a.entry.raw_key.cmp(&b.entry.raw_key))
1448 });
1449 }
1450
1451 fn sort_metadata_findings(&mut self) {
1452 self.sort_prop_drilling_chains();
1453 self.sort_thin_wrappers();
1454 self.sort_duplicate_prop_shapes();
1455
1456 self.misconfigured_dependency_overrides.sort_by(|a, b| {
1457 a.entry
1458 .path
1459 .cmp(&b.entry.path)
1460 .then(a.entry.line.cmp(&b.entry.line))
1461 .then(a.entry.raw_key.cmp(&b.entry.raw_key))
1462 });
1463
1464 self.feature_flags.sort_by(|a, b| {
1465 a.path
1466 .cmp(&b.path)
1467 .then(a.line.cmp(&b.line))
1468 .then(a.flag_name.cmp(&b.flag_name))
1469 });
1470
1471 self.security_unresolved_callee_diagnostics.sort_by(|a, b| {
1472 a.path
1473 .cmp(&b.path)
1474 .then(a.line.cmp(&b.line))
1475 .then(a.col.cmp(&b.col))
1476 .then(a.reason.cmp(&b.reason))
1477 .then(a.expression_kind.cmp(&b.expression_kind))
1478 });
1479 }
1480
1481 fn sort_export_usages(&mut self) {
1482 for usage in &mut self.export_usages {
1483 usage.reference_locations.sort_by(|a, b| {
1484 a.path
1485 .cmp(&b.path)
1486 .then(a.line.cmp(&b.line))
1487 .then(a.col.cmp(&b.col))
1488 });
1489 }
1490 self.export_usages.sort_by(|a, b| {
1491 a.path
1492 .cmp(&b.path)
1493 .then(a.line.cmp(&b.line))
1494 .then(a.export_name.cmp(&b.export_name))
1495 });
1496 }
1497}
1498
1499/// Sort key for catalog names: the default catalog ("default") sorts before any named catalog.
1500fn catalog_sort_key(name: &str) -> (u8, &str) {
1501 if name == "default" {
1502 (0, name)
1503 } else {
1504 (1, name)
1505 }
1506}
1507
1508/// A file that is not reachable from any entry point.
1509#[derive(Debug, Clone, Serialize)]
1510#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1511pub struct UnusedFile {
1512 /// Absolute path to the unused file.
1513 #[serde(serialize_with = "serde_path::serialize")]
1514 pub path: PathBuf,
1515}
1516
1517/// An export that is never imported by other modules.
1518#[derive(Debug, Clone, Serialize)]
1519#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1520pub struct UnusedExport {
1521 /// File containing the unused export.
1522 #[serde(serialize_with = "serde_path::serialize")]
1523 pub path: PathBuf,
1524 /// Name of the unused export.
1525 pub export_name: String,
1526 /// Whether this is a type-only export.
1527 pub is_type_only: bool,
1528 /// 1-based line number of the export.
1529 pub line: u32,
1530 /// 0-based byte column offset.
1531 pub col: u32,
1532 /// Byte offset into the source file (used by the fix command).
1533 pub span_start: u32,
1534 /// Whether this finding comes from a barrel/index re-export rather than the source definition.
1535 pub is_re_export: bool,
1536}
1537
1538/// A public export signature that references a same-file private type.
1539#[derive(Debug, Clone, Serialize)]
1540#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1541pub struct PrivateTypeLeak {
1542 /// File containing the exported symbol.
1543 #[serde(serialize_with = "serde_path::serialize")]
1544 pub path: PathBuf,
1545 /// Export whose public signature leaks the private type.
1546 pub export_name: String,
1547 /// Private type referenced by the public signature.
1548 pub type_name: String,
1549 /// 1-based line number of the leaking type reference.
1550 pub line: u32,
1551 /// 0-based byte column offset.
1552 pub col: u32,
1553 /// Byte offset of the type reference.
1554 pub span_start: u32,
1555}
1556
1557/// A `"use client"` file that exports a Next.js server-only / route-segment
1558/// config name. Next.js rejects this combination at build time; fallow catches
1559/// it statically before the build runs.
1560#[derive(Debug, Clone, Serialize)]
1561#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1562pub struct InvalidClientExport {
1563 /// File carrying the `"use client"` directive and the illegal export.
1564 #[serde(serialize_with = "serde_path::serialize")]
1565 pub path: PathBuf,
1566 /// Name of the server-only / route-config export that is illegal in a
1567 /// client file (e.g. `metadata`, `generateMetadata`, `revalidate`, `GET`).
1568 pub export_name: String,
1569 /// The file-level directive that makes the export illegal. Always
1570 /// `"use client"` today; carried so the message can name it verbatim.
1571 pub directive: String,
1572 /// 1-based line number of the export.
1573 pub line: u32,
1574 /// 0-based byte column offset of the export.
1575 pub col: u32,
1576}
1577
1578/// A barrel file that re-exports BOTH a `"use client"` origin module AND a
1579/// server-only origin module. Importing one name from such a barrel drags the
1580/// other's directive context across the React Server Components boundary (the
1581/// Next.js App Router footgun); fallow catches it statically.
1582#[derive(Debug, Clone, Serialize)]
1583#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1584pub struct MixedClientServerBarrel {
1585 /// The barrel file re-exporting both a client and a server-only origin.
1586 #[serde(serialize_with = "serde_path::serialize")]
1587 pub path: PathBuf,
1588 /// The `"use client"` origin's relative path or specifier as written in the
1589 /// barrel's offending re-export.
1590 pub client_origin: String,
1591 /// The server-only origin's relative path or specifier as written in the
1592 /// barrel's offending re-export.
1593 pub server_origin: String,
1594 /// 1-based line number of the barrel's first offending re-export.
1595 pub line: u32,
1596 /// 0-based byte column offset of the barrel's first offending re-export.
1597 pub col: u32,
1598}
1599
1600/// A `"use client"` / `"use server"` directive written as an expression
1601/// statement after a non-directive statement (an import, a const). The RSC
1602/// bundler only honors a directive in the leading prologue, so once any
1603/// statement precedes it the string is parsed as an ordinary expression and
1604/// silently ignored: the intended client/server boundary never takes effect.
1605/// The fix is to move the directive to the very top of the file.
1606#[derive(Debug, Clone, Serialize)]
1607#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1608pub struct MisplacedDirective {
1609 /// The file carrying the misplaced directive.
1610 #[serde(serialize_with = "serde_path::serialize")]
1611 pub path: PathBuf,
1612 /// The directive string as written, either `"use client"` or
1613 /// `"use server"` (without the surrounding quotes).
1614 pub directive: String,
1615 /// 1-based line number of the misplaced directive statement.
1616 pub line: u32,
1617 /// 0-based byte column offset of the misplaced directive statement.
1618 pub col: u32,
1619}
1620
1621/// A Vue `inject(KEY)` or Svelte `getContext(KEY)` whose symbol KEY is
1622/// `provide`/`setContext`'d nowhere in the analyzed project. The key is a
1623/// symbol with cross-file identity, so an unmatched key is a real dead-half DI
1624/// link: at runtime the inject returns `undefined`, surfaced only at render.
1625/// The fix is binary: provide the key somewhere, or remove the dead inject.
1626#[derive(Debug, Clone, Serialize)]
1627#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1628pub struct UnprovidedInject {
1629 /// The file carrying the orphan inject / getContext call.
1630 #[serde(serialize_with = "serde_path::serialize")]
1631 pub path: PathBuf,
1632 /// The injected key identifier as written at the call site.
1633 pub key_name: String,
1634 /// Which framework's DI API this came from: `"vue"` or `"svelte"`.
1635 pub framework: String,
1636 /// 1-based line number of the inject / getContext call.
1637 pub line: u32,
1638 /// 0-based byte column offset of the inject / getContext call.
1639 pub col: u32,
1640}
1641
1642/// A Next.js Server Action (an export of a `"use server"` file) that no code in
1643/// the analyzed project references: no import-and-call, no `action={fn}` JSX
1644/// binding, no `<form action={fn}>`. This is the cross-graph "declared but zero
1645/// consumers" direction, reclassified out of `unused-export` for `"use server"`
1646/// files so the finding carries the action-specific signal. It does NOT mean the
1647/// endpoint is unreachable: Next still registers the action id, so it stays
1648/// POST-able. It means no project code calls it (likely forgotten / dead, and a
1649/// candidate for removal to shrink surface area).
1650#[derive(Debug, Clone, Serialize)]
1651#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1652pub struct UnusedServerAction {
1653 /// The `"use server"` file that exports the unreferenced action.
1654 #[serde(serialize_with = "serde_path::serialize")]
1655 pub path: PathBuf,
1656 /// The exported action name as written, or `"default"` for a default export.
1657 pub action_name: String,
1658 /// 1-based line number of the export.
1659 pub line: u32,
1660 /// 0-based byte column offset of the export.
1661 pub col: u32,
1662}
1663
1664/// A SvelteKit `+page.{ts,server.ts,js,server.js}` `load()` return-object key
1665/// read by no consumer: not off the sibling `+page.svelte`'s `data.<key>`, nor
1666/// project-wide via `page.data.<key>` / `$page.data.<key>`. A dead load key runs
1667/// a real server/DB fetch cost on every request for data nothing renders. The
1668/// fix is a human call (delete the key, or wire a consumer): a load fetch may
1669/// have side effects, so there is no safe auto-fix.
1670#[derive(Debug, Clone, Serialize)]
1671#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1672pub struct UnusedLoadDataKey {
1673 /// The producer `+page.{ts,server.ts,js,server.js}` file declaring the key.
1674 #[serde(serialize_with = "serde_path::serialize")]
1675 pub path: PathBuf,
1676 /// The returned-object key name read by no consumer.
1677 pub key_name: String,
1678 /// 1-based line number of the key in the return object.
1679 pub line: u32,
1680 /// 0-based byte column offset of the key.
1681 pub col: u32,
1682 /// The route directory relative to the project root (`src/routes/blog`), for
1683 /// agent remediation and per-route trend aggregation. `None` when not
1684 /// determinable.
1685 #[serde(default, skip_serializing_if = "Option::is_none")]
1686 pub route_dir: Option<String>,
1687}
1688
1689/// A Vue/Svelte single-file component (the default export of a `.vue`/`.svelte`
1690/// file) that is reachable in the module graph but rendered NOWHERE in the
1691/// project: no `<Tag>`, no `:is`/`this=` binding, no `components`/`app.component`
1692/// registration, no `h()`/auto-import use, and no script value-read. It survives
1693/// `unused-file` (a barrel re-export keeps it reachable) and `unused-export`
1694/// (the re-export counts as a use), yet no file actually instantiates it.
1695#[derive(Debug, Clone, Serialize)]
1696#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1697pub struct UnrenderedComponent {
1698 /// The component file that is reachable but rendered nowhere.
1699 #[serde(serialize_with = "serde_path::serialize")]
1700 pub path: PathBuf,
1701 /// The component name. For `"vue"` / `"svelte"` / `"astro"` this is the SFC
1702 /// file stem (PascalCase); for `"angular"` it is the component class name; for
1703 /// `"lit"` it is the registered custom-element TAG (e.g. `x-foo`), not a file
1704 /// stem. Use `path` to anchor the file across all frameworks.
1705 pub component_name: String,
1706 /// Which framework this component belongs to: `"vue"`, `"svelte"`, `"astro"`,
1707 /// `"angular"`, or `"lit"`.
1708 pub framework: String,
1709 /// A barrel/file that re-exports this component, kept for the remediation
1710 /// trace ("reachable via X, rendered nowhere"). Absolute in memory,
1711 /// serialized workspace-relative (like `path`); `None` when not determinable.
1712 #[serde(
1713 serialize_with = "serde_path::serialize_option",
1714 skip_serializing_if = "Option::is_none"
1715 )]
1716 pub reachable_via: Option<PathBuf>,
1717 /// 1-based line number of the component (the file head; SFCs have no explicit
1718 /// default-export statement).
1719 pub line: u32,
1720 /// 0-based byte column offset.
1721 pub col: u32,
1722}
1723
1724/// A Vue `<script setup>` `defineProps`, Svelte 5 `$props()`, or React declared
1725/// prop that is referenced NOWHERE inside its own component. Single-component
1726/// finding, zero-FP doctrine: the component abstains on any opaque public or
1727/// fallthrough signal.
1728#[derive(Debug, Clone, Serialize)]
1729#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1730pub struct UnusedComponentProp {
1731 /// The component file declaring the unused prop.
1732 #[serde(serialize_with = "serde_path::serialize")]
1733 pub path: PathBuf,
1734 /// The component name.
1735 pub component_name: String,
1736 /// The declared prop name that is never referenced.
1737 pub prop_name: String,
1738 /// 1-based line number of the prop declaration.
1739 pub line: u32,
1740 /// 0-based byte column offset of the prop declaration.
1741 pub col: u32,
1742}
1743
1744/// A Vue `<script setup>` `defineEmits` declared event that is EMITTED nowhere
1745/// inside its own single-file component (no `emit('<name>')` call). Single-file
1746/// finding, zero-FP doctrine: the whole file abstains on any
1747/// unharvestable / dynamic-emit / whole-object-use / `defineModel` signal.
1748#[derive(Debug, Clone, Serialize)]
1749#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1750pub struct UnusedComponentEmit {
1751 /// The `.vue` SFC declaring the unused emit.
1752 #[serde(serialize_with = "serde_path::serialize")]
1753 pub path: PathBuf,
1754 /// The component name (the `.vue` file stem).
1755 pub component_name: String,
1756 /// The declared emit event name that is never emitted.
1757 pub emit_name: String,
1758 /// 1-based line number of the emit declaration.
1759 pub line: u32,
1760 /// 0-based byte column offset of the emit declaration.
1761 pub col: u32,
1762}
1763
1764/// A Svelte component dispatching a custom event via `createEventDispatcher()`
1765/// whose event name is listened to NOWHERE in the analyzed project. Cross-file
1766/// dead-output direction: the component fires an event nothing handles.
1767/// Zero-FP doctrine: the whole component abstains on any dynamic-dispatch or
1768/// whole-`dispatch`-value signal, and a listener on ANY component anywhere
1769/// credits the event name (the liberal over-credit direction).
1770#[derive(Debug, Clone, Serialize)]
1771#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1772pub struct UnusedSvelteEvent {
1773 /// The `.svelte` component dispatching the unlistened event.
1774 #[serde(serialize_with = "serde_path::serialize")]
1775 pub path: PathBuf,
1776 /// The component name (the `.svelte` file stem).
1777 pub component_name: String,
1778 /// The dispatched event name that is listened to nowhere.
1779 pub event_name: String,
1780 /// 1-based line number of the `dispatch('<name>')` call.
1781 pub line: u32,
1782 /// 0-based byte column offset of the `dispatch('<name>')` call.
1783 pub col: u32,
1784}
1785
1786/// One hop in a prop-drilling chain: a component that received the prop and
1787/// passed it along (or, at the chain ends, the source that owns it and the
1788/// consumer that substantively reads it).
1789#[derive(Debug, Clone, Serialize)]
1790#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1791pub struct PropDrillHop {
1792 /// The file containing this hop's component.
1793 #[serde(serialize_with = "serde_path::serialize")]
1794 pub file: PathBuf,
1795 /// 1-based line of the component definition (or the prop declaration at the
1796 /// source hop). Anchors a jump-to-source for the agent.
1797 pub line: u32,
1798 /// The component name at this hop.
1799 pub component: String,
1800}
1801
1802/// A located prop-drilling chain: a received prop forwarded unchanged through
1803/// `>= N` intermediate pass-through components, each of which only re-passes it,
1804/// until a component that substantively consumes it. The high-confidence signal
1805/// is "the received identifier is used ONLY as the root of forwarded child-JSX
1806/// attribute values", not the attribute name matching. Health signal (rule
1807/// defaults to `off`, opt-in): a small capped penalty plus a `health --hotspots`
1808/// surface, and located per-chain records so CI / an agent can act ("colocate or
1809/// lift to context at hop B"). Zero-FP doctrine: any spread / `cloneElement` /
1810/// element-as-prop / render-prop / context-provider / dynamic shape in the path
1811/// abstains the whole chain.
1812#[derive(Debug, Clone, Serialize)]
1813#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1814pub struct PropDrillingChain {
1815 /// The drilled prop name as declared at the chain SOURCE.
1816 pub prop: String,
1817 /// The chain depth = the number of components the prop is forwarded THROUGH
1818 /// (source + intermediates + consumer = `hops.len()`). Always `>= N`.
1819 pub depth: u32,
1820 /// The ordered hop trail from source to consumer. The first hop owns the
1821 /// prop, the middle hops are pass-throughs, the last hop consumes it. The
1822 /// finding anchor is the first hop (`path` / `line` for suppression + CI).
1823 pub hops: Vec<PropDrillHop>,
1824}
1825
1826/// A located thin-wrapper / passthrough component: a React/Preact component
1827/// whose entire body is `return <Child {...props}/>` (a single spread-forwarded
1828/// child render, no host wrapper, no own value-add). It is pure structural
1829/// indirection, a CANDIDATE for inlining at call sites or deleting. Health
1830/// signal (rule defaults to `off`, opt-in): never a correctness error. Zero-FP
1831/// doctrine: `forwardRef` / `memo` / exported / context-provider /
1832/// `cloneElement` / render-prop / named-attr / unresolved-child wrappers all
1833/// abstain (each is an intentional indirection or unprovable shape).
1834#[derive(Debug, Clone, Serialize)]
1835#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1836pub struct ThinWrapper {
1837 /// The file containing the wrapper component.
1838 #[serde(serialize_with = "serde_path::serialize")]
1839 pub file: PathBuf,
1840 /// 1-based line of the wrapper component definition (the finding anchor for
1841 /// jump-to-source and line-level suppression).
1842 pub line: u32,
1843 /// The wrapper component name.
1844 pub component: String,
1845 /// The single child component the wrapper forwards its props to (as written
1846 /// at the render site).
1847 pub child_component: String,
1848}
1849
1850/// One member of a duplicate-prop-shape group: the OTHER components that share
1851/// the same significant prop-name set, listed in each member's
1852/// `sharing_components`. Path-sorted for stable output. A located reference (no
1853/// `shape`, which is carried once on the owning [`DuplicatePropShape`]).
1854#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
1855#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1856pub struct DuplicatePropShapeMember {
1857 /// The file containing the sibling component.
1858 #[serde(serialize_with = "serde_path::serialize")]
1859 pub file: PathBuf,
1860 /// 1-based line of the sibling component definition.
1861 pub line: u32,
1862 /// The sibling component name.
1863 pub component: String,
1864}
1865
1866/// A React/Preact component that participates in a duplicate-prop-shape GROUP:
1867/// three or more distinct components across two or more files whose
1868/// statically-harvested, fully-known prop NAME set is byte-for-byte IDENTICAL
1869/// after excluding a fixed denylist of ubiquitous DOM / render-passthrough prop
1870/// names, with the REMAINING significant set holding four or more members. This
1871/// is a structural-refactor health signal (extract a shared `Props` type or a
1872/// base component), never a correctness error and never an auto-fix. One finding
1873/// is emitted per participating component; `sharing_components` lists the other
1874/// members of the same group. Health signal: the rule defaults to `off`
1875/// (opt-in), so this is dormant until enabled. Exact full-set identity only: a
1876/// superset / subset relationship does NOT group (so the finding always fits one
1877/// extracted shared type).
1878#[derive(Debug, Clone, Serialize)]
1879#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1880pub struct DuplicatePropShape {
1881 /// The file containing this component.
1882 #[serde(serialize_with = "serde_path::serialize")]
1883 pub file: PathBuf,
1884 /// 1-based line of this component definition (the finding anchor for
1885 /// jump-to-source and line-level suppression).
1886 pub line: u32,
1887 /// This component name.
1888 pub component: String,
1889 /// The shared SIGNIFICANT prop-name set (sorted, denylist-stripped). The
1890 /// unit being grouped; identical across every member of the group.
1891 pub shape: Vec<String>,
1892 /// The total number of components in this group (this one plus every
1893 /// sibling).
1894 pub group_size: u32,
1895 /// The OTHER components sharing this exact prop shape (path-sorted). A
1896 /// file-level-suppressed member drops from its own finding but still appears
1897 /// here, because the group is real regardless of suppression.
1898 pub sharing_components: Vec<DuplicatePropShapeMember>,
1899}
1900
1901/// An Angular `@Input()` / signal `input()` / `model()` declared input that is
1902/// read NOWHERE inside its own component (neither the inline/external template
1903/// nor the class body). Single-file dead-input direction; the Angular analogue
1904/// of [`UnusedComponentProp`]. The whole component abstains on an unresolved
1905/// `extends` heritage clause (a base class in another file may read `this.foo`).
1906#[derive(Debug, Clone, Serialize)]
1907#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1908pub struct UnusedComponentInput {
1909 /// The Angular component/directive `.ts` file declaring the unused input.
1910 #[serde(serialize_with = "serde_path::serialize")]
1911 pub path: PathBuf,
1912 /// The component name (the `.ts` file stem).
1913 pub component_name: String,
1914 /// The declared input name that is never read.
1915 pub input_name: String,
1916 /// 1-based line number of the input declaration.
1917 pub line: u32,
1918 /// 0-based byte column offset of the input declaration.
1919 pub col: u32,
1920}
1921
1922/// An Angular `@Output()` / signal `output()` declared output that is EMITTED
1923/// nowhere inside its own component (no `this.<output>.emit(...)`). Single-file
1924/// dead-output direction; the Angular analogue of [`UnusedComponentEmit`]. A
1925/// `model()` is recorded as an input only, so its framework-driven `update:`
1926/// emit is never flagged here. The whole component abstains on an unresolved
1927/// `extends` heritage clause.
1928#[derive(Debug, Clone, Serialize)]
1929#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1930pub struct UnusedComponentOutput {
1931 /// The Angular component/directive `.ts` file declaring the unused output.
1932 #[serde(serialize_with = "serde_path::serialize")]
1933 pub path: PathBuf,
1934 /// The component name (the `.ts` file stem).
1935 pub component_name: String,
1936 /// The declared output name that is never emitted.
1937 pub output_name: String,
1938 /// 1-based line number of the output declaration.
1939 pub line: u32,
1940 /// 0-based byte column offset of the output declaration.
1941 pub col: u32,
1942}
1943
1944/// Two or more Next.js App Router route files that resolve to the SAME URL
1945/// within one app-root. Next.js fails the build ("You cannot have two parallel
1946/// pages that resolve to the same path"); fallow catches it statically and
1947/// names every colliding file at once. One finding is emitted per colliding
1948/// file; `conflicting_paths` lists the sibling files that share the URL.
1949#[derive(Debug, Clone, Serialize)]
1950#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1951pub struct RouteCollision {
1952 /// This colliding route file (a `page` or `route` leaf).
1953 #[serde(serialize_with = "serde_path::serialize")]
1954 pub path: PathBuf,
1955 /// The URL pathname this file resolves to within its app-root, after
1956 /// stripping route groups `(x)` and parallel-slot `@slot` prefixes (e.g.
1957 /// `/about`, `/api/health`, `/blog/:slug`).
1958 pub url: String,
1959 /// The other route files that resolve to the same URL within the same
1960 /// app-root. Path-sorted for stable output / fingerprints.
1961 #[serde(serialize_with = "serde_path::serialize_vec")]
1962 pub conflicting_paths: Vec<PathBuf>,
1963 /// 1-based line number (file-level finding, always 1).
1964 pub line: u32,
1965 /// 0-based byte column offset (file-level finding, always 0).
1966 pub col: u32,
1967}
1968
1969/// Two or more sibling dynamic route segments at the SAME App Router tree
1970/// position using different param spellings (`[id]` vs `[slug]`, or `[...x]`
1971/// vs `[[...x]]`). Next.js throws "You cannot use different slug names for the
1972/// same dynamic path" at dev / production RUNTIME when the position is hit;
1973/// `next build` does NOT catch it, so fallow's static catch surfaces a route
1974/// that would otherwise pass CI and crash at request time. One finding is
1975/// emitted per involved file.
1976#[derive(Debug, Clone, Serialize)]
1977#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1978pub struct DynamicSegmentNameConflict {
1979 /// This route file living under one of the conflicting dynamic segments.
1980 #[serde(serialize_with = "serde_path::serialize")]
1981 pub path: PathBuf,
1982 /// The tree position (parent URL after group/slot normalization) where the
1983 /// dynamic segments conflict, e.g. `/shop` for `/shop/[id]` vs
1984 /// `/shop/[slug]`. The app-root prefix is stripped.
1985 pub position: String,
1986 /// The distinct conflicting dynamic-segment spellings at this position, as
1987 /// written (e.g. `["[id]", "[slug]"]`). Sorted for stable output.
1988 pub conflicting_segments: Vec<String>,
1989 /// The other route files at the same position under a conflicting dynamic
1990 /// segment. Path-sorted for stable output / fingerprints.
1991 #[serde(serialize_with = "serde_path::serialize_vec")]
1992 pub conflicting_paths: Vec<PathBuf>,
1993 /// 1-based line number (file-level finding, always 1).
1994 pub line: u32,
1995 /// 0-based byte column offset (file-level finding, always 0).
1996 pub col: u32,
1997}
1998
1999/// A dependency that is listed in package.json but never imported.
2000#[derive(Debug, Clone, Serialize)]
2001#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2002pub struct UnusedDependency {
2003 /// Package name, including internal workspace package names.
2004 pub package_name: String,
2005 /// Whether this is in `dependencies`, `devDependencies`, or `optionalDependencies`.
2006 pub location: DependencyLocation,
2007 /// Path to the package.json where this dependency is listed.
2008 /// For root deps this is `<root>/package.json`, for workspace deps it is `<ws>/package.json`.
2009 #[serde(serialize_with = "serde_path::serialize")]
2010 pub path: PathBuf,
2011 /// 1-based line number of the dependency entry in package.json.
2012 pub line: u32,
2013 /// Workspace roots that import this package even though the declaring workspace does not.
2014 #[serde(
2015 serialize_with = "serde_path::serialize_vec",
2016 skip_serializing_if = "Vec::is_empty"
2017 )]
2018 #[cfg_attr(feature = "schema", schemars(default))]
2019 pub used_in_workspaces: Vec<PathBuf>,
2020}
2021
2022/// Where in package.json a dependency is listed.
2023///
2024/// # Examples
2025///
2026/// ```
2027/// use fallow_types::results::DependencyLocation;
2028///
2029/// // All three variants are constructible
2030/// let loc = DependencyLocation::Dependencies;
2031/// let dev = DependencyLocation::DevDependencies;
2032/// let opt = DependencyLocation::OptionalDependencies;
2033/// // Debug output includes the variant name
2034/// assert!(format!("{loc:?}").contains("Dependencies"));
2035/// assert!(format!("{dev:?}").contains("DevDependencies"));
2036/// assert!(format!("{opt:?}").contains("OptionalDependencies"));
2037/// ```
2038#[derive(Debug, Clone, Serialize)]
2039#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2040#[serde(rename_all = "camelCase")]
2041pub enum DependencyLocation {
2042 /// Listed in `dependencies`.
2043 Dependencies,
2044 /// Listed in `devDependencies`.
2045 DevDependencies,
2046 /// Listed in `optionalDependencies`.
2047 OptionalDependencies,
2048}
2049
2050/// An unused enum or class member.
2051#[derive(Debug, Clone, Serialize)]
2052#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2053pub struct UnusedMember {
2054 /// File containing the unused member.
2055 #[serde(serialize_with = "serde_path::serialize")]
2056 pub path: PathBuf,
2057 /// Name of the parent enum or class.
2058 pub parent_name: String,
2059 /// Name of the unused member.
2060 pub member_name: String,
2061 /// Whether this is an enum member, class method, or class property.
2062 pub kind: MemberKind,
2063 /// 1-based line number.
2064 pub line: u32,
2065 /// 0-based byte column offset.
2066 pub col: u32,
2067}
2068
2069/// An import that could not be resolved.
2070#[derive(Debug, Clone, Serialize)]
2071#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2072pub struct UnresolvedImport {
2073 /// File containing the unresolved import.
2074 #[serde(serialize_with = "serde_path::serialize")]
2075 pub path: PathBuf,
2076 /// The import specifier that could not be resolved.
2077 pub specifier: String,
2078 /// 1-based line number.
2079 pub line: u32,
2080 /// 0-based byte column offset of the import statement.
2081 pub col: u32,
2082 /// 0-based byte column offset of the source string literal (the specifier in quotes).
2083 /// Used by the LSP to underline just the specifier, not the entire import line.
2084 pub specifier_col: u32,
2085}
2086
2087/// A dependency used in code but not listed in package.json.
2088#[derive(Debug, Clone, Serialize)]
2089#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2090pub struct UnlistedDependency {
2091 /// Package name, including internal workspace package names, that is
2092 /// imported but not listed in package.json.
2093 pub package_name: String,
2094 /// Import sites where this unlisted dependency is used (file path, line, column).
2095 pub imported_from: Vec<ImportSite>,
2096}
2097
2098/// A location where an import occurs.
2099#[derive(Debug, Clone, Serialize)]
2100#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2101pub struct ImportSite {
2102 /// File containing the import.
2103 #[serde(serialize_with = "serde_path::serialize")]
2104 pub path: PathBuf,
2105 /// 1-based line number.
2106 pub line: u32,
2107 /// 0-based byte column offset.
2108 pub col: u32,
2109}
2110
2111/// An export that appears multiple times across the project.
2112#[derive(Debug, Clone, Serialize)]
2113#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2114pub struct DuplicateExport {
2115 /// The duplicated export name.
2116 pub export_name: String,
2117 /// Locations where this export name appears.
2118 pub locations: Vec<DuplicateLocation>,
2119}
2120
2121/// A location where a duplicate export appears.
2122#[derive(Debug, Clone, Serialize)]
2123#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2124pub struct DuplicateLocation {
2125 /// File containing the duplicate export.
2126 #[serde(serialize_with = "serde_path::serialize")]
2127 pub path: PathBuf,
2128 /// 1-based line number.
2129 pub line: u32,
2130 /// 0-based byte column offset.
2131 pub col: u32,
2132}
2133
2134/// A production dependency that is only used via type-only imports.
2135/// In production builds, type imports are erased, so this dependency
2136/// is not needed at runtime and could be moved to devDependencies.
2137#[derive(Debug, Clone, Serialize)]
2138#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2139pub struct TypeOnlyDependency {
2140 /// Production dependency that is only used via type-only imports.
2141 pub package_name: String,
2142 /// Path to the package.json where the dependency is listed.
2143 #[serde(serialize_with = "serde_path::serialize")]
2144 pub path: PathBuf,
2145 /// 1-based line number of the dependency entry in package.json.
2146 pub line: u32,
2147}
2148
2149/// The kind of security candidate. Findings are CANDIDATES for downstream agent
2150/// verification, NOT verified vulnerabilities.
2151#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
2152#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2153#[serde(rename_all = "kebab-case")]
2154pub enum SecurityFindingKind {
2155 /// A `"use client"` file transitively imports a module that reads a
2156 /// non-public `process.env` secret (graph-structural; bespoke, not catalogue).
2157 ClientServerLeak,
2158 /// A syntactic sink site matched against the data-driven catalogue
2159 /// (`security_matchers.toml`). Serializes `"tainted-sink"`; the CWE class is
2160 /// carried in `category` + `cwe`. ONE variant covers all catalogue categories.
2161 TaintedSink,
2162}
2163
2164/// The role a hop plays in a security finding's structural import trace.
2165#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
2166#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2167#[serde(rename_all = "kebab-case")]
2168pub enum TraceHopRole {
2169 /// The `"use client"` boundary file the finding is anchored on.
2170 ClientBoundary,
2171 /// A module that reads an untrusted input source such as request data,
2172 /// where the candidate's sink argument actually traces back to that read in
2173 /// the same statement (arg-level, the strong intra-module association).
2174 UntrustedSource,
2175 /// A module that merely CONTAINS an untrusted-input source somewhere and is
2176 /// import-reachable to the sink module (module-level, issue #885). This is a
2177 /// reachability signal, NOT a proven value path: the specific source value
2178 /// is not shown to reach the sink argument. Labeled distinctly from
2179 /// `UntrustedSource` so a consumer never reads a module-level hop as a
2180 /// value-flow proof.
2181 ModuleSource,
2182 /// An intermediate module on the transitive import path.
2183 Intermediate,
2184 /// The module that reads the secret.
2185 SecretSource,
2186 /// The syntactic sink site of a catalogue-driven `tainted-sink` candidate
2187 /// (the single hop the `tainted_sink` detector emits). Distinct from
2188 /// `SecretSource`, which is specific to the `client-server-leak` rule.
2189 Sink,
2190}
2191
2192/// One hop in a security finding's structural trace. Stored as an absolute path
2193/// internally; JSON serialization strips the project root via
2194/// `serde_path::serialize`.
2195#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2196#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2197pub struct TraceHop {
2198 /// File on this hop of the import chain.
2199 #[serde(serialize_with = "serde_path::serialize")]
2200 pub path: PathBuf,
2201 /// 1-based line number. Import-chain hops point at the import site; the
2202 /// terminal secret-source hop points at the source module when extraction
2203 /// does not carry a more precise member-access span.
2204 pub line: u32,
2205 /// 0-based byte column offset.
2206 pub col: u32,
2207 /// Role of this hop in the chain.
2208 pub role: TraceHopRole,
2209}
2210
2211/// How strongly the untrusted-source signal is associated with the sink, a
2212/// structured discriminator so a consumer can tier candidates without parsing
2213/// the human `evidence` prose. Present only when
2214/// [`SecurityReachability::reachable_from_untrusted_source`] is true. Neither
2215/// value proves exploitability; both are ranking signals (issue #885 doctrine:
2216/// rank, never gate).
2217#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
2218#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2219#[serde(rename_all = "kebab-case")]
2220pub enum TaintConfidence {
2221 /// The sink's argument traces back to a known untrusted-source read in the
2222 /// SAME statement / module (the intra-module back-trace, issue #859). The
2223 /// strong, high-value candidate: a specific source expression is implicated.
2224 ArgLevel,
2225 /// The sink merely lives in a module that is import-reachable from a module
2226 /// containing an untrusted source (issue #885). The weak candidate: only the
2227 /// module is implicated, not a specific value path to the sink argument.
2228 ModuleLevel,
2229}
2230
2231/// Graph-derived reachability ranking signal for a security candidate. Computed
2232/// from the existing module graph after detection, never proven exploitable.
2233/// Used to surface candidates that sit on a request/runtime-reachable surface,
2234/// receive same-module source evidence, or are import-reachable from an
2235/// untrusted-source module above isolated helpers or scripts.
2236///
2237/// This is a relative-ordering signal, NOT a `confidence` or `signal_strength`
2238/// score: fallow does not prove the path is exploitable.
2239#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2240#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2241pub struct SecurityReachability {
2242 /// Whether the anchor module is reachable from a runtime/application entry
2243 /// point (route handlers, server entry, framework runtime roots), the
2244 /// closest graph proxy for an external/request input surface. Code reachable
2245 /// only from test entry points does not count.
2246 pub reachable_from_entry: bool,
2247 /// Whether the anchor module is reachable over value imports from a module
2248 /// that reads a known untrusted input source. Module-level only: this does
2249 /// not prove a specific source value reaches the sink argument.
2250 #[serde(default)]
2251 pub reachable_from_untrusted_source: bool,
2252 /// Structured tier of the untrusted-source association: `arg-level` when the
2253 /// sink argument traces to a same-module source read (strong), `module-level`
2254 /// when only the module is import-reachable from a source (weak). Present
2255 /// exactly when `reachable_from_untrusted_source` is true, so a consumer can
2256 /// separate strong from weak candidates from this field alone without parsing
2257 /// the `evidence` string. Not an exploitability proof.
2258 #[serde(default, skip_serializing_if = "Option::is_none")]
2259 pub taint_confidence: Option<TaintConfidence>,
2260 /// Number of value-import hops from the untrusted-source module to the sink
2261 /// module when `reachable_from_untrusted_source` is true.
2262 #[serde(default, skip_serializing_if = "Option::is_none")]
2263 pub untrusted_source_hop_count: Option<u32>,
2264 /// Module-level import path from the untrusted-source module to the sink
2265 /// anchor. Empty when no source module reaches this candidate. The path is a
2266 /// ranking explanation, not a value-flow proof.
2267 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2268 pub untrusted_source_trace: Vec<TraceHop>,
2269 /// Number of distinct modules that transitively depend on the anchor module
2270 /// (fan-in via the graph's reverse-dependency index). A higher value means a
2271 /// wider surface: more call sites could route untrusted input into the sink.
2272 pub blast_radius: u32,
2273 /// Whether the anchor module participates in an architecture-boundary
2274 /// violation found in the same run (as the importing or imported file).
2275 /// Optional pairing: a candidate that also crosses a declared boundary is a
2276 /// stronger review target.
2277 pub crosses_boundary: bool,
2278}
2279
2280/// Dead-code cross-link attached to a security candidate when fallow's dead-code
2281/// pass reports the same anchor as removable code.
2282#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2283#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2284pub struct SecurityDeadCodeContext {
2285 /// Dead-code issue kind that matched the security candidate.
2286 pub kind: SecurityDeadCodeKind,
2287 /// Unused export name when `kind` is `unused-export`.
2288 #[serde(default, skip_serializing_if = "Option::is_none")]
2289 pub export_name: Option<String>,
2290 /// Dead-code finding line when available.
2291 #[serde(default, skip_serializing_if = "Option::is_none")]
2292 pub line: Option<u32>,
2293 /// Agent-facing guidance for deciding between deletion and hardening.
2294 pub guidance: String,
2295}
2296
2297/// Dead-code issue kind linked to a security candidate.
2298#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
2299#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2300#[serde(rename_all = "kebab-case")]
2301pub enum SecurityDeadCodeKind {
2302 /// The candidate's anchor file is also reported as an unused file.
2303 UnusedFile,
2304 /// The candidate's anchor sits on an unused export declaration.
2305 UnusedExport,
2306}
2307
2308/// Internal row for a security sink-shaped callee that extraction could not
2309/// flatten to a static catalogue path.
2310#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2311#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2312pub struct SecurityUnresolvedCalleeDiagnostic {
2313 /// File containing the skipped callee. Absolute internally.
2314 #[serde(serialize_with = "serde_path::serialize")]
2315 pub path: PathBuf,
2316 /// 1-based line of the skipped callee.
2317 pub line: u32,
2318 /// 0-based byte column of the skipped callee.
2319 pub col: u32,
2320 /// Why the callee could not be flattened.
2321 pub reason: SkippedSecurityCalleeReason,
2322 /// Compact syntax shape of the skipped callee.
2323 pub expression_kind: SkippedSecurityCalleeExpressionKind,
2324}
2325
2326/// The sink slot of a [`SecurityCandidate`]: a self-contained description of the
2327/// matched sink site. Echoes the finding's own span (`path`/`line`/`col`) plus
2328/// the catalogue `category`/`cwe` and the captured `callee`, so an agent can act
2329/// on `candidate.sink` in isolation (e.g. after fanning a finding out to a
2330/// sub-agent) without reading the parent finding.
2331#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
2332#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2333pub struct SecurityCandidateSink {
2334 /// File of the sink site. Absolute internally; JSON strips the project root
2335 /// via `serde_path::serialize`.
2336 #[serde(serialize_with = "serde_path::serialize")]
2337 pub path: PathBuf,
2338 /// 1-based line of the sink site.
2339 pub line: u32,
2340 /// 0-based byte column of the sink site.
2341 pub col: u32,
2342 /// Catalogue category id of the sink (e.g. `"dangerous-html"`). For
2343 /// `client-server-leak` this is `None` for the secret-leak finding, and
2344 /// `Some("server-only-import")` when a `"use client"` cone reaches
2345 /// server-only code.
2346 #[serde(default, skip_serializing_if = "Option::is_none")]
2347 pub category: Option<String>,
2348 /// CWE number declared by the catalogue entry. `None` for
2349 /// `client-server-leak`; never fabricated beyond the catalogue's value.
2350 #[serde(default, skip_serializing_if = "Option::is_none")]
2351 pub cwe: Option<u32>,
2352 /// The sink callee (the dangerous function or member path, e.g.
2353 /// `"el.innerHTML"`, `"child_process.exec"`) captured by the catalogue match.
2354 /// `None` for `client-server-leak` and matches that name no callee.
2355 #[serde(default, skip_serializing_if = "Option::is_none")]
2356 pub callee: Option<String>,
2357 /// URL construction shape for SSRF and open-redirect style candidates when
2358 /// fallow can classify whether the origin is fixed or dynamic. Absent for
2359 /// non-URL sinks and unclassified URL expressions.
2360 #[serde(default, skip_serializing_if = "Option::is_none")]
2361 pub url_shape: Option<SecurityUrlShape>,
2362}
2363
2364/// A declared architecture-zone crossing, recovered by correlating a finding's
2365/// anchor against the run's architecture-boundary violations.
2366#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2367#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2368pub struct SecurityZoneCrossing {
2369 /// Zone the importing side belongs to.
2370 pub from: String,
2371 /// Zone the imported side belongs to.
2372 pub to: String,
2373}
2374
2375/// The boundary slot of a [`SecurityCandidate`]: which structural boundaries the
2376/// candidate's flow crosses. A flow that crosses a client/server or module
2377/// boundary is a stronger review target than a self-contained one; the boundary
2378/// is fallow's structural signal over a pure source-sink match.
2379///
2380/// Two further boundary kinds are RESERVED for a follow-up and are deliberately
2381/// absent here rather than emitted as always-false: `export_visibility` (is the
2382/// sink on a publicly-exported symbol?) and a package boundary (does the flow
2383/// cross an npm-package edge?). Both need new graph derivation that does not
2384/// exist today; emitting them as `false` would misreport "we checked and it does
2385/// not cross" when fallow has not checked at all.
2386#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
2387#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2388pub struct SecurityCandidateBoundary {
2389 /// Whether the finding crosses a client/server boundary (a `"use client"`
2390 /// file appears in the trace). True only for `client-server-leak` today;
2391 /// `tainted-sink` candidates carry no client/server marker.
2392 pub client_server: bool,
2393 /// Whether an untrusted source reaches the sink across one or more
2394 /// value-import (module) hops. Derived from the reachability hop count.
2395 pub cross_module: bool,
2396 /// The architecture-zone crossing when the anchor participates in a declared
2397 /// boundary-rule violation in the same run. `None` when it crosses no
2398 /// declared zone boundary.
2399 #[serde(default, skip_serializing_if = "Option::is_none")]
2400 pub architecture_zone: Option<SecurityZoneCrossing>,
2401}
2402
2403/// Network-destination context for a `secret-to-network` candidate (#890): where
2404/// the secret-bearing network call sends its data. Present only on
2405/// network-category candidates. A consuming agent uses it to triage exfil
2406/// (dynamic / untrusted destination) from intended auth (a literal provider
2407/// host) without re-reading source.
2408#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2409#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2410pub struct SecurityNetworkContext {
2411 /// The network call's destination as a static URL string literal, or absent
2412 /// when the destination is DYNAMIC (not a literal). A dynamic destination is
2413 /// the higher-signal exfil case; a literal provider host is usually intended
2414 /// auth.
2415 #[serde(default, skip_serializing_if = "Option::is_none")]
2416 pub destination: Option<String>,
2417}
2418
2419/// An agent-actionable candidate record on a [`SecurityFinding`]. fallow fills
2420/// `source_kind`, `sink`, and `boundary`. The exploitability IMPACT is
2421/// deliberately NOT a field: `severity` on the parent finding is only a
2422/// review-priority tier, while deciding exploitability remains the consuming
2423/// agent's job. A perpetually-null `impact` key would only train consumers to
2424/// ignore it. The agent reads this record, then writes its own impact verdict
2425/// downstream.
2426#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
2427#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2428pub struct SecurityCandidate {
2429 /// The kind of untrusted input that reaches the sink, as a stable catalogue
2430 /// source id (`"http-request-input"`, `"process-env"`, `"process-argv"`,
2431 /// `"message-event-data"`, `"location-input"`, ...). `None`/absent when no
2432 /// untrusted source was matched (always `None` for `client-server-leak`).
2433 /// This is an OPEN string set, driven by the data-driven source catalogue; a
2434 /// consumer should treat an unknown id as "untrusted source of unknown kind"
2435 /// and never drop the candidate on that basis.
2436 #[serde(default, skip_serializing_if = "Option::is_none")]
2437 pub source_kind: Option<String>,
2438 /// The sink the candidate fires on, self-contained so the record is
2439 /// actionable without reading the parent finding.
2440 pub sink: SecurityCandidateSink,
2441 /// The structural boundary the flow crosses.
2442 pub boundary: SecurityCandidateBoundary,
2443 /// Network-destination context, present only on `secret-to-network` (#890)
2444 /// candidates: the host the secret-bearing call targets, so an agent can
2445 /// triage exfil from intended auth. Absent for every other category.
2446 #[serde(default, skip_serializing_if = "Option::is_none")]
2447 pub network: Option<SecurityNetworkContext>,
2448}
2449
2450/// One endpoint (source or sink node) of a [`SecurityTaintFlow`].
2451#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2452#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2453pub struct TaintEndpoint {
2454 /// File of the endpoint. Absolute internally; JSON strips the project root.
2455 #[serde(serialize_with = "serde_path::serialize")]
2456 pub path: PathBuf,
2457 /// 1-based line of the endpoint.
2458 pub line: u32,
2459 /// 0-based byte column of the endpoint.
2460 pub col: u32,
2461}
2462
2463/// Compact taint-flow path shape. The ordered per-hop trace is NOT duplicated
2464/// here: it lives on [`SecurityReachability::untrusted_source_trace`]. This
2465/// carries only the flow's structural summary (intra-module flow plus the
2466/// cross-module hop count) so consumers do not parse two copies of the hops.
2467#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
2468#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2469pub struct TaintPath {
2470 /// Whether the source and sink sit in the same module (no import hop between
2471 /// them); the source-to-sink association is intra-module.
2472 pub intra_module: bool,
2473 /// Number of value-import hops from the untrusted-source module to the sink
2474 /// module. Zero for an intra-module flow.
2475 pub cross_module_hops: u32,
2476}
2477
2478/// A source-to-sink taint-flow triple, emitted only when an untrusted source is
2479/// import-reachable to the sink (`reachability.reachable_from_untrusted_source`).
2480/// The `{ source, sink, path }` shape matches the model agent SAST tooling
2481/// expects (cf. Semgrep `taint_source` / `taint_sink`, SARIF `threadFlows`).
2482#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2483#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2484pub struct SecurityTaintFlow {
2485 /// The untrusted-source endpoint (first hop of the reachability trace).
2486 pub source: TaintEndpoint,
2487 /// The sink endpoint (terminal hop of the reachability trace / the anchor).
2488 pub sink: TaintEndpoint,
2489 /// Compact flow shape: same-module flag plus module hop count. The full
2490 /// ordered path is `reachability.untrusted_source_trace`.
2491 pub path: TaintPath,
2492}
2493
2494/// Runtime coverage state for the function enclosing a security sink.
2495/// This is production-observation evidence, not an exploitability verdict.
2496#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
2497#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2498#[serde(rename_all = "kebab-case")]
2499pub enum SecurityRuntimeState {
2500 /// The sink sits inside a runtime hot path.
2501 RuntimeHot,
2502 /// The sink sits inside a tracked function with zero production invocations.
2503 RuntimeCold,
2504 /// The sink sits inside a tracked function the runtime layer marked as safe
2505 /// to delete because it was never executed.
2506 NeverExecuted,
2507 /// The sink sits inside a function that executed, but below the low-traffic
2508 /// threshold.
2509 LowTraffic,
2510 /// Runtime coverage could not classify the enclosing function.
2511 CoverageUnavailable,
2512 /// A static enclosing function was found, but the runtime report carried no
2513 /// matching evidence for it.
2514 RuntimeUnknown,
2515}
2516
2517/// Runtime coverage context attached to a security candidate when
2518/// `fallow security --runtime-coverage` is supplied.
2519#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2520#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2521pub struct SecurityRuntimeContext {
2522 /// Runtime state for the enclosing function.
2523 pub state: SecurityRuntimeState,
2524 /// Enclosing function name from static extraction.
2525 pub function: String,
2526 /// 1-based line where the enclosing function starts.
2527 pub line: u32,
2528 /// Observed invocation count when the runtime report provides it.
2529 #[serde(default, skip_serializing_if = "Option::is_none")]
2530 pub invocations: Option<u64>,
2531 /// Runtime coverage stable function id, when available.
2532 #[serde(default, skip_serializing_if = "Option::is_none")]
2533 pub stable_id: Option<String>,
2534 /// Short candidate-framed explanation of the runtime evidence.
2535 #[serde(default, skip_serializing_if = "Option::is_none")]
2536 pub evidence: Option<String>,
2537}
2538
2539/// Verification-priority tier for a security candidate. This is ranking, not an
2540/// exploitability verdict.
2541#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
2542#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2543#[serde(rename_all = "lowercase")]
2544pub enum SecuritySeverity {
2545 /// Highest-priority candidate based on reachability, boundary, or runtime-hot signals.
2546 High,
2547 /// Candidate has source-reachability evidence but no high-priority signal.
2548 Medium,
2549 /// Candidate has no source-reachability or boundary signal.
2550 Low,
2551}
2552
2553/// Defensive control found on an attack-surface path.
2554#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2555#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2556pub struct SecurityDefensiveControl {
2557 /// Control family.
2558 pub kind: SecurityControlKind,
2559 /// File of the control site. Absolute internally; JSON strips the project root.
2560 #[serde(serialize_with = "serde_path::serialize")]
2561 pub path: PathBuf,
2562 /// 1-based line of the control site.
2563 pub line: u32,
2564 /// 0-based byte column of the control site.
2565 pub col: u32,
2566 /// Flattened callee path or a stable synthetic guard name.
2567 pub callee: String,
2568}
2569
2570/// Agent-facing defensive-boundary verification context for one surface path.
2571#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2572#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2573pub struct SecurityDefensiveBoundary {
2574 /// Known controls detected along this path.
2575 pub controls: Vec<SecurityDefensiveControl>,
2576 /// Verification question for the consuming agent. It is a prompt, not a
2577 /// missing-guard verdict.
2578 pub verification_prompt: String,
2579}
2580
2581/// One untrusted entry to reachable sink path for `fallow security --surface`.
2582#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2583#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2584pub struct SecurityAttackSurfaceEntry {
2585 /// The untrusted-source endpoint.
2586 pub source: TaintEndpoint,
2587 /// The reachable sink endpoint and catalogue metadata.
2588 pub sink: SecurityCandidateSink,
2589 /// Ordered source to sink path. Same shape as the reachability trace so
2590 /// consumers can reuse existing path handling.
2591 pub path: Vec<TraceHop>,
2592 /// Defensive-boundary context detected on this path.
2593 pub defensive_boundary: SecurityDefensiveBoundary,
2594}
2595
2596/// A local security CANDIDATE for downstream agent verification, NOT a verified
2597/// vulnerability. Emitted only by `fallow security`, never under bare `fallow`
2598/// or the `audit` gate. There is deliberately no `confidence` or
2599/// `signal_strength` field: fallow does not prove exploitability, so the trace
2600/// (its hops and length) is the only honest signal.
2601#[derive(Debug, Clone, Serialize, Deserialize)]
2602#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2603pub struct SecurityFinding {
2604 /// Stable per-finding correlation id, identical across runs for the same
2605 /// rule + anchor path + line. An autonomous agent that triaged this
2606 /// candidate on a prior run uses it to correlate the candidate after a
2607 /// rebase. Equal to the SARIF `partialFingerprints` value for the same
2608 /// finding (one shared helper computes both).
2609 pub finding_id: String,
2610 /// The rule that produced this candidate.
2611 pub kind: SecurityFindingKind,
2612 /// The catalogue category id (e.g. `"dangerous-html"`). `Some` for
2613 /// `TaintedSink`. For `ClientServerLeak` this is `None` for the secret-leak
2614 /// finding, and `Some("server-only-import")` when a `"use client"` cone
2615 /// reaches server-only code.
2616 #[serde(default, skip_serializing_if = "Option::is_none")]
2617 pub category: Option<String>,
2618 /// The CWE number declared by the matched catalogue entry. `None` for
2619 /// `ClientServerLeak`; never fabricated beyond the catalogue's value.
2620 #[serde(default, skip_serializing_if = "Option::is_none")]
2621 pub cwe: Option<u32>,
2622 /// File the finding is anchored on (the client boundary). Absolute
2623 /// internally; JSON strips the project root via `serde_path::serialize`.
2624 #[serde(serialize_with = "serde_path::serialize")]
2625 pub path: PathBuf,
2626 /// 1-based line number of the anchor.
2627 pub line: u32,
2628 /// 0-based byte column offset of the anchor.
2629 pub col: u32,
2630 /// Agent/human-readable evidence (e.g. the named env var the chain reaches).
2631 pub evidence: String,
2632 /// Whether the sink argument was associated with a known untrusted source by
2633 /// the intra-module source-to-sink back-trace (issue #859): a local binding
2634 /// referenced in the argument was sourced from a catalogue source path
2635 /// (`req.query`, `process.argv`, message-event `data`, etc.). `true` ranks
2636 /// the candidate higher and annotates the evidence; `false` does NOT
2637 /// suppress the finding (the association is conservative, never a proof, and
2638 /// fallow prefers false-negatives over false-positives). Always `false` for
2639 /// `ClientServerLeak`. Skipped from JSON when `false` for output stability.
2640 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
2641 pub source_backed: bool,
2642 /// Internal cross-pass carrier (NEVER serialized): the (1-based line, 0-based
2643 /// col) of the arg-level source read, resolved by the detector when
2644 /// `source_backed` is true and a concrete read span was captured. The ranking
2645 /// pass uses it to anchor the taint trace's source node at the real read
2646 /// instead of the module import line. `None` for module-level findings and
2647 /// for arg-level findings with no concrete read span (synthetic
2648 /// framework-param / helper-return sources), where the trace falls back to
2649 /// the sink site.
2650 #[serde(skip)]
2651 pub source_read: Option<(u32, u32)>,
2652 /// Verification-priority tier derived from existing reachability, boundary,
2653 /// source-backed, and runtime signals. Candidate-only: this does not prove
2654 /// exploitability and does not change gates.
2655 pub severity: SecuritySeverity,
2656 /// Structural import-hop trace from the client boundary to the secret source.
2657 /// The hop count is the uncalibrated signal; fallow does not prove the path
2658 /// is exploitable.
2659 pub trace: Vec<TraceHop>,
2660 /// Machine-actionable next steps. Always emitted (possibly empty for
2661 /// forward-compat). For security candidates this is a single file-level
2662 /// suppress hint (`auto_fixable: false`); there is no auto-fix because
2663 /// verification is the agent's job, not fallow's.
2664 pub actions: Vec<IssueAction>,
2665 /// Dead-code cross-link when the same sink candidate sits in code fallow also
2666 /// reports as removable. Agents should verify the dead-code finding and delete
2667 /// the code instead of hardening the sink when deletion is safe.
2668 #[serde(default, skip_serializing_if = "Option::is_none")]
2669 pub dead_code: Option<SecurityDeadCodeContext>,
2670 /// Graph-derived reachability ranking signal (issues #860 and #885). `None`
2671 /// until the post-detection ranking pass fills it; additive on the wire
2672 /// (skipped when absent). Drives the order findings are emitted in:
2673 /// runtime-reachable candidates sort first, followed by source-backed and
2674 /// source-reachable candidates, then wider blast radius.
2675 #[serde(default, skip_serializing_if = "Option::is_none")]
2676 pub reachability: Option<SecurityReachability>,
2677 /// Agent-actionable candidate record: the untrusted input kind, the sink,
2678 /// and the boundary the flow crosses. fallow fills these three slots; the
2679 /// exploitability verdict is the agent's job and is not a field here. Always
2680 /// present.
2681 pub candidate: SecurityCandidate,
2682 /// Source-to-sink taint-flow triple, present only when an untrusted source
2683 /// is import-reachable to this sink. Absent (skipped) otherwise.
2684 #[serde(default, skip_serializing_if = "Option::is_none")]
2685 pub taint_flow: Option<SecurityTaintFlow>,
2686 /// Production runtime coverage context for the function enclosing this
2687 /// security sink. Present only when `fallow security --runtime-coverage`
2688 /// runs and the candidate is a `tainted-sink`.
2689 #[serde(default, skip_serializing_if = "Option::is_none")]
2690 pub runtime: Option<SecurityRuntimeContext>,
2691 /// Internal projection used by `fallow security --surface`. The CLI strips
2692 /// this from per-finding JSON and promotes it to the top-level
2693 /// `attack_surface` field only when requested.
2694 #[serde(default, skip_serializing_if = "Option::is_none")]
2695 pub attack_surface: Option<SecurityAttackSurfaceEntry>,
2696}
2697
2698/// A package manager catalog entry that no workspace package references via
2699/// the `catalog:` protocol.
2700///
2701/// The default catalog uses `catalog_name: "default"`. Named catalogs
2702/// (`catalogs.<name>`) use their declared name. The source file is
2703/// `pnpm-workspace.yaml` for pnpm catalogs or root `package.json` for Bun
2704/// catalogs.
2705#[derive(Debug, Clone, Serialize)]
2706#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2707pub struct UnusedCatalogEntry {
2708 /// Package name declared in the catalog (e.g. `"react"`, `"@scope/lib"`).
2709 pub entry_name: String,
2710 /// Catalog group: `"default"` for the default catalog map, or the named
2711 /// catalog key for entries declared under `catalogs.<name>`.
2712 pub catalog_name: String,
2713 /// Path to the catalog source file, relative to the analyzed root.
2714 #[serde(serialize_with = "serde_path::serialize")]
2715 pub path: PathBuf,
2716 /// 1-based line number of the catalog entry within the source file.
2717 pub line: u32,
2718 /// Workspace `package.json` files that declare the same package with a
2719 /// hardcoded version range instead of `catalog:`. Empty when no consumer
2720 /// uses a hardcoded version. Sorted lexicographically for deterministic
2721 /// output.
2722 #[serde(
2723 default,
2724 serialize_with = "serde_path::serialize_vec",
2725 skip_serializing_if = "Vec::is_empty"
2726 )]
2727 pub hardcoded_consumers: Vec<PathBuf>,
2728}
2729
2730/// A named `catalogs.<name>` group with no package entries.
2731#[derive(Debug, Clone, Serialize)]
2732#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2733pub struct EmptyCatalogGroup {
2734 /// Catalog group name declared under the `catalogs` map.
2735 pub catalog_name: String,
2736 /// Path to the catalog source file, relative to the analyzed root.
2737 #[serde(serialize_with = "serde_path::serialize")]
2738 pub path: PathBuf,
2739 /// 1-based line number of the empty group header within the source file.
2740 pub line: u32,
2741}
2742
2743/// A workspace package.json reference (`catalog:` or `catalog:<name>`) that points
2744/// at a catalog which does not declare the consumed package.
2745///
2746/// Package manager installs error when this happens. fallow surfaces it
2747/// statically so the failure is caught at `fallow dead-code` time, before any
2748/// install.
2749///
2750/// The default catalog (bare `catalog:`) uses `catalog_name: "default"`.
2751/// Named catalogs (`catalog:react17`) use the declared catalog name.
2752#[derive(Debug, Clone, Serialize)]
2753#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2754pub struct UnresolvedCatalogReference {
2755 /// Package name being referenced via the catalog protocol (e.g. `"react"`).
2756 pub entry_name: String,
2757 /// Catalog group the reference points at: `"default"` for bare `catalog:` references,
2758 /// or the named catalog key for `catalog:<name>` references.
2759 pub catalog_name: String,
2760 /// Absolute path to the consumer `package.json`. Matches the storage
2761 /// convention used by every path-anchored finding type (`UnusedFile`,
2762 /// `UnresolvedImport`, `UnusedExport`, etc.) so the shared filtering
2763 /// pipelines (`filter_results_by_changed_files`, per-file overrides,
2764 /// audit attribution) work without a separate root-join pass. JSON
2765 /// output strips the project-root prefix via `serde_path::serialize`.
2766 #[serde(serialize_with = "serde_path::serialize")]
2767 pub path: PathBuf,
2768 /// 1-based line number of the dependency entry in the consumer `package.json`.
2769 pub line: u32,
2770 /// Other catalogs in the same catalog source that DO declare this package.
2771 /// Empty when no catalog has the package. Sorted lexicographically. Lets
2772 /// agents and humans decide whether to switch the reference to a different
2773 /// catalog or to add the entry to the named catalog.
2774 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2775 pub available_in_catalogs: Vec<String>,
2776}
2777
2778/// Where an override entry was declared. Serialized as the filename label
2779/// (`"pnpm-workspace.yaml"` or `"package.json"`) so the value in JSON output
2780/// matches the value users write in `ignoreDependencyOverrides[].source`.
2781#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
2782#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2783pub enum DependencyOverrideSource {
2784 /// Top-level `overrides:` key in `pnpm-workspace.yaml`.
2785 #[serde(rename = "pnpm-workspace.yaml")]
2786 PnpmWorkspaceYaml,
2787 /// `pnpm.overrides` in a root `package.json`.
2788 #[serde(rename = "package.json")]
2789 PnpmPackageJson,
2790}
2791
2792impl DependencyOverrideSource {
2793 /// Stable string label matching the serde rename. Used in baseline keys,
2794 /// audit keys, jq comparisons, and `ignoreDependencyOverrides[].source`.
2795 #[must_use]
2796 pub const fn as_label(&self) -> &'static str {
2797 match self {
2798 Self::PnpmWorkspaceYaml => "pnpm-workspace.yaml",
2799 Self::PnpmPackageJson => "package.json",
2800 }
2801 }
2802}
2803
2804impl std::fmt::Display for DependencyOverrideSource {
2805 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2806 f.write_str(self.as_label())
2807 }
2808}
2809
2810/// An entry in pnpm's `overrides:` map (or the legacy `pnpm.overrides` in
2811/// `package.json`) whose target package is not declared in any workspace
2812/// `package.json` and is not present in `pnpm-lock.yaml`. Projects without a
2813/// readable lockfile fall back to package manifest checks; the `hint` field
2814/// flags that conservative mode.
2815#[derive(Debug, Clone, Serialize)]
2816#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2817pub struct UnusedDependencyOverride {
2818 /// The full original override key as written in the source (e.g.
2819 /// `"react>react-dom"`, `"@types/react@<18"`). Preserved for round-trip
2820 /// reporting so agents see the unmodified spelling.
2821 pub raw_key: String,
2822 /// The target package the override rewrites (e.g. `"react-dom"` for
2823 /// `"react>react-dom"`, `"@types/react"` for `"@types/react@<18"`).
2824 pub target_package: String,
2825 /// Optional parent package (left side of `>`). `None` for bare-target keys.
2826 #[serde(default, skip_serializing_if = "Option::is_none")]
2827 pub parent_package: Option<String>,
2828 /// Optional version selector on the target (e.g. `Some("<18")` for
2829 /// `"@types/react@<18"`).
2830 #[serde(default, skip_serializing_if = "Option::is_none")]
2831 pub version_constraint: Option<String>,
2832 /// The right-hand side of the entry: the version pnpm should force.
2833 pub version_range: String,
2834 /// File the override was declared in. Matches the value users write in
2835 /// `ignoreDependencyOverrides[].source`.
2836 pub source: DependencyOverrideSource,
2837 /// Path to the source file. `pnpm-workspace.yaml` or a `package.json`,
2838 /// stored as an absolute filesystem path so `--changed-since` and
2839 /// per-file `overrides.rules` can compare directly against the analyzer's
2840 /// changed-set / per-path rule lookups. JSON serialization strips the
2841 /// project root via `serde_path::serialize`, matching the
2842 /// `UnresolvedCatalogReference` convention.
2843 #[serde(serialize_with = "serde_path::serialize")]
2844 pub path: PathBuf,
2845 /// 1-based line number of the entry within the source file.
2846 pub line: u32,
2847 /// Soft hint reminding consumers to verify the override before removal.
2848 /// Emitted on every unused-override finding (both bare-target and
2849 /// parent-chain shapes) because projects without a readable lockfile still
2850 /// use the conservative package-manifest fallback.
2851 #[serde(default, skip_serializing_if = "Option::is_none")]
2852 pub hint: Option<String>,
2853}
2854
2855/// Why a dependency-override entry is misconfigured. `pnpm install` would
2856/// either fail at install time or silently no-op on these entries; surfacing
2857/// them statically catches the issue before pnpm does.
2858#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
2859#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2860#[serde(rename_all = "kebab-case")]
2861pub enum DependencyOverrideMisconfigReason {
2862 /// The override key could not be parsed into a recognised pnpm shape
2863 /// (e.g. dangling `>`, missing target, garbage characters).
2864 UnparsableKey,
2865 /// The override value is missing, empty, or contains line breaks.
2866 EmptyValue,
2867}
2868
2869impl DependencyOverrideMisconfigReason {
2870 /// Human-readable summary of the reason.
2871 #[must_use]
2872 pub const fn describe(self) -> &'static str {
2873 match self {
2874 Self::UnparsableKey => "override key cannot be parsed",
2875 Self::EmptyValue => "override value is missing or empty",
2876 }
2877 }
2878}
2879
2880/// An override entry whose key or value is malformed. Default severity is
2881/// `error` because pnpm refuses to install (or silently produces a no-op
2882/// override) when it encounters these shapes.
2883#[derive(Debug, Clone, Serialize)]
2884#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2885pub struct MisconfiguredDependencyOverride {
2886 /// The full original override key as written in the source.
2887 pub raw_key: String,
2888 /// Parsed target package name when the key was syntactically valid (the
2889 /// `EmptyValue` reason path). `None` for `UnparsableKey` findings whose
2890 /// key could not be parsed at all. Used by JSON `add-to-config` actions to
2891 /// emit a paste-ready `ignoreDependencyOverrides` value that matches the
2892 /// suppression matcher (which also keys on `target_package`); avoids the
2893 /// pitfall where `raw_key` like `"react@<18"` would not match the rule
2894 /// that targets package `"react"`.
2895 #[serde(default, skip_serializing_if = "Option::is_none")]
2896 pub target_package: Option<String>,
2897 /// The right-hand side of the entry, exactly as written. Empty when the
2898 /// value was missing.
2899 pub raw_value: String,
2900 /// Classifier for the misconfiguration. 'unparsable-key' = the key is not a
2901 /// valid pnpm shape; 'empty-value' = the value is missing, empty, or
2902 /// contains line breaks.
2903 pub reason: DependencyOverrideMisconfigReason,
2904 /// Where the override entry was declared.
2905 pub source: DependencyOverrideSource,
2906 /// Path to the source file. Stored as an absolute filesystem path so
2907 /// `--changed-since` and per-file `overrides.rules` can compare directly.
2908 /// JSON serialization strips the project root via `serde_path::serialize`.
2909 #[serde(serialize_with = "serde_path::serialize")]
2910 pub path: PathBuf,
2911 /// 1-based line number of the entry within the source file.
2912 pub line: u32,
2913}
2914
2915/// A production dependency that is only imported by test files.
2916/// Since it is never used in production code, it could be moved to devDependencies.
2917#[derive(Debug, Clone, Serialize)]
2918#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2919pub struct TestOnlyDependency {
2920 /// Production dependency that is only imported by test files, consider
2921 /// moving to devDependencies.
2922 pub package_name: String,
2923 /// Path to the package.json where the dependency is listed.
2924 #[serde(serialize_with = "serde_path::serialize")]
2925 pub path: PathBuf,
2926 /// 1-based line number of the dependency entry in package.json.
2927 pub line: u32,
2928}
2929
2930/// One import hop in a circular dependency: the file containing the import
2931/// and where that import statement sits.
2932///
2933/// `edges[i]` is the import IN `path` (the hop SOURCE, equal to the cycle's
2934/// `files[i]`) that points to the NEXT file in the cycle
2935/// (`files[(i + 1) % files.len()]`); the target is not repeated here to keep
2936/// the wire compact. Enables a per-file diagnostic squiggly anchored under
2937/// the offending import rather than a single squiggly on the first file.
2938///
2939/// `col` is a 0-based BYTE column, matching the cycle's top-level `col`;
2940/// converting it to a UTF-16 code-unit column for LSP clients is a tracked
2941/// follow-up shared with the existing field.
2942#[derive(Debug, Clone, Serialize, Deserialize)]
2943#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2944pub struct CircularDependencyEdge {
2945 /// The file containing the import (the hop SOURCE; equal to `files[i]`).
2946 #[serde(serialize_with = "serde_path::serialize")]
2947 pub path: PathBuf,
2948 /// 1-based line number of the import statement pointing to the next file.
2949 pub line: u32,
2950 /// 0-based byte column offset of the import statement.
2951 pub col: u32,
2952}
2953
2954/// A circular dependency chain detected in the module graph.
2955///
2956/// The `line` and `col` fields carry `#[serde(default)]` so callers reading
2957/// historical baseline JSON without these fields can still deserialize the
2958/// struct, but the JSON output layer always emits them (u32 always
2959/// serializes, never via `skip_serializing_if`). The schemars derive sees
2960/// the serde defaults and marks both fields optional in the generated
2961/// schema; the explicit `extend("required" = ...)` override here keeps the
2962/// schema's `required` array honest about what the JSON output actually
2963/// contains.
2964///
2965/// `edges` is deliberately kept OUT of the `required` extend: it is
2966/// `#[serde(default)]` (so historical baseline JSON without it still
2967/// deserializes) and the output layer always emits it, but listing it in
2968/// `required` would make pre-upgrade JSON fail validation against the new
2969/// schema. It is a normal additive field: always present in current output,
2970/// optional for backward compatibility.
2971#[derive(Debug, Clone, Serialize, Deserialize)]
2972#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2973#[cfg_attr(feature = "schema", schemars(extend("required" = ["files", "length", "line", "col"])))]
2974pub struct CircularDependency {
2975 /// Files forming the cycle, in import order.
2976 #[serde(serialize_with = "serde_path::serialize_vec")]
2977 pub files: Vec<PathBuf>,
2978 /// Number of files in the cycle.
2979 pub length: usize,
2980 /// 1-based line number of the import that starts the cycle (in the first file).
2981 #[serde(default)]
2982 pub line: u32,
2983 /// 0-based byte column offset of the import that starts the cycle.
2984 #[serde(default)]
2985 pub col: u32,
2986 /// Per-file import anchors, one entry per hop in cycle order: `edges[i]`
2987 /// is the import in `files[i]` pointing to `files[(i + 1) % len]`. Always
2988 /// the same length as `files`. Drives the per-file LSP diagnostic
2989 /// squiggly. `#[serde(default)]` so pre-`edges` baselines deserialize;
2990 /// always emitted on output but intentionally not in the schema's
2991 /// `required` set (see the struct doc).
2992 #[serde(default)]
2993 pub edges: Vec<CircularDependencyEdge>,
2994 /// Whether this cycle crosses workspace package boundaries.
2995 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
2996 pub is_cross_package: bool,
2997}
2998
2999/// A cycle or self-loop in the re-export edge subgraph.
3000///
3001/// Detected by Tarjan SCC over `(barrel, source)` re-export edges in
3002/// `crates/graph/src/graph/re_exports/`. A multi-node cycle is a strongly
3003/// connected component of size >= 2; a self-loop is a barrel that re-exports
3004/// from itself (often a rename leftover or accidental `export * from './'`).
3005/// Both are structural bugs because chain propagation through the loop is a
3006/// no-op: any symbol consumers think they are re-exporting through the cycle
3007/// silently fails to resolve.
3008#[derive(Debug, Clone, Serialize, Deserialize)]
3009#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3010pub struct ReExportCycle {
3011 /// Files participating in the cycle, sorted lexicographically. For a
3012 /// self-loop, exactly one entry.
3013 #[serde(serialize_with = "serde_path::serialize_vec")]
3014 pub files: Vec<PathBuf>,
3015 /// Which structural shape this finding describes.
3016 pub kind: ReExportCycleKind,
3017}
3018
3019/// Discriminator for [`ReExportCycle`]: which structural shape was detected.
3020#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
3021#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3022#[serde(rename_all = "kebab-case")]
3023pub enum ReExportCycleKind {
3024 /// Two or more barrel files re-export from each other in a loop
3025 /// (SCC of size >= 2).
3026 MultiNode,
3027 /// A single barrel file re-exports from itself.
3028 SelfLoop,
3029}
3030
3031/// An import that crosses an architecture boundary rule.
3032#[derive(Debug, Clone, Serialize)]
3033#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3034pub struct BoundaryViolation {
3035 /// The file making the disallowed import.
3036 #[serde(serialize_with = "serde_path::serialize")]
3037 pub from_path: PathBuf,
3038 /// The file being imported that violates the boundary.
3039 #[serde(serialize_with = "serde_path::serialize")]
3040 pub to_path: PathBuf,
3041 /// The zone the importing file belongs to.
3042 pub from_zone: String,
3043 /// The zone the imported file belongs to.
3044 pub to_zone: String,
3045 /// The raw import specifier from the source file.
3046 pub import_specifier: String,
3047 /// 1-based line number of the import statement in the source file.
3048 pub line: u32,
3049 /// 0-based byte column offset of the import statement.
3050 pub col: u32,
3051}
3052
3053/// A source file that does not match any configured architecture boundary zone.
3054#[derive(Debug, Clone, Serialize)]
3055#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3056pub struct BoundaryCoverageViolation {
3057 /// The unmatched source file.
3058 #[serde(serialize_with = "serde_path::serialize")]
3059 pub path: PathBuf,
3060 /// 1-based line number used for diagnostics.
3061 pub line: u32,
3062 /// 0-based byte column offset used for diagnostics.
3063 pub col: u32,
3064}
3065
3066/// A call from a zoned file to a callee forbidden for that zone via
3067/// `boundaries.calls.forbidden`. One finding is reported per unique callee
3068/// path per file (first occurrence wins).
3069#[derive(Debug, Clone, Serialize)]
3070#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3071pub struct BoundaryCallViolation {
3072 /// The zoned source file making the forbidden call.
3073 #[serde(serialize_with = "serde_path::serialize")]
3074 pub path: PathBuf,
3075 /// 1-based line number of the call site.
3076 pub line: u32,
3077 /// 0-based byte column offset of the call site.
3078 pub col: u32,
3079 /// The zone the calling file is classified into.
3080 pub zone: String,
3081 /// The callee path as written at the call site (e.g. `cp.exec`).
3082 pub callee: String,
3083 /// The configured pattern that matched (e.g. `child_process.*`), so
3084 /// consumers can see both the written path and the rule that fired.
3085 pub pattern: String,
3086}
3087
3088/// Which rule-pack rule kind produced a [`PolicyViolation`].
3089#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
3090#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3091#[serde(rename_all = "kebab-case")]
3092pub enum PolicyRuleKind {
3093 /// A call site matched a `banned-call` rule's callee patterns.
3094 BannedCall,
3095 /// An import or re-export specifier matched a `banned-import` rule.
3096 BannedImport,
3097 /// A call site matched a catalogue-derived `banned-effect` rule.
3098 BannedEffect,
3099}
3100
3101/// Effective severity of a single [`PolicyViolation`]. Per-rule `severity`
3102/// overrides the `rules."policy-violation"` master; `off` rules emit nothing,
3103/// so only `error` and `warn` appear on the wire. The exit-code gate inspects
3104/// this per-finding value, not the master severity.
3105#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
3106#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3107#[serde(rename_all = "lowercase")]
3108pub enum PolicyViolationSeverity {
3109 /// Fails CI (non-zero exit code).
3110 Error,
3111 /// Reported without failing CI.
3112 Warn,
3113}
3114
3115/// A banned call, banned import, or banned effect matched by a declarative rule
3116/// pack (`rulePacks` config). Banned-call and banned-effect findings report
3117/// one entry per unique callee path per file (first occurrence wins, matching
3118/// `boundary_call_violations`); banned-import findings anchor at each matching
3119/// import or re-export declaration.
3120#[derive(Debug, Clone, Serialize)]
3121#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3122pub struct PolicyViolation {
3123 /// The source file containing the banned call, import, or effectful usage.
3124 #[serde(serialize_with = "serde_path::serialize")]
3125 pub path: PathBuf,
3126 /// 1-based line number of the call site or import declaration.
3127 pub line: u32,
3128 /// 0-based byte column offset of the call site or import declaration.
3129 pub col: u32,
3130 /// Name of the rule pack that declared the matching rule.
3131 pub pack: String,
3132 /// Id of the matching rule inside the pack. `pack` plus `rule_id` is the
3133 /// finding's policy identity.
3134 pub rule_id: String,
3135 /// Which rule kind matched.
3136 pub kind: PolicyRuleKind,
3137 /// What matched: the written callee path for `banned-call` (e.g.
3138 /// `cp.exec`), the raw import specifier for `banned-import` (e.g.
3139 /// `moment/locale/nl`), or `<effect>: <callee>` for `banned-effect`.
3140 pub matched: String,
3141 /// Effective severity for this finding (per-rule `severity`, else the
3142 /// `rules."policy-violation"` master).
3143 pub severity: PolicyViolationSeverity,
3144 /// The rule's author-provided message, when set.
3145 #[serde(default, skip_serializing_if = "Option::is_none")]
3146 pub message: Option<String>,
3147}
3148
3149/// The origin of a stale suppression: inline comment or JSDoc tag.
3150#[derive(Debug, Clone, Serialize)]
3151#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3152#[serde(rename_all = "snake_case", tag = "type")]
3153pub enum SuppressionOrigin {
3154 /// A `// fallow-ignore-next-line` or `// fallow-ignore-file` comment.
3155 Comment {
3156 /// The issue kind token from the comment (e.g., "unused-exports"), or None for blanket.
3157 #[serde(default, skip_serializing_if = "Option::is_none")]
3158 issue_kind: Option<String>,
3159 /// Human-authored reason after `--`, when present.
3160 #[serde(default, skip_serializing_if = "Option::is_none")]
3161 reason: Option<String>,
3162 /// Whether this was a file-level suppression.
3163 is_file_level: bool,
3164 /// Whether `issue_kind` parses to a known `IssueKind`. False when the
3165 /// token is a typo or refers to a kind that was renamed or removed in
3166 /// a newer fallow release. JSON consumers (CI annotations, MCP agents,
3167 /// VS Code) branch on this to choose the right next-step text.
3168 /// Omitted from the wire when `true` so producers that have not yet
3169 /// adopted the field stay byte-compatible. See issue #449.
3170 #[serde(default = "default_true", skip_serializing_if = "is_true")]
3171 kind_known: bool,
3172 },
3173 /// An `@expected-unused` JSDoc tag on an export.
3174 JsdocTag {
3175 /// The name of the export that was tagged.
3176 export_name: String,
3177 /// Human-authored reason after `--`, when present.
3178 #[serde(default, skip_serializing_if = "Option::is_none")]
3179 reason: Option<String>,
3180 },
3181}
3182
3183#[expect(
3184 clippy::trivially_copy_pass_by_ref,
3185 reason = "serde skip_serializing_if takes a reference by contract"
3186)]
3187const fn is_true(b: &bool) -> bool {
3188 *b
3189}
3190
3191/// Default for `SuppressionOrigin::Comment.kind_known` when the field is
3192/// absent from a deserialized payload, paired with `skip_serializing_if = is_true`
3193/// so schemars marks the field non-required in the generated JSON Schema AND
3194/// the absent case round-trips to the recognized-kind interpretation.
3195/// Referenced by the always-emitted `#[serde(default = "default_true")]`
3196/// attribute. Today `SuppressionOrigin` derives only `Serialize`, so serde
3197/// itself never calls this; schemars (under the `schema` feature) reads the
3198/// attribute textually to mark `kind_known` non-required. The `cfg_attr`
3199/// applies `#[expect(dead_code)]` only on builds WITHOUT the `schema` feature
3200/// (where the function is genuinely dead): under the feature schemars
3201/// references it, the lint does not fire, and an unconditional `#[expect]`
3202/// would be unfulfilled. The function stays un-gated so a future
3203/// `Deserialize` derive on `SuppressionOrigin` does not produce a missing-
3204/// function compile error on non-`schema` builds.
3205#[cfg_attr(
3206 not(feature = "schema"),
3207 expect(
3208 dead_code,
3209 reason = "referenced via #[serde(default = ...)]; only consumed by schemars under the `schema` feature, dead on default builds today"
3210 )
3211)]
3212const fn default_true() -> bool {
3213 true
3214}
3215
3216/// A suppression comment or JSDoc tag that no longer matches any issue.
3217#[derive(Debug, Clone, Serialize)]
3218#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3219pub struct StaleSuppression {
3220 /// File containing the stale suppression.
3221 #[serde(serialize_with = "serde_path::serialize")]
3222 pub path: PathBuf,
3223 /// 1-based line number of the suppression comment or tag.
3224 pub line: u32,
3225 /// 0-based byte column offset.
3226 pub col: u32,
3227 /// The origin and details of the stale suppression.
3228 pub origin: SuppressionOrigin,
3229 /// True when `rules.require-suppression-reason` reported a suppression
3230 /// comment or tag that has no reason.
3231 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
3232 pub missing_reason: bool,
3233 /// Suggested next steps. Always emitted.
3234 pub actions: Vec<IssueAction>,
3235}
3236
3237impl StaleSuppression {
3238 /// Build the typed action list for this suppression finding.
3239 #[must_use]
3240 pub fn actions_for(missing_reason: bool) -> Vec<IssueAction> {
3241 let (kind, description) = if missing_reason {
3242 (
3243 FixActionType::AddSuppressionReason,
3244 "Add a human-authored reason after `--` on the suppression",
3245 )
3246 } else {
3247 (
3248 FixActionType::RemoveStaleSuppression,
3249 "Remove or update the stale suppression",
3250 )
3251 };
3252 let mut actions = vec![IssueAction::Fix(FixAction {
3253 kind,
3254 auto_fixable: false,
3255 description: description.to_string(),
3256 note: None,
3257 available_in_catalogs: None,
3258 suggested_target: None,
3259 })];
3260 if !missing_reason {
3261 actions.push(IssueAction::SuppressLine(SuppressLineAction {
3262 kind: SuppressLineKind::SuppressLine,
3263 auto_fixable: false,
3264 description:
3265 "Suppress this stale suppression finding with a comment above the suppression"
3266 .to_string(),
3267 comment: "// fallow-ignore-next-line stale-suppression".to_string(),
3268 scope: Some(SuppressLineScope::PerLocation),
3269 }));
3270 }
3271 actions
3272 }
3273
3274 /// Produce a human-readable description of this stale suppression.
3275 #[must_use]
3276 pub fn description(&self) -> String {
3277 match &self.origin {
3278 SuppressionOrigin::Comment {
3279 issue_kind,
3280 reason,
3281 is_file_level,
3282 ..
3283 } => {
3284 let directive = if *is_file_level {
3285 "fallow-ignore-file"
3286 } else {
3287 "fallow-ignore-next-line"
3288 };
3289 match issue_kind {
3290 Some(kind) => match reason {
3291 Some(reason) => format!("// {directive} {kind} -- {reason}"),
3292 None => format!("// {directive} {kind}"),
3293 },
3294 None => match reason {
3295 Some(reason) => format!("// {directive} -- {reason}"),
3296 None => format!("// {directive}"),
3297 },
3298 }
3299 }
3300 SuppressionOrigin::JsdocTag {
3301 export_name,
3302 reason,
3303 } => match reason {
3304 Some(reason) => format!("@expected-unused on {export_name} -- {reason}"),
3305 None => format!("@expected-unused on {export_name}"),
3306 },
3307 }
3308 }
3309
3310 /// Produce an explanation of why this suppression is stale.
3311 ///
3312 /// For comment suppressions where `kind_known == false`, surfaces the
3313 /// unknown token plus a Levenshtein "did you mean?" hint when one is
3314 /// within edit distance 2. Other tokens on the same comment line still
3315 /// apply normally (see issue #449).
3316 #[must_use]
3317 pub fn explanation(&self) -> String {
3318 match &self.origin {
3319 SuppressionOrigin::Comment {
3320 issue_kind,
3321 is_file_level,
3322 kind_known,
3323 ..
3324 } => {
3325 if self.missing_reason {
3326 return "suppression is missing a reason".to_string();
3327 }
3328 let scope = if *is_file_level {
3329 "in this file"
3330 } else {
3331 "on the next line"
3332 };
3333 match issue_kind {
3334 Some(kind) if !*kind_known => match closest_known_kind_name(kind) {
3335 Some(suggestion) => format!(
3336 "'{kind}' is not a recognized fallow issue kind. Did you mean '{suggestion}'? Other tokens on this line still apply."
3337 ),
3338 None => format!(
3339 "'{kind}' is not a recognized fallow issue kind. Other tokens on this line still apply."
3340 ),
3341 },
3342 Some(kind) => format!("no {kind} issue found {scope}"),
3343 None => format!("no issues found {scope}"),
3344 }
3345 }
3346 SuppressionOrigin::JsdocTag { export_name, .. } => {
3347 if self.missing_reason {
3348 return "suppression is missing a reason".to_string();
3349 }
3350 format!("{export_name} is now used")
3351 }
3352 }
3353 }
3354
3355 /// The suppressed `IssueKind`, if this was a comment suppression with a specific known kind.
3356 ///
3357 /// Returns `None` for unknown-kind comments (`kind_known == false`) and
3358 /// for JSDoc tags.
3359 #[must_use]
3360 pub fn suppressed_kind(&self) -> Option<IssueKind> {
3361 match &self.origin {
3362 SuppressionOrigin::Comment {
3363 issue_kind,
3364 kind_known: true,
3365 ..
3366 } => issue_kind.as_deref().and_then(IssueKind::parse),
3367 SuppressionOrigin::Comment { .. } | SuppressionOrigin::JsdocTag { .. } => None,
3368 }
3369 }
3370
3371 /// Per-format display message combining `description()` and `explanation()`
3372 /// for the unknown-kind case so SARIF, CodeClimate, and compact consumers
3373 /// surface the typo-fix copy and Levenshtein hint without needing to
3374 /// branch on `origin.kind_known` themselves. Stale-but-known and JSDoc
3375 /// origins keep the bare `description()` so existing wire bytes stay
3376 /// unchanged. See issue #449.
3377 #[must_use]
3378 pub fn display_message(&self) -> String {
3379 match &self.origin {
3380 SuppressionOrigin::Comment {
3381 kind_known: false, ..
3382 } => format!("{} ({})", self.description(), self.explanation()),
3383 SuppressionOrigin::Comment { .. } | SuppressionOrigin::JsdocTag { .. }
3384 if self.missing_reason =>
3385 {
3386 format!("{} ({})", self.description(), self.explanation())
3387 }
3388 SuppressionOrigin::Comment { .. } | SuppressionOrigin::JsdocTag { .. } => {
3389 self.description()
3390 }
3391 }
3392 }
3393}
3394
3395/// A suppression comment present in an analyzed file this run.
3396///
3397/// This is the "active-suppression state" the Fallow Impact value report needs
3398/// to tell a genuinely resolved finding (the code was fixed) from one merely
3399/// silenced by a newly-added `fallow-ignore`. It captures every PRESENT marker,
3400/// not only the ones a detector consumed: complexity and code-duplication
3401/// suppressions are consumed in the CLI layer rather than the core suppression
3402/// context, so presence is the single uniform signal that covers all impact
3403/// categories. A present-but-stale marker is harmless because impact keys on a
3404/// suppression that newly appeared between two recorded runs. It is internal:
3405/// never serialized into the public JSON output schema (the field on
3406/// [`AnalysisResults`] is `#[serde(skip)]`), only read in-process by
3407/// `fallow impact`.
3408#[derive(Debug, Clone)]
3409pub struct ActiveSuppression {
3410 /// Absolute path to the file carrying the suppression comment.
3411 pub path: PathBuf,
3412 /// The suppressed issue kind in kebab-case (e.g. `"unused-export"`), or
3413 /// `None` for a blanket marker that suppresses every kind on its target.
3414 pub kind: Option<String>,
3415 /// Whether this is a `fallow-ignore-file` (file-level) marker rather than a
3416 /// `fallow-ignore-next-line` marker.
3417 pub is_file_level: bool,
3418 /// Human-authored reason after `--`, when present.
3419 pub reason: Option<String>,
3420}
3421
3422/// The detection method used to identify a feature flag.
3423#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
3424#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3425#[serde(rename_all = "snake_case")]
3426pub enum FlagKind {
3427 /// Environment variable check (e.g., `process.env.FEATURE_X`).
3428 EnvironmentVariable,
3429 /// Feature flag SDK call (e.g., `useFlag('name')`, `variation('name', false)`).
3430 SdkCall,
3431 /// Config object property access (e.g., `config.features.newCheckout`).
3432 ConfigObject,
3433}
3434
3435/// Detection confidence for a feature flag finding.
3436#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
3437#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3438#[serde(rename_all = "snake_case")]
3439pub enum FlagConfidence {
3440 /// Low confidence: heuristic match (config object patterns).
3441 Low,
3442 /// Medium confidence: pattern match with some ambiguity.
3443 Medium,
3444 /// High confidence: unambiguous pattern (env vars, direct SDK calls).
3445 High,
3446}
3447
3448/// A detected feature flag use site.
3449#[derive(Debug, Clone, Serialize)]
3450#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3451pub struct FeatureFlag {
3452 /// File containing the feature flag usage.
3453 #[serde(serialize_with = "serde_path::serialize")]
3454 pub path: PathBuf,
3455 /// Name or identifier of the flag (e.g., `ENABLE_NEW_CHECKOUT`, `new-checkout`).
3456 pub flag_name: String,
3457 /// How the flag was detected.
3458 pub kind: FlagKind,
3459 /// Detection confidence level.
3460 pub confidence: FlagConfidence,
3461 /// 1-based line number.
3462 pub line: u32,
3463 /// 0-based byte column offset.
3464 pub col: u32,
3465 /// Start byte offset of the guarded code block (if-branch span), if detected.
3466 #[serde(skip)]
3467 pub guard_span_start: Option<u32>,
3468 /// End byte offset of the guarded code block (if-branch span), if detected.
3469 #[serde(skip)]
3470 pub guard_span_end: Option<u32>,
3471 /// SDK or provider name (e.g., "LaunchDarkly", "Statsig"), if detected from SDK call.
3472 #[serde(default, skip_serializing_if = "Option::is_none")]
3473 pub sdk_name: Option<String>,
3474 /// Line range of the guarded code block (derived from guard_span + line_offsets).
3475 /// Used for cross-reference with dead code findings.
3476 #[serde(skip)]
3477 pub guard_line_start: Option<u32>,
3478 /// End line of the guarded code block.
3479 #[serde(skip)]
3480 pub guard_line_end: Option<u32>,
3481 /// Unused exports found within the guarded code block.
3482 /// Populated by cross-reference with dead code analysis.
3483 #[serde(default, skip_serializing_if = "Vec::is_empty")]
3484 pub guarded_dead_exports: Vec<String>,
3485}
3486
3487// Size assertion: FeatureFlag is stored in a Vec per analysis run.
3488const _: () = assert!(std::mem::size_of::<FeatureFlag>() <= 160);
3489
3490/// Usage count for an export symbol. Used by the LSP Code Lens to show
3491/// reference counts above each export declaration.
3492#[derive(Debug, Clone, Serialize)]
3493#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3494pub struct ExportUsage {
3495 /// File containing the export.
3496 #[serde(serialize_with = "serde_path::serialize")]
3497 pub path: PathBuf,
3498 /// Name of the exported symbol.
3499 pub export_name: String,
3500 /// 1-based line number.
3501 pub line: u32,
3502 /// 0-based byte column offset.
3503 pub col: u32,
3504 /// Number of files that reference this export.
3505 pub reference_count: usize,
3506 /// Locations where this export is referenced. Used by the LSP Code Lens
3507 /// to enable click-to-navigate via `editor.action.showReferences`.
3508 pub reference_locations: Vec<ReferenceLocation>,
3509}
3510
3511/// A location where an export is referenced (import site in another file).
3512#[derive(Debug, Clone, Serialize)]
3513#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
3514pub struct ReferenceLocation {
3515 /// File containing the import that references the export.
3516 #[serde(serialize_with = "serde_path::serialize")]
3517 pub path: PathBuf,
3518 /// 1-based line number.
3519 pub line: u32,
3520 /// 0-based byte column offset.
3521 pub col: u32,
3522}
3523
3524#[cfg(test)]
3525mod tests {
3526 use super::*;
3527 use crate::output_dead_code::{
3528 BoundaryViolationFinding, CircularDependencyFinding, UnresolvedImportFinding,
3529 UnusedClassMemberFinding, UnusedEnumMemberFinding, UnusedExportFinding, UnusedFileFinding,
3530 UnusedTypeFinding,
3531 };
3532
3533 #[test]
3534 fn empty_results_no_issues() {
3535 let results = AnalysisResults::default();
3536 assert_eq!(results.total_issues(), 0);
3537 assert!(!results.has_issues());
3538 }
3539
3540 #[test]
3541 fn results_with_unused_file() {
3542 let mut results = AnalysisResults::default();
3543 results
3544 .unused_files
3545 .push(UnusedFileFinding::with_actions(UnusedFile {
3546 path: PathBuf::from("test.ts"),
3547 }));
3548 assert_eq!(results.total_issues(), 1);
3549 assert!(results.has_issues());
3550 }
3551
3552 #[test]
3553 fn results_with_unused_export() {
3554 let mut results = AnalysisResults::default();
3555 results
3556 .unused_exports
3557 .push(UnusedExportFinding::with_actions(UnusedExport {
3558 path: PathBuf::from("test.ts"),
3559 export_name: "foo".to_string(),
3560 is_type_only: false,
3561 line: 1,
3562 col: 0,
3563 span_start: 0,
3564 is_re_export: false,
3565 }));
3566 assert_eq!(results.total_issues(), 1);
3567 assert!(results.has_issues());
3568 }
3569
3570 #[test]
3571 fn merge_into_appends_counts_and_preserves_existing_optional_metadata() {
3572 let mut target = AnalysisResults {
3573 unused_files: vec![UnusedFileFinding::with_actions(UnusedFile {
3574 path: PathBuf::from("a.ts"),
3575 })],
3576 suppression_count: 2,
3577 security_unresolved_edge_files: 1,
3578 security_unresolved_callee_sites: 3,
3579 entry_point_summary: Some(EntryPointSummary {
3580 total: 1,
3581 by_source: vec![("existing".to_string(), 1)],
3582 }),
3583 ..AnalysisResults::default()
3584 };
3585 let source = AnalysisResults {
3586 unused_files: vec![UnusedFileFinding::with_actions(UnusedFile {
3587 path: PathBuf::from("b.ts"),
3588 })],
3589 suppression_count: 4,
3590 security_unresolved_edge_files: 5,
3591 security_unresolved_callee_sites: 6,
3592 unused_load_data_keys_global_abstain: true,
3593 entry_point_summary: Some(EntryPointSummary {
3594 total: 1,
3595 by_source: vec![("incoming".to_string(), 1)],
3596 }),
3597 render_fan_in: Some(RenderFanInMetric::default()),
3598 ..AnalysisResults::default()
3599 };
3600
3601 target.merge_into(source);
3602
3603 assert_eq!(target.unused_files.len(), 2);
3604 assert_eq!(target.suppression_count, 6);
3605 assert_eq!(target.security_unresolved_edge_files, 6);
3606 assert_eq!(target.security_unresolved_callee_sites, 9);
3607 assert!(target.unused_load_data_keys_global_abstain);
3608 assert_eq!(
3609 target
3610 .entry_point_summary
3611 .as_ref()
3612 .map(|summary| summary.total),
3613 Some(1)
3614 );
3615 assert_eq!(
3616 target
3617 .entry_point_summary
3618 .as_ref()
3619 .and_then(|summary| summary.by_source.first())
3620 .map(|(name, _)| name.as_str()),
3621 Some("existing")
3622 );
3623 assert!(target.render_fan_in.is_some());
3624 }
3625
3626 fn test_unused_export(path: &str, export_name: &str, is_type_only: bool) -> UnusedExport {
3627 UnusedExport {
3628 path: PathBuf::from(path),
3629 export_name: export_name.to_string(),
3630 is_type_only,
3631 line: 1,
3632 col: 0,
3633 span_start: 0,
3634 is_re_export: false,
3635 }
3636 }
3637
3638 fn test_unused_dependency(
3639 package_name: &str,
3640 location: DependencyLocation,
3641 ) -> UnusedDependency {
3642 UnusedDependency {
3643 package_name: package_name.to_string(),
3644 location,
3645 path: PathBuf::from("package.json"),
3646 line: 5,
3647 used_in_workspaces: Vec::new(),
3648 }
3649 }
3650
3651 fn test_unused_member(member_name: &str, kind: MemberKind) -> UnusedMember {
3652 UnusedMember {
3653 path: PathBuf::from("members.ts"),
3654 parent_name: "Parent".to_string(),
3655 member_name: member_name.to_string(),
3656 kind,
3657 line: 1,
3658 col: 0,
3659 }
3660 }
3661
3662 #[test]
3663 fn results_total_counts_all_types() {
3664 let results = AnalysisResults {
3665 unused_files: vec![UnusedFileFinding::with_actions(UnusedFile {
3666 path: PathBuf::from("a.ts"),
3667 })],
3668 unused_exports: vec![UnusedExportFinding::with_actions(test_unused_export(
3669 "b.ts", "x", false,
3670 ))],
3671 unused_types: vec![UnusedTypeFinding::with_actions(test_unused_export(
3672 "c.ts", "T", true,
3673 ))],
3674 unused_dependencies: vec![UnusedDependencyFinding::with_actions(
3675 test_unused_dependency("dep", DependencyLocation::Dependencies),
3676 )],
3677 unused_dev_dependencies: vec![UnusedDevDependencyFinding::with_actions(
3678 test_unused_dependency("dev", DependencyLocation::DevDependencies),
3679 )],
3680 unused_enum_members: vec![UnusedEnumMemberFinding::with_actions(test_unused_member(
3681 "A",
3682 MemberKind::EnumMember,
3683 ))],
3684 unused_class_members: vec![UnusedClassMemberFinding::with_actions(test_unused_member(
3685 "m",
3686 MemberKind::ClassMethod,
3687 ))],
3688 unresolved_imports: vec![UnresolvedImportFinding::with_actions(UnresolvedImport {
3689 path: PathBuf::from("f.ts"),
3690 specifier: "./missing".to_string(),
3691 line: 1,
3692 col: 0,
3693 specifier_col: 0,
3694 })],
3695 unlisted_dependencies: vec![UnlistedDependencyFinding::with_actions(
3696 UnlistedDependency {
3697 package_name: "unlisted".to_string(),
3698 imported_from: vec![ImportSite {
3699 path: PathBuf::from("g.ts"),
3700 line: 1,
3701 col: 0,
3702 }],
3703 },
3704 )],
3705 duplicate_exports: vec![DuplicateExportFinding::with_actions(DuplicateExport {
3706 export_name: "dup".to_string(),
3707 locations: vec![
3708 DuplicateLocation {
3709 path: PathBuf::from("h.ts"),
3710 line: 15,
3711 col: 0,
3712 },
3713 DuplicateLocation {
3714 path: PathBuf::from("i.ts"),
3715 line: 30,
3716 col: 0,
3717 },
3718 ],
3719 })],
3720 unused_optional_dependencies: vec![UnusedOptionalDependencyFinding::with_actions(
3721 test_unused_dependency("optional", DependencyLocation::OptionalDependencies),
3722 )],
3723 type_only_dependencies: vec![TypeOnlyDependencyFinding::with_actions(
3724 TypeOnlyDependency {
3725 package_name: "type-only".to_string(),
3726 path: PathBuf::from("package.json"),
3727 line: 8,
3728 },
3729 )],
3730 test_only_dependencies: vec![TestOnlyDependencyFinding::with_actions(
3731 TestOnlyDependency {
3732 package_name: "test-only".to_string(),
3733 path: PathBuf::from("package.json"),
3734 line: 9,
3735 },
3736 )],
3737 circular_dependencies: vec![CircularDependencyFinding::with_actions(
3738 CircularDependency {
3739 files: vec![PathBuf::from("a.ts"), PathBuf::from("b.ts")],
3740 length: 2,
3741 line: 3,
3742 col: 0,
3743 edges: Vec::new(),
3744 is_cross_package: false,
3745 },
3746 )],
3747 boundary_violations: vec![BoundaryViolationFinding::with_actions(BoundaryViolation {
3748 from_path: PathBuf::from("src/ui/Button.tsx"),
3749 to_path: PathBuf::from("src/db/queries.ts"),
3750 from_zone: "ui".to_string(),
3751 to_zone: "database".to_string(),
3752 import_specifier: "../db/queries".to_string(),
3753 line: 3,
3754 col: 0,
3755 })],
3756 ..Default::default()
3757 };
3758
3759 // 15 categories, one of each
3760 assert_eq!(results.total_issues(), 15);
3761 assert!(results.has_issues());
3762 }
3763
3764 // ── total_issues / has_issues consistency ──────────────────
3765
3766 #[test]
3767 fn total_issues_and_has_issues_are_consistent() {
3768 let results = AnalysisResults::default();
3769 assert_eq!(results.total_issues(), 0);
3770 assert!(!results.has_issues());
3771 assert_eq!(results.total_issues() > 0, results.has_issues());
3772 }
3773
3774 // ── total_issues counts each category independently ─────────
3775
3776 #[test]
3777 fn total_issues_sums_all_categories_independently() {
3778 let mut results = AnalysisResults::default();
3779 results
3780 .unused_files
3781 .push(UnusedFileFinding::with_actions(UnusedFile {
3782 path: PathBuf::from("a.ts"),
3783 }));
3784 assert_eq!(results.total_issues(), 1);
3785
3786 results
3787 .unused_files
3788 .push(UnusedFileFinding::with_actions(UnusedFile {
3789 path: PathBuf::from("b.ts"),
3790 }));
3791 assert_eq!(results.total_issues(), 2);
3792
3793 results
3794 .unresolved_imports
3795 .push(UnresolvedImportFinding::with_actions(UnresolvedImport {
3796 path: PathBuf::from("c.ts"),
3797 specifier: "./missing".to_string(),
3798 line: 1,
3799 col: 0,
3800 specifier_col: 0,
3801 }));
3802 assert_eq!(results.total_issues(), 3);
3803 }
3804
3805 // ── default is truly empty ──────────────────────────────────
3806
3807 #[test]
3808 fn default_results_all_fields_empty() {
3809 let r = AnalysisResults::default();
3810 assert!(r.unused_files.is_empty());
3811 assert!(r.unused_exports.is_empty());
3812 assert!(r.unused_types.is_empty());
3813 assert!(r.unused_dependencies.is_empty());
3814 assert!(r.unused_dev_dependencies.is_empty());
3815 assert!(r.unused_optional_dependencies.is_empty());
3816 assert!(r.unused_enum_members.is_empty());
3817 assert!(r.unused_class_members.is_empty());
3818 assert!(r.unresolved_imports.is_empty());
3819 assert!(r.unlisted_dependencies.is_empty());
3820 assert!(r.duplicate_exports.is_empty());
3821 assert!(r.type_only_dependencies.is_empty());
3822 assert!(r.test_only_dependencies.is_empty());
3823 assert!(r.circular_dependencies.is_empty());
3824 assert!(r.boundary_violations.is_empty());
3825 assert!(r.unused_catalog_entries.is_empty());
3826 assert!(r.unresolved_catalog_references.is_empty());
3827 assert!(r.export_usages.is_empty());
3828 }
3829
3830 // ── EntryPointSummary ────────────────────────────────────────
3831
3832 #[test]
3833 fn entry_point_summary_default() {
3834 let summary = EntryPointSummary::default();
3835 assert_eq!(summary.total, 0);
3836 assert!(summary.by_source.is_empty());
3837 }
3838
3839 #[test]
3840 fn entry_point_summary_not_in_default_results() {
3841 let r = AnalysisResults::default();
3842 assert!(r.entry_point_summary.is_none());
3843 }
3844
3845 #[test]
3846 fn entry_point_summary_some_preserves_data() {
3847 let r = AnalysisResults {
3848 entry_point_summary: Some(EntryPointSummary {
3849 total: 5,
3850 by_source: vec![("package.json".to_string(), 2), ("plugin".to_string(), 3)],
3851 }),
3852 ..AnalysisResults::default()
3853 };
3854 let summary = r.entry_point_summary.as_ref().unwrap();
3855 assert_eq!(summary.total, 5);
3856 assert_eq!(summary.by_source.len(), 2);
3857 assert_eq!(summary.by_source[0], ("package.json".to_string(), 2));
3858 }
3859
3860 // ── sort: unused_files by path ──────────────────────────────
3861
3862 #[test]
3863 fn sort_unused_files_by_path() {
3864 let mut r = AnalysisResults::default();
3865 r.unused_files
3866 .push(UnusedFileFinding::with_actions(UnusedFile {
3867 path: PathBuf::from("z.ts"),
3868 }));
3869 r.unused_files
3870 .push(UnusedFileFinding::with_actions(UnusedFile {
3871 path: PathBuf::from("a.ts"),
3872 }));
3873 r.unused_files
3874 .push(UnusedFileFinding::with_actions(UnusedFile {
3875 path: PathBuf::from("m.ts"),
3876 }));
3877 r.sort();
3878 let paths: Vec<_> = r
3879 .unused_files
3880 .iter()
3881 .map(|f| f.file.path.to_string_lossy().to_string())
3882 .collect();
3883 assert_eq!(paths, vec!["a.ts", "m.ts", "z.ts"]);
3884 }
3885
3886 // ── sort: unused_exports by path, line, name ────────────────
3887
3888 #[test]
3889 fn sort_unused_exports_by_path_line_name() {
3890 let mut r = AnalysisResults::default();
3891 let mk = |path: &str, line: u32, name: &str| {
3892 UnusedExportFinding::with_actions(UnusedExport {
3893 path: PathBuf::from(path),
3894 export_name: name.to_string(),
3895 is_type_only: false,
3896 line,
3897 col: 0,
3898 span_start: 0,
3899 is_re_export: false,
3900 })
3901 };
3902 r.unused_exports.push(mk("b.ts", 5, "beta"));
3903 r.unused_exports.push(mk("a.ts", 10, "zeta"));
3904 r.unused_exports.push(mk("a.ts", 10, "alpha"));
3905 r.unused_exports.push(mk("a.ts", 1, "gamma"));
3906 r.sort();
3907 let keys: Vec<_> = r
3908 .unused_exports
3909 .iter()
3910 .map(|e| {
3911 format!(
3912 "{}:{}:{}",
3913 e.export.path.to_string_lossy(),
3914 e.export.line,
3915 e.export.export_name
3916 )
3917 })
3918 .collect();
3919 assert_eq!(
3920 keys,
3921 vec![
3922 "a.ts:1:gamma",
3923 "a.ts:10:alpha",
3924 "a.ts:10:zeta",
3925 "b.ts:5:beta"
3926 ]
3927 );
3928 }
3929
3930 // ── sort: unused_types (same sort as unused_exports) ────────
3931
3932 #[test]
3933 fn sort_unused_types_by_path_line_name() {
3934 let mut r = AnalysisResults::default();
3935 let mk = |path: &str, line: u32, name: &str| {
3936 UnusedTypeFinding::with_actions(UnusedExport {
3937 path: PathBuf::from(path),
3938 export_name: name.to_string(),
3939 is_type_only: true,
3940 line,
3941 col: 0,
3942 span_start: 0,
3943 is_re_export: false,
3944 })
3945 };
3946 r.unused_types.push(mk("z.ts", 1, "Z"));
3947 r.unused_types.push(mk("a.ts", 1, "A"));
3948 r.sort();
3949 assert_eq!(r.unused_types[0].export.path, PathBuf::from("a.ts"));
3950 assert_eq!(r.unused_types[1].export.path, PathBuf::from("z.ts"));
3951 }
3952
3953 // ── sort: unused_dependencies by path, line, name ───────────
3954
3955 #[test]
3956 fn sort_unused_dependencies_by_path_line_name() {
3957 let mut r = AnalysisResults::default();
3958 let mk = |path: &str, line: u32, name: &str| {
3959 UnusedDependencyFinding::with_actions(UnusedDependency {
3960 package_name: name.to_string(),
3961 location: DependencyLocation::Dependencies,
3962 path: PathBuf::from(path),
3963 line,
3964 used_in_workspaces: Vec::new(),
3965 })
3966 };
3967 r.unused_dependencies.push(mk("b/package.json", 3, "zlib"));
3968 r.unused_dependencies.push(mk("a/package.json", 5, "react"));
3969 r.unused_dependencies.push(mk("a/package.json", 5, "axios"));
3970 r.sort();
3971 let names: Vec<_> = r
3972 .unused_dependencies
3973 .iter()
3974 .map(|d| d.dep.package_name.as_str())
3975 .collect();
3976 assert_eq!(names, vec!["axios", "react", "zlib"]);
3977 }
3978
3979 // ── sort: unused_dev_dependencies ───────────────────────────
3980
3981 #[test]
3982 fn sort_unused_dev_dependencies() {
3983 let mut r = AnalysisResults::default();
3984 r.unused_dev_dependencies
3985 .push(UnusedDevDependencyFinding::with_actions(UnusedDependency {
3986 package_name: "vitest".to_string(),
3987 location: DependencyLocation::DevDependencies,
3988 path: PathBuf::from("package.json"),
3989 line: 10,
3990 used_in_workspaces: Vec::new(),
3991 }));
3992 r.unused_dev_dependencies
3993 .push(UnusedDevDependencyFinding::with_actions(UnusedDependency {
3994 package_name: "jest".to_string(),
3995 location: DependencyLocation::DevDependencies,
3996 path: PathBuf::from("package.json"),
3997 line: 5,
3998 used_in_workspaces: Vec::new(),
3999 }));
4000 r.sort();
4001 assert_eq!(r.unused_dev_dependencies[0].dep.package_name, "jest");
4002 assert_eq!(r.unused_dev_dependencies[1].dep.package_name, "vitest");
4003 }
4004
4005 // ── sort: unused_optional_dependencies ──────────────────────
4006
4007 #[test]
4008 fn sort_unused_optional_dependencies() {
4009 let mut r = AnalysisResults::default();
4010 r.unused_optional_dependencies
4011 .push(UnusedOptionalDependencyFinding::with_actions(
4012 UnusedDependency {
4013 package_name: "zod".to_string(),
4014 location: DependencyLocation::OptionalDependencies,
4015 path: PathBuf::from("package.json"),
4016 line: 3,
4017 used_in_workspaces: Vec::new(),
4018 },
4019 ));
4020 r.unused_optional_dependencies
4021 .push(UnusedOptionalDependencyFinding::with_actions(
4022 UnusedDependency {
4023 package_name: "ajv".to_string(),
4024 location: DependencyLocation::OptionalDependencies,
4025 path: PathBuf::from("package.json"),
4026 line: 2,
4027 used_in_workspaces: Vec::new(),
4028 },
4029 ));
4030 r.sort();
4031 assert_eq!(r.unused_optional_dependencies[0].dep.package_name, "ajv");
4032 assert_eq!(r.unused_optional_dependencies[1].dep.package_name, "zod");
4033 }
4034
4035 // ── sort: unused_enum_members by path, line, parent, member ─
4036
4037 #[test]
4038 fn sort_unused_enum_members_by_path_line_parent_member() {
4039 let mut r = AnalysisResults::default();
4040 let mk = |path: &str, line: u32, parent: &str, member: &str| {
4041 UnusedEnumMemberFinding::with_actions(UnusedMember {
4042 path: PathBuf::from(path),
4043 parent_name: parent.to_string(),
4044 member_name: member.to_string(),
4045 kind: MemberKind::EnumMember,
4046 line,
4047 col: 0,
4048 })
4049 };
4050 r.unused_enum_members.push(mk("a.ts", 5, "Status", "Z"));
4051 r.unused_enum_members.push(mk("a.ts", 5, "Status", "A"));
4052 r.unused_enum_members.push(mk("a.ts", 1, "Direction", "Up"));
4053 r.sort();
4054 let keys: Vec<_> = r
4055 .unused_enum_members
4056 .iter()
4057 .map(|m| format!("{}:{}", m.member.parent_name, m.member.member_name))
4058 .collect();
4059 assert_eq!(keys, vec!["Direction:Up", "Status:A", "Status:Z"]);
4060 }
4061
4062 // ── sort: unused_class_members by path, line, parent, member
4063
4064 #[test]
4065 fn sort_unused_class_members() {
4066 let mut r = AnalysisResults::default();
4067 let mk = |path: &str, line: u32, parent: &str, member: &str| {
4068 UnusedClassMemberFinding::with_actions(UnusedMember {
4069 path: PathBuf::from(path),
4070 parent_name: parent.to_string(),
4071 member_name: member.to_string(),
4072 kind: MemberKind::ClassMethod,
4073 line,
4074 col: 0,
4075 })
4076 };
4077 r.unused_class_members.push(mk("b.ts", 1, "Foo", "z"));
4078 r.unused_class_members.push(mk("a.ts", 1, "Bar", "a"));
4079 r.sort();
4080 assert_eq!(r.unused_class_members[0].member.path, PathBuf::from("a.ts"));
4081 assert_eq!(r.unused_class_members[1].member.path, PathBuf::from("b.ts"));
4082 }
4083
4084 // ── sort: unresolved_imports by path, line, col, specifier ──
4085
4086 #[test]
4087 fn sort_unresolved_imports_by_path_line_col_specifier() {
4088 let mut r = AnalysisResults::default();
4089 let mk = |path: &str, line: u32, col: u32, spec: &str| {
4090 UnresolvedImportFinding::with_actions(UnresolvedImport {
4091 path: PathBuf::from(path),
4092 specifier: spec.to_string(),
4093 line,
4094 col,
4095 specifier_col: 0,
4096 })
4097 };
4098 r.unresolved_imports.push(mk("a.ts", 5, 0, "./z"));
4099 r.unresolved_imports.push(mk("a.ts", 5, 0, "./a"));
4100 r.unresolved_imports.push(mk("a.ts", 1, 0, "./m"));
4101 r.sort();
4102 let specs: Vec<_> = r
4103 .unresolved_imports
4104 .iter()
4105 .map(|i| i.import.specifier.as_str())
4106 .collect();
4107 assert_eq!(specs, vec!["./m", "./a", "./z"]);
4108 }
4109
4110 // ── sort: unlisted_dependencies + inner imported_from ───────
4111
4112 #[test]
4113 fn sort_unlisted_dependencies_by_name_and_inner_sites() {
4114 let mut r = AnalysisResults::default();
4115 r.unlisted_dependencies
4116 .push(UnlistedDependencyFinding::with_actions(
4117 UnlistedDependency {
4118 package_name: "zod".to_string(),
4119 imported_from: vec![
4120 ImportSite {
4121 path: PathBuf::from("b.ts"),
4122 line: 10,
4123 col: 0,
4124 },
4125 ImportSite {
4126 path: PathBuf::from("a.ts"),
4127 line: 1,
4128 col: 0,
4129 },
4130 ],
4131 },
4132 ));
4133 r.unlisted_dependencies
4134 .push(UnlistedDependencyFinding::with_actions(
4135 UnlistedDependency {
4136 package_name: "axios".to_string(),
4137 imported_from: vec![ImportSite {
4138 path: PathBuf::from("c.ts"),
4139 line: 1,
4140 col: 0,
4141 }],
4142 },
4143 ));
4144 r.sort();
4145
4146 // Outer sort: by package_name
4147 assert_eq!(r.unlisted_dependencies[0].dep.package_name, "axios");
4148 assert_eq!(r.unlisted_dependencies[1].dep.package_name, "zod");
4149
4150 // Inner sort: imported_from sorted by path, then line
4151 let zod_sites: Vec<_> = r.unlisted_dependencies[1]
4152 .dep
4153 .imported_from
4154 .iter()
4155 .map(|s| s.path.to_string_lossy().to_string())
4156 .collect();
4157 assert_eq!(zod_sites, vec!["a.ts", "b.ts"]);
4158 }
4159
4160 // ── sort: duplicate_exports + inner locations ───────────────
4161
4162 #[test]
4163 fn sort_duplicate_exports_by_name_and_inner_locations() {
4164 let mut r = AnalysisResults::default();
4165 r.duplicate_exports
4166 .push(DuplicateExportFinding::with_actions(DuplicateExport {
4167 export_name: "z".to_string(),
4168 locations: vec![
4169 DuplicateLocation {
4170 path: PathBuf::from("c.ts"),
4171 line: 1,
4172 col: 0,
4173 },
4174 DuplicateLocation {
4175 path: PathBuf::from("a.ts"),
4176 line: 5,
4177 col: 0,
4178 },
4179 ],
4180 }));
4181 r.duplicate_exports
4182 .push(DuplicateExportFinding::with_actions(DuplicateExport {
4183 export_name: "a".to_string(),
4184 locations: vec![DuplicateLocation {
4185 path: PathBuf::from("b.ts"),
4186 line: 1,
4187 col: 0,
4188 }],
4189 }));
4190 r.sort();
4191
4192 // Outer sort: by export_name
4193 assert_eq!(r.duplicate_exports[0].export.export_name, "a");
4194 assert_eq!(r.duplicate_exports[1].export.export_name, "z");
4195
4196 // Inner sort: locations sorted by path, then line
4197 let z_locs: Vec<_> = r.duplicate_exports[1]
4198 .export
4199 .locations
4200 .iter()
4201 .map(|l| l.path.to_string_lossy().to_string())
4202 .collect();
4203 assert_eq!(z_locs, vec!["a.ts", "c.ts"]);
4204 }
4205
4206 // ── sort: type_only_dependencies ────────────────────────────
4207
4208 #[test]
4209 fn sort_type_only_dependencies() {
4210 let mut r = AnalysisResults::default();
4211 r.type_only_dependencies
4212 .push(TypeOnlyDependencyFinding::with_actions(
4213 TypeOnlyDependency {
4214 package_name: "zod".to_string(),
4215 path: PathBuf::from("package.json"),
4216 line: 10,
4217 },
4218 ));
4219 r.type_only_dependencies
4220 .push(TypeOnlyDependencyFinding::with_actions(
4221 TypeOnlyDependency {
4222 package_name: "ajv".to_string(),
4223 path: PathBuf::from("package.json"),
4224 line: 5,
4225 },
4226 ));
4227 r.sort();
4228 assert_eq!(r.type_only_dependencies[0].dep.package_name, "ajv");
4229 assert_eq!(r.type_only_dependencies[1].dep.package_name, "zod");
4230 }
4231
4232 // ── sort: test_only_dependencies ────────────────────────────
4233
4234 #[test]
4235 fn sort_test_only_dependencies() {
4236 let mut r = AnalysisResults::default();
4237 r.test_only_dependencies
4238 .push(TestOnlyDependencyFinding::with_actions(
4239 TestOnlyDependency {
4240 package_name: "vitest".to_string(),
4241 path: PathBuf::from("package.json"),
4242 line: 15,
4243 },
4244 ));
4245 r.test_only_dependencies
4246 .push(TestOnlyDependencyFinding::with_actions(
4247 TestOnlyDependency {
4248 package_name: "jest".to_string(),
4249 path: PathBuf::from("package.json"),
4250 line: 10,
4251 },
4252 ));
4253 r.sort();
4254 assert_eq!(r.test_only_dependencies[0].dep.package_name, "jest");
4255 assert_eq!(r.test_only_dependencies[1].dep.package_name, "vitest");
4256 }
4257
4258 // ── sort: circular_dependencies by files, then length ───────
4259
4260 #[test]
4261 fn sort_circular_dependencies_by_files_then_length() {
4262 let mut r = AnalysisResults::default();
4263 r.circular_dependencies
4264 .push(CircularDependencyFinding::with_actions(
4265 CircularDependency {
4266 files: vec![PathBuf::from("b.ts"), PathBuf::from("c.ts")],
4267 length: 2,
4268 line: 1,
4269 col: 0,
4270 edges: Vec::new(),
4271 is_cross_package: false,
4272 },
4273 ));
4274 r.circular_dependencies
4275 .push(CircularDependencyFinding::with_actions(
4276 CircularDependency {
4277 files: vec![PathBuf::from("a.ts"), PathBuf::from("b.ts")],
4278 length: 2,
4279 line: 1,
4280 col: 0,
4281 edges: Vec::new(),
4282 is_cross_package: true,
4283 },
4284 ));
4285 r.sort();
4286 assert_eq!(
4287 r.circular_dependencies[0].cycle.files[0],
4288 PathBuf::from("a.ts")
4289 );
4290 assert_eq!(
4291 r.circular_dependencies[1].cycle.files[0],
4292 PathBuf::from("b.ts")
4293 );
4294 }
4295
4296 // ── sort: boundary_violations by from_path, line, col, to_path
4297
4298 #[test]
4299 fn sort_boundary_violations() {
4300 let mut r = AnalysisResults::default();
4301 let mk = |from: &str, line: u32, col: u32, to: &str| {
4302 BoundaryViolationFinding::with_actions(BoundaryViolation {
4303 from_path: PathBuf::from(from),
4304 to_path: PathBuf::from(to),
4305 from_zone: "a".to_string(),
4306 to_zone: "b".to_string(),
4307 import_specifier: to.to_string(),
4308 line,
4309 col,
4310 })
4311 };
4312 r.boundary_violations.push(mk("z.ts", 1, 0, "a.ts"));
4313 r.boundary_violations.push(mk("a.ts", 5, 0, "b.ts"));
4314 r.boundary_violations.push(mk("a.ts", 1, 0, "c.ts"));
4315 r.sort();
4316 let from_paths: Vec<_> = r
4317 .boundary_violations
4318 .iter()
4319 .map(|v| {
4320 format!(
4321 "{}:{}",
4322 v.violation.from_path.to_string_lossy(),
4323 v.violation.line
4324 )
4325 })
4326 .collect();
4327 assert_eq!(from_paths, vec!["a.ts:1", "a.ts:5", "z.ts:1"]);
4328 }
4329
4330 // ── sort: export_usages + inner reference_locations ─────────
4331
4332 #[test]
4333 fn sort_export_usages_and_inner_reference_locations() {
4334 let mut r = AnalysisResults::default();
4335 r.export_usages.push(ExportUsage {
4336 path: PathBuf::from("z.ts"),
4337 export_name: "foo".to_string(),
4338 line: 1,
4339 col: 0,
4340 reference_count: 2,
4341 reference_locations: vec![
4342 ReferenceLocation {
4343 path: PathBuf::from("c.ts"),
4344 line: 10,
4345 col: 0,
4346 },
4347 ReferenceLocation {
4348 path: PathBuf::from("a.ts"),
4349 line: 5,
4350 col: 0,
4351 },
4352 ],
4353 });
4354 r.export_usages.push(ExportUsage {
4355 path: PathBuf::from("a.ts"),
4356 export_name: "bar".to_string(),
4357 line: 1,
4358 col: 0,
4359 reference_count: 1,
4360 reference_locations: vec![ReferenceLocation {
4361 path: PathBuf::from("b.ts"),
4362 line: 1,
4363 col: 0,
4364 }],
4365 });
4366 r.sort();
4367
4368 // Outer sort: by path, then line, then export_name
4369 assert_eq!(r.export_usages[0].path, PathBuf::from("a.ts"));
4370 assert_eq!(r.export_usages[1].path, PathBuf::from("z.ts"));
4371
4372 // Inner sort: reference_locations sorted by path, line, col
4373 let refs: Vec<_> = r.export_usages[1]
4374 .reference_locations
4375 .iter()
4376 .map(|l| l.path.to_string_lossy().to_string())
4377 .collect();
4378 assert_eq!(refs, vec!["a.ts", "c.ts"]);
4379 }
4380
4381 // ── sort: empty results does not panic ──────────────────────
4382
4383 #[test]
4384 fn sort_empty_results_is_noop() {
4385 let mut r = AnalysisResults::default();
4386 r.sort(); // should not panic
4387 assert_eq!(r.total_issues(), 0);
4388 }
4389
4390 // ── sort: single-element lists remain stable ────────────────
4391
4392 #[test]
4393 fn sort_single_element_lists_stable() {
4394 let mut r = AnalysisResults::default();
4395 r.unused_files
4396 .push(UnusedFileFinding::with_actions(UnusedFile {
4397 path: PathBuf::from("only.ts"),
4398 }));
4399 r.sort();
4400 assert_eq!(r.unused_files[0].file.path, PathBuf::from("only.ts"));
4401 }
4402
4403 // ── serialization ──────────────────────────────────────────
4404
4405 #[test]
4406 fn serialize_empty_results() {
4407 let r = AnalysisResults::default();
4408 let json = serde_json::to_value(&r).unwrap();
4409
4410 // All arrays should be present and empty
4411 assert!(json["unused_files"].as_array().unwrap().is_empty());
4412 assert!(json["unused_exports"].as_array().unwrap().is_empty());
4413 assert!(json["circular_dependencies"].as_array().unwrap().is_empty());
4414
4415 // Skipped fields should be absent
4416 assert!(json.get("export_usages").is_none());
4417 assert!(json.get("entry_point_summary").is_none());
4418 }
4419
4420 #[test]
4421 fn serialize_unused_file_path() {
4422 let r = UnusedFile {
4423 path: PathBuf::from("src/utils/index.ts"),
4424 };
4425 let json = serde_json::to_value(&r).unwrap();
4426 assert_eq!(json["path"], "src/utils/index.ts");
4427 }
4428
4429 #[test]
4430 fn serialize_dependency_location_camel_case() {
4431 let dep = UnusedDependency {
4432 package_name: "react".to_string(),
4433 location: DependencyLocation::DevDependencies,
4434 path: PathBuf::from("package.json"),
4435 line: 5,
4436 used_in_workspaces: Vec::new(),
4437 };
4438 let json = serde_json::to_value(&dep).unwrap();
4439 assert_eq!(json["location"], "devDependencies");
4440
4441 let dep2 = UnusedDependency {
4442 package_name: "react".to_string(),
4443 location: DependencyLocation::Dependencies,
4444 path: PathBuf::from("package.json"),
4445 line: 3,
4446 used_in_workspaces: Vec::new(),
4447 };
4448 let json2 = serde_json::to_value(&dep2).unwrap();
4449 assert_eq!(json2["location"], "dependencies");
4450
4451 let dep3 = UnusedDependency {
4452 package_name: "fsevents".to_string(),
4453 location: DependencyLocation::OptionalDependencies,
4454 path: PathBuf::from("package.json"),
4455 line: 7,
4456 used_in_workspaces: Vec::new(),
4457 };
4458 let json3 = serde_json::to_value(&dep3).unwrap();
4459 assert_eq!(json3["location"], "optionalDependencies");
4460 }
4461
4462 #[test]
4463 fn serialize_circular_dependency_skips_false_cross_package() {
4464 let cd = CircularDependency {
4465 files: vec![PathBuf::from("a.ts"), PathBuf::from("b.ts")],
4466 length: 2,
4467 line: 1,
4468 col: 0,
4469 edges: Vec::new(),
4470 is_cross_package: false,
4471 };
4472 let json = serde_json::to_value(&cd).unwrap();
4473 // skip_serializing_if = "std::ops::Not::not" means false is skipped
4474 assert!(json.get("is_cross_package").is_none());
4475 }
4476
4477 #[test]
4478 fn serialize_circular_dependency_includes_true_cross_package() {
4479 let cd = CircularDependency {
4480 files: vec![PathBuf::from("a.ts"), PathBuf::from("b.ts")],
4481 length: 2,
4482 line: 1,
4483 col: 0,
4484 edges: Vec::new(),
4485 is_cross_package: true,
4486 };
4487 let json = serde_json::to_value(&cd).unwrap();
4488 assert_eq!(json["is_cross_package"], true);
4489 }
4490
4491 #[test]
4492 fn serialize_unused_export_fields() {
4493 let e = UnusedExport {
4494 path: PathBuf::from("src/mod.ts"),
4495 export_name: "helper".to_string(),
4496 is_type_only: true,
4497 line: 42,
4498 col: 7,
4499 span_start: 100,
4500 is_re_export: true,
4501 };
4502 let json = serde_json::to_value(&e).unwrap();
4503 assert_eq!(json["path"], "src/mod.ts");
4504 assert_eq!(json["export_name"], "helper");
4505 assert_eq!(json["is_type_only"], true);
4506 assert_eq!(json["line"], 42);
4507 assert_eq!(json["col"], 7);
4508 assert_eq!(json["span_start"], 100);
4509 assert_eq!(json["is_re_export"], true);
4510 }
4511
4512 #[test]
4513 fn serialize_boundary_violation_fields() {
4514 let v = BoundaryViolation {
4515 from_path: PathBuf::from("src/ui/button.tsx"),
4516 to_path: PathBuf::from("src/db/queries.ts"),
4517 from_zone: "ui".to_string(),
4518 to_zone: "db".to_string(),
4519 import_specifier: "../db/queries".to_string(),
4520 line: 3,
4521 col: 0,
4522 };
4523 let json = serde_json::to_value(&v).unwrap();
4524 assert_eq!(json["from_path"], "src/ui/button.tsx");
4525 assert_eq!(json["to_path"], "src/db/queries.ts");
4526 assert_eq!(json["from_zone"], "ui");
4527 assert_eq!(json["to_zone"], "db");
4528 assert_eq!(json["import_specifier"], "../db/queries");
4529 }
4530
4531 #[test]
4532 fn serialize_unlisted_dependency_with_import_sites() {
4533 let d = UnlistedDependency {
4534 package_name: "chalk".to_string(),
4535 imported_from: vec![
4536 ImportSite {
4537 path: PathBuf::from("a.ts"),
4538 line: 1,
4539 col: 0,
4540 },
4541 ImportSite {
4542 path: PathBuf::from("b.ts"),
4543 line: 5,
4544 col: 3,
4545 },
4546 ],
4547 };
4548 let json = serde_json::to_value(&d).unwrap();
4549 assert_eq!(json["package_name"], "chalk");
4550 let sites = json["imported_from"].as_array().unwrap();
4551 assert_eq!(sites.len(), 2);
4552 assert_eq!(sites[0]["path"], "a.ts");
4553 assert_eq!(sites[1]["line"], 5);
4554 }
4555
4556 #[test]
4557 fn serialize_duplicate_export_with_locations() {
4558 let d = DuplicateExport {
4559 export_name: "Button".to_string(),
4560 locations: vec![
4561 DuplicateLocation {
4562 path: PathBuf::from("src/a.ts"),
4563 line: 10,
4564 col: 0,
4565 },
4566 DuplicateLocation {
4567 path: PathBuf::from("src/b.ts"),
4568 line: 20,
4569 col: 5,
4570 },
4571 ],
4572 };
4573 let json = serde_json::to_value(&d).unwrap();
4574 assert_eq!(json["export_name"], "Button");
4575 let locs = json["locations"].as_array().unwrap();
4576 assert_eq!(locs.len(), 2);
4577 assert_eq!(locs[0]["line"], 10);
4578 assert_eq!(locs[1]["col"], 5);
4579 }
4580
4581 #[test]
4582 fn serialize_type_only_dependency() {
4583 let d = TypeOnlyDependency {
4584 package_name: "@types/react".to_string(),
4585 path: PathBuf::from("package.json"),
4586 line: 12,
4587 };
4588 let json = serde_json::to_value(&d).unwrap();
4589 assert_eq!(json["package_name"], "@types/react");
4590 assert_eq!(json["line"], 12);
4591 }
4592
4593 #[test]
4594 fn serialize_test_only_dependency() {
4595 let d = TestOnlyDependency {
4596 package_name: "vitest".to_string(),
4597 path: PathBuf::from("package.json"),
4598 line: 8,
4599 };
4600 let json = serde_json::to_value(&d).unwrap();
4601 assert_eq!(json["package_name"], "vitest");
4602 assert_eq!(json["line"], 8);
4603 }
4604
4605 #[test]
4606 fn serialize_unused_member() {
4607 let m = UnusedMember {
4608 path: PathBuf::from("enums.ts"),
4609 parent_name: "Status".to_string(),
4610 member_name: "Pending".to_string(),
4611 kind: MemberKind::EnumMember,
4612 line: 3,
4613 col: 4,
4614 };
4615 let json = serde_json::to_value(&m).unwrap();
4616 assert_eq!(json["parent_name"], "Status");
4617 assert_eq!(json["member_name"], "Pending");
4618 assert_eq!(json["line"], 3);
4619 }
4620
4621 #[test]
4622 fn serialize_unresolved_import() {
4623 let i = UnresolvedImport {
4624 path: PathBuf::from("app.ts"),
4625 specifier: "./missing-module".to_string(),
4626 line: 7,
4627 col: 0,
4628 specifier_col: 21,
4629 };
4630 let json = serde_json::to_value(&i).unwrap();
4631 assert_eq!(json["specifier"], "./missing-module");
4632 assert_eq!(json["specifier_col"], 21);
4633 }
4634
4635 // ── deserialize: CircularDependency serde(default) fields ──
4636
4637 #[test]
4638 fn deserialize_circular_dependency_with_defaults() {
4639 // CircularDependency derives Deserialize; line/col/is_cross_package have #[serde(default)]
4640 let json = r#"{"files":["a.ts","b.ts"],"length":2}"#;
4641 let cd: CircularDependency = serde_json::from_str(json).unwrap();
4642 assert_eq!(cd.files.len(), 2);
4643 assert_eq!(cd.length, 2);
4644 assert_eq!(cd.line, 0);
4645 assert_eq!(cd.col, 0);
4646 assert!(!cd.is_cross_package);
4647 }
4648
4649 #[test]
4650 fn deserialize_circular_dependency_with_all_fields() {
4651 let json =
4652 r#"{"files":["a.ts","b.ts"],"length":2,"line":5,"col":10,"is_cross_package":true}"#;
4653 let cd: CircularDependency = serde_json::from_str(json).unwrap();
4654 assert_eq!(cd.line, 5);
4655 assert_eq!(cd.col, 10);
4656 assert!(cd.is_cross_package);
4657 }
4658
4659 // ── clone produces independent copies ───────────────────────
4660
4661 #[test]
4662 fn clone_results_are_independent() {
4663 let mut r = AnalysisResults::default();
4664 r.unused_files
4665 .push(UnusedFileFinding::with_actions(UnusedFile {
4666 path: PathBuf::from("a.ts"),
4667 }));
4668 let mut cloned = r.clone();
4669 cloned
4670 .unused_files
4671 .push(UnusedFileFinding::with_actions(UnusedFile {
4672 path: PathBuf::from("b.ts"),
4673 }));
4674 assert_eq!(r.total_issues(), 1);
4675 assert_eq!(cloned.total_issues(), 2);
4676 }
4677
4678 // ── export_usages not counted in total_issues ───────────────
4679
4680 #[test]
4681 fn export_usages_not_counted_in_total_issues() {
4682 let mut r = AnalysisResults::default();
4683 r.export_usages.push(ExportUsage {
4684 path: PathBuf::from("mod.ts"),
4685 export_name: "foo".to_string(),
4686 line: 1,
4687 col: 0,
4688 reference_count: 3,
4689 reference_locations: vec![],
4690 });
4691 // export_usages is metadata, not an issue type
4692 assert_eq!(r.total_issues(), 0);
4693 assert!(!r.has_issues());
4694 }
4695
4696 // ── entry_point_summary not counted in total_issues ─────────
4697
4698 #[test]
4699 fn entry_point_summary_not_counted_in_total_issues() {
4700 let r = AnalysisResults {
4701 entry_point_summary: Some(EntryPointSummary {
4702 total: 10,
4703 by_source: vec![("config".to_string(), 10)],
4704 }),
4705 ..AnalysisResults::default()
4706 };
4707 assert_eq!(r.total_issues(), 0);
4708 assert!(!r.has_issues());
4709 }
4710}