Skip to main content

fallow_extract/cache/
types.rs

1//! Serialization types for the incremental parse cache.
2//!
3//! All types use bitcode `Encode`/`Decode` for fast binary serialization.
4
5use bitcode::{Decode, Encode};
6
7use crate::MemberKind;
8
9/// Cache version, bump when the cache format or cached extraction semantics change.
10///
11/// Bumped to 89 for issue #475: extraction now strips a leading UTF-8 BOM
12/// before hashing and computing line offsets, so pre-fix entries whose source
13/// included a BOM carry hashes over the wrong byte sequence and would
14/// fast-path into stale `member_accesses` / `exports` for any BOM-bearing
15/// file. The bump invalidates user caches once on upgrade; subsequent runs
16/// are warm.
17///
18/// Bumped to 90 for issue #540: CSS Modules class extraction now strips
19/// `@layer` and `@import` at-rule preludes before scanning class names, so
20/// pre-fix entries for `.module.css` files using nested cascade-layer syntax
21/// (`@layer foo.bar { ... }`) carry phantom `bar` / `baz` exports that the
22/// new scanner no longer produces.
23///
24/// Bumped to 91 for issue #549: CSS Modules class extraction now records a
25/// real `Span` pointing at each class's declaration position in the source.
26/// Pre-fix cache entries for `.module.css` / `.module.scss` files carry
27/// `Span::default()` (start=0, end=0) on every export, which renders every
28/// finding at line:1 col:0; the new scanner produces real offsets.
29///
30/// Bumped to 92 for issue #563: feature flag extraction recognizes additional
31/// built-in SDK providers (PostHog, Vercel Flags, Optimizely, Eppo, plus more
32/// ConfigCat surfaces) and Vercel `flag({ key: "..." })` object arguments, so
33/// pre-fix entries can carry stale `flag_uses`.
34///
35/// Bumped to 93 for issue #589: Node `module.register()` loader calls now
36/// emit `DynamicImportInfo.destructured_names` populated with the loader-hook
37/// allowlist (current `initialize` / `resolve` / `load` / `globalPreload`
38/// plus legacy `getFormat` / `getSource` / `transformSource`) for every
39/// relative or `file:` specifier, including specifiers bound via
40/// `new URL(..., import.meta.url)`. Pre-fix entries carry empty
41/// `destructured_names` for the same source, so they would silently miss
42/// the named-export credit until the file is touched.
43///
44/// Bumped to 94 for issue #586: Playwright helper fixture extraction recognizes
45/// helpers with local setup before the final `return base.extend<T>(...)`, so
46/// pre-fix entries can miss fixture definition sentinels.
47///
48/// Bumped to 95 for the Glimmer `<template>` scanner: imported-binding usage
49/// and `MemberAccess { object: "this", member }` records for `{{this.foo}}`
50/// template references are now folded into the extractor before
51/// `into_module_info`. Pre-fix entries for `.gts` / `.gjs` files omit both,
52/// so template-only imports surface as `unused-import` and template-only
53/// class members as `unused-class-member` until the cache is re-extracted.
54///
55/// Bumped to 96 for issue #640: generic JSX `<script src>` and
56/// `<link rel="stylesheet|modulepreload" href>` attributes no longer emit
57/// synthetic `SideEffect` imports, so pre-fix entries can carry stale JSX
58/// resource edges that surface as false `unresolved-imports`.
59pub(super) const CACHE_VERSION: u32 = 96;
60
61/// Duplication token cache version. Bump when duplicate tokenization,
62/// normalization, or the on-disk token cache schema changes.
63pub const DUPES_CACHE_VERSION: u32 = 4;
64
65/// Default maximum cache size (256 MB). Overridable per-project via
66/// `cache.maxSizeMb` in the config file or `FALLOW_CACHE_MAX_SIZE` env var.
67/// Also used as the hard ceiling on load-time deserialization as a defence
68/// against pathological on-disk files.
69pub const DEFAULT_CACHE_MAX_SIZE: usize = 256 * 1024 * 1024;
70
71/// Trigger LRU eviction when the serialized cache exceeds 80% of the cap.
72/// Basis points (1/100 of a percent) for integer arithmetic without floats.
73pub(super) const EVICTION_TRIGGER_BPS: usize = 8000;
74
75/// Evict down to 60% of the cap so subsequent saves leave headroom.
76pub(super) const EVICTION_TARGET_BPS: usize = 6000;
77
78/// Promote the eviction log from `debug!` to `info!` when at least 25% of
79/// entries are removed in a single save. Default-noise concerns mean
80/// small-turnover saves should not be visible without `RUST_LOG=debug`.
81pub(super) const EVICTION_SIGNIFICANT_BPS: usize = 2500;
82
83/// Import kind discriminant for `CachedImport`:
84/// 0 = Named, 1 = Default, 2 = Namespace, 3 = `SideEffect`.
85pub(super) const IMPORT_KIND_NAMED: u8 = 0;
86pub(super) const IMPORT_KIND_DEFAULT: u8 = 1;
87pub(super) const IMPORT_KIND_NAMESPACE: u8 = 2;
88pub(super) const IMPORT_KIND_SIDE_EFFECT: u8 = 3;
89
90/// Cached data for a single module.
91#[derive(Debug, Clone, Encode, Decode)]
92pub struct CachedModule {
93    /// xxh3 hash of the file content.
94    pub content_hash: u64,
95    /// File modification time (seconds since epoch) for fast cache validation.
96    /// When mtime+size match the on-disk file, we skip reading file content entirely.
97    pub mtime_secs: u64,
98    /// File size in bytes for fast cache validation.
99    pub file_size: u64,
100    /// Seconds-since-epoch at the time this entry was last WRITTEN
101    /// (first parse or content-change refresh). NOT updated on cache-hit
102    /// reads: `update_cache` already iterates every in-scope file every run,
103    /// so refreshing on read would collapse the LRU to "last run this file
104    /// was discovered" for every retained entry. With write-only refresh,
105    /// the LRU genuinely targets stale (in-scope-but-unchanged-for-many-runs)
106    /// entries. Used by `CacheStore::save` for write-time eviction ordering.
107    pub last_access_secs: u64,
108    /// Exported symbols.
109    pub exports: Vec<CachedExport>,
110    /// Import specifiers.
111    pub imports: Vec<CachedImport>,
112    /// Re-export specifiers.
113    pub re_exports: Vec<CachedReExport>,
114    /// Dynamic import specifiers.
115    pub dynamic_imports: Vec<CachedDynamicImport>,
116    /// `require()` specifiers.
117    pub require_calls: Vec<CachedRequireCall>,
118    /// Static member accesses (e.g., `Status.Active`).
119    pub member_accesses: Vec<crate::MemberAccess>,
120    /// Identifiers used as whole objects (Object.values, for..in, spread, etc.).
121    pub whole_object_uses: Vec<String>,
122    /// Dynamic import patterns with partial static resolution.
123    pub dynamic_import_patterns: Vec<CachedDynamicImportPattern>,
124    /// Whether this module uses CJS exports.
125    pub has_cjs_exports: bool,
126    /// Whether this module declares at least one Angular `@Component({
127    /// templateUrl: ... })` decorator. Mirrors `ModuleInfo.has_angular_component_template_url`
128    /// so the CRAP-inherit walker's gate survives a warm-cache load.
129    pub has_angular_component_template_url: bool,
130    /// Local names of import bindings that are never referenced in this file.
131    pub unused_import_bindings: Vec<String>,
132    /// Local import bindings referenced from type positions.
133    pub type_referenced_import_bindings: Vec<String>,
134    /// Local import bindings referenced from value positions.
135    pub value_referenced_import_bindings: Vec<String>,
136    /// Inline suppression directives.
137    pub suppressions: Vec<CachedSuppression>,
138    /// Suppression tokens that did not parse to any known `IssueKind`. See #449.
139    pub unknown_suppression_kinds: Vec<CachedUnknownSuppressionKind>,
140    /// Pre-computed line-start byte offsets for O(log N) byte-to-line/col conversion.
141    pub line_offsets: Vec<u32>,
142    /// Per-function complexity metrics.
143    pub complexity: Vec<fallow_types::extract::FunctionComplexity>,
144    /// Feature flag use sites.
145    pub flag_uses: Vec<fallow_types::extract::FlagUse>,
146    /// Heritage metadata for exported classes.
147    pub class_heritage: Vec<fallow_types::extract::ClassHeritageInfo>,
148    /// Local type-capable declarations.
149    pub local_type_declarations: Vec<CachedLocalTypeDeclaration>,
150    /// Type references from exported public signatures.
151    pub public_signature_type_references: Vec<CachedPublicSignatureTypeReference>,
152    /// Namespace-import aliases re-exported through an object literal
153    /// (`export const API = { foo }` where `foo` is `import * as foo from './bar'`).
154    pub namespace_object_aliases: Vec<CachedNamespaceObjectAlias>,
155}
156
157/// Cached namespace-object alias.
158#[derive(Debug, Clone, Encode, Decode)]
159pub struct CachedNamespaceObjectAlias {
160    /// Canonical export name on this module.
161    pub via_export_name: String,
162    /// Dotted suffix of the property path relative to the export.
163    pub suffix: String,
164    /// Local name of the namespace import on this module.
165    pub namespace_local: String,
166}
167
168/// Cached local type declaration.
169#[derive(Debug, Clone, Encode, Decode)]
170pub struct CachedLocalTypeDeclaration {
171    /// Local declaration name.
172    pub name: String,
173    /// Byte offset of the declaration span start.
174    pub span_start: u32,
175    /// Byte offset of the declaration span end.
176    pub span_end: u32,
177}
178
179/// Cached public signature type reference.
180#[derive(Debug, Clone, Encode, Decode)]
181pub struct CachedPublicSignatureTypeReference {
182    /// Exported symbol whose signature contains the reference.
183    pub export_name: String,
184    /// Referenced type name.
185    pub type_name: String,
186    /// Byte offset of the reference span start.
187    pub span_start: u32,
188    /// Byte offset of the reference span end.
189    pub span_end: u32,
190}
191
192/// Cached suppression directive.
193#[derive(Debug, Clone, Encode, Decode)]
194pub struct CachedSuppression {
195    /// 1-based line this suppression applies to. 0 = file-wide.
196    pub line: u32,
197    /// 1-based line where the comment itself appears.
198    pub comment_line: u32,
199    /// 0 = suppress all, 1-20 = `IssueKind` discriminant.
200    pub kind: u8,
201}
202
203/// Cached unknown suppression kind token (see #449).
204#[derive(Debug, Clone, Encode, Decode)]
205pub struct CachedUnknownSuppressionKind {
206    /// 1-based line where the comment itself appears.
207    pub comment_line: u32,
208    /// True when the marker was `fallow-ignore-file`.
209    pub is_file_level: bool,
210    /// The verbatim token that did not parse.
211    pub token: String,
212}
213
214/// Cached export data for a single export declaration.
215#[derive(Debug, Clone, Encode, Decode)]
216pub struct CachedExport {
217    /// Export name (or "default" for default exports).
218    pub name: String,
219    /// Whether this is a default export.
220    pub is_default: bool,
221    /// Whether this is a type-only export.
222    pub is_type_only: bool,
223    /// Whether this export is registered through a runtime side effect at
224    /// module load time (Lit `@customElement` decorator or
225    /// `customElements.define` call). Persisted so warm-cache runs continue
226    /// to skip unused-export reporting for these classes.
227    pub is_side_effect_used: bool,
228    /// Visibility tag discriminant (0=None, 1=Public, 2=Internal, 3=Beta, 4=Alpha).
229    pub visibility: u8,
230    /// The local binding name, if different.
231    pub local_name: Option<String>,
232    /// Byte offset of the export span start.
233    pub span_start: u32,
234    /// Byte offset of the export span end.
235    pub span_end: u32,
236    /// Members of this export (for enums and classes).
237    pub members: Vec<CachedMember>,
238    /// The local name of the parent class from `extends` clause, if any.
239    pub super_class: Option<String>,
240}
241
242/// Cached import data for a single import declaration.
243#[derive(Debug, Clone, Encode, Decode)]
244pub struct CachedImport {
245    /// The import specifier.
246    pub source: String,
247    /// For Named imports, the imported symbol name. Empty for other kinds.
248    pub imported_name: String,
249    /// The local binding name.
250    pub local_name: String,
251    /// Whether this is a type-only import.
252    pub is_type_only: bool,
253    /// Whether this import originated from an SFC `<style>` block / `<style src>` (CSS context).
254    pub from_style: bool,
255    /// Import kind: 0=Named, 1=Default, 2=Namespace, 3=SideEffect.
256    pub kind: u8,
257    /// Byte offset of the import span start.
258    pub span_start: u32,
259    /// Byte offset of the import span end.
260    pub span_end: u32,
261    /// Byte offset of the source string literal span start.
262    pub source_span_start: u32,
263    /// Byte offset of the source string literal span end.
264    pub source_span_end: u32,
265}
266
267/// Cached dynamic import data.
268#[derive(Debug, Clone, Encode, Decode)]
269pub struct CachedDynamicImport {
270    /// The import specifier.
271    pub source: String,
272    /// Byte offset of the span start.
273    pub span_start: u32,
274    /// Byte offset of the span end.
275    pub span_end: u32,
276    /// Names destructured from the import result.
277    pub destructured_names: Vec<String>,
278    /// Local variable name for namespace imports.
279    pub local_name: Option<String>,
280    /// True when this dynamic import was synthesised by fallow (see
281    /// `DynamicImportInfo::is_speculative`).
282    pub is_speculative: bool,
283}
284
285/// Cached `require()` call data.
286#[derive(Debug, Clone, Encode, Decode)]
287pub struct CachedRequireCall {
288    /// The require specifier.
289    pub source: String,
290    /// Byte offset of the span start.
291    pub span_start: u32,
292    /// Byte offset of the span end.
293    pub span_end: u32,
294    /// Names destructured from the require result.
295    pub destructured_names: Vec<String>,
296    /// Local variable name for namespace requires.
297    pub local_name: Option<String>,
298}
299
300/// Cached re-export data.
301#[derive(Debug, Clone, Encode, Decode)]
302pub struct CachedReExport {
303    /// The module being re-exported from.
304    pub source: String,
305    /// Name imported from the source.
306    pub imported_name: String,
307    /// Name exported from this module.
308    pub exported_name: String,
309    /// Whether this is a type-only re-export.
310    pub is_type_only: bool,
311    /// Byte offset of the re-export span start (for line-number reporting).
312    pub span_start: u32,
313    /// Byte offset of the re-export span end.
314    pub span_end: u32,
315}
316
317/// Cached enum or class member data.
318#[derive(Debug, Clone, Encode, Decode)]
319pub struct CachedMember {
320    /// Member name.
321    pub name: String,
322    /// Member kind (enum, method, or property).
323    pub kind: MemberKind,
324    /// Byte offset of the span start.
325    pub span_start: u32,
326    /// Byte offset of the span end.
327    pub span_end: u32,
328    /// Whether this member has decorators.
329    pub has_decorator: bool,
330    /// Full dotted path of each decorator (e.g. `step`, `ns.foo`).
331    /// Empty for undecorated members and decorators with non-identifier
332    /// expressions.
333    pub decorator_names: Vec<String>,
334    /// True when this is a static method that returns a fresh instance of
335    /// the class: body returns `new this()` / `new <SameClassName>()`, or the
336    /// declared return type matches the class name. Treated as a factory.
337    /// See issues #346, #387.
338    pub is_instance_returning_static: bool,
339    /// True when this instance method's call result is an instance of the
340    /// same class (declared return type matches the class name, or body's
341    /// last statement is `return this`). Drives fluent-chain credit. See
342    /// issue #387.
343    pub is_self_returning: bool,
344}
345
346/// Cached dynamic import pattern data (template literals, `import.meta.glob`).
347#[derive(Debug, Clone, Encode, Decode)]
348pub struct CachedDynamicImportPattern {
349    /// Static prefix of the import path.
350    pub prefix: String,
351    /// Static suffix, if any.
352    pub suffix: Option<String>,
353    /// Byte offset of the span start.
354    pub span_start: u32,
355    /// Byte offset of the span end.
356    pub span_end: u32,
357}