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    /// Break a re-export cycle by removing an `export * from` (or
157    /// `export { ... } from`) statement on any one member file. Re-export
158    /// cycles are structurally always bugs (chain propagation through the
159    /// loop is a no-op), so there is no auto-fix; the action is manual.
160    RefactorReExportCycle,
161    /// Resolve a boundary violation by refactoring the import.
162    RefactorBoundary,
163    /// Convert an import statement to a type-only import (used by
164    /// private-type-leak findings).
165    ExportType,
166    /// Remove an unused catalog entry from `pnpm-workspace.yaml`.
167    RemoveCatalogEntry,
168    /// Remove an empty named catalog group from `pnpm-workspace.yaml`.
169    RemoveEmptyCatalogGroup,
170    /// Update an existing `catalog:` reference in a workspace `package.json`
171    /// to point at a different (declared) catalog.
172    UpdateCatalogReference,
173    /// Add the missing entry to the referenced catalog.
174    AddCatalogEntry,
175    /// Remove the catalog reference from the workspace `package.json` and
176    /// replace it with a hardcoded version.
177    RemoveCatalogReference,
178    /// Remove an unused dependency override entry.
179    RemoveDependencyOverride,
180    /// Fix a misconfigured dependency override entry (unparsable key or empty
181    /// value).
182    FixDependencyOverride,
183}
184
185/// Inline-comment suppression for a single finding line.
186#[derive(Debug, Clone, Serialize)]
187#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
188pub struct SuppressLineAction {
189    /// Action type identifier.
190    #[serde(rename = "type")]
191    pub kind: SuppressLineKind,
192    /// Always false for suppress actions.
193    pub auto_fixable: bool,
194    /// Human-readable description of the suppression.
195    pub description: String,
196    /// The inline comment to place above the line (e.g.,
197    /// `// fallow-ignore-next-line unused-export`). When multiple
198    /// suppressible findings share the same path and line, this may contain a
199    /// comma-separated issue-kind list such as
200    /// `// fallow-ignore-next-line unused-export, complexity`.
201    pub comment: String,
202    /// Present on multi-location issue types (e.g., `duplicate_exports`) to
203    /// indicate the comment must be applied at each location.
204    #[serde(default, skip_serializing_if = "Option::is_none")]
205    pub scope: Option<SuppressLineScope>,
206}
207
208/// Singleton discriminant for [`SuppressLineAction`].
209#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
210#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
211#[serde(rename_all = "kebab-case")]
212pub enum SuppressLineKind {
213    /// `// fallow-ignore-next-line <kind>` directive.
214    SuppressLine,
215}
216
217/// Scope marker for line suppressions that span multiple locations.
218#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
219#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
220#[serde(rename_all = "kebab-case")]
221pub enum SuppressLineScope {
222    /// Apply the suppression comment at each location of the multi-location
223    /// finding (e.g., every `duplicate_exports` site).
224    PerLocation,
225}
226
227/// File-wide suppression placed at the top of the source file.
228#[derive(Debug, Clone, Serialize)]
229#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
230pub struct SuppressFileAction {
231    /// Action type identifier.
232    #[serde(rename = "type")]
233    pub kind: SuppressFileKind,
234    /// Always false for suppress actions.
235    pub auto_fixable: bool,
236    /// Human-readable description of the suppression.
237    pub description: String,
238    /// The file-level comment to place at the top of the file (e.g.,
239    /// `// fallow-ignore-file unused-file`).
240    pub comment: String,
241}
242
243/// Singleton discriminant for [`SuppressFileAction`].
244#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
245#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
246#[serde(rename_all = "kebab-case")]
247pub enum SuppressFileKind {
248    /// `// fallow-ignore-file <kind>` directive.
249    SuppressFile,
250}
251
252/// Edit a fallow config file (`.fallowrc.json`, `fallow.toml`, etc.) to
253/// add the offending value to an `ignore*` rule.
254#[derive(Debug, Clone, Serialize)]
255#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
256pub struct AddToConfigAction {
257    /// Action type identifier.
258    #[serde(rename = "type")]
259    pub kind: AddToConfigKind,
260    /// True when `fallow fix` can apply this config action automatically.
261    /// Evaluated PER FINDING, not per action type: `ignoreExports`
262    /// duplicate-export actions are auto-fixable when `fallow fix` can
263    /// safely write the rule, which today means EITHER a fallow config
264    /// file already exists OR no config exists and the working directory
265    /// is NOT inside a monorepo subpackage (in which case the applier
266    /// creates `.fallowrc.json` from `fallow init`'s framework-aware
267    /// scaffolding). The action is `false` inside a monorepo subpackage
268    /// with no workspace-root config because the applier refuses to
269    /// fragment per-package configs across the monorepo. Older scalar
270    /// config-ignore actions (e.g. `ignoreDependencies` on dependency
271    /// findings) are always manual today. Filter on this bool of each
272    /// individual action, not on the `type` alone. See the [`IssueAction`]
273    /// enum-level docs for the full list of per-instance flips.
274    pub auto_fixable: bool,
275    /// Human-readable description of the config change.
276    pub description: String,
277    /// The fallow config key to add the value to (e.g.,
278    /// `ignoreDependencies`).
279    pub config_key: String,
280    /// Value to add to the config key. Shape depends on `config_key`. For
281    /// scalar config keys (`ignoreDependencies`, others) this is a string
282    /// such as `"lodash"`. For `ignoreExports` this is an array of
283    /// `{ file, exports }` rule objects so the snippet can be merged into
284    /// the user's config verbatim. For `ignoreCatalogReferences` and
285    /// `ignoreDependencyOverrides` this is an object whose shape matches the
286    /// rule entry users add to their fallow config.
287    pub value: AddToConfigValue,
288    /// Optional URL pointing at a stable JSON Schema fragment that describes
289    /// the shape of `value`. Agents that intend to validate `value` before
290    /// writing it into a user's config can fetch the linked schema and run
291    /// it against `value`. The URL is a JSON Pointer fragment into fallow's
292    /// main config schema (e.g.
293    /// `schema.json#/properties/ignoreExports` for the ignoreExports
294    /// action, or `schema.json#/properties/ignoreDependencies/items` for
295    /// the per-package ignoreDependencies action). Strictly additive:
296    /// consumers that ignore the field keep working unchanged.
297    #[serde(default, skip_serializing_if = "Option::is_none")]
298    pub value_schema: Option<String>,
299}
300
301/// Singleton discriminant for [`AddToConfigAction`].
302#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
303#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
304#[serde(rename_all = "kebab-case")]
305pub enum AddToConfigKind {
306    /// Append a value into a fallow config `ignore*` list.
307    AddToConfig,
308}
309
310/// Value payload for [`AddToConfigAction::value`]. The variants line up with
311/// the documented per-`config_key` shapes; deserialization is untagged so
312/// downstream consumers can switch on the JSON value's type.
313#[derive(Debug, Clone, Serialize)]
314#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
315#[serde(untagged)]
316pub enum AddToConfigValue {
317    /// Scalar string value (e.g., a package name for
318    /// `ignoreDependencies: ["lodash"]`).
319    Scalar(String),
320    /// Array of file+export rule objects for `ignoreExports`.
321    ExportsRules(Vec<IgnoreExportsRule>),
322    /// Free-form object for rule-shaped keys like
323    /// `ignoreCatalogReferences` / `ignoreDependencyOverrides`. The shape
324    /// matches the rule entry users add to their fallow config; consumers
325    /// validate against the per-key schema referenced by `value_schema`.
326    RuleObject(serde_json::Map<String, serde_json::Value>),
327}
328
329/// Single `ignoreExports` rule entry. The fallow config accepts an array of
330/// these under the `ignoreExports` key.
331#[derive(Debug, Clone, Serialize)]
332#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
333pub struct IgnoreExportsRule {
334    /// File path (forward slashes, relative to project root) to which this
335    /// rule applies. Globs are accepted.
336    pub file: String,
337    /// Names of exports inside `file` to silently treat as used.
338    pub exports: Vec<String>,
339}