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