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 = 62;
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}
75
76/// Cached local type declaration.
77#[derive(Debug, Clone, Encode, Decode)]
78pub struct CachedLocalTypeDeclaration {
79    /// Local declaration name.
80    pub name: String,
81    /// Byte offset of the declaration span start.
82    pub span_start: u32,
83    /// Byte offset of the declaration span end.
84    pub span_end: u32,
85}
86
87/// Cached public signature type reference.
88#[derive(Debug, Clone, Encode, Decode)]
89pub struct CachedPublicSignatureTypeReference {
90    /// Exported symbol whose signature contains the reference.
91    pub export_name: String,
92    /// Referenced type name.
93    pub type_name: String,
94    /// Byte offset of the reference span start.
95    pub span_start: u32,
96    /// Byte offset of the reference span end.
97    pub span_end: u32,
98}
99
100/// Cached suppression directive.
101#[derive(Debug, Clone, Encode, Decode)]
102pub struct CachedSuppression {
103    /// 1-based line this suppression applies to. 0 = file-wide.
104    pub line: u32,
105    /// 1-based line where the comment itself appears.
106    pub comment_line: u32,
107    /// 0 = suppress all, 1-20 = `IssueKind` discriminant.
108    pub kind: u8,
109}
110
111/// Cached export data for a single export declaration.
112#[derive(Debug, Clone, Encode, Decode)]
113pub struct CachedExport {
114    /// Export name (or "default" for default exports).
115    pub name: String,
116    /// Whether this is a default export.
117    pub is_default: bool,
118    /// Whether this is a type-only export.
119    pub is_type_only: bool,
120    /// Visibility tag discriminant (0=None, 1=Public, 2=Internal, 3=Beta, 4=Alpha).
121    pub visibility: u8,
122    /// The local binding name, if different.
123    pub local_name: Option<String>,
124    /// Byte offset of the export span start.
125    pub span_start: u32,
126    /// Byte offset of the export span end.
127    pub span_end: u32,
128    /// Members of this export (for enums and classes).
129    pub members: Vec<CachedMember>,
130    /// The local name of the parent class from `extends` clause, if any.
131    pub super_class: Option<String>,
132}
133
134/// Cached import data for a single import declaration.
135#[derive(Debug, Clone, Encode, Decode)]
136pub struct CachedImport {
137    /// The import specifier.
138    pub source: String,
139    /// For Named imports, the imported symbol name. Empty for other kinds.
140    pub imported_name: String,
141    /// The local binding name.
142    pub local_name: String,
143    /// Whether this is a type-only import.
144    pub is_type_only: bool,
145    /// Whether this import originated from an SFC `<style>` block / `<style src>` (CSS context).
146    pub from_style: bool,
147    /// Import kind: 0=Named, 1=Default, 2=Namespace, 3=SideEffect.
148    pub kind: u8,
149    /// Byte offset of the import span start.
150    pub span_start: u32,
151    /// Byte offset of the import span end.
152    pub span_end: u32,
153    /// Byte offset of the source string literal span start.
154    pub source_span_start: u32,
155    /// Byte offset of the source string literal span end.
156    pub source_span_end: u32,
157}
158
159/// Cached dynamic import data.
160#[derive(Debug, Clone, Encode, Decode)]
161pub struct CachedDynamicImport {
162    /// The import specifier.
163    pub source: String,
164    /// Byte offset of the span start.
165    pub span_start: u32,
166    /// Byte offset of the span end.
167    pub span_end: u32,
168    /// Names destructured from the import result.
169    pub destructured_names: Vec<String>,
170    /// Local variable name for namespace imports.
171    pub local_name: Option<String>,
172}
173
174/// Cached `require()` call data.
175#[derive(Debug, Clone, Encode, Decode)]
176pub struct CachedRequireCall {
177    /// The require specifier.
178    pub source: String,
179    /// Byte offset of the span start.
180    pub span_start: u32,
181    /// Byte offset of the span end.
182    pub span_end: u32,
183    /// Names destructured from the require result.
184    pub destructured_names: Vec<String>,
185    /// Local variable name for namespace requires.
186    pub local_name: Option<String>,
187}
188
189/// Cached re-export data.
190#[derive(Debug, Clone, Encode, Decode)]
191pub struct CachedReExport {
192    /// The module being re-exported from.
193    pub source: String,
194    /// Name imported from the source.
195    pub imported_name: String,
196    /// Name exported from this module.
197    pub exported_name: String,
198    /// Whether this is a type-only re-export.
199    pub is_type_only: bool,
200    /// Byte offset of the re-export span start (for line-number reporting).
201    pub span_start: u32,
202    /// Byte offset of the re-export span end.
203    pub span_end: u32,
204}
205
206/// Cached enum or class member data.
207#[derive(Debug, Clone, Encode, Decode)]
208pub struct CachedMember {
209    /// Member name.
210    pub name: String,
211    /// Member kind (enum, method, or property).
212    pub kind: MemberKind,
213    /// Byte offset of the span start.
214    pub span_start: u32,
215    /// Byte offset of the span end.
216    pub span_end: u32,
217    /// Whether this member has decorators.
218    pub has_decorator: bool,
219}
220
221/// Cached dynamic import pattern data (template literals, `import.meta.glob`).
222#[derive(Debug, Clone, Encode, Decode)]
223pub struct CachedDynamicImportPattern {
224    /// Static prefix of the import path.
225    pub prefix: String,
226    /// Static suffix, if any.
227    pub suffix: Option<String>,
228    /// Byte offset of the span start.
229    pub span_start: u32,
230    /// Byte offset of the span end.
231    pub span_end: u32,
232}