Skip to main content

fallow_extract/cache/
types.rs

1//! Serialization types for the incremental parse cache.
2//!
3//! All types use bincode `Encode`/`Decode` for fast binary serialization.
4
5use bincode::{Decode, Encode};
6
7use crate::MemberKind;
8
9/// Cache version — bump when the cache format changes.
10pub(super) const CACHE_VERSION: u32 = 14;
11
12/// Maximum cache file size to deserialize (256 MB).
13pub(super) const MAX_CACHE_SIZE: usize = 256 * 1024 * 1024;
14
15/// Import kind discriminant for `CachedImport`:
16/// 0 = Named, 1 = Default, 2 = Namespace, 3 = `SideEffect`.
17pub(super) const IMPORT_KIND_NAMED: u8 = 0;
18pub(super) const IMPORT_KIND_DEFAULT: u8 = 1;
19pub(super) const IMPORT_KIND_NAMESPACE: u8 = 2;
20pub(super) const IMPORT_KIND_SIDE_EFFECT: u8 = 3;
21
22/// Cached data for a single module.
23#[derive(Debug, Clone, Encode, Decode)]
24pub struct CachedModule {
25    /// xxh3 hash of the file content.
26    pub content_hash: u64,
27    /// File modification time (seconds since epoch) for fast cache validation.
28    /// When mtime+size match the on-disk file, we skip reading file content entirely.
29    pub mtime_secs: u64,
30    /// File size in bytes for fast cache validation.
31    pub file_size: u64,
32    /// Exported symbols.
33    pub exports: Vec<CachedExport>,
34    /// Import specifiers.
35    pub imports: Vec<CachedImport>,
36    /// Re-export specifiers.
37    pub re_exports: Vec<CachedReExport>,
38    /// Dynamic import specifiers.
39    pub dynamic_imports: Vec<CachedDynamicImport>,
40    /// `require()` specifiers.
41    pub require_calls: Vec<CachedRequireCall>,
42    /// Static member accesses (e.g., `Status.Active`).
43    pub member_accesses: Vec<crate::MemberAccess>,
44    /// Identifiers used as whole objects (Object.values, for..in, spread, etc.).
45    pub whole_object_uses: Vec<String>,
46    /// Dynamic import patterns with partial static resolution.
47    pub dynamic_import_patterns: Vec<CachedDynamicImportPattern>,
48    /// Whether this module uses CJS exports.
49    pub has_cjs_exports: bool,
50    /// Local names of import bindings that are never referenced in this file.
51    pub unused_import_bindings: Vec<String>,
52    /// Inline suppression directives.
53    pub suppressions: Vec<CachedSuppression>,
54    /// Pre-computed line-start byte offsets for O(log N) byte-to-line/col conversion.
55    pub line_offsets: Vec<u32>,
56}
57
58/// Cached suppression directive.
59#[derive(Debug, Clone, Encode, Decode)]
60pub struct CachedSuppression {
61    /// 1-based line this suppression applies to. 0 = file-wide.
62    pub line: u32,
63    /// 0 = suppress all, 1-10 = `IssueKind` discriminant.
64    pub kind: u8,
65}
66
67/// Cached export data for a single export declaration.
68#[derive(Debug, Clone, Encode, Decode)]
69pub struct CachedExport {
70    /// Export name (or "default" for default exports).
71    pub name: String,
72    /// Whether this is a default export.
73    pub is_default: bool,
74    /// Whether this is a type-only export.
75    pub is_type_only: bool,
76    /// Whether this export has a `@public` JSDoc tag.
77    pub is_public: bool,
78    /// The local binding name, if different.
79    pub local_name: Option<String>,
80    /// Byte offset of the export span start.
81    pub span_start: u32,
82    /// Byte offset of the export span end.
83    pub span_end: u32,
84    /// Members of this export (for enums and classes).
85    pub members: Vec<CachedMember>,
86}
87
88/// Cached import data for a single import declaration.
89#[derive(Debug, Clone, Encode, Decode)]
90pub struct CachedImport {
91    /// The import specifier.
92    pub source: String,
93    /// For Named imports, the imported symbol name. Empty for other kinds.
94    pub imported_name: String,
95    /// The local binding name.
96    pub local_name: String,
97    /// Whether this is a type-only import.
98    pub is_type_only: bool,
99    /// Import kind: 0=Named, 1=Default, 2=Namespace, 3=SideEffect.
100    pub kind: u8,
101    /// Byte offset of the import span start.
102    pub span_start: u32,
103    /// Byte offset of the import span end.
104    pub span_end: u32,
105}
106
107/// Cached dynamic import data.
108#[derive(Debug, Clone, Encode, Decode)]
109pub struct CachedDynamicImport {
110    /// The import specifier.
111    pub source: String,
112    /// Byte offset of the span start.
113    pub span_start: u32,
114    /// Byte offset of the span end.
115    pub span_end: u32,
116    /// Names destructured from the import result.
117    pub destructured_names: Vec<String>,
118    /// Local variable name for namespace imports.
119    pub local_name: Option<String>,
120}
121
122/// Cached `require()` call data.
123#[derive(Debug, Clone, Encode, Decode)]
124pub struct CachedRequireCall {
125    /// The require specifier.
126    pub source: String,
127    /// Byte offset of the span start.
128    pub span_start: u32,
129    /// Byte offset of the span end.
130    pub span_end: u32,
131    /// Names destructured from the require result.
132    pub destructured_names: Vec<String>,
133    /// Local variable name for namespace requires.
134    pub local_name: Option<String>,
135}
136
137/// Cached re-export data.
138#[derive(Debug, Clone, Encode, Decode)]
139pub struct CachedReExport {
140    /// The module being re-exported from.
141    pub source: String,
142    /// Name imported from the source.
143    pub imported_name: String,
144    /// Name exported from this module.
145    pub exported_name: String,
146    /// Whether this is a type-only re-export.
147    pub is_type_only: bool,
148}
149
150/// Cached enum or class member data.
151#[derive(Debug, Clone, Encode, Decode)]
152pub struct CachedMember {
153    /// Member name.
154    pub name: String,
155    /// Member kind (enum, method, or property).
156    pub kind: MemberKind,
157    /// Byte offset of the span start.
158    pub span_start: u32,
159    /// Byte offset of the span end.
160    pub span_end: u32,
161    /// Whether this member has decorators.
162    pub has_decorator: bool,
163}
164
165/// Cached dynamic import pattern data (template literals, `import.meta.glob`).
166#[derive(Debug, Clone, Encode, Decode)]
167pub struct CachedDynamicImportPattern {
168    /// Static prefix of the import path.
169    pub prefix: String,
170    /// Static suffix, if any.
171    pub suffix: Option<String>,
172    /// Byte offset of the span start.
173    pub span_start: u32,
174    /// Byte offset of the span end.
175    pub span_end: u32,
176}