Skip to main content

fallow_types/
results_component_intel.rs

1//! Render fan-in counts and the React hook, prop drill and component
2//! summaries of a run.
3
4use super::*;
5
6/// Per-component render fan-in counts plus the precomputed concentration aggregates.
7///
8/// DESCRIPTIVE blast-radius signal (NOT a rule, finding, or threshold): the
9/// component-graph analogue of module-level fan-in. Module fan-in counts
10/// importing MODULES; render fan-in counts JSX render CALL SITES (a shared
11/// `<Button>` is rendered in far more places than it is imported).
12///
13/// `per_component` is the internal carrier (keyed for hotspot path annotation),
14/// `#[serde(skip)]` on [`AnalysisResults`] so it never appears under bare
15/// `fallow` / `audit`; the aggregates feed the descriptive `VitalSigns` block
16/// (`p95_render_fan_in` / `render_fan_in_high_pct` / `max_render_fan_in`).
17///
18/// UNDERCOUNT is the documented safe direction: a child rendered via a JSX
19/// spread, a dynamic / `createElement(var)` form, or a member-expression tag
20/// (`<Lib.Button/>`) is not resolved by the shared `ChildResolver` and so
21/// increments no component's fan-in. A true high-fan-in component can only be
22/// undersold, never falsely flagged. A rare name-collision over-credit is
23/// possible via the default-import sole-component fallback (inherited verbatim
24/// from the prop-drilling / thin-wrapper resolver); low-harm for a descriptive,
25/// non-gating metric.
26#[derive(Debug, Clone, Default)]
27pub struct RenderFanInMetric {
28    /// Per-component render-site + distinct-parent counts. Keyed by
29    /// `(component file path, component name)` so the hotspot surface can map a
30    /// file back to its top component's fan-in. Components rendered nowhere ARE
31    /// included as a real `0` so the percentile distribution is not skewed.
32    pub per_component: Vec<RenderFanInComponent>,
33    /// 95th-percentile DISTINCT-PARENTS render fan-in across components (the
34    /// per-component distribution analogue of the module-fan-in p95). `None` on
35    /// an empty population. Mirrors `compute_coupling_concentration`.
36    pub p95_distinct_parents: Option<u32>,
37    /// Percentage of components whose distinct-parents render fan-in exceeds the
38    /// `max(p95, 10)` threshold (the same floor coupling concentration uses).
39    /// `None` on an empty population.
40    pub high_pct: Option<f64>,
41    /// The single highest distinct-parent count across all components (the
42    /// headline blast-radius number: the most distinct render locations any one
43    /// component is rendered from, the honest edit-ripple count). `None` on an
44    /// empty population. `render_sites` (incl. repeats) is secondary per-component
45    /// context, never the headline.
46    pub max_distinct_parents: Option<u32>,
47}
48
49/// One component's render fan-in detail: how many JSX render SITES target it and
50/// how many DISTINCT parent components render it.
51#[derive(Debug, Clone)]
52pub struct RenderFanInComponent {
53    /// Absolute path of the file declaring the component.
54    pub file: PathBuf,
55    /// The component name.
56    pub component: String,
57    /// Total JSX render sites that resolve to this component across the project
58    /// (each capitalized / member JSX tag is one site). SECONDARY context ("incl.
59    /// repeats"): a single parent rendering one child five times is five sites but
60    /// one distinct parent, so render_sites overcounts blast radius.
61    pub render_sites: u32,
62    /// Distinct `(parent_file, parent_component)` keys that render this
63    /// component. The HEADLINE blast-radius axis: the honest count of distinct
64    /// render LOCATIONS, the percentiled distribution analogue of "distinct importers".
65    pub distinct_parents: u32,
66}
67
68/// Per-kind hook counts for a React component, summarized from `hook_uses`.
69/// DESCRIPTIVE editor context (the LSP code-lens hook breakdown), never a
70/// finding, severity, or `total_issues` input. `custom` collects every
71/// `use*`-named call that is not one of the four built-ins.
72#[derive(Debug, Clone, Default, PartialEq, Eq)]
73pub struct ReactHookSummary {
74    /// `useState(...)` call count.
75    pub state: u16,
76    /// `useEffect(...)` call count.
77    pub effect: u16,
78    /// `useMemo(...)` call count.
79    pub memo: u16,
80    /// `useCallback(...)` call count.
81    pub callback: u16,
82    /// Count of any other `use*`-named call (a custom hook).
83    pub custom: u16,
84}
85
86/// A prop-drilling trace for a prop at the ROOT of a forwarding chain.
87/// DESCRIPTIVE ambient editor context (the LSP per-prop hover): the prop is
88/// forwarded unchanged through `depth` components before a component
89/// substantively consumes it. Reuses the `prop-drilling` chain machinery's
90/// abstain ladder (spread / `cloneElement` / dynamic / provider-in-subtree drop
91/// the whole chain), so the trace is honest. NOT a finding (the opt-in
92/// `prop-drilling` rule owns the finding); this rides the `#[serde(skip)]`
93/// `ReactComponentIntel` carrier.
94#[derive(Debug, Clone, PartialEq, Eq)]
95pub struct ReactPropDrill {
96    /// The chain depth = number of components the prop is forwarded THROUGH
97    /// (source + intermediates + consumer), matching `PropDrillingChain.depth`.
98    pub depth: u32,
99    /// The ordered component names from source to consumer (`hops[0]` owns the
100    /// prop, the last consumes it).
101    pub hops: Vec<String>,
102}
103
104/// Per-prop usage intelligence for one React component prop. DESCRIPTIVE editor
105/// context (the LSP per-prop hover): whether the prop is read in the component
106/// body and how many render sites pass it. NOT a finding (the
107/// `unused-component-prop` React arm owns the deadness rule); this is ambient
108/// signal. `anchor_line` / `anchor_col` follow the same convention the React
109/// `unused-component-prop` findings use (1-based line, byte-derived col from
110/// `byte_offset_to_line_col`).
111#[derive(Debug, Clone, PartialEq, Eq)]
112pub struct ReactPropIntel {
113    /// The declared prop name.
114    pub name: String,
115    /// 1-based line of the prop declaration (anchors the hover).
116    pub anchor_line: u32,
117    /// Column of the prop declaration (byte-derived, matching the React
118    /// `unused-component-prop` finding convention).
119    pub anchor_col: u32,
120    /// Whether the prop is referenced in the component body (`used_in_script`
121    /// for the React arm: a resolved reference to the destructured local).
122    pub used_in_body: bool,
123    /// Count of render sites (test/spec/story/fixture files excluded) whose
124    /// passed-attribute set contains this prop name.
125    pub passed_from_sites: u32,
126    /// A prop-drilling trace, present only when this prop is the ROOT of a
127    /// forwarding chain that reaches a consumer through `>= N` pass-through
128    /// components. `None` for an ordinary prop. Test/spec/story/fixture source
129    /// components never carry a drill trace.
130    pub drill: Option<ReactPropDrill>,
131}
132
133/// Per-component render + prop + hook intelligence for one React component.
134/// DESCRIPTIVE ambient editor context surfaced by the LSP (a component summary
135/// code lens plus per-prop hovers), NOT a finding, IssueKind, severity, or
136/// `total_issues` input. Carried in-process on the `#[serde(skip)]`
137/// `AnalysisResults::react_component_intel` field (like
138/// [`RenderFanInMetric`]); never serialized, so bare `fallow` / `audit` and the
139/// JSON / schema surface are untouched.
140///
141/// Counts are HONEST: test/spec/story/fixture render sites are excluded from
142/// `render_sites`, `distinct_parents`, and per-prop `passed_from_sites`, and
143/// `distinct_parents` (not the repeat-inflated `render_sites`) is the headline,
144/// mirroring the render-fan-in metric's discipline.
145#[derive(Debug, Clone, PartialEq, Eq)]
146pub struct ReactComponentIntel {
147    /// Absolute path of the file declaring the component.
148    pub path: PathBuf,
149    /// The component name.
150    pub component_name: String,
151    /// 1-based line of the component definition (anchors the code lens).
152    pub anchor_line: u32,
153    /// Column of the component definition (byte-derived).
154    pub anchor_col: u32,
155    /// Total JSX render SITES that resolve to this component (each capitalized /
156    /// member JSX tag is one site). SECONDARY context: a single parent rendering
157    /// the child five times is five sites but one distinct parent.
158    pub render_sites: u32,
159    /// Distinct `(parent_file, parent_component)` keys rendering this component.
160    /// The HEADLINE blast-radius count (never the repeat-inflated site count).
161    pub distinct_parents: u32,
162    /// Number of declared props on this component.
163    pub prop_count: u16,
164    /// Per-kind hook counts.
165    pub hooks: ReactHookSummary,
166    /// Per-prop usage intelligence (one entry per declared prop).
167    pub props: Vec<ReactPropIntel>,
168}