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`.
59///
60/// Bumped to 97 for issue #639: MDX import/export extraction now skips
61/// fenced Markdown code blocks, so pre-fix entries can carry stale example
62/// imports that surface as false `unresolved-imports`.
63///
64/// Bumped to 98 for issue #638: statically resolvable `child_process.fork()`
65/// targets now emit `DynamicImportInfo` entries for local runner files.
66/// Pre-fix entries omit those dynamic imports, so forked script files can be
67/// reported as unused until the file is re-extracted.
68pub(super) const CACHE_VERSION: u32 = 98;
69
70/// Duplication token cache version. Bump when duplicate tokenization,
71/// normalization, or the on-disk token cache schema changes.
72pub const DUPES_CACHE_VERSION: u32 = 4;
73
74/// Default maximum cache size (256 MB). Overridable per-project via
75/// `cache.maxSizeMb` in the config file or `FALLOW_CACHE_MAX_SIZE` env var.
76/// Also used as the hard ceiling on load-time deserialization as a defence
77/// against pathological on-disk files.
78pub const DEFAULT_CACHE_MAX_SIZE: usize = 256 * 1024 * 1024;
79
80/// Trigger LRU eviction when the serialized cache exceeds 80% of the cap.
81/// Basis points (1/100 of a percent) for integer arithmetic without floats.
82pub(super) const EVICTION_TRIGGER_BPS: usize = 8000;
83
84/// Evict down to 60% of the cap so subsequent saves leave headroom.
85pub(super) const EVICTION_TARGET_BPS: usize = 6000;
86
87/// Promote the eviction log from `debug!` to `info!` when at least 25% of
88/// entries are removed in a single save. Default-noise concerns mean
89/// small-turnover saves should not be visible without `RUST_LOG=debug`.
90pub(super) const EVICTION_SIGNIFICANT_BPS: usize = 2500;
91
92/// Import kind discriminant for `CachedImport`:
93/// 0 = Named, 1 = Default, 2 = Namespace, 3 = `SideEffect`.
94pub(super) const IMPORT_KIND_NAMED: u8 = 0;
95pub(super) const IMPORT_KIND_DEFAULT: u8 = 1;
96pub(super) const IMPORT_KIND_NAMESPACE: u8 = 2;
97pub(super) const IMPORT_KIND_SIDE_EFFECT: u8 = 3;
98
99macro_rules! assert_cached_type_size {
100 ($ty:ty, $size:expr) => {
101 const _: () = assert!(
102 std::mem::size_of::<$ty>() == $size,
103 concat!(
104 stringify!($ty),
105 " size changed; bump CACHE_VERSION if the cached wire shape or extraction semantics changed, then update this assertion"
106 )
107 );
108 };
109}
110
111assert_cached_type_size!(CachedModule, 520);
112assert_cached_type_size!(CachedNamespaceObjectAlias, 72);
113assert_cached_type_size!(CachedLocalTypeDeclaration, 32);
114assert_cached_type_size!(CachedPublicSignatureTypeReference, 56);
115assert_cached_type_size!(CachedSuppression, 12);
116assert_cached_type_size!(CachedUnknownSuppressionKind, 32);
117assert_cached_type_size!(CachedExport, 112);
118assert_cached_type_size!(CachedImport, 96);
119assert_cached_type_size!(CachedDynamicImport, 88);
120assert_cached_type_size!(CachedRequireCall, 80);
121assert_cached_type_size!(CachedReExport, 88);
122assert_cached_type_size!(CachedMember, 64);
123assert_cached_type_size!(CachedDynamicImportPattern, 56);
124assert_cached_type_size!(crate::MemberAccess, 48);
125assert_cached_type_size!(fallow_types::extract::FunctionComplexity, 48);
126assert_cached_type_size!(fallow_types::extract::FlagUse, 80);
127assert_cached_type_size!(fallow_types::extract::ClassHeritageInfo, 96);
128
129/// Cached data for a single module.
130#[derive(Debug, Clone, Encode, Decode)]
131pub struct CachedModule {
132 /// xxh3 hash of the file content.
133 pub content_hash: u64,
134 /// File modification time (seconds since epoch) for fast cache validation.
135 /// When mtime+size match the on-disk file, we skip reading file content entirely.
136 pub mtime_secs: u64,
137 /// File size in bytes for fast cache validation.
138 pub file_size: u64,
139 /// Seconds-since-epoch at the time this entry was last WRITTEN
140 /// (first parse or content-change refresh). NOT updated on cache-hit
141 /// reads: `update_cache` already iterates every in-scope file every run,
142 /// so refreshing on read would collapse the LRU to "last run this file
143 /// was discovered" for every retained entry. With write-only refresh,
144 /// the LRU genuinely targets stale (in-scope-but-unchanged-for-many-runs)
145 /// entries. Used by `CacheStore::save` for write-time eviction ordering.
146 pub last_access_secs: u64,
147 /// Exported symbols.
148 pub exports: Vec<CachedExport>,
149 /// Import specifiers.
150 pub imports: Vec<CachedImport>,
151 /// Re-export specifiers.
152 pub re_exports: Vec<CachedReExport>,
153 /// Dynamic import specifiers.
154 pub dynamic_imports: Vec<CachedDynamicImport>,
155 /// `require()` specifiers.
156 pub require_calls: Vec<CachedRequireCall>,
157 /// Static member accesses (e.g., `Status.Active`).
158 pub member_accesses: Vec<crate::MemberAccess>,
159 /// Identifiers used as whole objects (Object.values, for..in, spread, etc.).
160 pub whole_object_uses: Vec<String>,
161 /// Dynamic import patterns with partial static resolution.
162 pub dynamic_import_patterns: Vec<CachedDynamicImportPattern>,
163 /// Whether this module uses CJS exports.
164 pub has_cjs_exports: bool,
165 /// Whether this module declares at least one Angular `@Component({
166 /// templateUrl: ... })` decorator. Mirrors `ModuleInfo.has_angular_component_template_url`
167 /// so the CRAP-inherit walker's gate survives a warm-cache load.
168 pub has_angular_component_template_url: bool,
169 /// Local names of import bindings that are never referenced in this file.
170 pub unused_import_bindings: Vec<String>,
171 /// Local import bindings referenced from type positions.
172 pub type_referenced_import_bindings: Vec<String>,
173 /// Local import bindings referenced from value positions.
174 pub value_referenced_import_bindings: Vec<String>,
175 /// Inline suppression directives.
176 pub suppressions: Vec<CachedSuppression>,
177 /// Suppression tokens that did not parse to any known `IssueKind`. See #449.
178 pub unknown_suppression_kinds: Vec<CachedUnknownSuppressionKind>,
179 /// Pre-computed line-start byte offsets for O(log N) byte-to-line/col conversion.
180 pub line_offsets: Vec<u32>,
181 /// Per-function complexity metrics.
182 pub complexity: Vec<fallow_types::extract::FunctionComplexity>,
183 /// Feature flag use sites.
184 pub flag_uses: Vec<fallow_types::extract::FlagUse>,
185 /// Heritage metadata for exported classes.
186 pub class_heritage: Vec<fallow_types::extract::ClassHeritageInfo>,
187 /// Local type-capable declarations.
188 pub local_type_declarations: Vec<CachedLocalTypeDeclaration>,
189 /// Type references from exported public signatures.
190 pub public_signature_type_references: Vec<CachedPublicSignatureTypeReference>,
191 /// Namespace-import aliases re-exported through an object literal
192 /// (`export const API = { foo }` where `foo` is `import * as foo from './bar'`).
193 pub namespace_object_aliases: Vec<CachedNamespaceObjectAlias>,
194}
195
196/// Cached namespace-object alias.
197#[derive(Debug, Clone, Encode, Decode)]
198pub struct CachedNamespaceObjectAlias {
199 /// Canonical export name on this module.
200 pub via_export_name: String,
201 /// Dotted suffix of the property path relative to the export.
202 pub suffix: String,
203 /// Local name of the namespace import on this module.
204 pub namespace_local: String,
205}
206
207/// Cached local type declaration.
208#[derive(Debug, Clone, Encode, Decode)]
209pub struct CachedLocalTypeDeclaration {
210 /// Local declaration name.
211 pub name: String,
212 /// Byte offset of the declaration span start.
213 pub span_start: u32,
214 /// Byte offset of the declaration span end.
215 pub span_end: u32,
216}
217
218/// Cached public signature type reference.
219#[derive(Debug, Clone, Encode, Decode)]
220pub struct CachedPublicSignatureTypeReference {
221 /// Exported symbol whose signature contains the reference.
222 pub export_name: String,
223 /// Referenced type name.
224 pub type_name: String,
225 /// Byte offset of the reference span start.
226 pub span_start: u32,
227 /// Byte offset of the reference span end.
228 pub span_end: u32,
229}
230
231/// Cached suppression directive.
232#[derive(Debug, Clone, Encode, Decode)]
233pub struct CachedSuppression {
234 /// 1-based line this suppression applies to. 0 = file-wide.
235 pub line: u32,
236 /// 1-based line where the comment itself appears.
237 pub comment_line: u32,
238 /// 0 = suppress all, 1-20 = `IssueKind` discriminant.
239 pub kind: u8,
240}
241
242/// Cached unknown suppression kind token (see #449).
243#[derive(Debug, Clone, Encode, Decode)]
244pub struct CachedUnknownSuppressionKind {
245 /// 1-based line where the comment itself appears.
246 pub comment_line: u32,
247 /// True when the marker was `fallow-ignore-file`.
248 pub is_file_level: bool,
249 /// The verbatim token that did not parse.
250 pub token: String,
251}
252
253/// Cached export data for a single export declaration.
254#[derive(Debug, Clone, Encode, Decode)]
255pub struct CachedExport {
256 /// Export name (or "default" for default exports).
257 pub name: String,
258 /// Whether this is a default export.
259 pub is_default: bool,
260 /// Whether this is a type-only export.
261 pub is_type_only: bool,
262 /// Whether this export is registered through a runtime side effect at
263 /// module load time (Lit `@customElement` decorator or
264 /// `customElements.define` call). Persisted so warm-cache runs continue
265 /// to skip unused-export reporting for these classes.
266 pub is_side_effect_used: bool,
267 /// Visibility tag discriminant (0=None, 1=Public, 2=Internal, 3=Beta, 4=Alpha).
268 pub visibility: u8,
269 /// The local binding name, if different.
270 pub local_name: Option<String>,
271 /// Byte offset of the export span start.
272 pub span_start: u32,
273 /// Byte offset of the export span end.
274 pub span_end: u32,
275 /// Members of this export (for enums and classes).
276 pub members: Vec<CachedMember>,
277 /// The local name of the parent class from `extends` clause, if any.
278 pub super_class: Option<String>,
279}
280
281/// Cached import data for a single import declaration.
282#[derive(Debug, Clone, Encode, Decode)]
283pub struct CachedImport {
284 /// The import specifier.
285 pub source: String,
286 /// For Named imports, the imported symbol name. Empty for other kinds.
287 pub imported_name: String,
288 /// The local binding name.
289 pub local_name: String,
290 /// Whether this is a type-only import.
291 pub is_type_only: bool,
292 /// Whether this import originated from an SFC `<style>` block / `<style src>` (CSS context).
293 pub from_style: bool,
294 /// Import kind: 0=Named, 1=Default, 2=Namespace, 3=SideEffect.
295 pub kind: u8,
296 /// Byte offset of the import span start.
297 pub span_start: u32,
298 /// Byte offset of the import span end.
299 pub span_end: u32,
300 /// Byte offset of the source string literal span start.
301 pub source_span_start: u32,
302 /// Byte offset of the source string literal span end.
303 pub source_span_end: u32,
304}
305
306/// Cached dynamic import data.
307#[derive(Debug, Clone, Encode, Decode)]
308pub struct CachedDynamicImport {
309 /// The import specifier.
310 pub source: String,
311 /// Byte offset of the span start.
312 pub span_start: u32,
313 /// Byte offset of the span end.
314 pub span_end: u32,
315 /// Names destructured from the import result.
316 pub destructured_names: Vec<String>,
317 /// Local variable name for namespace imports.
318 pub local_name: Option<String>,
319 /// True when this dynamic import was synthesised by fallow (see
320 /// `DynamicImportInfo::is_speculative`).
321 pub is_speculative: bool,
322}
323
324/// Cached `require()` call data.
325#[derive(Debug, Clone, Encode, Decode)]
326pub struct CachedRequireCall {
327 /// The require specifier.
328 pub source: String,
329 /// Byte offset of the span start.
330 pub span_start: u32,
331 /// Byte offset of the span end.
332 pub span_end: u32,
333 /// Names destructured from the require result.
334 pub destructured_names: Vec<String>,
335 /// Local variable name for namespace requires.
336 pub local_name: Option<String>,
337}
338
339/// Cached re-export data.
340#[derive(Debug, Clone, Encode, Decode)]
341pub struct CachedReExport {
342 /// The module being re-exported from.
343 pub source: String,
344 /// Name imported from the source.
345 pub imported_name: String,
346 /// Name exported from this module.
347 pub exported_name: String,
348 /// Whether this is a type-only re-export.
349 pub is_type_only: bool,
350 /// Byte offset of the re-export span start (for line-number reporting).
351 pub span_start: u32,
352 /// Byte offset of the re-export span end.
353 pub span_end: u32,
354}
355
356/// Cached enum or class member data.
357#[derive(Debug, Clone, Encode, Decode)]
358pub struct CachedMember {
359 /// Member name.
360 pub name: String,
361 /// Member kind (enum, method, or property).
362 pub kind: MemberKind,
363 /// Byte offset of the span start.
364 pub span_start: u32,
365 /// Byte offset of the span end.
366 pub span_end: u32,
367 /// Whether this member has decorators.
368 pub has_decorator: bool,
369 /// Full dotted path of each decorator (e.g. `step`, `ns.foo`).
370 /// Empty for undecorated members and decorators with non-identifier
371 /// expressions.
372 pub decorator_names: Vec<String>,
373 /// True when this is a static method that returns a fresh instance of
374 /// the class: body returns `new this()` / `new <SameClassName>()`, or the
375 /// declared return type matches the class name. Treated as a factory.
376 /// See issues #346, #387.
377 pub is_instance_returning_static: bool,
378 /// True when this instance method's call result is an instance of the
379 /// same class (declared return type matches the class name, or body's
380 /// last statement is `return this`). Drives fluent-chain credit. See
381 /// issue #387.
382 pub is_self_returning: bool,
383}
384
385/// Cached dynamic import pattern data (template literals, `import.meta.glob`).
386#[derive(Debug, Clone, Encode, Decode)]
387pub struct CachedDynamicImportPattern {
388 /// Static prefix of the import path.
389 pub prefix: String,
390 /// Static suffix, if any.
391 pub suffix: Option<String>,
392 /// Byte offset of the span start.
393 pub span_start: u32,
394 /// Byte offset of the span end.
395 pub span_end: u32,
396}