Skip to main content

fallow_output/
health_css.rs

1/// Structural CSS analytics surfaced by `fallow health --css`.
2#[derive(Debug, Clone, serde::Serialize)]
3#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
4pub struct CssAnalyticsReport {
5    /// Stylesheets with at least one structurally notable rule, in scan order.
6    pub files: Vec<CssFileAnalytics>,
7    /// Project-wide CSS aggregates across every analyzed stylesheet.
8    pub summary: CssAnalyticsSummary,
9    /// Vue SFCs whose `<style scoped>` defines classes used nowhere else in the
10    /// component (cleanup candidates).
11    #[serde(default, skip_serializing_if = "Vec::is_empty")]
12    pub scoped_unused: Vec<ScopedUnusedClasses>,
13    /// `@keyframes` defined but referenced via no `animation` / `animation-name`
14    /// in any stylesheet, with the stylesheet that defines them (cleanup
15    /// candidates; an animation name can still be applied from JavaScript).
16    /// The "defined-but-unused" direction.
17    #[serde(default, skip_serializing_if = "Vec::is_empty")]
18    pub unreferenced_keyframes: Vec<UnreferencedKeyframes>,
19    /// Animation references (`animation` / `animation-name`) to a `@keyframes`
20    /// name that is defined in NO stylesheet anywhere in the project, with the
21    /// first stylesheet that references them. The "used-but-undefined" direction
22    /// (the inverse of `unreferenced_keyframes`): usually a typo or a removed
23    /// animation, occasionally a `@keyframes` defined in CSS-in-JS (which the
24    /// CSS parser never sees). Conservative candidates, never gated findings.
25    #[serde(default, skip_serializing_if = "Vec::is_empty")]
26    pub undefined_keyframes: Vec<UndefinedKeyframes>,
27    /// Groups of style rules across the project that share an identical
28    /// declaration block (4+ declarations, sorted and `!important`-aware),
29    /// grouped by content: copy-paste consolidation candidates (fallow's
30    /// duplication signal applied to CSS). Sorted by estimated savings
31    /// descending.
32    #[serde(default, skip_serializing_if = "Vec::is_empty")]
33    pub duplicate_declaration_blocks: Vec<CssDuplicateBlock>,
34    /// CVA / shadcn variant class strings that repeat the same normalized class
35    /// block in several variant values. Kept separate from CSS declaration-block
36    /// duplication because the source is JS config, not parsed CSS rules.
37    #[serde(default, skip_serializing_if = "Vec::is_empty")]
38    pub cva_duplicate_variant_blocks: Vec<CvaDuplicateVariantBlock>,
39    /// CVA / shadcn variant class strings that hardcode a Tailwind arbitrary
40    /// value even though an existing token has the same or nearest comparable
41    /// value. Advisory: variants often encode product semantics, so agents
42    /// should verify intent before replacing.
43    #[serde(default, skip_serializing_if = "Vec::is_empty")]
44    pub cva_variant_token_drifts: Vec<CvaVariantTokenDrift>,
45    /// Tailwind arbitrary-value utilities (`w-[13px]`, `bg-[#abc]`) found in
46    /// markup, which hardcode a one-off value instead of a configured scale
47    /// token (design-token bypass). Present only when the project uses Tailwind.
48    /// Sorted by use count descending. Candidates, not findings: an arbitrary
49    /// value is sometimes the right call.
50    #[serde(default, skip_serializing_if = "Vec::is_empty")]
51    pub tailwind_arbitrary_values: Vec<TailwindArbitraryValue>,
52    /// Located raw CSS declaration values that bypass token surfaces (`var()`,
53    /// `token()`, `theme()`) on scale-sensitive axes such as color, font-size,
54    /// line-height, radius, and shadow. Conservative candidates: a raw value can
55    /// be intentional, but introduced raw values are useful audit feedback.
56    #[serde(default, skip_serializing_if = "Vec::is_empty")]
57    pub raw_style_values: Vec<RawStyleValue>,
58    /// Unused CSS at-rule entities: an `@property` registered but never read via
59    /// `var()` in any stylesheet, or an `@layer` declared but never populated by
60    /// a block. Cleanup candidates (an `@property` can be read from JS; a layer
61    /// can be populated via `@import layer()`). Located by first definition.
62    #[serde(default, skip_serializing_if = "Vec::is_empty")]
63    pub unused_at_rules: Vec<UnusedAtRule>,
64    /// Static `class` / `className` tokens in markup that match no CSS class
65    /// defined anywhere in the project AND are one edit away from a class that
66    /// IS defined (a likely typo or stale rename, with the suggested class). The
67    /// CSS analogue of an unresolved import; the near-miss restriction keeps it
68    /// near-zero false-positive (Tailwind utilities and third-party classes are
69    /// not one edit from an authored class). Candidates, never gated: the token
70    /// could be defined in CSS-in-JS or an external stylesheet the parser never
71    /// sees. Sorted by `(path, line, class)`.
72    #[serde(default, skip_serializing_if = "Vec::is_empty")]
73    pub unresolved_class_references: Vec<UnresolvedClassReference>,
74    /// Global CSS classes (defined in a plain `.css`/`.scss` rule) whose literal
75    /// name is referenced by NO in-project markup, static or dynamic (the CSS
76    /// analogue of an unused export). Heavily gated to stay near-zero-false-
77    /// positive: emitted only when the project is plain-CSS-dominant, the
78    /// stylesheet is locally consumed (not a published design-system surface),
79    /// and the whole project is in scope. Candidates, never gated findings: the
80    /// class may be used by an HTML email, server template, CMS, or Markdown the
81    /// parser never scans. Sorted by `(path, line, class)`.
82    #[serde(default, skip_serializing_if = "Vec::is_empty")]
83    pub unreferenced_css_classes: Vec<UnreferencedCssClass>,
84    /// `@font-face` families declared in a stylesheet but referenced by no
85    /// `font-family` anywhere in the project: a dead web-font payload (the font
86    /// file is downloaded but never applied). Located at the declaring
87    /// stylesheet. Cleanup candidates: the family could be applied from inline
88    /// styles or set via JavaScript. Sorted by `(path, family)`.
89    #[serde(default, skip_serializing_if = "Vec::is_empty")]
90    pub unused_font_faces: Vec<UnusedFontFace>,
91    /// Tailwind v4 `@theme` design tokens (`--color-brand`, `--radius-card`)
92    /// defined in a stylesheet but used by no generated utility, `var()` read,
93    /// `@apply`, or arbitrary value anywhere in the project: dead design tokens
94    /// (the `unused-export` of the token era). Present only when the project is
95    /// Tailwind v4 (a `tailwindcss` dependency plus at least one `@theme` block)
96    /// and not a plugin / published-library / partial-scope run. Candidates,
97    /// never gated findings: the token may be consumed by a Tailwind plugin or a
98    /// downstream repo. Sorted by `(path, line, token)`.
99    #[serde(default, skip_serializing_if = "Vec::is_empty")]
100    pub unused_theme_tokens: Vec<UnusedThemeToken>,
101    /// Tailwind v4 theme tokens whose comparable values are close to another
102    /// token in the same theme dictionary. These are opt-in `--css-deep`
103    /// candidates because they need whole-project token context.
104    #[serde(default, skip_serializing_if = "Vec::is_empty")]
105    pub near_duplicate_theme_tokens: Vec<NearDuplicateThemeToken>,
106    /// A location-aware reverse index of Tailwind v4 `@theme` token consumers:
107    /// per token, where it is consumed (`var()` reads, `@apply` bodies, generated
108    /// utility classes) and through which surface, plus the full `consumer_count`
109    /// (a static lower bound) and the defining site. Built from the same gated
110    /// candidate set as `unused_theme_tokens` (v4 + non-plugin + non-published +
111    /// whole-scope), so a token with `consumer_count: 0` is the same "nothing
112    /// consumes this" signal. Sorted by token; empty when the project is not
113    /// Tailwind v4 or a plugin / published-library / partial-scope run gated the
114    /// scan out.
115    #[serde(default, skip_serializing_if = "Vec::is_empty")]
116    pub token_consumers: Vec<TokenConsumers>,
117    /// The project authors `font-size` values in several units (`px`, `rem`,
118    /// `em`, `%`), with a per-unit distinct-value count: a type-scale
119    /// inconsistency smell (mixing `px` and `rem` for type works against
120    /// user-zoom accessibility). Present only above a conservative floor.
121    /// Advisory candidate, never gated: the spread can be intentional (fixed
122    /// chrome in `px`, body type in `rem`).
123    ///
124    /// Color-notation mixing (hex vs rgb vs hsl) is deliberately NOT surfaced:
125    /// the CSS parser canonicalizes every legacy sRGB notation to hex before
126    /// fallow sees the value, so the authored distinction is already gone and
127    /// cannot be recovered without a separate raw-token pass.
128    #[serde(default, skip_serializing_if = "Option::is_none")]
129    pub font_size_unit_mix: Option<CssNotationConsistency>,
130}
131
132/// A design-token notation-consistency candidate: the distinct notations used
133/// across the codebase for one value axis (today, length units on `font-size`),
134/// with a per-notation distinct-value count. Emitted only above a floor, since
135/// mixing notations for one axis is a "no single source of truth" smell.
136/// Advisory: the action is "standardize on one notation", not a single search.
137#[derive(Debug, Clone, serde::Serialize)]
138#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
139pub struct CssNotationConsistency {
140    /// The value axis these notations describe, e.g. `"Colors"` or
141    /// `"Font sizes"`.
142    pub axis: String,
143    /// Per-notation distinct-value counts, sorted by count descending then
144    /// notation name (so the dominant notation is first and ties are stable).
145    pub notations: Vec<CssNotationCount>,
146    /// Read-only guidance step(s), so consumers can iterate `actions` uniformly
147    /// across every candidate type. Always at least one entry.
148    pub actions: Vec<CssCandidateAction>,
149}
150
151/// One notation bucket and the count of distinct values authored in it.
152#[derive(Debug, Clone, serde::Serialize)]
153#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
154pub struct CssNotationCount {
155    /// The notation family, e.g. `"hex"`, `"rgb"`, `"hsl"`, `"modern"`, `"px"`,
156    /// `"rem"`, `"em"`, `"%"`.
157    pub notation: String,
158    /// Distinct values authored in this notation across the codebase.
159    pub count: u32,
160}
161
162/// An unused CSS at-rule entity (an `@property` registration with no `var()`
163/// reference, or an `@layer` declaration never populated), located by its first
164/// definition. A cleanup candidate, never a gated finding.
165#[derive(Debug, Clone, serde::Serialize)]
166#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
167pub struct UnusedAtRule {
168    /// Which kind of at-rule entity is unused.
169    #[serde(rename = "type")]
170    pub kind: UnusedAtRuleKind,
171    /// The entity name (`--x` for `@property`, the layer name for `@layer`).
172    pub name: String,
173    /// Project-root-relative, forward-slash path to the first defining stylesheet.
174    pub path: String,
175    /// Read-only verification step(s) before removal (parity with other findings).
176    pub actions: Vec<CssCandidateAction>,
177}
178
179/// Discriminant for [`UnusedAtRule::kind`].
180#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
181#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
182#[serde(rename_all = "kebab-case")]
183#[repr(u8)]
184pub enum UnusedAtRuleKind {
185    /// An `@property --x { }` registered but never referenced via `var()`.
186    PropertyRegistration,
187    /// An `@layer a` declared (in a statement or named block) but never
188    /// populated by a `@layer a { }` block.
189    Layer,
190}
191
192/// A distinct Tailwind arbitrary-value utility token used in markup, with its
193/// total use count and first location (a design-token-bypass candidate).
194#[derive(Debug, Clone, serde::Serialize)]
195#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
196pub struct TailwindArbitraryValue {
197    /// The `prefix-[value]` token (e.g. `w-[13px]`). Variant prefixes are
198    /// stripped, so `hover:w-[13px]` and `w-[13px]` aggregate under `w-[13px]`.
199    pub value: String,
200    /// Total occurrences across all scanned markup files.
201    pub count: u32,
202    /// Project-root-relative, forward-slash path to the first file using it.
203    pub path: String,
204    /// 1-based line of the first occurrence.
205    pub line: u32,
206    /// Read-only action(s): a find-all-occurrences search so the token can be
207    /// replaced with a scale token. Always at least one entry, so consumers can
208    /// iterate `actions` uniformly across every finding type.
209    pub actions: Vec<CssCandidateAction>,
210}
211
212/// A located raw CSS declaration value on a scale-sensitive styling axis.
213#[derive(Debug, Clone, serde::Serialize)]
214#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
215pub struct RawStyleValue {
216    /// Value axis, e.g. `color`, `font-size`, `line-height`, `radius`, or `shadow`.
217    pub axis: String,
218    /// CSS property where the raw value appears.
219    pub property: String,
220    /// Rendered declaration value.
221    pub value: String,
222    /// Project-root-relative, forward-slash path to the stylesheet.
223    pub path: String,
224    /// 1-based line of the containing style rule.
225    pub line: u32,
226    /// Concrete token with the same or nearest comparable value, when resolved.
227    #[serde(default, skip_serializing_if = "Option::is_none")]
228    pub nearest_token: Option<NearestStylingToken>,
229    /// Read-only guidance step(s). Never auto-fixable.
230    pub actions: Vec<CssCandidateAction>,
231}
232
233/// A group of style rules across the project that share an identical declaration
234/// block: a copy-paste consolidation candidate (fallow's duplication signal
235/// applied to CSS). Only blocks of 4+ declarations appearing in 2+ rules are
236/// reported, so the signal stays a strong copy-paste indicator rather than
237/// flagging legitimately-repeated small blocks.
238#[derive(Debug, Clone, serde::Serialize)]
239#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
240pub struct CssDuplicateBlock {
241    /// Declarations in the shared block.
242    pub declaration_count: u16,
243    /// Number of rules that share the block (always >= 2).
244    pub occurrence_count: u32,
245    /// Declarations removable by extracting the block into one shared rule:
246    /// `(occurrence_count - 1) * declaration_count`.
247    pub estimated_savings: u32,
248    /// The rules sharing the block, sorted by `(path, line)`.
249    pub occurrences: Vec<CssBlockOccurrence>,
250    /// Read-only guidance step(s), so consumers can iterate `actions`
251    /// uniformly across every finding type. Always at least one entry.
252    pub actions: Vec<CssCandidateAction>,
253}
254
255/// One occurrence of a duplicate declaration block.
256#[derive(Debug, Clone, serde::Serialize)]
257#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
258pub struct CssBlockOccurrence {
259    /// Project-root-relative, forward-slash path to the stylesheet.
260    pub path: String,
261    /// 1-based line of the rule's first selector.
262    pub line: u32,
263}
264
265/// A duplicated CVA / shadcn variant class block.
266#[derive(Debug, Clone, serde::Serialize)]
267#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
268pub struct CvaDuplicateVariantBlock {
269    /// Normalized class block shared by several variant values.
270    pub value: String,
271    /// Number of variant values with this class block.
272    pub occurrence_count: u32,
273    /// First locations of the duplicate values, sorted by path and line.
274    pub occurrences: Vec<CssBlockOccurrence>,
275    /// Read-only guidance step(s), so consumers can iterate `actions`
276    /// uniformly across every candidate type.
277    pub actions: Vec<CssCandidateAction>,
278}
279
280/// A CVA / shadcn variant class value that can reuse an existing styling token.
281#[derive(Debug, Clone, serde::Serialize)]
282#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
283pub struct CvaVariantTokenDrift {
284    /// Tailwind arbitrary-value utility inside the variant class string.
285    pub class_token: String,
286    /// Normalized value inside the arbitrary utility.
287    pub value: String,
288    /// Full normalized variant class block containing the token.
289    pub variant_classes: String,
290    /// Project-root-relative, forward-slash path to the variant definition.
291    pub path: String,
292    /// 1-based line of the variant class string.
293    pub line: u32,
294    /// Existing token candidate to reuse.
295    pub nearest_token: NearestStylingToken,
296    /// Read-only guidance step(s), so consumers can iterate `actions`
297    /// uniformly across every candidate type.
298    pub actions: Vec<CssCandidateAction>,
299}
300
301/// A `@keyframes` defined in a stylesheet but referenced by no animation in any
302/// stylesheet (cleanup candidate).
303#[derive(Debug, Clone, serde::Serialize)]
304#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
305pub struct UnreferencedKeyframes {
306    /// The `@keyframes` name.
307    pub name: String,
308    /// Project-root-relative, forward-slash path to the stylesheet that defines it.
309    pub path: String,
310    /// Read-only verification step(s) an agent can run before removing the
311    /// candidate. Always at least one entry, so consumers can iterate
312    /// `actions` uniformly across every finding type.
313    pub actions: Vec<CssCandidateAction>,
314}
315
316/// An `@font-face` family declared in a stylesheet but referenced by no
317/// `font-family` anywhere in the project: a dead web-font payload. A cleanup
318/// candidate (the family could be applied from inline styles or JavaScript).
319#[derive(Debug, Clone, serde::Serialize)]
320#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
321pub struct UnusedFontFace {
322    /// The declared font family name (quotes stripped).
323    pub family: String,
324    /// Project-root-relative, forward-slash path to the declaring stylesheet.
325    pub path: String,
326    /// Read-only verification step(s) before removing. Always at least one entry,
327    /// so consumers can iterate `actions` uniformly across every finding type.
328    pub actions: Vec<CssCandidateAction>,
329}
330
331/// A Tailwind v4 `@theme` design token defined in a stylesheet whose generated
332/// utility, `var()` reads, and arbitrary-value references appear nowhere in the
333/// project: a dead design token (the `unused-export` of the token era). A
334/// candidate, never a gated finding: the token could be consumed by a Tailwind
335/// plugin, a published design-system surface, or a non-CSS-aware build step the
336/// scan cannot see (those cases are gated out before this is emitted).
337#[derive(Debug, Clone, serde::Serialize)]
338#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
339pub struct UnusedThemeToken {
340    /// The full custom property as authored, including the `--` prefix
341    /// (`--color-brand`).
342    pub token: String,
343    /// The Tailwind v4 theme namespace the token belongs to (`color`, `radius`,
344    /// `font-weight`, `breakpoint`, ...).
345    pub namespace: String,
346    /// Project-root-relative, forward-slash path to the declaring stylesheet.
347    pub path: String,
348    /// 1-based line of the token's definition inside the `@theme` block.
349    pub line: u32,
350    /// Read-only verification step(s) before removing. Always at least one entry,
351    /// so consumers can iterate `actions` uniformly across every finding type.
352    pub actions: Vec<CssCandidateAction>,
353}
354
355/// A Tailwind v4 `@theme` token that appears to duplicate an existing token by
356/// value. Emitted conservatively for comparable token namespaces, with the
357/// nearest existing token named so an agent has a concrete reuse target.
358#[derive(Debug, Clone, serde::Serialize)]
359#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
360pub struct NearDuplicateThemeToken {
361    /// The full custom property as authored, including the `--` prefix.
362    pub token: String,
363    /// The normalized authored token value.
364    pub value: String,
365    /// Project-root-relative, forward-slash path to the token definition.
366    pub path: String,
367    /// 1-based line of the token definition inside the `@theme` block.
368    pub line: u32,
369    /// The nearest existing token candidate to reuse instead.
370    pub nearest_token: NearestStylingToken,
371    /// Read-only guidance step(s) before replacing the token reference.
372    pub actions: Vec<CssCandidateAction>,
373}
374
375/// A styling token candidate that can replace or explain a finding.
376#[derive(Debug, Clone, serde::Serialize)]
377#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
378pub struct NearestStylingToken {
379    /// Token name, e.g. `--color-brand`.
380    pub name: String,
381    /// Normalized token value.
382    pub value: String,
383    /// Project-root-relative, forward-slash definition path.
384    pub path: String,
385    /// 1-based definition line.
386    pub line: u32,
387    /// Distance from the finding value. Lower is closer; units depend on the
388    /// comparable token namespace.
389    pub distance: f64,
390}
391
392/// Where one Tailwind v4 `@theme` token is consumed, and through which surface.
393/// One entry in a [`TokenConsumers::consumers`] sample.
394#[derive(Debug, Clone, serde::Serialize)]
395#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
396pub struct TokenConsumerLocation {
397    /// Project-root-relative, forward-slash path to the consuming file.
398    pub path: String,
399    /// 1-based line of the consuming reference in that file.
400    pub line: u32,
401    /// Which surface consumes the token at this location.
402    pub kind: ConsumerKind,
403}
404
405/// The surface through which a design token is consumed. The `theme-var` /
406/// `css-var` / `utility` / `apply` kinds are Tailwind v4 `@theme` consumption; the
407/// `js-member` / `js-call` kinds are CSS-in-JS consumption (member access on an
408/// imported StyleX/vanilla-extract token binding, or a PandaCSS `token('...')`
409/// call). The kind is the disjoint origin signal that distinguishes a Tailwind
410/// token entry from a CSS-in-JS token entry in the shared `token_consumers` list.
411#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
412#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
413#[serde(rename_all = "kebab-case")]
414pub enum ConsumerKind {
415    /// A `var(--token)` read inside a `@theme` block interior (a token backing
416    /// another token).
417    ThemeVar,
418    /// A `var(--token)` read in regular CSS, outside any `@theme` block.
419    CssVar,
420    /// A generated utility class ending in `-<name>` (`bg-brand` consuming
421    /// `--color-brand`) found in markup / className strings / CSS-in-JS.
422    Utility,
423    /// A class-shaped token inside an `@apply` body in a stylesheet.
424    Apply,
425    /// A cross-module JS member access on an imported CSS-in-JS token binding
426    /// (`import { vars } from './tokens'; vars.color.primary`), for StyleX
427    /// `defineVars` / vanilla-extract `createTheme` family tokens.
428    JsMember,
429    /// A CSS-in-JS function call that consumes a token by path, such as
430    /// PandaCSS `token('colors.brand')` or `css({ color: 'colors.brand' })`.
431    JsCall,
432}
433
434/// A location-aware reverse index of where one design token is consumed, so an
435/// agent editing the token can see its blast radius before changing or removing
436/// it. Covers TWO token origins. The always-available discriminator is the `token`
437/// SHAPE: a Tailwind token is the `--`-prefixed custom property (`--color-brand`),
438/// a CSS-in-JS token is a dotted access path with no `--` prefix
439/// (`vars.color.primary`). The per-consumer `kind` also discriminates origin, but
440/// only when `consumer_count > 0` (a `consumer_count: 0` entry has an empty
441/// `consumers` array and thus no `kind`), so branch on the `token` prefix for the
442/// zero-consumer case. The two origins:
443///
444/// - Tailwind v4 `@theme` tokens (kinds `theme-var` / `css-var` / `utility` /
445///   `apply`), built from the same gated candidate set as `unused_theme_tokens`
446///   (v4 + non-plugin + non-published + whole-scope), so a `consumer_count: 0`
447///   corroborates the `unused_theme_tokens` "nothing consumes this" finding.
448/// - CSS-in-JS tokens (kind `js-member` / `js-call`) from StyleX `defineVars`,
449///   vanilla-extract `createTheme` family definitions, and PandaCSS `defineTokens`,
450///   consumed via cross-module member access or PandaCSS `token('...')` calls. NOTE:
451///   CSS-in-JS has NO corroborating dead-token finding (there is no
452///   `unused_theme_tokens` analogue), so a CSS-in-JS `consumer_count: 0` is a weaker
453///   signal than the Tailwind case (and the cross-file scan is relative-import or
454///   generated-token-helper only, so alias / bare-package imports are not counted).
455///
456/// This is DESCRIPTIVE context (a blast-radius lookup), not a finding, so it
457/// deliberately carries no `actions` array (unlike the cleanup-candidate types in
458/// this module). `consumer_count` is always a STATIC lower bound (a computed class
459/// name like `bg-${color}`, or a CSS-in-JS access through an unresolved alias
460/// import, is not counted).
461#[derive(Debug, Clone, serde::Serialize)]
462#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
463pub struct TokenConsumers {
464    /// The token identity. For a Tailwind `@theme` token this is the full custom
465    /// property as authored, INCLUDING the `--` prefix (`--color-brand`). For a
466    /// CSS-in-JS token (kind `js-member`) this is the binding-qualified dotted
467    /// access path, NO `--` prefix (`vars.color.primary`), matching how consumers
468    /// read it. The presence of the `--` prefix distinguishes the two origins.
469    pub token: String,
470    /// For a Tailwind token, the v4 theme namespace (`color`, `radius`,
471    /// `font-weight`, ...). For a CSS-in-JS token (kind `js-member`), the defining
472    /// export BINDING the token set is accessed through (`vars`), which identifies
473    /// the token set, NOT a semantic group. (The field is thus overloaded by
474    /// origin; branch on `consumers[].kind` or the `token` shape.)
475    pub namespace: String,
476    /// Project-root-relative, forward-slash path to the declaring stylesheet
477    /// (Tailwind) or the JS/TS token-definition file (CSS-in-JS).
478    pub definition_path: String,
479    /// 1-based line of the token's definition (inside the `@theme` block for
480    /// Tailwind; the token key inside the `defineVars`/`createTheme` object for
481    /// CSS-in-JS).
482    pub definition_line: u32,
483    /// The FULL number of consumer locations found, a STATIC LOWER BOUND: a
484    /// computed class name (`bg-${color}`) or a value read outside CSS/markup the
485    /// scan never sees is not counted. This is the aggregate over every consumer,
486    /// computed BEFORE [`consumers`](Self::consumers) is capped to a sample.
487    pub consumer_count: u32,
488    /// A capped, deterministically-sorted sample of consumer locations (at most
489    /// [`TOKEN_CONSUMER_SAMPLE_CAP`]). The full count lives in
490    /// [`consumer_count`](Self::consumer_count); use this list to jump to
491    /// representative consumers, not to enumerate every one.
492    pub consumers: Vec<TokenConsumerLocation>,
493}
494
495/// Maximum number of consumer locations sampled into [`TokenConsumers::consumers`].
496/// The full count is preserved in [`TokenConsumers::consumer_count`]
497/// (aggregate-before-truncate), so capping the sample never distorts the count.
498pub const TOKEN_CONSUMER_SAMPLE_CAP: usize = 20;
499
500/// A global CSS class defined in a plain `.css`/`.scss` rule whose literal name
501/// is referenced by no in-project markup (the CSS analogue of an unused export).
502/// A heavily-gated candidate, never a gated finding: the class may be applied
503/// from an HTML email, server template, CMS, or Markdown the parser never sees.
504#[derive(Debug, Clone, serde::Serialize)]
505#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
506pub struct UnreferencedCssClass {
507    /// The class name (no dot).
508    pub class: String,
509    /// Project-root-relative, forward-slash path to the defining stylesheet.
510    pub path: String,
511    /// 1-based line of the class's first definition.
512    pub line: u32,
513    /// Read-only verification step(s) before removing. Always at least one entry,
514    /// so consumers can iterate `actions` uniformly across every finding type.
515    pub actions: Vec<CssCandidateAction>,
516}
517
518/// An animation reference (`animation` / `animation-name`) to a `@keyframes`
519/// name that is defined in no stylesheet anywhere in the project (the
520/// "used-but-undefined" direction). Usually a typo or a removed animation;
521/// occasionally a `@keyframes` defined in CSS-in-JS the CSS parser never sees.
522#[derive(Debug, Clone, serde::Serialize)]
523#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
524pub struct UndefinedKeyframes {
525    /// The referenced `@keyframes` name that resolves to no definition.
526    pub name: String,
527    /// Project-root-relative, forward-slash path to the first stylesheet that
528    /// references it.
529    pub path: String,
530    /// Read-only verification step(s) an agent can run before fixing the
531    /// reference. Always at least one entry, so consumers can iterate `actions`
532    /// uniformly across every finding type.
533    pub actions: Vec<CssCandidateAction>,
534}
535
536/// A static `class` / `className` token in markup that matches no CSS class
537/// defined anywhere in the project but is one edit away from a class that IS
538/// defined (a likely typo or stale rename). The CSS analogue of an unresolved
539/// import. A candidate, never a gated finding: the token could be defined in
540/// CSS-in-JS or an external stylesheet the parser never sees.
541#[derive(Debug, Clone, serde::Serialize)]
542#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
543pub struct UnresolvedClassReference {
544    /// The static class token referenced in markup (no dot).
545    pub class: String,
546    /// The defined CSS class one edit away: the likely intended class.
547    pub suggestion: String,
548    /// Project-root-relative, forward-slash path to the markup file.
549    pub path: String,
550    /// 1-based line of the `class` / `className` attribute.
551    pub line: u32,
552    /// Read-only verification step(s) before fixing the reference. Always at
553    /// least one entry, so consumers can iterate `actions` uniformly across
554    /// every finding type.
555    pub actions: Vec<CssCandidateAction>,
556}
557
558/// A Vue SFC's `<style scoped>` classes that appear nowhere else in the
559/// component (cleanup candidates).
560#[derive(Debug, Clone, serde::Serialize)]
561#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
562pub struct ScopedUnusedClasses {
563    /// Project-root-relative, forward-slash path to the SFC.
564    pub path: String,
565    /// The scoped class names with no use elsewhere in the component, sorted.
566    pub classes: Vec<String>,
567    /// Read-only verification step(s) an agent can run before removing the
568    /// candidate. Always at least one entry, so consumers can iterate
569    /// `actions` uniformly across every finding type.
570    pub actions: Vec<CssCandidateAction>,
571}
572
573/// One advisory STYLING FINDING: the graduation of a descriptive css candidate
574/// into a first-class, severity-aware, suppressible finding surfaced in
575/// `fallow audit`. The styling domain's OWN finding type (not borrowed into the
576/// dead-code `AnalysisResults`, and not glued in the CLI). `code` is the kebab
577/// IssueKind code (e.g. `css-token-drift`), so severity / inline suppression /
578/// SARIF / MCP all resolve via the shared `issue_meta` contract through
579/// `IssueKind::parse(code)`. One `Vec<StylingFinding>` carries every styling
580/// family; the `code` discriminates.
581#[derive(Debug, Clone, serde::Serialize)]
582#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
583pub struct StylingFinding {
584    /// The kebab IssueKind code, e.g. `css-token-drift`.
585    pub code: String,
586    /// The specific sub-kind within the family, e.g. `tailwind-arbitrary-value`.
587    pub sub_kind: String,
588    /// Workspace-relative path of the finding.
589    pub path: String,
590    /// 1-based line of the finding.
591    pub line: u32,
592    /// The offending literal value, e.g. `w-[13px]`.
593    pub value: String,
594    /// Effective severity after applying `rules.css-*` config. Styling defaults
595    /// to `warn`, but projects can escalate a family to `error` for audit gates
596    /// and CI formats.
597    pub effective_severity: StylingFindingSeverity,
598    /// Optional static lower-bound blast radius. For a dead design token this is
599    /// `0`; for other styling findings it is omitted.
600    #[serde(default, skip_serializing_if = "Option::is_none")]
601    pub blast_radius: Option<u32>,
602    /// Confidence hint for agents and review UIs. Structural findings are high,
603    /// reachability findings are low because dynamic consumers may exist.
604    #[serde(default, skip_serializing_if = "Option::is_none")]
605    pub confidence: Option<StylingFindingConfidence>,
606    /// Suggested handling posture for agents. This is advisory data, fallow
607    /// still never applies styling changes automatically.
608    #[serde(default, skip_serializing_if = "Option::is_none")]
609    pub agent_disposition: Option<StylingAgentDisposition>,
610    /// Concrete reuse target for token-drift findings, when one can be resolved.
611    #[serde(default, skip_serializing_if = "Option::is_none")]
612    pub nearest_token: Option<NearestStylingToken>,
613    /// One concise machine-readable edit hint for agent consumers.
614    #[serde(default, skip_serializing_if = "Option::is_none")]
615    pub fix_hint: Option<String>,
616    /// Suggested next steps (verify / suppress; never an auto-fix).
617    pub actions: Vec<CssCandidateAction>,
618}
619
620/// Effective configured severity for a styling finding.
621#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
622#[serde(rename_all = "kebab-case")]
623#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
624pub enum StylingFindingSeverity {
625    Warn,
626    Error,
627}
628
629/// Confidence hint for a [`StylingFinding`].
630#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
631#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
632#[serde(rename_all = "kebab-case")]
633pub enum StylingFindingConfidence {
634    /// The finding is local and structural.
635    High,
636    /// The finding depends on reachability and should be verified.
637    Low,
638}
639
640/// Agent handling hint for a [`StylingFinding`].
641#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
642#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
643#[serde(rename_all = "kebab-case")]
644pub enum StylingAgentDisposition {
645    /// The finding names a concrete structural edit target.
646    FixConfidently,
647    /// Verify dynamic or external consumers before changing code.
648    VerifyFirst,
649}
650
651/// A read-only verification step attached to a CSS cleanup candidate.
652///
653/// CSS candidates (unreferenced `@keyframes`, unused scoped classes) are never
654/// auto-removed: an animation name can still be applied from JavaScript, and a
655/// class can be assembled from a dynamic string binding. The action gives an
656/// agent a machine-readable next step, mirroring the `actions` array carried by
657/// every other health finding, plus an optional runnable probe to confirm the
658/// candidate is genuinely unused before deleting it.
659#[derive(Debug, Clone, serde::Serialize)]
660#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
661pub struct CssCandidateAction {
662    /// Action type identifier (`verify-unused`).
663    #[serde(rename = "type")]
664    pub kind: CssCandidateActionType,
665    /// Always `false`: CSS candidates are never auto-fixed (`fallow fix` does
666    /// not touch them) because the residual consumer may live outside CSS.
667    pub auto_fixable: bool,
668    /// Human-readable description of what to confirm before removing.
669    pub description: String,
670    /// A runnable, read-only, placeholder-free token search that surfaces any
671    /// out-of-CSS use of the candidate. Absent when no shell-safe command can
672    /// be built (e.g. the residual risk is a dynamic string binding that a
673    /// single search cannot probe), in which case `description` is the guide.
674    #[serde(default, skip_serializing_if = "Option::is_none")]
675    pub command: Option<String>,
676}
677
678/// Discriminant for [`CssCandidateAction::kind`].
679#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
680#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
681#[serde(rename_all = "kebab-case")]
682pub enum CssCandidateActionType {
683    /// Confirm the candidate has no JavaScript / HTML / dynamic consumer
684    /// before removing it (the defined-but-unused candidates).
685    VerifyUnused,
686    /// Confirm the referenced name is genuinely undefined (not defined in
687    /// CSS-in-JS the parser cannot see) before treating it as a typo (the
688    /// used-but-undefined candidates).
689    VerifyUndefined,
690    /// Extract the shared declaration block into one rule and reference it from
691    /// each occurrence (the duplicate-declaration-block candidates).
692    Consolidate,
693    /// Replace a Tailwind arbitrary value with a configured scale token, or
694    /// confirm the one-off is intentional (the arbitrary-value candidates).
695    ReplaceWithToken,
696    /// Standardize an inconsistent value axis on a single notation (the
697    /// color-format / length-unit mixing candidates).
698    Standardize,
699    /// Simplify a selector, reduce nesting, or remove unnecessary `!important`
700    /// usage after verifying the cascade.
701    SimplifySelector,
702}
703
704impl CssCandidateAction {
705    /// Read-only guidance for a selector / nesting / important-density finding.
706    #[must_use]
707    pub fn simplify_selector(reason: &str) -> Self {
708        Self {
709            kind: CssCandidateActionType::SimplifySelector,
710            auto_fixable: false,
711            description: format!(
712                "Review cascade impact, then simplify this selector or rule because {reason}."
713            ),
714            command: None,
715        }
716    }
717
718    /// Verify action for an unused `@font-face` family: a read-only token search
719    /// for any inline-style or JavaScript application of the family before
720    /// removing the dead web-font.
721    #[must_use]
722    pub fn verify_unused_font_face(family: &str) -> Self {
723        Self {
724            kind: CssCandidateActionType::VerifyUnused,
725            auto_fixable: false,
726            description: format!(
727                "Confirm the \"{family}\" font family is not applied from an inline style or JavaScript before removing the @font-face and its font files."
728            ),
729            command: safe_token_search(family),
730        }
731    }
732
733    /// Verify action for an unused Tailwind v4 `@theme` token: a read-only search
734    /// that embeds the LITERAL terms an agent should grep for, the generated
735    /// utility suffix (`bg-<name>` / `text-<name>` / `<namespace>-<name>`), the
736    /// `var(--<ns>-<name>)` read, and the arbitrary `[--<ns>-<name>]` value,
737    /// before removing the token. Verify-then-remove; never auto-fixable.
738    #[must_use]
739    pub fn verify_unused_theme_token(token: &str, namespace: &str, name: &str) -> Self {
740        Self {
741            kind: CssCandidateActionType::VerifyUnused,
742            auto_fixable: false,
743            description: format!(
744                "Confirm the {token} @theme token is used by nothing, no `*-{name}` utility (e.g. `bg-{name}` / `text-{name}` / `{namespace}-{name}`) in markup or @apply, no `var({token})` read in any stylesheet or JS, and no arbitrary `[{token}]` value, before removing it from the @theme block."
745            ),
746            command: theme_token_search(namespace, name),
747        }
748    }
749
750    /// Guidance for a near-duplicate theme token: reuse the named existing
751    /// token after checking semantic intent.
752    #[must_use]
753    pub fn replace_near_duplicate_token(token: &str, nearest: &str) -> Self {
754        Self {
755            kind: CssCandidateActionType::ReplaceWithToken,
756            auto_fixable: false,
757            description: format!(
758                "Verify {token} is not an intentional semantic alias, then reuse {nearest} instead."
759            ),
760            command: safe_token_search(token),
761        }
762    }
763
764    /// Verify action for an unreferenced global CSS class: name the surfaces the
765    /// in-project scan does NOT cover (the class could be applied from there) and
766    /// ship a read-only token search to double-check before removing.
767    #[must_use]
768    pub fn verify_unreferenced_class(name: &str) -> Self {
769        Self {
770            kind: CssCandidateActionType::VerifyUnused,
771            auto_fixable: false,
772            description: format!(
773                "Confirm no HTML email, server-rendered template, or CMS content applies the \"{name}\" class before removing it (fallow scanned in-project JS/TS/HTML/Vue/Svelte/Astro/Markdown markup)."
774            ),
775            command: safe_token_search(name),
776        }
777    }
778
779    /// Verify action for an unreferenced `@keyframes`: a read-only token search
780    /// for any JavaScript or template reference that applies the animation
781    /// (which the CSS-only scan cannot see).
782    #[must_use]
783    pub fn verify_keyframe(name: &str) -> Self {
784        Self {
785            kind: CssCandidateActionType::VerifyUnused,
786            auto_fixable: false,
787            description: format!(
788                "Confirm no JavaScript or template applies the \"{name}\" animation before removing the @keyframes."
789            ),
790            command: safe_token_search(name),
791        }
792    }
793
794    /// Verify action for an animation reference to a `@keyframes` that is
795    /// defined in no stylesheet: a read-only token search for a CSS-in-JS
796    /// `@keyframes`/animation definition of the name (styled-components,
797    /// Emotion, vanilla-extract) before treating the reference as a typo.
798    #[must_use]
799    pub fn verify_undefined_keyframe(name: &str) -> Self {
800        Self {
801            kind: CssCandidateActionType::VerifyUndefined,
802            auto_fixable: false,
803            description: format!(
804                "Confirm \"{name}\" is not a @keyframes defined in CSS-in-JS (styled-components, Emotion, vanilla-extract) before treating the animation reference as a typo."
805            ),
806            command: safe_token_search(name),
807        }
808    }
809
810    /// Guidance action for a mixed value axis (colors authored in several
811    /// notations, or font sizes in several units): standardize on the single
812    /// dominant notation. No command (this is a project-wide refactor, and the
813    /// per-notation breakdown already quantifies the spread); the residual
814    /// judgment is whether the spread is an intentional migration in progress.
815    #[must_use]
816    pub fn standardize_notation(axis: &str, dominant: &str) -> Self {
817        Self {
818            kind: CssCandidateActionType::Standardize,
819            auto_fixable: false,
820            description: format!(
821                "{axis} are authored in several notations; standardize on one ({dominant} is the most common) so the scale is a single source of truth, unless this is an intentional migration in progress."
822            ),
823            command: None,
824        }
825    }
826
827    /// Guidance action for a duplicate declaration block: consolidate the shared
828    /// declarations into one rule. No command (consolidation is a refactor, and
829    /// the occurrences list already names every site); the residual judgment is
830    /// whether the rules are intentionally separate overrides.
831    #[must_use]
832    pub fn consolidate_block(occurrence_count: u32) -> Self {
833        Self {
834            kind: CssCandidateActionType::Consolidate,
835            auto_fixable: false,
836            description: format!(
837                "Extract this declaration block into one rule and reference it from all {occurrence_count} occurrences, unless they are intentionally separate overrides."
838            ),
839            command: None,
840        }
841    }
842
843    /// Action for a Tailwind arbitrary-value bypass: a read-only fixed-string
844    /// search for every occurrence of the token so it can be replaced with a
845    /// scale token (or confirmed an intentional one-off). The value is a Tailwind
846    /// utility token (no quotes / whitespace by construction), so it is safe to
847    /// single-quote; the `-F` keeps the `[` / `]` literal rather than a glob.
848    #[must_use]
849    pub fn replace_arbitrary_value(value: &str) -> Self {
850        let command = (!value.contains('\'')).then(|| {
851            format!(
852                "grep -rnF '{value}' --include='*.jsx' --include='*.tsx' --include='*.html' --include='*.vue' --include='*.svelte' --include='*.astro' ."
853            )
854        });
855        Self {
856            kind: CssCandidateActionType::ReplaceWithToken,
857            auto_fixable: false,
858            description:
859                "Replace this one-off arbitrary value with a scale token from your Tailwind theme, or confirm it is intentional."
860                    .to_string(),
861            command,
862        }
863    }
864
865    /// Guidance for a CVA / shadcn variant arbitrary value: replace the
866    /// utility with a token-backed variant class after checking variant intent.
867    #[must_use]
868    pub fn replace_cva_variant_arbitrary_value(class_token: &str, nearest: &str) -> Self {
869        Self {
870            kind: CssCandidateActionType::ReplaceWithToken,
871            auto_fixable: false,
872            description: format!(
873                "Verify this CVA variant value is not an intentional one-off, then replace {class_token} with a class backed by {nearest}."
874            ),
875            command: safe_token_search(class_token),
876        }
877    }
878
879    /// Guidance for a raw CSS value on a scale-sensitive axis: replace with an
880    /// existing token or confirm the one-off is intentional.
881    #[must_use]
882    pub fn replace_raw_style_value(axis: &str, value: &str) -> Self {
883        Self {
884            kind: CssCandidateActionType::ReplaceWithToken,
885            auto_fixable: false,
886            description: format!(
887                "Replace this raw {axis} value with an existing design token or CSS custom property, or confirm this one-off is intentional."
888            ),
889            command: safe_token_search(value),
890        }
891    }
892
893    /// Verify action for an unused CSS at-rule entity: a read-only search for
894    /// any out-of-CSS consumer (JS reading an `@property`; an `@import layer()`
895    /// populating a layer) before removing it.
896    #[must_use]
897    pub fn verify_unused_at_rule(kind: UnusedAtRuleKind, name: &str) -> Self {
898        let description = match kind {
899            UnusedAtRuleKind::PropertyRegistration => format!(
900                "Confirm \"{name}\" is not read or set from JavaScript before removing the @property registration."
901            ),
902            UnusedAtRuleKind::Layer => format!(
903                "Confirm the @layer \"{name}\" is not populated via @import layer() before removing the declaration."
904            ),
905        };
906        Self {
907            kind: CssCandidateActionType::VerifyUnused,
908            auto_fixable: false,
909            description,
910            command: safe_token_search(name),
911        }
912    }
913
914    /// Verify action for a markup class token that matches no defined CSS class
915    /// but is one edit from a class that is defined: surface the suggestion and a
916    /// read-only token search so the residual risk (a class defined in CSS-in-JS
917    /// or an external stylesheet) can be ruled out before fixing the typo.
918    #[must_use]
919    pub fn verify_unresolved_class(class: &str, suggestion: &str) -> Self {
920        Self {
921            kind: CssCandidateActionType::VerifyUndefined,
922            auto_fixable: false,
923            description: format!(
924                "\"{class}\" matches no CSS class; did you mean \"{suggestion}\"? Confirm \"{class}\" is not defined in CSS-in-JS or an external stylesheet before fixing the reference."
925            ),
926            command: safe_token_search(class),
927        }
928    }
929
930    /// Verify action for a Vue SFC's unused scoped classes. The component-scoped
931    /// scan already covers every static use, so the only residual risk is a
932    /// class assembled from a dynamic string; that is a manual check, so the
933    /// action carries guidance but no command.
934    #[must_use]
935    pub fn verify_scoped_classes() -> Self {
936        Self {
937            kind: CssCandidateActionType::VerifyUnused,
938            auto_fixable: false,
939            description:
940                "Confirm none of these scoped classes is assembled from a dynamic string (e.g. `:class=\"prefix + name\"`) before removing them."
941                    .to_string(),
942            command: None,
943        }
944    }
945}
946
947/// Build a read-only, placeholder-free, namespace-QUALIFIED search for a Tailwind
948/// v4 `@theme` token, or `None` when the namespace / name is not a plain CSS
949/// identifier (so the emitted command is always shell-safe). The pattern matches
950/// any `*-<name>` utility (`bg-<name>`, `rounded-<name>`, `font-<name>`, ...) AND
951/// the `--<ns>-<name>` custom property (covering `var()` reads and `[--ns-name]`
952/// arbitrary values), deliberately NOT a bare `<name>` (which would substring-hit
953/// every file for a dictionary-word token like `brand` / `card`).
954fn theme_token_search(namespace: &str, name: &str) -> Option<String> {
955    let is_plain = |s: &str| {
956        !s.is_empty()
957            && s.bytes()
958                .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
959    };
960    (is_plain(namespace) && is_plain(name)).then(|| {
961        format!(
962            "grep -rnE -- '-{name}\\b|--{namespace}-{name}' --include='*.css' --include='*.html' --include='*.js' --include='*.jsx' --include='*.ts' --include='*.tsx' --include='*.vue' --include='*.svelte' --include='*.astro' ."
963        )
964    })
965}
966
967/// Build a read-only, placeholder-free token search for `name`, or `None` when
968/// the name is not a plain CSS identifier, so the emitted command is always
969/// shell-safe without quoting tricks.
970fn safe_token_search(name: &str) -> Option<String> {
971    let is_plain = !name.is_empty()
972        && name
973            .bytes()
974            .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_');
975    is_plain.then(|| {
976        format!(
977            "grep -rnw '{name}' --include='*.js' --include='*.jsx' --include='*.ts' --include='*.tsx' --include='*.vue' --include='*.svelte' --include='*.astro' --include='*.html' --include='*.md' --include='*.mdx' ."
978        )
979    })
980}
981
982/// Per-stylesheet CSS analytics.
983#[derive(Debug, Clone, serde::Serialize)]
984#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
985pub struct CssFileAnalytics {
986    /// Project-root-relative, forward-slash path.
987    pub path: String,
988    /// The stylesheet's structural metrics.
989    pub analytics: fallow_types::extract::CssAnalytics,
990}
991
992/// Project-wide CSS analytics aggregates across every analyzed stylesheet
993/// (including stylesheets with no notable rule, which are not listed
994/// individually in `files`).
995#[derive(Debug, Clone, Default, serde::Serialize)]
996#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
997pub struct CssAnalyticsSummary {
998    /// Stylesheets analyzed: standard `.css` files, Vue/Svelte SFC `<style>`
999    /// blocks, and (dep-gated) CSS-in-JS, both the tagged-template form and the
1000    /// object form (`style({...})` / `stylex.create({...})` / `css({...})`). SCSS
1001    /// is skipped. Note: flat atomic object CSS-in-JS (StyleX/Panda) is counted
1002    /// here and contributes to these aggregates, but has no notable rules, so its
1003    /// files never appear in the per-file `files` list.
1004    pub files_analyzed: u32,
1005    /// Total style rules across analyzed stylesheets.
1006    pub total_rules: u32,
1007    /// Total declarations across analyzed stylesheets.
1008    pub total_declarations: u32,
1009    /// Total `!important` declarations across analyzed stylesheets.
1010    pub important_declarations: u32,
1011    /// Total empty style rules across analyzed stylesheets.
1012    pub empty_rules: u32,
1013    /// Deepest style-rule nesting depth observed across analyzed stylesheets.
1014    pub max_nesting_depth: u8,
1015    /// Distinct color values (authored form) across the whole codebase. A high
1016    /// count signals an uncontrolled palette (design-token sprawl).
1017    pub unique_colors: u32,
1018    /// Distinct `font-size` values across the whole codebase.
1019    pub unique_font_sizes: u32,
1020    /// Distinct `z-index` values across the whole codebase.
1021    pub unique_z_indexes: u32,
1022    /// Distinct `box-shadow` values across the whole codebase (shadow-scale sprawl).
1023    pub unique_box_shadows: u32,
1024    /// Distinct `border-radius` values across the whole codebase (radius-scale sprawl).
1025    pub unique_border_radii: u32,
1026    /// Distinct `line-height` values across the whole codebase (type-scale sprawl).
1027    pub unique_line_heights: u32,
1028    /// Distinct custom properties (`--x`) defined anywhere in the codebase.
1029    pub custom_properties_defined: u32,
1030    /// Custom properties defined but never referenced via `var()` in any
1031    /// stylesheet (the defined-but-unused direction). These are cleanup
1032    /// CANDIDATES, not confirmed dead: a property may still be read or set from
1033    /// JavaScript or inline HTML styles.
1034    pub custom_properties_unreferenced: u32,
1035    /// Distinct custom properties referenced via `var()` that are defined in no
1036    /// stylesheet anywhere (the used-but-undefined direction). A COUNT only, not
1037    /// a located list: a `var(--x)` with no CSS definition is extremely common
1038    /// in JavaScript-driven theming and design-token libraries, so locating
1039    /// these would be net-noise. The count is an architecture signal (how much
1040    /// of the `var()` surface is resolved outside CSS), not a finding.
1041    pub custom_properties_undefined: u32,
1042    /// Distinct `@keyframes` defined anywhere in the codebase.
1043    pub keyframes_defined: u32,
1044    /// `@keyframes` defined but never referenced via `animation` /
1045    /// `animation-name` in any stylesheet (the defined-but-unused direction;
1046    /// cleanup CANDIDATES; an animation name can still be applied from
1047    /// JavaScript).
1048    pub keyframes_unreferenced: u32,
1049    /// Distinct animation names referenced via `animation` / `animation-name`
1050    /// that resolve to no `@keyframes` definition anywhere (the used-but-
1051    /// undefined direction). Located in `undefined_keyframes`; usually a typo or
1052    /// a removed animation.
1053    pub keyframes_undefined: u32,
1054    /// Total Vue `<style scoped>` classes used nowhere else in their component
1055    /// (cleanup candidates), across all SFCs.
1056    pub scoped_unused_classes: u32,
1057    /// Number of distinct declaration blocks (4+ declarations) that appear in
1058    /// two or more rules across the project (copy-paste consolidation
1059    /// candidates). Located in `duplicate_declaration_blocks`.
1060    pub duplicate_declaration_blocks: u32,
1061    /// Total declarations removable by consolidating every duplicate block:
1062    /// the sum of `(occurrence_count - 1) * declaration_count` across groups.
1063    pub duplicate_declarations_total: u32,
1064    /// Distinct Tailwind arbitrary-value tokens used in markup (design-token
1065    /// bypass). Zero when the project does not use Tailwind. Located in
1066    /// `tailwind_arbitrary_values`.
1067    pub tailwind_arbitrary_values: u32,
1068    /// Total Tailwind arbitrary-value occurrences across markup.
1069    pub tailwind_arbitrary_value_uses: u32,
1070    /// Preprocessor stylesheets (`.scss`, `.sass`, `.less`) seen by the styling
1071    /// scan. These are parsed textually for local candidates, not compiled.
1072    pub preprocessor_stylesheets: u32,
1073    /// True when project-wide class reachability was skipped because
1074    /// preprocessor stylesheets outnumber plain CSS, making generated classes
1075    /// invisible without a Sass/Less compiler.
1076    pub preprocessor_reachability_abstained: bool,
1077    /// Located raw CSS declaration values that bypass token surfaces on
1078    /// scale-sensitive axes. Located in `raw_style_values`.
1079    pub raw_style_values: u32,
1080    /// `@property` registrations never referenced via `var()` in any stylesheet
1081    /// (located in `unused_at_rules`). Cleanup candidates.
1082    pub unused_property_registrations: u32,
1083    /// Cascade layers declared but never populated by a block (located in
1084    /// `unused_at_rules`). Cleanup candidates.
1085    pub unused_layers: u32,
1086    /// Static markup class tokens that match no defined CSS class but are one
1087    /// edit from a defined class (likely typos / stale renames). Located in
1088    /// `unresolved_class_references`. Candidates, never gated.
1089    pub unresolved_class_references: u32,
1090    /// Global CSS classes defined in a stylesheet but referenced by no in-project
1091    /// markup (located in `unreferenced_css_classes`). Heavily gated cleanup
1092    /// candidates; zero on preprocessor-dominant or partial-scope runs.
1093    pub unreferenced_css_classes: u32,
1094    /// `@font-face` families declared but referenced by no `font-family` anywhere
1095    /// (located in `unused_font_faces`). Dead web-font cleanup candidates.
1096    pub unused_font_faces: u32,
1097    /// Tailwind v4 `@theme` design tokens defined but used by no generated
1098    /// utility, `var()`, `@apply`, or arbitrary value anywhere (located in
1099    /// `unused_theme_tokens`). Dead-design-token cleanup candidates; zero when
1100    /// the project is not Tailwind v4 or a plugin / published-library /
1101    /// partial-scope run gated the scan out.
1102    pub unused_theme_tokens: u32,
1103    /// Tailwind v4 theme tokens whose comparable values are close to another
1104    /// token in the same theme dictionary. Located in
1105    /// `near_duplicate_theme_tokens`.
1106    pub near_duplicate_theme_tokens: u32,
1107    /// Number of distinct `font-size` units (`px` / `rem` / `em` / `%`) authored
1108    /// across the codebase. Mixing units is a type-scale consistency smell,
1109    /// broken out in `font_size_unit_mix`.
1110    pub font_size_units_used: u32,
1111    /// Number of analyzed stylesheets whose per-rule `notable_rules` list was
1112    /// truncated at the per-file cap, so a consumer knows the per-rule detail is
1113    /// incomplete without walking every file.
1114    pub notable_truncated_files: u32,
1115}
1116
1117#[cfg(test)]
1118#[allow(
1119    clippy::unwrap_used,
1120    reason = "tests use unwrap to keep serialization assertions concise"
1121)]
1122mod tests {
1123    use super::*;
1124
1125    #[test]
1126    fn consumer_kind_serializes_kebab_case() {
1127        let kinds = [
1128            (ConsumerKind::ThemeVar, "\"theme-var\""),
1129            (ConsumerKind::CssVar, "\"css-var\""),
1130            (ConsumerKind::Utility, "\"utility\""),
1131            (ConsumerKind::Apply, "\"apply\""),
1132        ];
1133        for (kind, expected) in kinds {
1134            assert_eq!(serde_json::to_string(&kind).unwrap(), expected);
1135        }
1136    }
1137
1138    #[test]
1139    fn token_consumers_serializes_full_shape() {
1140        let entry = TokenConsumers {
1141            token: "--color-brand".to_string(),
1142            namespace: "color".to_string(),
1143            definition_path: "src/theme.css".to_string(),
1144            definition_line: 4,
1145            consumer_count: 2,
1146            consumers: vec![
1147                TokenConsumerLocation {
1148                    path: "src/Button.tsx".to_string(),
1149                    line: 12,
1150                    kind: ConsumerKind::Utility,
1151                },
1152                TokenConsumerLocation {
1153                    path: "src/theme.css".to_string(),
1154                    line: 9,
1155                    kind: ConsumerKind::CssVar,
1156                },
1157            ],
1158        };
1159        let value = serde_json::to_value(&entry).unwrap();
1160        assert_eq!(value["consumer_count"], 2);
1161        assert_eq!(value["definition_line"], 4);
1162        assert_eq!(value["consumers"][0]["kind"], "utility");
1163        assert_eq!(value["consumers"][1]["kind"], "css-var");
1164    }
1165
1166    #[test]
1167    fn token_consumers_omitted_when_empty() {
1168        let report = CssAnalyticsReport {
1169            files: Vec::new(),
1170            summary: CssAnalyticsSummary::default(),
1171            scoped_unused: Vec::new(),
1172            unreferenced_keyframes: Vec::new(),
1173            undefined_keyframes: Vec::new(),
1174            duplicate_declaration_blocks: Vec::new(),
1175            cva_duplicate_variant_blocks: Vec::new(),
1176            cva_variant_token_drifts: Vec::new(),
1177            tailwind_arbitrary_values: Vec::new(),
1178            raw_style_values: Vec::new(),
1179            unused_at_rules: Vec::new(),
1180            unresolved_class_references: Vec::new(),
1181            unreferenced_css_classes: Vec::new(),
1182            unused_font_faces: Vec::new(),
1183            unused_theme_tokens: Vec::new(),
1184            near_duplicate_theme_tokens: Vec::new(),
1185            token_consumers: Vec::new(),
1186            font_size_unit_mix: None,
1187        };
1188        let value = serde_json::to_value(&report).unwrap();
1189        assert!(
1190            value.get("token_consumers").is_none(),
1191            "empty token_consumers must be skipped"
1192        );
1193    }
1194
1195    #[test]
1196    fn token_consumers_present_when_non_empty() {
1197        let report = CssAnalyticsReport {
1198            files: Vec::new(),
1199            summary: CssAnalyticsSummary::default(),
1200            scoped_unused: Vec::new(),
1201            unreferenced_keyframes: Vec::new(),
1202            undefined_keyframes: Vec::new(),
1203            duplicate_declaration_blocks: Vec::new(),
1204            cva_duplicate_variant_blocks: Vec::new(),
1205            cva_variant_token_drifts: Vec::new(),
1206            tailwind_arbitrary_values: Vec::new(),
1207            raw_style_values: Vec::new(),
1208            unused_at_rules: Vec::new(),
1209            unresolved_class_references: Vec::new(),
1210            unreferenced_css_classes: Vec::new(),
1211            unused_font_faces: Vec::new(),
1212            unused_theme_tokens: Vec::new(),
1213            near_duplicate_theme_tokens: Vec::new(),
1214            token_consumers: vec![TokenConsumers {
1215                token: "--color-brand".to_string(),
1216                namespace: "color".to_string(),
1217                definition_path: "src/theme.css".to_string(),
1218                definition_line: 4,
1219                consumer_count: 0,
1220                consumers: Vec::new(),
1221            }],
1222            font_size_unit_mix: None,
1223        };
1224        let value = serde_json::to_value(&report).unwrap();
1225        assert_eq!(value["token_consumers"][0]["consumer_count"], 0);
1226    }
1227}