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.
10pub(super) const CACHE_VERSION: u32 = 71;
11
12/// Duplication token cache version — bump when duplicate tokenization,
13/// normalization, or the on-disk token cache schema changes.
14pub const DUPES_CACHE_VERSION: u32 = 2;
15
16/// Maximum cache file size to deserialize (256 MB).
17pub(super) const MAX_CACHE_SIZE: usize = 256 * 1024 * 1024;
18
19/// Import kind discriminant for `CachedImport`:
20/// 0 = Named, 1 = Default, 2 = Namespace, 3 = `SideEffect`.
21pub(super) const IMPORT_KIND_NAMED: u8 = 0;
22pub(super) const IMPORT_KIND_DEFAULT: u8 = 1;
23pub(super) const IMPORT_KIND_NAMESPACE: u8 = 2;
24pub(super) const IMPORT_KIND_SIDE_EFFECT: u8 = 3;
25
26/// Cached data for a single module.
27#[derive(Debug, Clone, Encode, Decode)]
28pub struct CachedModule {
29    /// xxh3 hash of the file content.
30    pub content_hash: u64,
31    /// File modification time (seconds since epoch) for fast cache validation.
32    /// When mtime+size match the on-disk file, we skip reading file content entirely.
33    pub mtime_secs: u64,
34    /// File size in bytes for fast cache validation.
35    pub file_size: u64,
36    /// Exported symbols.
37    pub exports: Vec<CachedExport>,
38    /// Import specifiers.
39    pub imports: Vec<CachedImport>,
40    /// Re-export specifiers.
41    pub re_exports: Vec<CachedReExport>,
42    /// Dynamic import specifiers.
43    pub dynamic_imports: Vec<CachedDynamicImport>,
44    /// `require()` specifiers.
45    pub require_calls: Vec<CachedRequireCall>,
46    /// Static member accesses (e.g., `Status.Active`).
47    pub member_accesses: Vec<crate::MemberAccess>,
48    /// Identifiers used as whole objects (Object.values, for..in, spread, etc.).
49    pub whole_object_uses: Vec<String>,
50    /// Dynamic import patterns with partial static resolution.
51    pub dynamic_import_patterns: Vec<CachedDynamicImportPattern>,
52    /// Whether this module uses CJS exports.
53    pub has_cjs_exports: bool,
54    /// Local names of import bindings that are never referenced in this file.
55    pub unused_import_bindings: Vec<String>,
56    /// Local import bindings referenced from type positions.
57    pub type_referenced_import_bindings: Vec<String>,
58    /// Local import bindings referenced from value positions.
59    pub value_referenced_import_bindings: Vec<String>,
60    /// Inline suppression directives.
61    pub suppressions: Vec<CachedSuppression>,
62    /// Pre-computed line-start byte offsets for O(log N) byte-to-line/col conversion.
63    pub line_offsets: Vec<u32>,
64    /// Per-function complexity metrics.
65    pub complexity: Vec<fallow_types::extract::FunctionComplexity>,
66    /// Feature flag use sites.
67    pub flag_uses: Vec<fallow_types::extract::FlagUse>,
68    /// Heritage metadata for exported classes.
69    pub class_heritage: Vec<fallow_types::extract::ClassHeritageInfo>,
70    /// Local type-capable declarations.
71    pub local_type_declarations: Vec<CachedLocalTypeDeclaration>,
72    /// Type references from exported public signatures.
73    pub public_signature_type_references: Vec<CachedPublicSignatureTypeReference>,
74    /// Namespace-import aliases re-exported through an object literal
75    /// (`export const API = { foo }` where `foo` is `import * as foo from './bar'`).
76    pub namespace_object_aliases: Vec<CachedNamespaceObjectAlias>,
77}
78
79/// Cached namespace-object alias.
80#[derive(Debug, Clone, Encode, Decode)]
81pub struct CachedNamespaceObjectAlias {
82    /// Canonical export name on this module.
83    pub via_export_name: String,
84    /// Dotted suffix of the property path relative to the export.
85    pub suffix: String,
86    /// Local name of the namespace import on this module.
87    pub namespace_local: String,
88}
89
90/// Cached local type declaration.
91#[derive(Debug, Clone, Encode, Decode)]
92pub struct CachedLocalTypeDeclaration {
93    /// Local declaration name.
94    pub name: String,
95    /// Byte offset of the declaration span start.
96    pub span_start: u32,
97    /// Byte offset of the declaration span end.
98    pub span_end: u32,
99}
100
101/// Cached public signature type reference.
102#[derive(Debug, Clone, Encode, Decode)]
103pub struct CachedPublicSignatureTypeReference {
104    /// Exported symbol whose signature contains the reference.
105    pub export_name: String,
106    /// Referenced type name.
107    pub type_name: String,
108    /// Byte offset of the reference span start.
109    pub span_start: u32,
110    /// Byte offset of the reference span end.
111    pub span_end: u32,
112}
113
114/// Cached suppression directive.
115#[derive(Debug, Clone, Encode, Decode)]
116pub struct CachedSuppression {
117    /// 1-based line this suppression applies to. 0 = file-wide.
118    pub line: u32,
119    /// 1-based line where the comment itself appears.
120    pub comment_line: u32,
121    /// 0 = suppress all, 1-20 = `IssueKind` discriminant.
122    pub kind: u8,
123}
124
125/// Cached export data for a single export declaration.
126#[derive(Debug, Clone, Encode, Decode)]
127pub struct CachedExport {
128    /// Export name (or "default" for default exports).
129    pub name: String,
130    /// Whether this is a default export.
131    pub is_default: bool,
132    /// Whether this is a type-only export.
133    pub is_type_only: bool,
134    /// Whether this export is registered through a runtime side effect at
135    /// module load time (Lit `@customElement` decorator or
136    /// `customElements.define` call). Persisted so warm-cache runs continue
137    /// to skip unused-export reporting for these classes.
138    pub is_side_effect_used: bool,
139    /// Visibility tag discriminant (0=None, 1=Public, 2=Internal, 3=Beta, 4=Alpha).
140    pub visibility: u8,
141    /// The local binding name, if different.
142    pub local_name: Option<String>,
143    /// Byte offset of the export span start.
144    pub span_start: u32,
145    /// Byte offset of the export span end.
146    pub span_end: u32,
147    /// Members of this export (for enums and classes).
148    pub members: Vec<CachedMember>,
149    /// The local name of the parent class from `extends` clause, if any.
150    pub super_class: Option<String>,
151}
152
153/// Cached import data for a single import declaration.
154#[derive(Debug, Clone, Encode, Decode)]
155pub struct CachedImport {
156    /// The import specifier.
157    pub source: String,
158    /// For Named imports, the imported symbol name. Empty for other kinds.
159    pub imported_name: String,
160    /// The local binding name.
161    pub local_name: String,
162    /// Whether this is a type-only import.
163    pub is_type_only: bool,
164    /// Whether this import originated from an SFC `<style>` block / `<style src>` (CSS context).
165    pub from_style: bool,
166    /// Import kind: 0=Named, 1=Default, 2=Namespace, 3=SideEffect.
167    pub kind: u8,
168    /// Byte offset of the import span start.
169    pub span_start: u32,
170    /// Byte offset of the import span end.
171    pub span_end: u32,
172    /// Byte offset of the source string literal span start.
173    pub source_span_start: u32,
174    /// Byte offset of the source string literal span end.
175    pub source_span_end: u32,
176}
177
178/// Cached dynamic import data.
179#[derive(Debug, Clone, Encode, Decode)]
180pub struct CachedDynamicImport {
181    /// The import specifier.
182    pub source: String,
183    /// Byte offset of the span start.
184    pub span_start: u32,
185    /// Byte offset of the span end.
186    pub span_end: u32,
187    /// Names destructured from the import result.
188    pub destructured_names: Vec<String>,
189    /// Local variable name for namespace imports.
190    pub local_name: Option<String>,
191}
192
193/// Cached `require()` call data.
194#[derive(Debug, Clone, Encode, Decode)]
195pub struct CachedRequireCall {
196    /// The require specifier.
197    pub source: String,
198    /// Byte offset of the span start.
199    pub span_start: u32,
200    /// Byte offset of the span end.
201    pub span_end: u32,
202    /// Names destructured from the require result.
203    pub destructured_names: Vec<String>,
204    /// Local variable name for namespace requires.
205    pub local_name: Option<String>,
206}
207
208/// Cached re-export data.
209#[derive(Debug, Clone, Encode, Decode)]
210pub struct CachedReExport {
211    /// The module being re-exported from.
212    pub source: String,
213    /// Name imported from the source.
214    pub imported_name: String,
215    /// Name exported from this module.
216    pub exported_name: String,
217    /// Whether this is a type-only re-export.
218    pub is_type_only: bool,
219    /// Byte offset of the re-export span start (for line-number reporting).
220    pub span_start: u32,
221    /// Byte offset of the re-export span end.
222    pub span_end: u32,
223}
224
225/// Cached enum or class member data.
226#[derive(Debug, Clone, Encode, Decode)]
227pub struct CachedMember {
228    /// Member name.
229    pub name: String,
230    /// Member kind (enum, method, or property).
231    pub kind: MemberKind,
232    /// Byte offset of the span start.
233    pub span_start: u32,
234    /// Byte offset of the span end.
235    pub span_end: u32,
236    /// Whether this member has decorators.
237    pub has_decorator: bool,
238}
239
240/// Cached dynamic import pattern data (template literals, `import.meta.glob`).
241#[derive(Debug, Clone, Encode, Decode)]
242pub struct CachedDynamicImportPattern {
243    /// Static prefix of the import path.
244    pub prefix: String,
245    /// Static suffix, if any.
246    pub suffix: Option<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}