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`.
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.
68///
69/// Bumped to 99 for issue #605: methods reached via `new Class(...).method()`
70/// receivers (direct and fluent-chain) now emit member accesses crediting the
71/// constructed class. Pre-fix entries lack those accesses, so such methods can
72/// be reported as unused class members until the file is re-extracted.
73///
74/// Bumped to 100 for issue #608: static Iconify icon strings (`icon="jam:github"`,
75/// `name="ic:round-home"`) in markup now populate `iconify_prefixes` so the
76/// `@iconify-json/<prefix>` package is credited. Pre-fix entries omit the field,
77/// so icon-set packages can be reported as unused until the file is re-extracted.
78///
79/// Bumped to 101 for issue #704: SFC template tags that match no import now
80/// populate `auto_import_candidates` for convention auto-import resolution.
81/// Pre-fix entries omit the field, so Nuxt components consumed only via template
82/// tags are not edge-credited until the file is re-extracted.
83///
84/// Bumped to 102 for issue #742: `FunctionComplexity` now carries an
85/// `Option<String> source_hash` (content digest of the function's full-span
86/// source slice) so runtime-coverage baselines survive line moves. Pre-fix
87/// cache entries lack the field, so the hash is absent until re-extraction.
88///
89/// Bumped to 103 for issue #752: typed destructure bindings
90/// (`let { resultState }: Props = $props()`, `function f({ x }: Props)`) now
91/// populate `binding_target_names`, which changes the `member_accesses` emitted
92/// for those files. Pre-fix cache entries lack the additional member accesses.
93///
94/// Bumped to 104 for issue #445: MDX, Astro, Vue/Svelte SFC, and CSS/SCSS
95/// container extraction now remaps source-authored spans back to the original
96/// file byte offsets. Pre-fix entries can carry synthetic extracted-buffer
97/// positions, so diagnostics can point at line 1 or compacted MDX lines until
98/// the file is re-extracted.
99///
100/// Bumped to 105 for issue #739: JS/TS and Vue/Svelte SFC script extraction
101/// now populates `auto_import_candidates` from unresolved value references.
102/// Pre-fix entries omit these candidates, so convention script auto-imports
103/// are not edge-credited until the file is re-extracted.
104///
105/// Bumped to 106 for `fallow security`: JS/TS extraction now stores file-level
106/// directives (`"use client"`, `"use server"`) in the parse cache so client
107/// boundary detection does not depend on stale cached module info.
108///
109/// Bumped to 107 for issue #835: Svelte `<script src>` references no longer
110/// emit synthetic imports because they are runtime markup, not bundled SFC
111/// script modules. Pre-fix entries can carry stale root-relative imports that
112/// surface as false `unresolved-imports`.
113///
114/// Bumped to 108 for three extraction-semantics changes shipping together:
115/// - issue #839: `declare` ambient class properties are no longer extracted as
116///   class members (they emit no JS and cannot be value-referenced), so pre-fix
117///   entries carry phantom members that surface as false `unused-class-member`.
118/// - issue #840: extensionless `new URL(specifier, import.meta.url)` dynamic
119///   imports now persist `is_speculative = true` so a directory target
120///   (`new URL('./services', import.meta.url)`) is silently dropped when the
121///   resolver finds no module; pre-fix entries carry `is_speculative = false`
122///   and surface as false `unresolved-imports`.
123/// - issue #845: a method call on an `instanceof`-narrowed value now emits a
124///   member access against the narrowed class, changing the persisted
125///   `member_accesses`; pre-fix entries miss the credit and surface as false
126///   `unused-class-member`.
127///
128/// Bumped to 109 for the data-driven security matcher catalogue: JS/TS
129/// extraction now captures non-literal sink sites into `security_sinks`, each
130/// carrying an `arg_kind` discriminator (template-with-substitution, concat,
131/// object, call, other) so the catalogue can require unsafe SQL shapes and
132/// exclude safely-parameterized `` sql`${x}` `` templates and object-form
133/// `.execute({ sql, args })` arguments. Pre-109 entries lack the field, so their
134/// sink sites do not feed the catalogue until the file is re-extracted.
135pub(super) const CACHE_VERSION: u32 = 109;
136
137/// Duplication token cache version. Bump when duplicate tokenization,
138/// normalization, or the on-disk token cache schema changes.
139pub const DUPES_CACHE_VERSION: u32 = 4;
140
141/// Default maximum cache size (256 MB). Overridable per-project via
142/// `cache.maxSizeMb` in the config file or `FALLOW_CACHE_MAX_SIZE` env var.
143/// Also used as the hard ceiling on load-time deserialization as a defence
144/// against pathological on-disk files.
145pub const DEFAULT_CACHE_MAX_SIZE: usize = 256 * 1024 * 1024;
146
147/// Trigger LRU eviction when the serialized cache exceeds 80% of the cap.
148/// Basis points (1/100 of a percent) for integer arithmetic without floats.
149pub(super) const EVICTION_TRIGGER_BPS: usize = 8000;
150
151/// Evict down to 60% of the cap so subsequent saves leave headroom.
152pub(super) const EVICTION_TARGET_BPS: usize = 6000;
153
154/// Promote the eviction log from `debug!` to `info!` when at least 25% of
155/// entries are removed in a single save. Default-noise concerns mean
156/// small-turnover saves should not be visible without `RUST_LOG=debug`.
157pub(super) const EVICTION_SIGNIFICANT_BPS: usize = 2500;
158
159/// Import kind discriminant for `CachedImport`:
160/// 0 = Named, 1 = Default, 2 = Namespace, 3 = `SideEffect`.
161pub(super) const IMPORT_KIND_NAMED: u8 = 0;
162pub(super) const IMPORT_KIND_DEFAULT: u8 = 1;
163pub(super) const IMPORT_KIND_NAMESPACE: u8 = 2;
164pub(super) const IMPORT_KIND_SIDE_EFFECT: u8 = 3;
165
166macro_rules! assert_cached_type_size {
167    ($ty:ty, $size:expr) => {
168        const _: () = assert!(
169            std::mem::size_of::<$ty>() == $size,
170            concat!(
171                stringify!($ty),
172                " size changed; bump CACHE_VERSION if the cached wire shape or extraction semantics changed, then update this assertion"
173            )
174        );
175    };
176}
177
178assert_cached_type_size!(CachedModule, 616);
179assert_cached_type_size!(CachedNamespaceObjectAlias, 72);
180assert_cached_type_size!(CachedLocalTypeDeclaration, 32);
181assert_cached_type_size!(CachedPublicSignatureTypeReference, 56);
182assert_cached_type_size!(CachedSuppression, 12);
183assert_cached_type_size!(CachedUnknownSuppressionKind, 32);
184assert_cached_type_size!(CachedExport, 112);
185assert_cached_type_size!(CachedImport, 96);
186assert_cached_type_size!(CachedDynamicImport, 88);
187assert_cached_type_size!(CachedRequireCall, 80);
188assert_cached_type_size!(CachedReExport, 88);
189assert_cached_type_size!(CachedMember, 64);
190assert_cached_type_size!(CachedDynamicImportPattern, 56);
191assert_cached_type_size!(crate::MemberAccess, 48);
192assert_cached_type_size!(fallow_types::extract::SinkSite, 40);
193assert_cached_type_size!(fallow_types::extract::FunctionComplexity, 72);
194assert_cached_type_size!(fallow_types::extract::FlagUse, 80);
195assert_cached_type_size!(fallow_types::extract::ClassHeritageInfo, 96);
196
197/// Cached data for a single module.
198#[derive(Debug, Clone, Encode, Decode)]
199pub struct CachedModule {
200    /// xxh3 hash of the file content.
201    pub content_hash: u64,
202    /// File modification time (seconds since epoch) for fast cache validation.
203    /// When mtime+size match the on-disk file, we skip reading file content entirely.
204    pub mtime_secs: u64,
205    /// File size in bytes for fast cache validation.
206    pub file_size: u64,
207    /// Seconds-since-epoch at the time this entry was last WRITTEN
208    /// (first parse or content-change refresh). NOT updated on cache-hit
209    /// reads: `update_cache` already iterates every in-scope file every run,
210    /// so refreshing on read would collapse the LRU to "last run this file
211    /// was discovered" for every retained entry. With write-only refresh,
212    /// the LRU genuinely targets stale (in-scope-but-unchanged-for-many-runs)
213    /// entries. Used by `CacheStore::save` for write-time eviction ordering.
214    pub last_access_secs: u64,
215    /// Exported symbols.
216    pub exports: Vec<CachedExport>,
217    /// Import specifiers.
218    pub imports: Vec<CachedImport>,
219    /// Re-export specifiers.
220    pub re_exports: Vec<CachedReExport>,
221    /// Dynamic import specifiers.
222    pub dynamic_imports: Vec<CachedDynamicImport>,
223    /// `require()` specifiers.
224    pub require_calls: Vec<CachedRequireCall>,
225    /// Static member accesses (e.g., `Status.Active`).
226    pub member_accesses: Vec<crate::MemberAccess>,
227    /// Identifiers used as whole objects (Object.values, for..in, spread, etc.).
228    pub whole_object_uses: Vec<String>,
229    /// Dynamic import patterns with partial static resolution.
230    pub dynamic_import_patterns: Vec<CachedDynamicImportPattern>,
231    /// Whether this module uses CJS exports.
232    pub has_cjs_exports: bool,
233    /// Whether this module declares at least one Angular `@Component({
234    /// templateUrl: ... })` decorator. Mirrors `ModuleInfo.has_angular_component_template_url`
235    /// so the CRAP-inherit walker's gate survives a warm-cache load.
236    pub has_angular_component_template_url: bool,
237    /// Local names of import bindings that are never referenced in this file.
238    pub unused_import_bindings: Vec<String>,
239    /// Local import bindings referenced from type positions.
240    pub type_referenced_import_bindings: Vec<String>,
241    /// Local import bindings referenced from value positions.
242    pub value_referenced_import_bindings: Vec<String>,
243    /// Inline suppression directives.
244    pub suppressions: Vec<CachedSuppression>,
245    /// Suppression tokens that did not parse to any known `IssueKind`. See #449.
246    pub unknown_suppression_kinds: Vec<CachedUnknownSuppressionKind>,
247    /// Pre-computed line-start byte offsets for O(log N) byte-to-line/col conversion.
248    pub line_offsets: Vec<u32>,
249    /// Per-function complexity metrics.
250    pub complexity: Vec<fallow_types::extract::FunctionComplexity>,
251    /// Feature flag use sites.
252    pub flag_uses: Vec<fallow_types::extract::FlagUse>,
253    /// Heritage metadata for exported classes.
254    pub class_heritage: Vec<fallow_types::extract::ClassHeritageInfo>,
255    /// Local type-capable declarations.
256    pub local_type_declarations: Vec<CachedLocalTypeDeclaration>,
257    /// Type references from exported public signatures.
258    pub public_signature_type_references: Vec<CachedPublicSignatureTypeReference>,
259    /// Namespace-import aliases re-exported through an object literal
260    /// (`export const API = { foo }` where `foo` is `import * as foo from './bar'`).
261    pub namespace_object_aliases: Vec<CachedNamespaceObjectAlias>,
262    /// Iconify collection prefixes found in static icon props (issue #608).
263    pub iconify_prefixes: Vec<String>,
264    /// Bare identifier names that are candidates for convention auto-import
265    /// resolution (issue #704). Content-local, so they round-trip through the
266    /// cache; resolution against the plugin table happens at graph-build time.
267    pub auto_import_candidates: Vec<String>,
268    /// File-level string directives (`"use client"`, `"use server"`). Content-local,
269    /// round-trips through the cache so the security `client-server-leak` detector
270    /// sees directives on warm-cache loads.
271    pub directives: Vec<String>,
272    /// Captured non-literal security sink sites (category-blind). Round-trips
273    /// through the cache so the catalogue-driven `tainted_sink` detector sees
274    /// sinks on warm-cache loads.
275    pub security_sinks: Vec<fallow_types::extract::SinkSite>,
276    /// Count of sink-shaped nodes whose callee could not be flattened to a
277    /// static path. Round-trips so the in-band blind-spot count is stable.
278    pub security_sinks_skipped: u32,
279}
280
281/// Cached namespace-object alias.
282#[derive(Debug, Clone, Encode, Decode)]
283pub struct CachedNamespaceObjectAlias {
284    /// Canonical export name on this module.
285    pub via_export_name: String,
286    /// Dotted suffix of the property path relative to the export.
287    pub suffix: String,
288    /// Local name of the namespace import on this module.
289    pub namespace_local: String,
290}
291
292/// Cached local type declaration.
293#[derive(Debug, Clone, Encode, Decode)]
294pub struct CachedLocalTypeDeclaration {
295    /// Local declaration name.
296    pub name: String,
297    /// Byte offset of the declaration span start.
298    pub span_start: u32,
299    /// Byte offset of the declaration span end.
300    pub span_end: u32,
301}
302
303/// Cached public signature type reference.
304#[derive(Debug, Clone, Encode, Decode)]
305pub struct CachedPublicSignatureTypeReference {
306    /// Exported symbol whose signature contains the reference.
307    pub export_name: String,
308    /// Referenced type name.
309    pub type_name: String,
310    /// Byte offset of the reference span start.
311    pub span_start: u32,
312    /// Byte offset of the reference span end.
313    pub span_end: u32,
314}
315
316/// Cached suppression directive.
317#[derive(Debug, Clone, Encode, Decode)]
318pub struct CachedSuppression {
319    /// 1-based line this suppression applies to. 0 = file-wide.
320    pub line: u32,
321    /// 1-based line where the comment itself appears.
322    pub comment_line: u32,
323    /// 0 = suppress all, 1-20 = `IssueKind` discriminant.
324    pub kind: u8,
325}
326
327/// Cached unknown suppression kind token (see #449).
328#[derive(Debug, Clone, Encode, Decode)]
329pub struct CachedUnknownSuppressionKind {
330    /// 1-based line where the comment itself appears.
331    pub comment_line: u32,
332    /// True when the marker was `fallow-ignore-file`.
333    pub is_file_level: bool,
334    /// The verbatim token that did not parse.
335    pub token: String,
336}
337
338/// Cached export data for a single export declaration.
339#[derive(Debug, Clone, Encode, Decode)]
340pub struct CachedExport {
341    /// Export name (or "default" for default exports).
342    pub name: String,
343    /// Whether this is a default export.
344    pub is_default: bool,
345    /// Whether this is a type-only export.
346    pub is_type_only: bool,
347    /// Whether this export is registered through a runtime side effect at
348    /// module load time (Lit `@customElement` decorator or
349    /// `customElements.define` call). Persisted so warm-cache runs continue
350    /// to skip unused-export reporting for these classes.
351    pub is_side_effect_used: bool,
352    /// Visibility tag discriminant (0=None, 1=Public, 2=Internal, 3=Beta, 4=Alpha).
353    pub visibility: u8,
354    /// The local binding name, if different.
355    pub local_name: Option<String>,
356    /// Byte offset of the export span start.
357    pub span_start: u32,
358    /// Byte offset of the export span end.
359    pub span_end: u32,
360    /// Members of this export (for enums and classes).
361    pub members: Vec<CachedMember>,
362    /// The local name of the parent class from `extends` clause, if any.
363    pub super_class: Option<String>,
364}
365
366/// Cached import data for a single import declaration.
367#[derive(Debug, Clone, Encode, Decode)]
368pub struct CachedImport {
369    /// The import specifier.
370    pub source: String,
371    /// For Named imports, the imported symbol name. Empty for other kinds.
372    pub imported_name: String,
373    /// The local binding name.
374    pub local_name: String,
375    /// Whether this is a type-only import.
376    pub is_type_only: bool,
377    /// Whether this import originated from an SFC `<style>` block / `<style src>` (CSS context).
378    pub from_style: bool,
379    /// Import kind: 0=Named, 1=Default, 2=Namespace, 3=SideEffect.
380    pub kind: u8,
381    /// Byte offset of the import span start.
382    pub span_start: u32,
383    /// Byte offset of the import span end.
384    pub span_end: u32,
385    /// Byte offset of the source string literal span start.
386    pub source_span_start: u32,
387    /// Byte offset of the source string literal span end.
388    pub source_span_end: u32,
389}
390
391/// Cached dynamic import data.
392#[derive(Debug, Clone, Encode, Decode)]
393pub struct CachedDynamicImport {
394    /// The import specifier.
395    pub source: String,
396    /// Byte offset of the span start.
397    pub span_start: u32,
398    /// Byte offset of the span end.
399    pub span_end: u32,
400    /// Names destructured from the import result.
401    pub destructured_names: Vec<String>,
402    /// Local variable name for namespace imports.
403    pub local_name: Option<String>,
404    /// True when this dynamic import was synthesised by fallow (see
405    /// `DynamicImportInfo::is_speculative`).
406    pub is_speculative: bool,
407}
408
409/// Cached `require()` call data.
410#[derive(Debug, Clone, Encode, Decode)]
411pub struct CachedRequireCall {
412    /// The require specifier.
413    pub source: String,
414    /// Byte offset of the span start.
415    pub span_start: u32,
416    /// Byte offset of the span end.
417    pub span_end: u32,
418    /// Names destructured from the require result.
419    pub destructured_names: Vec<String>,
420    /// Local variable name for namespace requires.
421    pub local_name: Option<String>,
422}
423
424/// Cached re-export data.
425#[derive(Debug, Clone, Encode, Decode)]
426pub struct CachedReExport {
427    /// The module being re-exported from.
428    pub source: String,
429    /// Name imported from the source.
430    pub imported_name: String,
431    /// Name exported from this module.
432    pub exported_name: String,
433    /// Whether this is a type-only re-export.
434    pub is_type_only: bool,
435    /// Byte offset of the re-export span start (for line-number reporting).
436    pub span_start: u32,
437    /// Byte offset of the re-export span end.
438    pub span_end: u32,
439}
440
441/// Cached enum or class member data.
442#[derive(Debug, Clone, Encode, Decode)]
443pub struct CachedMember {
444    /// Member name.
445    pub name: String,
446    /// Member kind (enum, method, or property).
447    pub kind: MemberKind,
448    /// Byte offset of the span start.
449    pub span_start: u32,
450    /// Byte offset of the span end.
451    pub span_end: u32,
452    /// Whether this member has decorators.
453    pub has_decorator: bool,
454    /// Full dotted path of each decorator (e.g. `step`, `ns.foo`).
455    /// Empty for undecorated members and decorators with non-identifier
456    /// expressions.
457    pub decorator_names: Vec<String>,
458    /// True when this is a static method that returns a fresh instance of
459    /// the class: body returns `new this()` / `new <SameClassName>()`, or the
460    /// declared return type matches the class name. Treated as a factory.
461    /// See issues #346, #387.
462    pub is_instance_returning_static: bool,
463    /// True when this instance method's call result is an instance of the
464    /// same class (declared return type matches the class name, or body's
465    /// last statement is `return this`). Drives fluent-chain credit. See
466    /// issue #387.
467    pub is_self_returning: bool,
468}
469
470/// Cached dynamic import pattern data (template literals, `import.meta.glob`).
471#[derive(Debug, Clone, Encode, Decode)]
472pub struct CachedDynamicImportPattern {
473    /// Static prefix of the import path.
474    pub prefix: String,
475    /// Static suffix, if any.
476    pub suffix: Option<String>,
477    /// Byte offset of the span start.
478    pub span_start: u32,
479    /// Byte offset of the span end.
480    pub span_end: u32,
481}