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