Skip to main content

fallow_types/
output.rs

1//! Types that describe fallow's JSON output contract.
2//!
3//! Today the JSON serialization layer (`crates/cli/src/report/json.rs`) builds
4//! its output via `serde_json::json!` macros. The types defined here are the
5//! schema-side counterpart of that output: they document, with Rust's type
6//! system, the augmentations the JSON layer adds to each per-finding struct
7//! (the `actions` array on every finding, the optional `introduced` flag in
8//! audit-mode sub-results).
9//!
10//! The `schema-emit` binary derives `JsonSchema` for these types (gated by the
11//! `schema` cargo feature) so the public `docs/output-schema.json` stays in
12//! sync with the Rust source of truth. A future refactor will route the JSON
13//! emission path through these types directly, eliminating the drift class
14//! between the augmentation list here and the `serde_json::json!` builders.
15
16use serde::Serialize;
17
18/// A suggested action attached to a finding in the JSON output. Each finding
19/// carries an `actions` array; consumers (agents, IDE clients, CI bots) can
20/// dispatch on the `type` discriminant to choose the right remediation.
21///
22/// The discriminator is `type` (snake_case `type` field), the payload uses the
23/// matching kebab-case identifier per variant.
24///
25/// ## `auto_fixable` is per-finding, not per action type
26///
27/// Every action variant carries an `auto_fixable: bool` field. The value is
28/// evaluated PER FINDING, not per action type: the same action type may
29/// appear with `auto_fixable: true` on one finding and `auto_fixable: false`
30/// on another, depending on per-instance guards in the `fallow fix` applier.
31/// Agents that filter on `auto_fixable: true` must branch on the bool of
32/// each individual finding's action, not on the action `type` alone.
33///
34/// Current per-instance flips:
35///
36/// - `remove-catalog-entry` (`unused-catalog-entries`): `true` only when the
37///   finding's `hardcoded_consumers` array is empty. When a workspace
38///   package still pins a hardcoded version of the same package, `fallow fix`
39///   skips the entry to avoid breaking `pnpm install`, and the action is
40///   emitted with `auto_fixable: false`.
41/// - `remove-dependency` vs `move-dependency` (dependency findings): when the
42///   finding's `used_in_workspaces` array is non-empty, the primary action
43///   flips to `move-dependency` with `auto_fixable: false` (`fallow fix` will
44///   not remove a dependency that another workspace imports). On findings
45///   without cross-workspace consumers the action stays `remove-dependency`
46///   with `auto_fixable: true`.
47/// - `add-to-config` for `ignoreExports` (`duplicate-exports`): `true` when
48///   `fallow fix` can safely apply the action without further user setup.
49///   That is: a fallow config file exists on disk, OR no config exists AND
50///   the working directory is NOT inside a monorepo subpackage (in which
51///   case the applier creates `.fallowrc.json` from `fallow init`'s
52///   framework-aware scaffolding and layers the new rules on top).
53///   `false` inside a monorepo subpackage with no workspace-root config
54///   (the applier refuses to fragment per-package configs across the
55///   monorepo and points at the workspace root instead).
56/// - `update-catalog-reference` (`unresolved-catalog-references`): always
57///   `false` today (the catalog-switching applier is not wired in yet); the
58///   field is non-singleton so that future enablement does not require a
59///   schema change.
60///
61/// All `suppress-line` and `suppress-file` actions are uniformly
62/// `auto_fixable: false`. The field is non-singleton on the wire so that a
63/// future auto-applier (e.g. an LLM-driven suppression writer) can promote
64/// individual variants without a schema bump.
65#[derive(Debug, Clone, Serialize)]
66#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
67#[serde(untagged)]
68pub enum IssueAction {
69    /// A code-change fix the user can apply (auto-fixable by `fallow fix` for
70    /// some variants, manual for others).
71    Fix(FixAction),
72    /// Place a `// fallow-ignore-next-line ...` comment above the offending
73    /// line. Always manual.
74    SuppressLine(SuppressLineAction),
75    /// Place a `// fallow-ignore-file ...` comment at the top of the file.
76    /// Always manual.
77    SuppressFile(SuppressFileAction),
78    /// Add the offending finding to the fallow config (e.g.
79    /// `ignoreDependencies: ["lodash"]`). Auto-fixable for the array-shaped
80    /// `ignoreExports` variant when `fallow fix` can safely apply the
81    /// action (config file exists, or no config exists and the working
82    /// directory is not inside a monorepo subpackage); manual otherwise.
83    AddToConfig(AddToConfigAction),
84}
85
86/// A code-change fix. `type` is one of the kebab-case identifiers in
87/// [`FixActionType`].
88#[derive(Debug, Clone, Serialize)]
89#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
90pub struct FixAction {
91    /// Kebab-case identifier for the fix action.
92    #[serde(rename = "type")]
93    pub kind: FixActionType,
94    /// Whether `fallow fix` can apply this fix automatically. Evaluated PER
95    /// FINDING, not per action type: the same `type` may carry
96    /// `auto_fixable: true` on one finding and `auto_fixable: false` on
97    /// another when per-instance guards in the applier discriminate (e.g.
98    /// `remove-catalog-entry` flips on `hardcoded_consumers`, the primary
99    /// dependency action flips between `remove-dependency` /
100    /// `move-dependency` on `used_in_workspaces`). Filter on this bool of
101    /// each individual action, not on `type`. See the [`IssueAction`]
102    /// enum-level docs for the full list of per-instance flips.
103    pub auto_fixable: bool,
104    /// Human-readable description of the fix.
105    pub description: String,
106    /// Optional context note. Present on non-auto-fixable actions, and on
107    /// auto-fixable re-export findings to warn about public API surface.
108    #[serde(default, skip_serializing_if = "Option::is_none")]
109    pub note: Option<String>,
110    /// Only present on `update-catalog-reference` actions: catalogs in the
111    /// same workspace that DO declare the package, sorted lexicographically.
112    /// Lets agents pick the catalog to switch to without re-reading the
113    /// source.
114    #[serde(default, skip_serializing_if = "Option::is_none")]
115    pub available_in_catalogs: Option<Vec<String>>,
116    /// Only present on `update-catalog-reference` actions when exactly one
117    /// alternative catalog declares the package: the unambiguous switch
118    /// target. Lets deterministic (non-LLM) agents land the edit without
119    /// picking from a list. Absent when `available_in_catalogs` has zero
120    /// or more than one entry.
121    #[serde(default, skip_serializing_if = "Option::is_none")]
122    pub suggested_target: Option<String>,
123}
124
125/// Discriminant string for [`FixAction`]. Kebab-case per the JSON output
126/// contract.
127#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
128#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
129#[serde(rename_all = "kebab-case")]
130pub enum FixActionType {
131    /// Remove an export declaration from a source file.
132    RemoveExport,
133    /// Delete an entire unused file.
134    DeleteFile,
135    /// Remove an entry from `dependencies` / `devDependencies` in
136    /// `package.json`.
137    RemoveDependency,
138    /// Move an entry between `dependencies` and `devDependencies`.
139    MoveDependency,
140    /// Remove an enum member from a TypeScript enum.
141    RemoveEnumMember,
142    /// Remove a class member (method or property).
143    RemoveClassMember,
144    /// Resolve an unresolved import (manual).
145    ResolveImport,
146    /// Install a missing dependency.
147    InstallDependency,
148    /// Remove a duplicate export (the canonical action for
149    /// `duplicate-exports`).
150    RemoveDuplicate,
151    /// Move a production dependency to `devDependencies`
152    /// (used by type-only-dependency and test-only-dependency findings).
153    MoveToDev,
154    /// Break a circular dependency by refactoring imports.
155    RefactorCycle,
156    /// Resolve a boundary violation by refactoring the import.
157    RefactorBoundary,
158    /// Convert an import statement to a type-only import (used by
159    /// private-type-leak findings).
160    ExportType,
161    /// Remove an unused catalog entry from `pnpm-workspace.yaml`.
162    RemoveCatalogEntry,
163    /// Remove an empty named catalog group from `pnpm-workspace.yaml`.
164    RemoveEmptyCatalogGroup,
165    /// Update an existing `catalog:` reference in a workspace `package.json`
166    /// to point at a different (declared) catalog.
167    UpdateCatalogReference,
168    /// Add the missing entry to the referenced catalog.
169    AddCatalogEntry,
170    /// Remove the catalog reference from the workspace `package.json` and
171    /// replace it with a hardcoded version.
172    RemoveCatalogReference,
173    /// Remove an unused dependency override entry.
174    RemoveDependencyOverride,
175    /// Fix a misconfigured dependency override entry (unparsable key or empty
176    /// value).
177    FixDependencyOverride,
178}
179
180/// Inline-comment suppression for a single finding line.
181#[derive(Debug, Clone, Serialize)]
182#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
183pub struct SuppressLineAction {
184    /// Action type identifier.
185    #[serde(rename = "type")]
186    pub kind: SuppressLineKind,
187    /// Always false for suppress actions.
188    pub auto_fixable: bool,
189    /// Human-readable description of the suppression.
190    pub description: String,
191    /// The inline comment to place above the line (e.g.,
192    /// `// fallow-ignore-next-line unused-export`). When multiple
193    /// suppressible findings share the same path and line, this may contain a
194    /// comma-separated issue-kind list such as
195    /// `// fallow-ignore-next-line unused-export, complexity`.
196    pub comment: String,
197    /// Present on multi-location issue types (e.g., `duplicate_exports`) to
198    /// indicate the comment must be applied at each location.
199    #[serde(default, skip_serializing_if = "Option::is_none")]
200    pub scope: Option<SuppressLineScope>,
201}
202
203/// Singleton discriminant for [`SuppressLineAction`].
204#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
205#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
206#[serde(rename_all = "kebab-case")]
207pub enum SuppressLineKind {
208    /// `// fallow-ignore-next-line <kind>` directive.
209    SuppressLine,
210}
211
212/// Scope marker for line suppressions that span multiple locations.
213#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
214#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
215#[serde(rename_all = "kebab-case")]
216pub enum SuppressLineScope {
217    /// Apply the suppression comment at each location of the multi-location
218    /// finding (e.g., every `duplicate_exports` site).
219    PerLocation,
220}
221
222/// File-wide suppression placed at the top of the source file.
223#[derive(Debug, Clone, Serialize)]
224#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
225pub struct SuppressFileAction {
226    /// Action type identifier.
227    #[serde(rename = "type")]
228    pub kind: SuppressFileKind,
229    /// Always false for suppress actions.
230    pub auto_fixable: bool,
231    /// Human-readable description of the suppression.
232    pub description: String,
233    /// The file-level comment to place at the top of the file (e.g.,
234    /// `// fallow-ignore-file unused-file`).
235    pub comment: String,
236}
237
238/// Singleton discriminant for [`SuppressFileAction`].
239#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
240#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
241#[serde(rename_all = "kebab-case")]
242pub enum SuppressFileKind {
243    /// `// fallow-ignore-file <kind>` directive.
244    SuppressFile,
245}
246
247/// Edit a fallow config file (`.fallowrc.json`, `fallow.toml`, etc.) to
248/// add the offending value to an `ignore*` rule.
249#[derive(Debug, Clone, Serialize)]
250#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
251pub struct AddToConfigAction {
252    /// Action type identifier.
253    #[serde(rename = "type")]
254    pub kind: AddToConfigKind,
255    /// True when `fallow fix` can apply this config action automatically.
256    /// Evaluated PER FINDING, not per action type: `ignoreExports`
257    /// duplicate-export actions are auto-fixable when `fallow fix` can
258    /// safely write the rule, which today means EITHER a fallow config
259    /// file already exists OR no config exists and the working directory
260    /// is NOT inside a monorepo subpackage (in which case the applier
261    /// creates `.fallowrc.json` from `fallow init`'s framework-aware
262    /// scaffolding). The action is `false` inside a monorepo subpackage
263    /// with no workspace-root config because the applier refuses to
264    /// fragment per-package configs across the monorepo. Older scalar
265    /// config-ignore actions (e.g. `ignoreDependencies` on dependency
266    /// findings) are always manual today. Filter on this bool of each
267    /// individual action, not on the `type` alone. See the [`IssueAction`]
268    /// enum-level docs for the full list of per-instance flips.
269    pub auto_fixable: bool,
270    /// Human-readable description of the config change.
271    pub description: String,
272    /// The fallow config key to add the value to (e.g.,
273    /// `ignoreDependencies`).
274    pub config_key: String,
275    /// Value to add to the config key. Shape depends on `config_key`. For
276    /// scalar config keys (`ignoreDependencies`, others) this is a string
277    /// such as `"lodash"`. For `ignoreExports` this is an array of
278    /// `{ file, exports }` rule objects so the snippet can be merged into
279    /// the user's config verbatim. For `ignoreCatalogReferences` and
280    /// `ignoreDependencyOverrides` this is an object whose shape matches the
281    /// rule entry users add to their fallow config.
282    pub value: AddToConfigValue,
283    /// Optional URL pointing at a stable JSON Schema fragment that describes
284    /// the shape of `value`. Agents that intend to validate `value` before
285    /// writing it into a user's config can fetch the linked schema and run
286    /// it against `value`. The URL is a JSON Pointer fragment into fallow's
287    /// main config schema (e.g.
288    /// `schema.json#/properties/ignoreExports` for the ignoreExports
289    /// action, or `schema.json#/properties/ignoreDependencies/items` for
290    /// the per-package ignoreDependencies action). Strictly additive:
291    /// consumers that ignore the field keep working unchanged.
292    #[serde(default, skip_serializing_if = "Option::is_none")]
293    pub value_schema: Option<String>,
294}
295
296/// Singleton discriminant for [`AddToConfigAction`].
297#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
298#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
299#[serde(rename_all = "kebab-case")]
300pub enum AddToConfigKind {
301    /// Append a value into a fallow config `ignore*` list.
302    AddToConfig,
303}
304
305/// Value payload for [`AddToConfigAction::value`]. The variants line up with
306/// the documented per-`config_key` shapes; deserialization is untagged so
307/// downstream consumers can switch on the JSON value's type.
308#[derive(Debug, Clone, Serialize)]
309#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
310#[serde(untagged)]
311pub enum AddToConfigValue {
312    /// Scalar string value (e.g., a package name for
313    /// `ignoreDependencies: ["lodash"]`).
314    Scalar(String),
315    /// Array of file+export rule objects for `ignoreExports`.
316    ExportsRules(Vec<IgnoreExportsRule>),
317    /// Free-form object for rule-shaped keys like
318    /// `ignoreCatalogReferences` / `ignoreDependencyOverrides`. The shape
319    /// matches the rule entry users add to their fallow config; consumers
320    /// validate against the per-key schema referenced by `value_schema`.
321    RuleObject(serde_json::Map<String, serde_json::Value>),
322}
323
324/// Single `ignoreExports` rule entry. The fallow config accepts an array of
325/// these under the `ignoreExports` key.
326#[derive(Debug, Clone, Serialize)]
327#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
328pub struct IgnoreExportsRule {
329    /// File path (forward slashes, relative to project root) to which this
330    /// rule applies. Globs are accepted.
331    pub file: String,
332    /// Names of exports inside `file` to silently treat as used.
333    pub exports: Vec<String>,
334}