Skip to main content

fallow_types/
output_dead_code.rs

1//! Typed envelope wrappers for the simple 1:1 dead-code findings whose
2//! actions are entirely determined by the wrapper type (no per-instance
3//! discriminants beyond what the bare finding already exposes).
4//!
5//! Each wrapper flattens the bare finding via `#[serde(flatten)]` so the
6//! wire shape matches the previous `actions`-grafted output byte-for-byte.
7//! `actions` is populated at construction time via each wrapper's
8//! `with_actions` constructor and replaces the per-finding `inject_actions`
9//! post-pass in `crates/cli/src/report/json.rs`. `introduced` carries the optional audit
10//! breadcrumb that `crates/cli/src/audit.rs::annotate_issue_array` inserts
11//! into the JSON object via `map.insert`; the wrapper-level field stays
12//! `None` when serialized directly from Rust and is set by the audit pass
13//! only when the issue was introduced relative to the merge-base.
14//!
15//! All nine wrappers ship with `IssueAction` arrays today; they pay the
16//! `serde_json` dependency cost because `IssueAction` transitively
17//! references `AddToConfigValue::RuleObject(serde_json::Map<...>)`. The
18//! variants the wrappers actually emit (`Fix`, `SuppressLine`,
19//! `SuppressFile`, `AddToConfig`) are small, but reusing the existing enum
20//! keeps the wire-shape contract identical to the legacy post-pass.
21//!
22//! `introduced` is typed as `Option<AuditIntroduced>` (transparent newtype
23//! over `bool`) so the regenerated schema renders the field via
24//! `$ref: #/definitions/AuditIntroduced`, matching the reference the prior
25//! post-pass augmentation graft used. The audit pass continues to inject a
26//! bare bool via `map.insert("introduced", ...)`; serde reads it back into
27//! `AuditIntroduced` transparently. The field stays absent at the wire when
28//! `None` (`skip_serializing_if`).
29
30use serde::Serialize;
31
32use crate::envelope::AuditIntroduced;
33use crate::output::{
34    AddToConfigAction, AddToConfigKind, AddToConfigValue, FixAction, FixActionType,
35    IgnoreExportsRule, IssueAction, SuppressFileAction, SuppressFileKind, SuppressLineAction,
36    SuppressLineKind, SuppressLineScope,
37};
38use crate::results::{
39    BoundaryCallViolation, BoundaryCoverageViolation, BoundaryViolation, CircularDependency,
40    DependencyOverrideSource, DuplicateExport, EmptyCatalogGroup, MisconfiguredDependencyOverride,
41    PolicyViolation, PrivateTypeLeak, ReExportCycle, ReExportCycleKind, TestOnlyDependency,
42    TypeOnlyDependency, UnlistedDependency, UnresolvedCatalogReference, UnresolvedImport,
43    UnusedCatalogEntry, UnusedDependency, UnusedDependencyOverride, UnusedExport, UnusedFile,
44    UnusedMember,
45};
46
47/// Shared note for the `duplicate-exports` fix action. Mirrors the const used
48/// by the human report (see `crates/cli/src/report/shared.rs`); kept here so
49/// the wire-format builder reads from the same source of truth.
50pub const NAMESPACE_BARREL_HINT: &str = "If every location is the sole `index.*` of its directory, this is likely an intentional namespace-barrel API. Prefer adding these files to `ignoreExports` over removing exports.";
51
52/// JSON Schema fragment URL for the `add-to-config` `ignoreExports` action's
53/// `value` payload. Pinned to the main branch so users browsing the action
54/// value can navigate directly to the rule shape.
55const IGNORE_EXPORTS_VALUE_SCHEMA: &str =
56    "https://raw.githubusercontent.com/fallow-rs/fallow/main/schema.json#/properties/ignoreExports";
57
58/// JSON Schema fragment URL for the `ignoreCatalogReferences` rule items
59/// referenced by `add-to-config` actions on `unresolved-catalog-references`.
60const IGNORE_CATALOG_REFERENCES_VALUE_SCHEMA: &str = "https://raw.githubusercontent.com/fallow-rs/fallow/main/schema.json#/properties/ignoreCatalogReferences/items";
61
62/// JSON Schema fragment URL for the `ignoreDependencyOverrides` rule items
63/// referenced by `add-to-config` actions on both the unused- and
64/// misconfigured-override findings.
65const IGNORE_DEPENDENCY_OVERRIDES_VALUE_SCHEMA: &str = "https://raw.githubusercontent.com/fallow-rs/fallow/main/schema.json#/properties/ignoreDependencyOverrides/items";
66
67/// Wire-shape envelope for an [`UnusedFile`] finding. The bare finding
68/// flattens in via `#[serde(flatten)]`, with a typed `actions` array
69/// populated at construction time and the audit-pass `introduced` flag
70/// attached as an optional sibling.
71#[derive(Debug, Clone, Serialize)]
72#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
73pub struct UnusedFileFinding {
74    /// The underlying dead-code entry.
75    #[serde(flatten)]
76    pub file: UnusedFile,
77    /// Suggested next steps: a `delete-file` primary and a `suppress-file`
78    /// secondary. Always emitted (possibly empty for forward-compat).
79    pub actions: Vec<IssueAction>,
80    /// Set by the audit pass when this finding is introduced relative to
81    /// the merge-base. `None` when serialized directly from Rust.
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub introduced: Option<AuditIntroduced>,
84}
85
86impl UnusedFileFinding {
87    /// Build the wrapper from a raw [`UnusedFile`], computing the typed
88    /// `actions` array inline. `introduced` stays `None` and is set later
89    /// by `annotate_dead_code_json` if the audit pass runs.
90    #[must_use]
91    pub fn with_actions(file: UnusedFile) -> Self {
92        let actions = vec![
93            IssueAction::Fix(FixAction {
94                kind: FixActionType::DeleteFile,
95                auto_fixable: false,
96                description: "Delete this file".to_string(),
97                note: Some(
98                    "File deletion may remove runtime functionality not visible to static analysis"
99                        .to_string(),
100                ),
101                available_in_catalogs: None,
102                suggested_target: None,
103            }),
104            IssueAction::SuppressFile(SuppressFileAction {
105                kind: SuppressFileKind::SuppressFile,
106                auto_fixable: false,
107                description: "Suppress with a file-level comment at the top of the file"
108                    .to_string(),
109                comment: "// fallow-ignore-file unused-file".to_string(),
110            }),
111        ];
112        Self {
113            file,
114            actions,
115            introduced: None,
116        }
117    }
118}
119
120/// Wire-shape envelope for a [`PrivateTypeLeak`] finding. Mirrors
121/// [`UnusedFileFinding`]: flattens the bare finding and carries a typed
122/// `actions` array (`export-type` primary plus `suppress-line` secondary).
123#[derive(Debug, Clone, Serialize)]
124#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
125pub struct PrivateTypeLeakFinding {
126    /// The underlying dead-code entry.
127    #[serde(flatten)]
128    pub leak: PrivateTypeLeak,
129    /// Suggested next steps. Always emitted (possibly empty for
130    /// forward-compat).
131    pub actions: Vec<IssueAction>,
132    /// Set by the audit pass when this finding is introduced relative to
133    /// the merge-base.
134    #[serde(default, skip_serializing_if = "Option::is_none")]
135    pub introduced: Option<AuditIntroduced>,
136}
137
138impl PrivateTypeLeakFinding {
139    /// Build the wrapper from a raw [`PrivateTypeLeak`].
140    #[must_use]
141    pub fn with_actions(leak: PrivateTypeLeak) -> Self {
142        let actions = vec![
143            IssueAction::Fix(FixAction {
144                kind: FixActionType::ExportType,
145                auto_fixable: false,
146                description: "Export the referenced private type by name".to_string(),
147                note: Some(
148                    "Keep the type exported while it is part of a public signature".to_string(),
149                ),
150                available_in_catalogs: None,
151                suggested_target: None,
152            }),
153            IssueAction::SuppressLine(SuppressLineAction {
154                kind: SuppressLineKind::SuppressLine,
155                auto_fixable: false,
156                description: "Suppress with an inline comment above the line".to_string(),
157                comment: "// fallow-ignore-next-line private-type-leak".to_string(),
158                scope: None,
159            }),
160        ];
161        Self {
162            leak,
163            actions,
164            introduced: None,
165        }
166    }
167}
168
169/// Wire-shape envelope for an [`UnresolvedImport`] finding. Mirrors
170/// [`UnusedFileFinding`]: flattens the bare finding and carries a typed
171/// `actions` array (`resolve-import` primary plus config and inline
172/// suppression actions).
173#[derive(Debug, Clone, Serialize)]
174#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
175pub struct UnresolvedImportFinding {
176    /// The underlying dead-code entry.
177    #[serde(flatten)]
178    pub import: UnresolvedImport,
179    /// Suggested next steps. Always emitted (possibly empty for
180    /// forward-compat).
181    pub actions: Vec<IssueAction>,
182    /// Set by the audit pass when this finding is introduced relative to
183    /// the merge-base.
184    #[serde(default, skip_serializing_if = "Option::is_none")]
185    pub introduced: Option<AuditIntroduced>,
186}
187
188impl UnresolvedImportFinding {
189    /// Build the wrapper from a raw [`UnresolvedImport`].
190    #[must_use]
191    pub fn with_actions(import: UnresolvedImport) -> Self {
192        let actions = vec![
193            IssueAction::Fix(FixAction {
194                kind: FixActionType::ResolveImport,
195                auto_fixable: false,
196                description: "Fix the import specifier or install the missing module".to_string(),
197                note: Some(
198                    "Verify the module path and check tsconfig paths configuration".to_string(),
199                ),
200                available_in_catalogs: None,
201                suggested_target: None,
202            }),
203            IssueAction::AddToConfig(AddToConfigAction {
204                kind: AddToConfigKind::AddToConfig,
205                auto_fixable: false,
206                description: format!(
207                    "Add \"{}\" to ignoreUnresolvedImports in fallow config",
208                    import.specifier
209                ),
210                config_key: "ignoreUnresolvedImports".to_string(),
211                value: AddToConfigValue::Scalar(import.specifier.clone()),
212                value_schema: Some(
213                    "https://raw.githubusercontent.com/fallow-rs/fallow/main/schema.json#/properties/ignoreUnresolvedImports/items"
214                        .to_string(),
215                ),
216            }),
217            IssueAction::SuppressLine(SuppressLineAction {
218                kind: SuppressLineKind::SuppressLine,
219                auto_fixable: false,
220                description: "Suppress with an inline comment above the line".to_string(),
221                comment: "// fallow-ignore-next-line unresolved-import".to_string(),
222                scope: None,
223            }),
224        ];
225        Self {
226            import,
227            actions,
228            introduced: None,
229        }
230    }
231}
232
233/// Wire-shape envelope for a [`CircularDependency`] finding. Mirrors
234/// [`UnusedFileFinding`]: flattens the bare finding and carries a typed
235/// `actions` array (`refactor-cycle` primary plus `suppress-line`
236/// secondary).
237#[derive(Debug, Clone, Serialize)]
238#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
239pub struct CircularDependencyFinding {
240    /// The underlying dead-code entry.
241    #[serde(flatten)]
242    pub cycle: CircularDependency,
243    /// Suggested next steps. Always emitted (possibly empty for
244    /// forward-compat).
245    pub actions: Vec<IssueAction>,
246    /// Set by the audit pass when this finding is introduced relative to
247    /// the merge-base.
248    #[serde(default, skip_serializing_if = "Option::is_none")]
249    pub introduced: Option<AuditIntroduced>,
250}
251
252impl CircularDependencyFinding {
253    /// Build the wrapper from a raw [`CircularDependency`].
254    #[must_use]
255    pub fn with_actions(cycle: CircularDependency) -> Self {
256        let actions = vec![
257            IssueAction::Fix(FixAction {
258                kind: FixActionType::RefactorCycle,
259                auto_fixable: false,
260                description: "Extract shared logic into a separate module to break the cycle"
261                    .to_string(),
262                note: Some(
263                    "Circular imports can cause initialization issues and make code harder to reason about"
264                        .to_string(),
265                ),
266                available_in_catalogs: None,
267                suggested_target: None,
268            }),
269            IssueAction::SuppressLine(SuppressLineAction {
270                kind: SuppressLineKind::SuppressLine,
271                auto_fixable: false,
272                description: "Suppress with an inline comment above the line".to_string(),
273                comment: "// fallow-ignore-next-line circular-dependency".to_string(),
274                scope: None,
275            }),
276        ];
277        Self {
278            cycle,
279            actions,
280            introduced: None,
281        }
282    }
283}
284
285/// Wire-shape envelope for a [`ReExportCycle`] finding. Mirrors
286/// [`CircularDependencyFinding`]: flattens the bare finding and carries a
287/// typed `actions` array (`refactor-re-export-cycle` informational primary
288/// plus `suppress-file` secondary; cycles are file-scoped so a single
289/// file-level suppression on the alphabetically-first member breaks the
290/// cycle, and no `// fallow-ignore-next-line` form makes sense because the
291/// diagnostic is anchored at line 1 col 0 of each member).
292#[derive(Debug, Clone, Serialize)]
293#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
294pub struct ReExportCycleFinding {
295    /// The underlying dead-code entry.
296    #[serde(flatten)]
297    pub cycle: ReExportCycle,
298    /// Suggested next steps. Always emitted (possibly empty for
299    /// forward-compat).
300    pub actions: Vec<IssueAction>,
301    /// Set by the audit pass when this finding is introduced relative to
302    /// the merge-base.
303    #[serde(default, skip_serializing_if = "Option::is_none")]
304    pub introduced: Option<AuditIntroduced>,
305}
306
307impl ReExportCycleFinding {
308    /// Build the wrapper from a raw [`ReExportCycle`].
309    ///
310    /// The `SuppressFile` action targets the alphabetically-first member
311    /// (`cycle.files[0]`; the `files` Vec is already sorted at graph layer);
312    /// for multi-node cycles the description names the other members so
313    /// consumers see context for why one file-level suppression suffices.
314    #[must_use]
315    pub fn with_actions(cycle: ReExportCycle) -> Self {
316        // The description is a path-free hint about the suppression's
317        // structural effect; the cycle's member list already ships in the
318        // sibling `files` field, so consumers can correlate without
319        // re-reading the description (and absolute paths cannot leak in
320        // here, which the wrapper has no root-prefix context to strip).
321        let suppress_description = match cycle.kind {
322            ReExportCycleKind::SelfLoop => {
323                "Suppress with a file-level comment at the top of this file. \
324                 The cycle is a self-loop, so the suppression covers the entire finding."
325                    .to_string()
326            }
327            ReExportCycleKind::MultiNode => {
328                "Suppress with a file-level comment at the top of this file. \
329                 One suppression on any member breaks the cycle for every member \
330                 (see the sibling `files` array)."
331                    .to_string()
332            }
333        };
334        let actions = vec![
335            IssueAction::Fix(FixAction {
336                kind: FixActionType::RefactorReExportCycle,
337                auto_fixable: false,
338                description: "Remove one `export * from` (or `export { ... } from`) \
339                              statement on any one member to break the cycle"
340                    .to_string(),
341                note: Some(
342                    "Re-export cycles are structurally a no-op: chain propagation through \
343                     the loop never reaches a terminating module, so imports from any member \
344                     may silently come up empty."
345                        .to_string(),
346                ),
347                available_in_catalogs: None,
348                suggested_target: None,
349            }),
350            IssueAction::SuppressFile(SuppressFileAction {
351                kind: SuppressFileKind::SuppressFile,
352                auto_fixable: false,
353                description: suppress_description,
354                comment: "// fallow-ignore-file re-export-cycle".to_string(),
355            }),
356        ];
357        Self {
358            cycle,
359            actions,
360            introduced: None,
361        }
362    }
363}
364
365/// Wire-shape envelope for a [`BoundaryViolation`] finding. Mirrors
366/// [`UnusedFileFinding`]: flattens the bare finding and carries a typed
367/// `actions` array (`refactor-boundary` primary plus `suppress-line`
368/// secondary).
369#[derive(Debug, Clone, Serialize)]
370#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
371pub struct BoundaryViolationFinding {
372    /// The underlying dead-code entry.
373    #[serde(flatten)]
374    pub violation: BoundaryViolation,
375    /// Suggested next steps. Always emitted (possibly empty for
376    /// forward-compat).
377    pub actions: Vec<IssueAction>,
378    /// Set by the audit pass when this finding is introduced relative to
379    /// the merge-base.
380    #[serde(default, skip_serializing_if = "Option::is_none")]
381    pub introduced: Option<AuditIntroduced>,
382}
383
384impl BoundaryViolationFinding {
385    /// Build the wrapper from a raw [`BoundaryViolation`].
386    #[must_use]
387    pub fn with_actions(violation: BoundaryViolation) -> Self {
388        let actions = vec![
389            IssueAction::Fix(FixAction {
390                kind: FixActionType::RefactorBoundary,
391                auto_fixable: false,
392                description: "Move the import through an allowed zone or restructure the dependency"
393                    .to_string(),
394                note: Some(
395                    "This import crosses an architecture boundary that is not permitted by the configured rules"
396                        .to_string(),
397                ),
398                available_in_catalogs: None,
399                suggested_target: None,
400            }),
401            IssueAction::SuppressLine(SuppressLineAction {
402                kind: SuppressLineKind::SuppressLine,
403                auto_fixable: false,
404                description: "Suppress with an inline comment above the line".to_string(),
405                comment: "// fallow-ignore-next-line boundary-violation".to_string(),
406                scope: None,
407            }),
408        ];
409        Self {
410            violation,
411            actions,
412            introduced: None,
413        }
414    }
415}
416
417/// Wire-shape envelope for a [`BoundaryCoverageViolation`] finding. Carries
418/// actions for assigning the file to a zone or explicitly allowing it to stay
419/// unmatched.
420#[derive(Debug, Clone, Serialize)]
421#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
422pub struct BoundaryCoverageViolationFinding {
423    /// The underlying coverage entry.
424    #[serde(flatten)]
425    pub violation: BoundaryCoverageViolation,
426    /// Suggested next steps.
427    pub actions: Vec<IssueAction>,
428    /// Set by the audit pass when this finding is introduced relative to
429    /// the merge-base.
430    #[serde(default, skip_serializing_if = "Option::is_none")]
431    pub introduced: Option<AuditIntroduced>,
432}
433
434impl BoundaryCoverageViolationFinding {
435    /// Build the wrapper from a raw [`BoundaryCoverageViolation`].
436    #[must_use]
437    pub fn with_actions(violation: BoundaryCoverageViolation) -> Self {
438        let path = violation.path.to_string_lossy().replace('\\', "/");
439        let actions = vec![
440            IssueAction::Fix(FixAction {
441                kind: FixActionType::RefactorBoundary,
442                auto_fixable: false,
443                description: "Add this file to a boundary zone pattern or move it under an existing zone"
444                    .to_string(),
445                note: Some(
446                    "Boundary coverage is enabled, so every analyzed source file must match a zone unless allow-listed"
447                        .to_string(),
448                ),
449                available_in_catalogs: None,
450                suggested_target: None,
451            }),
452            IssueAction::AddToConfig(AddToConfigAction {
453                kind: AddToConfigKind::AddToConfig,
454                auto_fixable: false,
455                description: format!(
456                    "Add \"{path}\" to boundaries.coverage.allowUnmatched in fallow config"
457                ),
458                config_key: "boundaries.coverage.allowUnmatched".to_string(),
459                value: AddToConfigValue::Scalar(path),
460                value_schema: Some(
461                    "https://raw.githubusercontent.com/fallow-rs/fallow/main/schema.json#/properties/boundaries/properties/coverage/properties/allowUnmatched/items"
462                        .to_string(),
463                ),
464            }),
465            IssueAction::SuppressFile(SuppressFileAction {
466                kind: SuppressFileKind::SuppressFile,
467                auto_fixable: false,
468                description: "Suppress with a file-level comment at the top of the file"
469                    .to_string(),
470                comment: "// fallow-ignore-file boundary-violation".to_string(),
471            }),
472        ];
473        Self {
474            violation,
475            actions,
476            introduced: None,
477        }
478    }
479}
480
481/// Wire-shape envelope for a [`BoundaryCallViolation`] finding. Carries
482/// actions for refactoring the forbidden call out of the zone or suppressing
483/// it with the shared `boundary-violation` token.
484#[derive(Debug, Clone, Serialize)]
485#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
486pub struct BoundaryCallViolationFinding {
487    /// The underlying forbidden-call entry.
488    #[serde(flatten)]
489    pub violation: BoundaryCallViolation,
490    /// Suggested next steps.
491    pub actions: Vec<IssueAction>,
492    /// Set by the audit pass when this finding is introduced relative to
493    /// the merge-base.
494    #[serde(default, skip_serializing_if = "Option::is_none")]
495    pub introduced: Option<AuditIntroduced>,
496}
497
498impl BoundaryCallViolationFinding {
499    /// Build the wrapper from a raw [`BoundaryCallViolation`].
500    #[must_use]
501    pub fn with_actions(violation: BoundaryCallViolation) -> Self {
502        let actions = vec![
503            IssueAction::Fix(FixAction {
504                kind: FixActionType::RefactorBoundary,
505                auto_fixable: false,
506                description: format!(
507                    "Move the `{}` call out of zone '{}' or behind an allowed abstraction",
508                    violation.callee, violation.zone,
509                ),
510                note: Some(format!(
511                    "`boundaries.calls.forbidden` bans callees matching `{}` from zone '{}'. The check is syntactic: it applies only to files classified into a zone and does not follow aliased or re-bound callees",
512                    violation.pattern, violation.zone,
513                )),
514                available_in_catalogs: None,
515                suggested_target: None,
516            }),
517            IssueAction::SuppressLine(SuppressLineAction {
518                kind: SuppressLineKind::SuppressLine,
519                auto_fixable: false,
520                description: "Suppress with an inline comment above the line".to_string(),
521                comment: "// fallow-ignore-next-line boundary-violation".to_string(),
522                scope: None,
523            }),
524            IssueAction::SuppressFile(SuppressFileAction {
525                kind: SuppressFileKind::SuppressFile,
526                auto_fixable: false,
527                description: "Suppress with a file-level comment at the top of the file"
528                    .to_string(),
529                comment: "// fallow-ignore-file boundary-violation".to_string(),
530            }),
531        ];
532        Self {
533            violation,
534            actions,
535            introduced: None,
536        }
537    }
538}
539
540/// Wire-shape envelope for a [`PolicyViolation`] finding. Carries actions for
541/// replacing the banned call or import, or suppressing it with the
542/// `policy-violation` token.
543#[derive(Debug, Clone, Serialize)]
544#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
545pub struct PolicyViolationFinding {
546    /// The underlying rule-pack policy entry.
547    #[serde(flatten)]
548    pub violation: PolicyViolation,
549    /// Suggested next steps.
550    pub actions: Vec<IssueAction>,
551    /// Set by the audit pass when this finding is introduced relative to
552    /// the merge-base.
553    #[serde(default, skip_serializing_if = "Option::is_none")]
554    pub introduced: Option<AuditIntroduced>,
555}
556
557impl PolicyViolationFinding {
558    /// Build the wrapper from a raw [`PolicyViolation`].
559    #[must_use]
560    pub fn with_actions(violation: PolicyViolation) -> Self {
561        let what = match violation.kind {
562            crate::results::PolicyRuleKind::BannedCall => "call",
563            crate::results::PolicyRuleKind::BannedImport => "import",
564        };
565        let description = match &violation.message {
566            Some(message) => format!("Replace the `{}` {what}: {message}", violation.matched),
567            None => format!("Replace the `{}` {what}", violation.matched),
568        };
569        let actions = vec![
570            IssueAction::Fix(FixAction {
571                kind: FixActionType::ResolvePolicyViolation,
572                auto_fixable: false,
573                description,
574                note: Some(format!(
575                    "Rule `{}/{}` from the configured rule packs bans this {what}. The check is syntactic: it does not follow aliased or re-bound callees, and import matching uses the raw specifier",
576                    violation.pack, violation.rule_id,
577                )),
578                available_in_catalogs: None,
579                suggested_target: None,
580            }),
581            IssueAction::SuppressLine(SuppressLineAction {
582                kind: SuppressLineKind::SuppressLine,
583                auto_fixable: false,
584                description: "Suppress with an inline comment above the line. The token covers every rule-pack rule on that line"
585                    .to_string(),
586                comment: "// fallow-ignore-next-line policy-violation".to_string(),
587                scope: None,
588            }),
589            IssueAction::SuppressFile(SuppressFileAction {
590                kind: SuppressFileKind::SuppressFile,
591                auto_fixable: false,
592                description: "Suppress with a file-level comment at the top of the file. The token covers every rule-pack rule in the file, not just this rule"
593                    .to_string(),
594                comment: "// fallow-ignore-file policy-violation".to_string(),
595            }),
596        ];
597        Self {
598            violation,
599            actions,
600            introduced: None,
601        }
602    }
603}
604
605/// Wire-shape envelope for an [`UnusedExport`] finding consumed under the
606/// `unused_exports` key. Same Rust struct as [`UnusedTypeFinding`], with a
607/// different fix description so consumers can tell value-export from
608/// type-export removal at the action level.
609#[derive(Debug, Clone, Serialize)]
610#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
611pub struct UnusedExportFinding {
612    /// The underlying dead-code entry.
613    #[serde(flatten)]
614    pub export: UnusedExport,
615    /// Suggested next steps. Always emitted (possibly empty for
616    /// forward-compat).
617    pub actions: Vec<IssueAction>,
618    /// Set by the audit pass when this finding is introduced relative to
619    /// the merge-base.
620    #[serde(default, skip_serializing_if = "Option::is_none")]
621    pub introduced: Option<AuditIntroduced>,
622}
623
624impl UnusedExportFinding {
625    /// Build the wrapper. When `export.is_re_export` is true, the fix
626    /// action's `note` warns about possible public-API surface; otherwise
627    /// `note` is absent on the fix action.
628    #[must_use]
629    pub fn with_actions(export: UnusedExport) -> Self {
630        let note = if export.is_re_export {
631            Some(
632                "This finding originates from a re-export; verify it is not part of your public API before removing"
633                    .to_string(),
634            )
635        } else {
636            None
637        };
638        let actions = vec![
639            IssueAction::Fix(FixAction {
640                kind: FixActionType::RemoveExport,
641                auto_fixable: true,
642                description: "Remove the unused export from the public API".to_string(),
643                note,
644                available_in_catalogs: None,
645                suggested_target: None,
646            }),
647            IssueAction::SuppressLine(SuppressLineAction {
648                kind: SuppressLineKind::SuppressLine,
649                auto_fixable: false,
650                description: "Suppress with an inline comment above the line".to_string(),
651                comment: "// fallow-ignore-next-line unused-export".to_string(),
652                scope: None,
653            }),
654        ];
655        Self {
656            export,
657            actions,
658            introduced: None,
659        }
660    }
661}
662
663/// Wire-shape envelope for an [`UnusedExport`] finding consumed under the
664/// `unused_types` key. Wraps the same bare [`UnusedExport`] struct as
665/// [`UnusedExportFinding`] but emits a fix action targeted at type-only
666/// declarations, with the same `is_re_export`-aware note swap.
667#[derive(Debug, Clone, Serialize)]
668#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
669pub struct UnusedTypeFinding {
670    /// The underlying dead-code entry.
671    #[serde(flatten)]
672    pub export: UnusedExport,
673    /// Suggested next steps. Always emitted (possibly empty for
674    /// forward-compat).
675    pub actions: Vec<IssueAction>,
676    /// Set by the audit pass when this finding is introduced relative to
677    /// the merge-base.
678    #[serde(default, skip_serializing_if = "Option::is_none")]
679    pub introduced: Option<AuditIntroduced>,
680}
681
682impl UnusedTypeFinding {
683    /// Build the wrapper. `is_re_export` swaps the fix note the same way as
684    /// [`UnusedExportFinding::with_actions`].
685    #[must_use]
686    pub fn with_actions(export: UnusedExport) -> Self {
687        let note = if export.is_re_export {
688            Some(
689                "This finding originates from a re-export; verify it is not part of your public API before removing"
690                    .to_string(),
691            )
692        } else {
693            None
694        };
695        let actions = vec![
696            IssueAction::Fix(FixAction {
697                kind: FixActionType::RemoveExport,
698                auto_fixable: true,
699                description:
700                    "Remove the `export` (or `export type`) keyword from the type declaration"
701                        .to_string(),
702                note,
703                available_in_catalogs: None,
704                suggested_target: None,
705            }),
706            IssueAction::SuppressLine(SuppressLineAction {
707                kind: SuppressLineKind::SuppressLine,
708                auto_fixable: false,
709                description: "Suppress with an inline comment above the line".to_string(),
710                comment: "// fallow-ignore-next-line unused-type".to_string(),
711                scope: None,
712            }),
713        ];
714        Self {
715            export,
716            actions,
717            introduced: None,
718        }
719    }
720}
721
722/// Wire-shape envelope for an [`UnusedMember`] finding consumed under the
723/// `unused_enum_members` key.
724#[derive(Debug, Clone, Serialize)]
725#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
726pub struct UnusedEnumMemberFinding {
727    /// The underlying dead-code entry.
728    #[serde(flatten)]
729    pub member: UnusedMember,
730    /// Suggested next steps. Always emitted (possibly empty for
731    /// forward-compat).
732    pub actions: Vec<IssueAction>,
733    /// Set by the audit pass when this finding is introduced relative to
734    /// the merge-base.
735    #[serde(default, skip_serializing_if = "Option::is_none")]
736    pub introduced: Option<AuditIntroduced>,
737}
738
739impl UnusedEnumMemberFinding {
740    /// Build the wrapper from a raw [`UnusedMember`].
741    #[must_use]
742    pub fn with_actions(member: UnusedMember) -> Self {
743        let actions = vec![
744            IssueAction::Fix(FixAction {
745                kind: FixActionType::RemoveEnumMember,
746                auto_fixable: true,
747                description: "Remove this enum member".to_string(),
748                note: None,
749                available_in_catalogs: None,
750                suggested_target: None,
751            }),
752            IssueAction::SuppressLine(SuppressLineAction {
753                kind: SuppressLineKind::SuppressLine,
754                auto_fixable: false,
755                description: "Suppress with an inline comment above the line".to_string(),
756                comment: "// fallow-ignore-next-line unused-enum-member".to_string(),
757                scope: None,
758            }),
759        ];
760        Self {
761            member,
762            actions,
763            introduced: None,
764        }
765    }
766}
767
768/// Wire-shape envelope for an [`UnusedMember`] finding consumed under the
769/// `unused_class_members` key. Same Rust struct as
770/// [`UnusedEnumMemberFinding`]; the fix action and suppress comment carry
771/// the class-member kebab-case identifier instead.
772#[derive(Debug, Clone, Serialize)]
773#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
774pub struct UnusedClassMemberFinding {
775    /// The underlying dead-code entry.
776    #[serde(flatten)]
777    pub member: UnusedMember,
778    /// Suggested next steps. Always emitted (possibly empty for
779    /// forward-compat).
780    pub actions: Vec<IssueAction>,
781    /// Set by the audit pass when this finding is introduced relative to
782    /// the merge-base.
783    #[serde(default, skip_serializing_if = "Option::is_none")]
784    pub introduced: Option<AuditIntroduced>,
785}
786
787impl UnusedClassMemberFinding {
788    /// Build the wrapper from a raw [`UnusedMember`]. Class-member fixes
789    /// are not auto-applied (members can be used via dependency injection
790    /// or decorators), so `auto_fixable` is `false` and a context note is
791    /// attached.
792    #[must_use]
793    pub fn with_actions(member: UnusedMember) -> Self {
794        let actions = vec![
795            IssueAction::Fix(FixAction {
796                kind: FixActionType::RemoveClassMember,
797                auto_fixable: false,
798                description: "Remove this class member".to_string(),
799                note: Some(
800                    "Class member may be used via dependency injection or decorators".to_string(),
801                ),
802                available_in_catalogs: None,
803                suggested_target: None,
804            }),
805            IssueAction::SuppressLine(SuppressLineAction {
806                kind: SuppressLineKind::SuppressLine,
807                auto_fixable: false,
808                description: "Suppress with an inline comment above the line".to_string(),
809                comment: "// fallow-ignore-next-line unused-class-member".to_string(),
810                scope: None,
811            }),
812        ];
813        Self {
814            member,
815            actions,
816            introduced: None,
817        }
818    }
819}
820
821/// Build the `IssueAction` vec for the three `unused_dependencies`,
822/// `unused_dev_dependencies`, `unused_optional_dependencies` views over the
823/// same bare [`UnusedDependency`] struct. Each wrapper differs only in the
824/// `package_json_location` string (`"dependencies"` / `"devDependencies"` /
825/// `"optionalDependencies"`) baked into the fix-action description and in
826/// the `suppress_issue_kind` used by the inline-suppress comment. All three
827/// share the cross-workspace swap (when `dep.used_in_workspaces` is
828/// non-empty the primary fix flips from `remove-dependency` to
829/// `move-dependency` because the dep is imported by ANOTHER workspace and
830/// `fallow fix` cannot safely remove it).
831fn build_unused_dependency_actions(
832    dep: &UnusedDependency,
833    package_json_location: &str,
834    suppress_issue_kind: &str,
835) -> Vec<IssueAction> {
836    let mut actions = Vec::with_capacity(2);
837    let cross_workspace = !dep.used_in_workspaces.is_empty();
838    actions.push(if cross_workspace {
839        IssueAction::Fix(FixAction {
840            kind: FixActionType::MoveDependency,
841            auto_fixable: false,
842            description: "Move this dependency to the workspace package.json that imports it"
843                .to_string(),
844            note: Some(
845                "fallow fix will not remove dependencies that are imported by another workspace"
846                    .to_string(),
847            ),
848            available_in_catalogs: None,
849            suggested_target: None,
850        })
851    } else {
852        IssueAction::Fix(FixAction {
853            kind: FixActionType::RemoveDependency,
854            auto_fixable: true,
855            description: format!("Remove from {package_json_location} in package.json"),
856            note: None,
857            available_in_catalogs: None,
858            suggested_target: None,
859        })
860    });
861    actions.push(build_ignore_dependencies_suppress_action(
862        &dep.package_name,
863        suppress_issue_kind,
864    ));
865    actions
866}
867
868/// Build the standard `add-to-config` `ignoreDependencies` suppress action
869/// for any finding whose primary key is a package name. Used by the four
870/// dependency-family wrappers (unused / unlisted / type-only / test-only).
871/// The `_suppress_issue_kind` argument is currently unused; the pre-2.76
872/// `inject_actions` post-pass also did not embed the issue kind in this
873/// shape (no inline `// fallow-ignore-next-line ...` comment because the
874/// finding is anchored at a package.json line, not at a source-file line).
875fn build_ignore_dependencies_suppress_action(
876    package_name: &str,
877    _suppress_issue_kind: &str,
878) -> IssueAction {
879    IssueAction::AddToConfig(AddToConfigAction {
880        kind: AddToConfigKind::AddToConfig,
881        auto_fixable: false,
882        description: format!("Add \"{package_name}\" to ignoreDependencies in fallow config"),
883        config_key: "ignoreDependencies".to_string(),
884        value: AddToConfigValue::Scalar(package_name.to_string()),
885        value_schema: Some(
886            "https://raw.githubusercontent.com/fallow-rs/fallow/main/schema.json#/properties/ignoreDependencies/items"
887                .to_string(),
888        ),
889    })
890}
891
892/// Wire-shape envelope for an [`UnusedDependency`] finding consumed under
893/// the `unused_dependencies` key (production deps). Flattens the bare
894/// finding; the typed `actions` array carries either a `remove-dependency`
895/// or `move-dependency` primary depending on
896/// `inner.used_in_workspaces`.
897#[derive(Debug, Clone, Serialize)]
898#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
899pub struct UnusedDependencyFinding {
900    /// The underlying dead-code entry.
901    #[serde(flatten)]
902    pub dep: UnusedDependency,
903    /// Suggested next steps. Always emitted (possibly empty for
904    /// forward-compat).
905    pub actions: Vec<IssueAction>,
906    /// Set by the audit pass when this finding is introduced relative to
907    /// the merge-base.
908    #[serde(default, skip_serializing_if = "Option::is_none")]
909    pub introduced: Option<AuditIntroduced>,
910}
911
912impl UnusedDependencyFinding {
913    /// Build the wrapper. Switches the primary fix from `remove-dependency`
914    /// to `move-dependency` when the dep is imported by another workspace.
915    #[must_use]
916    pub fn with_actions(dep: UnusedDependency) -> Self {
917        let actions = build_unused_dependency_actions(&dep, "dependencies", "unused-dependency");
918        Self {
919            dep,
920            actions,
921            introduced: None,
922        }
923    }
924}
925
926/// Wire-shape envelope for an [`UnusedDependency`] finding consumed under
927/// the `unused_dev_dependencies` key. Same bare struct as
928/// [`UnusedDependencyFinding`]; the fix description points at
929/// `devDependencies` and the suppress comment uses
930/// `unused-dev-dependency`.
931#[derive(Debug, Clone, Serialize)]
932#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
933pub struct UnusedDevDependencyFinding {
934    /// The underlying dead-code entry.
935    #[serde(flatten)]
936    pub dep: UnusedDependency,
937    /// Suggested next steps. Always emitted (possibly empty for
938    /// forward-compat).
939    pub actions: Vec<IssueAction>,
940    /// Set by the audit pass when this finding is introduced relative to
941    /// the merge-base.
942    #[serde(default, skip_serializing_if = "Option::is_none")]
943    pub introduced: Option<AuditIntroduced>,
944}
945
946impl UnusedDevDependencyFinding {
947    /// Build the wrapper.
948    #[must_use]
949    pub fn with_actions(dep: UnusedDependency) -> Self {
950        let actions =
951            build_unused_dependency_actions(&dep, "devDependencies", "unused-dev-dependency");
952        Self {
953            dep,
954            actions,
955            introduced: None,
956        }
957    }
958}
959
960/// Wire-shape envelope for an [`UnusedDependency`] finding consumed under
961/// the `unused_optional_dependencies` key. Same bare struct as
962/// [`UnusedDependencyFinding`]; the fix description points at
963/// `optionalDependencies`. Reuses the `unused-dependency` suppress
964/// `IssueKind` because there is no dedicated variant for optional deps.
965#[derive(Debug, Clone, Serialize)]
966#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
967pub struct UnusedOptionalDependencyFinding {
968    /// The underlying dead-code entry.
969    #[serde(flatten)]
970    pub dep: UnusedDependency,
971    /// Suggested next steps. Always emitted (possibly empty for
972    /// forward-compat).
973    pub actions: Vec<IssueAction>,
974    /// Set by the audit pass when this finding is introduced relative to
975    /// the merge-base.
976    #[serde(default, skip_serializing_if = "Option::is_none")]
977    pub introduced: Option<AuditIntroduced>,
978}
979
980impl UnusedOptionalDependencyFinding {
981    /// Build the wrapper.
982    #[must_use]
983    pub fn with_actions(dep: UnusedDependency) -> Self {
984        let actions =
985            build_unused_dependency_actions(&dep, "optionalDependencies", "unused-dependency");
986        Self {
987            dep,
988            actions,
989            introduced: None,
990        }
991    }
992}
993
994/// Wire-shape envelope for an [`UnlistedDependency`] finding. Carries an
995/// `install-dependency` primary (non-auto-fixable) plus the standard
996/// `ignoreDependencies` config suppress.
997#[derive(Debug, Clone, Serialize)]
998#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
999pub struct UnlistedDependencyFinding {
1000    /// The underlying dead-code entry.
1001    #[serde(flatten)]
1002    pub dep: UnlistedDependency,
1003    /// Suggested next steps. Always emitted (possibly empty for
1004    /// forward-compat).
1005    pub actions: Vec<IssueAction>,
1006    /// Set by the audit pass when this finding is introduced relative to
1007    /// the merge-base.
1008    #[serde(default, skip_serializing_if = "Option::is_none")]
1009    pub introduced: Option<AuditIntroduced>,
1010}
1011
1012impl UnlistedDependencyFinding {
1013    /// Build the wrapper.
1014    #[must_use]
1015    pub fn with_actions(dep: UnlistedDependency) -> Self {
1016        let actions = vec![
1017            IssueAction::Fix(FixAction {
1018                kind: FixActionType::InstallDependency,
1019                auto_fixable: false,
1020                description: "Add this package to dependencies in package.json".to_string(),
1021                note: Some(
1022                    "Verify this package should be a direct dependency before adding".to_string(),
1023                ),
1024                available_in_catalogs: None,
1025                suggested_target: None,
1026            }),
1027            build_ignore_dependencies_suppress_action(&dep.package_name, "unlisted-dependency"),
1028        ];
1029        Self {
1030            dep,
1031            actions,
1032            introduced: None,
1033        }
1034    }
1035}
1036
1037/// Wire-shape envelope for a [`TypeOnlyDependency`] finding. Carries a
1038/// `move-to-dev` primary plus the standard `ignoreDependencies` config
1039/// suppress.
1040#[derive(Debug, Clone, Serialize)]
1041#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1042pub struct TypeOnlyDependencyFinding {
1043    /// The underlying dead-code entry.
1044    #[serde(flatten)]
1045    pub dep: TypeOnlyDependency,
1046    /// Suggested next steps. Always emitted (possibly empty for
1047    /// forward-compat).
1048    pub actions: Vec<IssueAction>,
1049    /// Set by the audit pass when this finding is introduced relative to
1050    /// the merge-base.
1051    #[serde(default, skip_serializing_if = "Option::is_none")]
1052    pub introduced: Option<AuditIntroduced>,
1053}
1054
1055impl TypeOnlyDependencyFinding {
1056    /// Build the wrapper.
1057    #[must_use]
1058    pub fn with_actions(dep: TypeOnlyDependency) -> Self {
1059        let actions = vec![
1060            IssueAction::Fix(FixAction {
1061                kind: FixActionType::MoveToDev,
1062                auto_fixable: false,
1063                description: "Move to devDependencies (only type imports are used)".to_string(),
1064                note: Some(
1065                    "Type imports are erased at runtime so this dependency is not needed in production"
1066                        .to_string(),
1067                ),
1068                available_in_catalogs: None,
1069                suggested_target: None,
1070            }),
1071            build_ignore_dependencies_suppress_action(&dep.package_name, "type-only-dependency"),
1072        ];
1073        Self {
1074            dep,
1075            actions,
1076            introduced: None,
1077        }
1078    }
1079}
1080
1081/// Wire-shape envelope for a [`TestOnlyDependency`] finding. Carries a
1082/// `move-to-dev` primary (different prose than [`TypeOnlyDependencyFinding`])
1083/// plus the standard `ignoreDependencies` config suppress.
1084#[derive(Debug, Clone, Serialize)]
1085#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1086pub struct TestOnlyDependencyFinding {
1087    /// The underlying dead-code entry.
1088    #[serde(flatten)]
1089    pub dep: TestOnlyDependency,
1090    /// Suggested next steps. Always emitted (possibly empty for
1091    /// forward-compat).
1092    pub actions: Vec<IssueAction>,
1093    /// Set by the audit pass when this finding is introduced relative to
1094    /// the merge-base.
1095    #[serde(default, skip_serializing_if = "Option::is_none")]
1096    pub introduced: Option<AuditIntroduced>,
1097}
1098
1099impl TestOnlyDependencyFinding {
1100    /// Build the wrapper.
1101    #[must_use]
1102    pub fn with_actions(dep: TestOnlyDependency) -> Self {
1103        let actions = vec![
1104            IssueAction::Fix(FixAction {
1105                kind: FixActionType::MoveToDev,
1106                auto_fixable: false,
1107                description: "Move to devDependencies (only test files import this)".to_string(),
1108                note: Some(
1109                    "Only test files import this package so it does not need to be a production dependency"
1110                        .to_string(),
1111                ),
1112                available_in_catalogs: None,
1113                suggested_target: None,
1114            }),
1115            build_ignore_dependencies_suppress_action(&dep.package_name, "test-only-dependency"),
1116        ];
1117        Self {
1118            dep,
1119            actions,
1120            introduced: None,
1121        }
1122    }
1123}
1124
1125// ── Catalog / dep-override family ───────────────────────────────
1126//
1127// These six wrappers replace the legacy `inject_actions` post-pass in
1128// `crates/cli/src/report/json.rs` for the catalog and dependency-override
1129// findings. Each `with_actions(...)` builds the typed `actions` array
1130// directly from the inner struct (and any per-call context such as
1131// `config_fixable`), so the wire shape is identical to the pre-2.76
1132// post-pass output but the Rust compiler now owns the action contract.
1133
1134/// Wire-shape envelope for a [`DuplicateExport`] finding. Carries up to
1135/// three actions in position-locked order: an `add-to-config` `ignoreExports`
1136/// snippet (only when `locations[]` carries at least one path) followed by
1137/// the `remove-duplicate` fix and the multi-location suppress.
1138///
1139/// The `add-to-config` action sits at position 0 because the documented
1140/// primary slot points at the safe, non-destructive path: the shadcn /
1141/// Radix / bits-ui namespace-barrel case where every `index.*` reexports
1142/// the directory's neighbours. The `remove-duplicate` fix stays as the
1143/// secondary so consumers that pattern-match on `actions[0].type` for
1144/// "primary fix" never propose deletion of an intentional barrel surface.
1145#[derive(Debug, Clone, Serialize)]
1146#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1147pub struct DuplicateExportFinding {
1148    /// The underlying finding.
1149    #[serde(flatten)]
1150    pub export: DuplicateExport,
1151    /// Suggested next steps. Always emitted (possibly empty for
1152    /// forward-compat).
1153    pub actions: Vec<IssueAction>,
1154    /// Set by the audit pass when this finding is introduced relative to
1155    /// the merge-base.
1156    #[serde(default, skip_serializing_if = "Option::is_none")]
1157    pub introduced: Option<AuditIntroduced>,
1158}
1159
1160impl DuplicateExportFinding {
1161    /// Build the wrapper with the `add-to-config` action's `auto_fixable`
1162    /// defaulting to `false`. The CLI's `build_json_with_config_fixable`
1163    /// path layers the actual `config_fixable` signal via
1164    /// [`Self::set_config_fixable`] right before serialization (the
1165    /// fix-applier readiness check lives in `fallow-cli::fix` and is not
1166    /// reachable from the analyzer layer where wrappers are first built).
1167    /// Embedders that build `AnalysisResults` directly and never route
1168    /// through the CLI's JSON path keep the conservative default.
1169    #[must_use]
1170    pub fn with_actions(export: DuplicateExport) -> Self {
1171        let mut actions: Vec<IssueAction> = Vec::with_capacity(3);
1172
1173        if let Some(rules) = build_duplicate_exports_ignore_rules(&export) {
1174            actions.push(IssueAction::AddToConfig(AddToConfigAction {
1175                kind: AddToConfigKind::AddToConfig,
1176                auto_fixable: false,
1177                description: "Add an ignoreExports rule so these files are excluded from duplicate-export grouping (use when this duplication is an intentional namespace-barrel API).".to_string(),
1178                config_key: "ignoreExports".to_string(),
1179                value: AddToConfigValue::ExportsRules(rules),
1180                value_schema: Some(IGNORE_EXPORTS_VALUE_SCHEMA.to_string()),
1181            }));
1182        }
1183
1184        actions.push(IssueAction::Fix(FixAction {
1185            kind: FixActionType::RemoveDuplicate,
1186            auto_fixable: false,
1187            description: "Keep one canonical export location and remove the others".to_string(),
1188            note: Some(NAMESPACE_BARREL_HINT.to_string()),
1189            available_in_catalogs: None,
1190            suggested_target: None,
1191        }));
1192
1193        actions.push(IssueAction::SuppressLine(SuppressLineAction {
1194            kind: SuppressLineKind::SuppressLine,
1195            auto_fixable: false,
1196            description: "Suppress with an inline comment above the line".to_string(),
1197            comment: "// fallow-ignore-next-line duplicate-export".to_string(),
1198            scope: Some(SuppressLineScope::PerLocation),
1199        }));
1200
1201        Self {
1202            export,
1203            actions,
1204            introduced: None,
1205        }
1206    }
1207
1208    /// Update the position-0 `add-to-config` action's `auto_fixable` flag.
1209    /// Idempotent and a no-op when position 0 is not an `add-to-config`
1210    /// action (happens when the finding has no locations). Called by the
1211    /// CLI's JSON serializer with the result of
1212    /// `crate::fix::is_config_fixable` before emitting bytes.
1213    pub fn set_config_fixable(&mut self, fixable: bool) {
1214        if let Some(IssueAction::AddToConfig(action)) = self.actions.first_mut() {
1215            action.auto_fixable = fixable;
1216        }
1217    }
1218}
1219
1220/// Build a paste-ready `ignoreExports` config value from a duplicate-export
1221/// finding's locations. Returns one `{ file, exports: ["*"] }` entry per
1222/// distinct file in insertion order. `None` when no locations carry a path.
1223fn build_duplicate_exports_ignore_rules(
1224    export: &DuplicateExport,
1225) -> Option<Vec<IgnoreExportsRule>> {
1226    let mut entries: Vec<IgnoreExportsRule> = Vec::with_capacity(export.locations.len());
1227    for loc in &export.locations {
1228        // Normalize separators to forward slashes so pasting the action value
1229        // into `.fallowrc.json` produces a portable rule. On Windows
1230        // `to_string_lossy` preserves backslashes, which the old
1231        // `inject_actions` post-pass implicitly normalized because it read
1232        // the path AFTER `strip_root_prefix` had already run through
1233        // `normalize_uri`; the typed wrapper builds the value before
1234        // serialization, so the normalization has to be explicit here.
1235        let path = loc.path.to_string_lossy().replace('\\', "/");
1236        if path.is_empty() {
1237            continue;
1238        }
1239        if entries.iter().any(|existing| existing.file == path) {
1240            continue;
1241        }
1242        entries.push(IgnoreExportsRule {
1243            file: path,
1244            exports: vec!["*".to_string()],
1245        });
1246    }
1247    if entries.is_empty() {
1248        None
1249    } else {
1250        Some(entries)
1251    }
1252}
1253
1254/// Wire-shape envelope for an [`UnusedCatalogEntry`] finding. Per-instance
1255/// `auto_fixable` flips to `false` when `hardcoded_consumers` is non-empty:
1256/// the entry cannot be removed safely while a workspace package still pins
1257/// the same package via a hardcoded version range.
1258#[derive(Debug, Clone, Serialize)]
1259#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1260pub struct UnusedCatalogEntryFinding {
1261    /// The underlying finding.
1262    #[serde(flatten)]
1263    pub entry: UnusedCatalogEntry,
1264    /// Suggested next steps. Always emitted.
1265    pub actions: Vec<IssueAction>,
1266    /// Set by the audit pass when this finding is introduced relative to
1267    /// the merge-base.
1268    #[serde(default, skip_serializing_if = "Option::is_none")]
1269    pub introduced: Option<AuditIntroduced>,
1270}
1271
1272impl UnusedCatalogEntryFinding {
1273    /// Build the wrapper. Per-instance `auto_fixable` is `true` only when
1274    /// `hardcoded_consumers` is empty; otherwise `fallow fix` skips the
1275    /// entry to avoid breaking `pnpm install` on the holdout consumer.
1276    #[must_use]
1277    pub fn with_actions(entry: UnusedCatalogEntry) -> Self {
1278        let auto_fixable = entry.hardcoded_consumers.is_empty();
1279        let actions = vec![
1280            IssueAction::Fix(FixAction {
1281                kind: FixActionType::RemoveCatalogEntry,
1282                auto_fixable,
1283                description: "Remove the entry from pnpm-workspace.yaml".to_string(),
1284                note: Some(
1285                    "If any consumer declares the same package with a hardcoded version, switch the consumer to `catalog:` before removing"
1286                        .to_string(),
1287                ),
1288                available_in_catalogs: None,
1289                suggested_target: None,
1290            }),
1291            IssueAction::SuppressLine(SuppressLineAction {
1292                kind: SuppressLineKind::SuppressLine,
1293                auto_fixable: false,
1294                description: "Suppress with a YAML comment above the line".to_string(),
1295                comment: "# fallow-ignore-next-line unused-catalog-entry".to_string(),
1296                scope: None,
1297            }),
1298        ];
1299        Self {
1300            entry,
1301            actions,
1302            introduced: None,
1303        }
1304    }
1305}
1306
1307/// Wire-shape envelope for an [`EmptyCatalogGroup`] finding. Carries a
1308/// straightforward `remove-empty-catalog-group` primary plus a YAML-comment
1309/// suppress.
1310#[derive(Debug, Clone, Serialize)]
1311#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1312pub struct EmptyCatalogGroupFinding {
1313    /// The underlying finding.
1314    #[serde(flatten)]
1315    pub group: EmptyCatalogGroup,
1316    /// Suggested next steps. Always emitted.
1317    pub actions: Vec<IssueAction>,
1318    /// Set by the audit pass when this finding is introduced relative to
1319    /// the merge-base.
1320    #[serde(default, skip_serializing_if = "Option::is_none")]
1321    pub introduced: Option<AuditIntroduced>,
1322}
1323
1324impl EmptyCatalogGroupFinding {
1325    /// Build the wrapper.
1326    #[must_use]
1327    pub fn with_actions(group: EmptyCatalogGroup) -> Self {
1328        let actions = vec![
1329            IssueAction::Fix(FixAction {
1330                kind: FixActionType::RemoveEmptyCatalogGroup,
1331                auto_fixable: true,
1332                description: "Remove the empty named catalog group from pnpm-workspace.yaml"
1333                    .to_string(),
1334                note: Some(
1335                    "Only named groups under `catalogs:` are flagged; the top-level `catalog:` hook is intentionally ignored"
1336                        .to_string(),
1337                ),
1338                available_in_catalogs: None,
1339                suggested_target: None,
1340            }),
1341            IssueAction::SuppressLine(SuppressLineAction {
1342                kind: SuppressLineKind::SuppressLine,
1343                auto_fixable: false,
1344                description: "Suppress with a YAML comment above the line".to_string(),
1345                comment: "# fallow-ignore-next-line empty-catalog-group".to_string(),
1346                scope: None,
1347            }),
1348        ];
1349        Self {
1350            group,
1351            actions,
1352            introduced: None,
1353        }
1354    }
1355}
1356
1357/// Wire-shape envelope for an [`UnresolvedCatalogReference`] finding. The
1358/// primary action at position 0 discriminates on `available_in_catalogs`:
1359/// `add-catalog-entry` when the array is empty (no other catalog declares
1360/// the package), or `update-catalog-reference` when at least one
1361/// alternative exists. When exactly one alternative exists, the action
1362/// also carries `suggested_target` so deterministic agents can land the
1363/// edit without picking from a list.
1364#[derive(Debug, Clone, Serialize)]
1365#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1366pub struct UnresolvedCatalogReferenceFinding {
1367    /// The underlying finding.
1368    #[serde(flatten)]
1369    pub reference: UnresolvedCatalogReference,
1370    /// Suggested next steps. Always emitted; position 0 is the discriminated
1371    /// primary (see struct docs).
1372    pub actions: Vec<IssueAction>,
1373    /// Set by the audit pass when this finding is introduced relative to
1374    /// the merge-base.
1375    #[serde(default, skip_serializing_if = "Option::is_none")]
1376    pub introduced: Option<AuditIntroduced>,
1377}
1378
1379impl UnresolvedCatalogReferenceFinding {
1380    /// Build the wrapper. The discriminator at position 0 is the
1381    /// `add-catalog-entry` vs `update-catalog-reference` pick documented on
1382    /// the struct.
1383    #[must_use]
1384    pub fn with_actions(reference: UnresolvedCatalogReference) -> Self {
1385        // Normalize separators to forward slashes so the
1386        // `ignoreCatalogReferences.consumer` action value is portable when
1387        // pasted into a Windows-authored config. See
1388        // `build_duplicate_exports_ignore_rules` for the same pattern.
1389        let consumer_path = reference.path.to_string_lossy().replace('\\', "/");
1390        let primary = if reference.available_in_catalogs.is_empty() {
1391            IssueAction::Fix(FixAction {
1392                kind: FixActionType::AddCatalogEntry,
1393                auto_fixable: false,
1394                description: format!(
1395                    "Add `{}` to the `{}` catalog in pnpm-workspace.yaml",
1396                    reference.entry_name, reference.catalog_name
1397                ),
1398                note: Some(
1399                    "Pin a version that satisfies the consumer's import; no other catalog declares this package today"
1400                        .to_string(),
1401                ),
1402                available_in_catalogs: None,
1403                suggested_target: None,
1404            })
1405        } else {
1406            let available = reference.available_in_catalogs.clone();
1407            let suggested_target = (available.len() == 1).then(|| available[0].clone());
1408            IssueAction::Fix(FixAction {
1409                kind: FixActionType::UpdateCatalogReference,
1410                auto_fixable: false,
1411                description: format!(
1412                    "Switch the reference from `catalog:{}` to a catalog that declares `{}`",
1413                    reference.catalog_name, reference.entry_name
1414                ),
1415                note: None,
1416                available_in_catalogs: Some(available),
1417                suggested_target,
1418            })
1419        };
1420
1421        let fallback = IssueAction::Fix(FixAction {
1422            kind: FixActionType::RemoveCatalogReference,
1423            auto_fixable: false,
1424            description:
1425                "Remove the catalog reference and pin a hardcoded version in package.json"
1426                    .to_string(),
1427            note: Some(
1428                "Use only when neither another catalog declares the package nor the named catalog should grow to include it"
1429                    .to_string(),
1430            ),
1431            available_in_catalogs: None,
1432            suggested_target: None,
1433        });
1434
1435        let mut suppress_value = serde_json::Map::new();
1436        suppress_value.insert(
1437            "package".to_string(),
1438            serde_json::Value::String(reference.entry_name.clone()),
1439        );
1440        suppress_value.insert(
1441            "catalog".to_string(),
1442            serde_json::Value::String(reference.catalog_name.clone()),
1443        );
1444        suppress_value.insert(
1445            "consumer".to_string(),
1446            serde_json::Value::String(consumer_path),
1447        );
1448        let suppress = IssueAction::AddToConfig(AddToConfigAction {
1449            kind: AddToConfigKind::AddToConfig,
1450            auto_fixable: false,
1451            description: "Suppress this reference via ignoreCatalogReferences in fallow config (use when the catalog edit is intentionally landing in a separate PR or the package is a placeholder).".to_string(),
1452            config_key: "ignoreCatalogReferences".to_string(),
1453            value: AddToConfigValue::RuleObject(suppress_value),
1454            value_schema: Some(IGNORE_CATALOG_REFERENCES_VALUE_SCHEMA.to_string()),
1455        });
1456
1457        Self {
1458            reference,
1459            actions: vec![primary, fallback, suppress],
1460            introduced: None,
1461        }
1462    }
1463}
1464
1465/// Wire-shape envelope for an [`UnusedDependencyOverride`] finding. Carries
1466/// a `remove-dependency-override` primary plus an `add-to-config`
1467/// `ignoreDependencyOverrides` suppress scoped to the target package and
1468/// declaration source.
1469#[derive(Debug, Clone, Serialize)]
1470#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1471pub struct UnusedDependencyOverrideFinding {
1472    /// The underlying finding.
1473    #[serde(flatten)]
1474    pub entry: UnusedDependencyOverride,
1475    /// Suggested next steps. Always emitted.
1476    pub actions: Vec<IssueAction>,
1477    /// Set by the audit pass when this finding is introduced relative to
1478    /// the merge-base.
1479    #[serde(default, skip_serializing_if = "Option::is_none")]
1480    pub introduced: Option<AuditIntroduced>,
1481}
1482
1483impl UnusedDependencyOverrideFinding {
1484    /// Build the wrapper.
1485    #[must_use]
1486    pub fn with_actions(entry: UnusedDependencyOverride) -> Self {
1487        let mut actions: Vec<IssueAction> = Vec::with_capacity(2);
1488        actions.push(IssueAction::Fix(FixAction {
1489            kind: FixActionType::RemoveDependencyOverride,
1490            auto_fixable: false,
1491            description: "Remove the override entry from pnpm-workspace.yaml or pnpm.overrides"
1492                .to_string(),
1493            note: Some(
1494                "Conservative static check; verify against `pnpm install --frozen-lockfile` before removing in case the override targets a transitive dependency (CVE-fix pattern)"
1495                    .to_string(),
1496            ),
1497            available_in_catalogs: None,
1498            suggested_target: None,
1499        }));
1500
1501        if let Some(suppress) = build_ignore_dependency_overrides_suppress(
1502            Some(&entry.target_package),
1503            &entry.raw_key,
1504            entry.source,
1505        ) {
1506            actions.push(suppress);
1507        }
1508
1509        Self {
1510            entry,
1511            actions,
1512            introduced: None,
1513        }
1514    }
1515}
1516
1517/// Wire-shape envelope for a [`MisconfiguredDependencyOverride`] finding.
1518/// Carries a `fix-dependency-override` primary plus the conditional
1519/// `add-to-config` `ignoreDependencyOverrides` suppress (skipped when both
1520/// `target_package` and `raw_key` are empty, since the rule matcher keys on
1521/// a non-empty package name).
1522#[derive(Debug, Clone, Serialize)]
1523#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1524pub struct MisconfiguredDependencyOverrideFinding {
1525    /// The underlying finding.
1526    #[serde(flatten)]
1527    pub entry: MisconfiguredDependencyOverride,
1528    /// Suggested next steps. Always emitted.
1529    pub actions: Vec<IssueAction>,
1530    /// Set by the audit pass when this finding is introduced relative to
1531    /// the merge-base.
1532    #[serde(default, skip_serializing_if = "Option::is_none")]
1533    pub introduced: Option<AuditIntroduced>,
1534}
1535
1536impl MisconfiguredDependencyOverrideFinding {
1537    /// Build the wrapper. The suppress action is omitted when neither
1538    /// `target_package` (set on `EmptyValue` cases) nor `raw_key` provides a
1539    /// non-empty package name; an `ignoreDependencyOverrides` entry with
1540    /// `package: ""` would be silently ignored by the config parser.
1541    #[must_use]
1542    pub fn with_actions(entry: MisconfiguredDependencyOverride) -> Self {
1543        let mut actions: Vec<IssueAction> = Vec::with_capacity(2);
1544        actions.push(IssueAction::Fix(FixAction {
1545            kind: FixActionType::FixDependencyOverride,
1546            auto_fixable: false,
1547            description:
1548                "Fix the override key or value: pnpm refuses to honor entries with an unparsable key or empty value"
1549                    .to_string(),
1550            note: Some(
1551                "Common shapes: bare `pkg`, scoped `@scope/pkg`, version-selector `pkg@<2`, parent-chain `parent>child`. Valid values include semver ranges, `-` (removal), `$ref` (self-ref), and `npm:alias@^1`."
1552                    .to_string(),
1553            ),
1554            available_in_catalogs: None,
1555            suggested_target: None,
1556        }));
1557
1558        if let Some(suppress) = build_ignore_dependency_overrides_suppress(
1559            entry.target_package.as_deref(),
1560            &entry.raw_key,
1561            entry.source,
1562        ) {
1563            actions.push(suppress);
1564        }
1565
1566        Self {
1567            entry,
1568            actions,
1569            introduced: None,
1570        }
1571    }
1572}
1573
1574/// Shared `add-to-config` `ignoreDependencyOverrides` builder for the two
1575/// override findings. Returns `None` when no non-empty package name is
1576/// available; the config parser silently drops entries with an empty
1577/// `package` field, so emitting one would be a no-op that misleads agents.
1578fn build_ignore_dependency_overrides_suppress(
1579    target_package: Option<&str>,
1580    raw_key: &str,
1581    source: DependencyOverrideSource,
1582) -> Option<IssueAction> {
1583    let package = target_package
1584        .filter(|s| !s.is_empty())
1585        .or_else(|| Some(raw_key).filter(|s| !s.is_empty()))?
1586        .to_string();
1587    let mut value = serde_json::Map::new();
1588    value.insert("package".to_string(), serde_json::Value::String(package));
1589    value.insert(
1590        "source".to_string(),
1591        serde_json::Value::String(source.as_label().to_string()),
1592    );
1593    Some(IssueAction::AddToConfig(AddToConfigAction {
1594        kind: AddToConfigKind::AddToConfig,
1595        auto_fixable: false,
1596        description: "Suppress this override finding via ignoreDependencyOverrides in fallow config (use for CVE-fix overrides that target a purely-transitive package).".to_string(),
1597        config_key: "ignoreDependencyOverrides".to_string(),
1598        value: AddToConfigValue::RuleObject(value),
1599        value_schema: Some(IGNORE_DEPENDENCY_OVERRIDES_VALUE_SCHEMA.to_string()),
1600    }))
1601}
1602
1603// ── Position-0 invariant golden tests ───────────────────────────
1604//
1605// These tests document the load-bearing position-0 semantics that flow
1606// downstream into the GitHub Action / GitLab CI jq scripts, the MCP server
1607// `actions[0].type` pattern-match, and the VS Code LSP code-action
1608// rendering. Snapshot tests assert structural equality; these named tests
1609// document WHY position 0 has a specific value, so a future refactor that
1610// re-orders actions tells you what broke instead of just "the snapshot
1611// changed".
1612#[cfg(test)]
1613mod position_0_invariants {
1614    use super::*;
1615    use crate::output::FixActionType;
1616    use crate::results::{DependencyOverrideSource, DuplicateLocation};
1617    use std::path::PathBuf;
1618
1619    /// Helper: extract the kebab-case `type` discriminant from an
1620    /// [`IssueAction`] at a specific position. Returns `None` when the
1621    /// position is out of bounds or the action shape lacks a discriminant
1622    /// (today every variant has one).
1623    fn action_type(action: &IssueAction) -> &'static str {
1624        match action {
1625            IssueAction::Fix(fix) => match fix.kind {
1626                FixActionType::RemoveExport => "remove-export",
1627                FixActionType::DeleteFile => "delete-file",
1628                FixActionType::RemoveDependency => "remove-dependency",
1629                FixActionType::MoveDependency => "move-dependency",
1630                FixActionType::RemoveEnumMember => "remove-enum-member",
1631                FixActionType::RemoveClassMember => "remove-class-member",
1632                FixActionType::ResolveImport => "resolve-import",
1633                FixActionType::InstallDependency => "install-dependency",
1634                FixActionType::RemoveDuplicate => "remove-duplicate",
1635                FixActionType::MoveToDev => "move-to-dev",
1636                FixActionType::RefactorCycle => "refactor-cycle",
1637                FixActionType::RefactorReExportCycle => "refactor-re-export-cycle",
1638                FixActionType::RefactorBoundary => "refactor-boundary",
1639                FixActionType::ExportType => "export-type",
1640                FixActionType::RemoveCatalogEntry => "remove-catalog-entry",
1641                FixActionType::RemoveEmptyCatalogGroup => "remove-empty-catalog-group",
1642                FixActionType::UpdateCatalogReference => "update-catalog-reference",
1643                FixActionType::AddCatalogEntry => "add-catalog-entry",
1644                FixActionType::RemoveCatalogReference => "remove-catalog-reference",
1645                FixActionType::RemoveDependencyOverride => "remove-dependency-override",
1646                FixActionType::FixDependencyOverride => "fix-dependency-override",
1647                FixActionType::ResolvePolicyViolation => "resolve-policy-violation",
1648            },
1649            IssueAction::SuppressLine(_) => "suppress-line",
1650            IssueAction::SuppressFile(_) => "suppress-file",
1651            IssueAction::AddToConfig(_) => "add-to-config",
1652        }
1653    }
1654
1655    #[test]
1656    fn unresolved_import_actions_include_ignore_unresolved_imports_config_suppress() {
1657        let inner = UnresolvedImport {
1658            specifier: "@example/icons".to_string(),
1659            path: PathBuf::from("src/index.ts"),
1660            line: 4,
1661            col: 12,
1662            specifier_col: 18,
1663        };
1664        let finding = UnresolvedImportFinding::with_actions(inner);
1665
1666        assert_eq!(action_type(&finding.actions[0]), "resolve-import");
1667        assert_eq!(action_type(&finding.actions[1]), "add-to-config");
1668        let IssueAction::AddToConfig(action) = &finding.actions[1] else {
1669            panic!("position-1 should be AddToConfig");
1670        };
1671        assert!(!action.auto_fixable);
1672        assert_eq!(action.config_key, "ignoreUnresolvedImports");
1673        let AddToConfigValue::Scalar(value) = &action.value else {
1674            panic!("ignoreUnresolvedImports action should carry a scalar value");
1675        };
1676        assert_eq!(value, "@example/icons");
1677        assert_eq!(
1678            action.value_schema.as_deref(),
1679            Some(
1680                "https://raw.githubusercontent.com/fallow-rs/fallow/main/schema.json#/properties/ignoreUnresolvedImports/items"
1681            )
1682        );
1683    }
1684
1685    /// Invariant: when no other catalog declares the package, position 0
1686    /// of `unresolved_catalog_references[].actions` is `add-catalog-entry`,
1687    /// directing the agent to grow the targeted catalog.
1688    ///
1689    /// Downstream consumers (MCP `actions[0].type` dispatch, jq scripts in
1690    /// `action/jq/review-comments-check.jq` and `ci/jq/review-check.jq`)
1691    /// pattern-match on this string. A future refactor that puts the
1692    /// generic `remove-catalog-reference` fallback at position 0 would
1693    /// flip every CI annotation from "add this entry" to "remove this
1694    /// reference", reversing the recommended action.
1695    #[test]
1696    fn unresolved_catalog_position_0_is_add_when_no_alternatives() {
1697        let inner = UnresolvedCatalogReference {
1698            entry_name: "react".to_string(),
1699            catalog_name: "default".to_string(),
1700            path: PathBuf::from("apps/web/package.json"),
1701            line: 7,
1702            available_in_catalogs: Vec::new(),
1703        };
1704        let finding = UnresolvedCatalogReferenceFinding::with_actions(inner);
1705        assert_eq!(
1706            action_type(&finding.actions[0]),
1707            "add-catalog-entry",
1708            "position-0 must be `add-catalog-entry` when no alternative catalog declares the package"
1709        );
1710        let IssueAction::Fix(fix) = &finding.actions[0] else {
1711            panic!("position-0 should be an IssueAction::Fix");
1712        };
1713        assert!(
1714            fix.available_in_catalogs.is_none(),
1715            "add-catalog-entry must NOT carry available_in_catalogs"
1716        );
1717        assert!(
1718            fix.suggested_target.is_none(),
1719            "add-catalog-entry must NOT carry suggested_target"
1720        );
1721    }
1722
1723    /// Invariant: when at least one alternative catalog declares the
1724    /// package, position 0 flips to `update-catalog-reference` and carries
1725    /// the alternative list. When exactly one alternative exists, the
1726    /// action also carries `suggested_target` so deterministic agents can
1727    /// land the edit without picking from the list. This is the
1728    /// counterpart to `unresolved_catalog_position_0_is_add_when_no_alternatives`.
1729    #[test]
1730    fn unresolved_catalog_position_0_is_update_when_alternatives_exist() {
1731        let inner = UnresolvedCatalogReference {
1732            entry_name: "react".to_string(),
1733            catalog_name: "default".to_string(),
1734            path: PathBuf::from("apps/web/package.json"),
1735            line: 7,
1736            available_in_catalogs: vec!["react18".to_string()],
1737        };
1738        let finding = UnresolvedCatalogReferenceFinding::with_actions(inner);
1739        assert_eq!(
1740            action_type(&finding.actions[0]),
1741            "update-catalog-reference",
1742            "position-0 must be `update-catalog-reference` when at least one alternative catalog declares the package"
1743        );
1744        let IssueAction::Fix(fix) = &finding.actions[0] else {
1745            panic!("position-0 should be an IssueAction::Fix");
1746        };
1747        assert_eq!(
1748            fix.available_in_catalogs.as_deref(),
1749            Some(&["react18".to_string()][..]),
1750            "update-catalog-reference must carry the alternative list"
1751        );
1752        assert_eq!(
1753            fix.suggested_target.as_deref(),
1754            Some("react18"),
1755            "single-alternative case must surface `suggested_target` for deterministic agents"
1756        );
1757
1758        // Two alternatives: still update, but no unambiguous target.
1759        let inner_two = UnresolvedCatalogReference {
1760            entry_name: "react".to_string(),
1761            catalog_name: "default".to_string(),
1762            path: PathBuf::from("apps/web/package.json"),
1763            line: 7,
1764            available_in_catalogs: vec!["react17".to_string(), "react18".to_string()],
1765        };
1766        let finding_two = UnresolvedCatalogReferenceFinding::with_actions(inner_two);
1767        assert_eq!(
1768            action_type(&finding_two.actions[0]),
1769            "update-catalog-reference"
1770        );
1771        let IssueAction::Fix(fix_two) = &finding_two.actions[0] else {
1772            panic!("position-0 should be an IssueAction::Fix");
1773        };
1774        assert!(
1775            fix_two.suggested_target.is_none(),
1776            "multi-alternative case must NOT carry `suggested_target` (agent must pick)"
1777        );
1778    }
1779
1780    /// Invariant: position 0 of `duplicate_exports[].actions` is
1781    /// `add-to-config` (the safe `ignoreExports` rule for the
1782    /// namespace-barrel case), NOT the destructive `remove-duplicate`.
1783    ///
1784    /// This protects the shadcn / Radix / bits-ui pattern where every
1785    /// `components/ui/<name>/index.ts` intentionally re-exports the same
1786    /// short names. Any consumer that reads `actions[0].type` as "the
1787    /// recommended fix" must see the non-destructive path first; flipping
1788    /// position 0 to `remove-duplicate` would propose deleting an
1789    /// intentional API surface.
1790    ///
1791    /// This test pins position 0 across both possible auto_fixable values
1792    /// for the add-to-config action (the per-instance flip flag handled
1793    /// by `set_config_fixable`).
1794    #[test]
1795    fn duplicate_exports_position_0_is_add_to_config_not_remove_duplicate() {
1796        let inner = DuplicateExport {
1797            export_name: "Root".to_string(),
1798            locations: vec![
1799                DuplicateLocation {
1800                    path: PathBuf::from("components/ui/accordion/index.ts"),
1801                    line: 1,
1802                    col: 0,
1803                },
1804                DuplicateLocation {
1805                    path: PathBuf::from("components/ui/dialog/index.ts"),
1806                    line: 1,
1807                    col: 0,
1808                },
1809            ],
1810        };
1811        let finding = DuplicateExportFinding::with_actions(inner);
1812        assert_eq!(
1813            action_type(&finding.actions[0]),
1814            "add-to-config",
1815            "position-0 must be `add-to-config` (safe `ignoreExports` path), NOT `remove-duplicate`"
1816        );
1817        assert_eq!(
1818            action_type(&finding.actions[1]),
1819            "remove-duplicate",
1820            "position-1 must be the destructive `remove-duplicate` fallback"
1821        );
1822
1823        // `set_config_fixable(true)` flips the position-0 add-to-config
1824        // bool but must NOT re-order positions.
1825        let mut promoted = finding;
1826        promoted.set_config_fixable(true);
1827        assert_eq!(action_type(&promoted.actions[0]), "add-to-config");
1828        let IssueAction::AddToConfig(action) = &promoted.actions[0] else {
1829            panic!("position-0 should still be AddToConfig after set_config_fixable");
1830        };
1831        assert!(
1832            action.auto_fixable,
1833            "set_config_fixable(true) must flip auto_fixable"
1834        );
1835    }
1836
1837    /// Invariant: a duplicate-exports finding with empty `locations`
1838    /// degenerate input drops the `add-to-config` action entirely, so
1839    /// position 0 falls through to `remove-duplicate`. Documents the
1840    /// degenerate-case contract.
1841    #[test]
1842    fn duplicate_exports_no_locations_falls_through_to_remove_duplicate() {
1843        let inner = DuplicateExport {
1844            export_name: "Root".to_string(),
1845            locations: Vec::new(),
1846        };
1847        let finding = DuplicateExportFinding::with_actions(inner);
1848        assert_eq!(
1849            action_type(&finding.actions[0]),
1850            "remove-duplicate",
1851            "with no locations there is no ignoreExports rule to suggest; the destructive remove becomes position-0"
1852        );
1853
1854        // `set_config_fixable(true)` is a no-op on this shape.
1855        let mut promoted = finding;
1856        promoted.set_config_fixable(true);
1857        assert_eq!(
1858            action_type(&promoted.actions[0]),
1859            "remove-duplicate",
1860            "set_config_fixable is a no-op when position-0 is not add-to-config"
1861        );
1862    }
1863
1864    /// Invariant: misconfigured-dependency-override with empty
1865    /// `target_package` AND empty `raw_key` drops the suppress action
1866    /// (no usable package name for the `ignoreDependencyOverrides`
1867    /// matcher; emitting `package: ""` would be silently dropped by the
1868    /// config parser). Documents the suppress-omission contract.
1869    #[test]
1870    fn misconfigured_override_drops_suppress_when_no_package_name() {
1871        let inner = MisconfiguredDependencyOverride {
1872            raw_key: String::new(),
1873            target_package: None,
1874            raw_value: String::new(),
1875            reason: crate::results::DependencyOverrideMisconfigReason::EmptyValue,
1876            source: DependencyOverrideSource::PnpmWorkspaceYaml,
1877            path: PathBuf::from("pnpm-workspace.yaml"),
1878            line: 12,
1879        };
1880        let finding = MisconfiguredDependencyOverrideFinding::with_actions(inner);
1881        // Only the primary fix-dependency-override action: no suppress.
1882        assert_eq!(finding.actions.len(), 1);
1883        assert_eq!(action_type(&finding.actions[0]), "fix-dependency-override");
1884    }
1885}