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