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 = 41;
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 /// Per-function complexity metrics.
57 pub complexity: Vec<fallow_types::extract::FunctionComplexity>,
58 /// Feature flag use sites.
59 pub flag_uses: Vec<fallow_types::extract::FlagUse>,
60}
61
62/// Cached suppression directive.
63#[derive(Debug, Clone, Encode, Decode)]
64pub struct CachedSuppression {
65 /// 1-based line this suppression applies to. 0 = file-wide.
66 pub line: u32,
67 /// 1-based line where the comment itself appears.
68 pub comment_line: u32,
69 /// 0 = suppress all, 1-19 = `IssueKind` discriminant.
70 pub kind: u8,
71}
72
73/// Cached export data for a single export declaration.
74#[derive(Debug, Clone, Encode, Decode)]
75pub struct CachedExport {
76 /// Export name (or "default" for default exports).
77 pub name: String,
78 /// Whether this is a default export.
79 pub is_default: bool,
80 /// Whether this is a type-only export.
81 pub is_type_only: bool,
82 /// Visibility tag discriminant (0=None, 1=Public, 2=Internal, 3=Beta, 4=Alpha).
83 pub visibility: u8,
84 /// The local binding name, if different.
85 pub local_name: Option<String>,
86 /// Byte offset of the export span start.
87 pub span_start: u32,
88 /// Byte offset of the export span end.
89 pub span_end: u32,
90 /// Members of this export (for enums and classes).
91 pub members: Vec<CachedMember>,
92 /// The local name of the parent class from `extends` clause, if any.
93 pub super_class: Option<String>,
94}
95
96/// Cached import data for a single import declaration.
97#[derive(Debug, Clone, Encode, Decode)]
98pub struct CachedImport {
99 /// The import specifier.
100 pub source: String,
101 /// For Named imports, the imported symbol name. Empty for other kinds.
102 pub imported_name: String,
103 /// The local binding name.
104 pub local_name: String,
105 /// Whether this is a type-only import.
106 pub is_type_only: bool,
107 /// Import kind: 0=Named, 1=Default, 2=Namespace, 3=SideEffect.
108 pub kind: u8,
109 /// Byte offset of the import span start.
110 pub span_start: u32,
111 /// Byte offset of the import span end.
112 pub span_end: u32,
113 /// Byte offset of the source string literal span start.
114 pub source_span_start: u32,
115 /// Byte offset of the source string literal span end.
116 pub source_span_end: u32,
117}
118
119/// Cached dynamic import data.
120#[derive(Debug, Clone, Encode, Decode)]
121pub struct CachedDynamicImport {
122 /// The import specifier.
123 pub source: String,
124 /// Byte offset of the span start.
125 pub span_start: u32,
126 /// Byte offset of the span end.
127 pub span_end: u32,
128 /// Names destructured from the import result.
129 pub destructured_names: Vec<String>,
130 /// Local variable name for namespace imports.
131 pub local_name: Option<String>,
132}
133
134/// Cached `require()` call data.
135#[derive(Debug, Clone, Encode, Decode)]
136pub struct CachedRequireCall {
137 /// The require specifier.
138 pub source: String,
139 /// Byte offset of the span start.
140 pub span_start: u32,
141 /// Byte offset of the span end.
142 pub span_end: u32,
143 /// Names destructured from the require result.
144 pub destructured_names: Vec<String>,
145 /// Local variable name for namespace requires.
146 pub local_name: Option<String>,
147}
148
149/// Cached re-export data.
150#[derive(Debug, Clone, Encode, Decode)]
151pub struct CachedReExport {
152 /// The module being re-exported from.
153 pub source: String,
154 /// Name imported from the source.
155 pub imported_name: String,
156 /// Name exported from this module.
157 pub exported_name: String,
158 /// Whether this is a type-only re-export.
159 pub is_type_only: bool,
160 /// Byte offset of the re-export span start (for line-number reporting).
161 pub span_start: u32,
162 /// Byte offset of the re-export span end.
163 pub span_end: u32,
164}
165
166/// Cached enum or class member data.
167#[derive(Debug, Clone, Encode, Decode)]
168pub struct CachedMember {
169 /// Member name.
170 pub name: String,
171 /// Member kind (enum, method, or property).
172 pub kind: MemberKind,
173 /// Byte offset of the span start.
174 pub span_start: u32,
175 /// Byte offset of the span end.
176 pub span_end: u32,
177 /// Whether this member has decorators.
178 pub has_decorator: bool,
179}
180
181/// Cached dynamic import pattern data (template literals, `import.meta.glob`).
182#[derive(Debug, Clone, Encode, Decode)]
183pub struct CachedDynamicImportPattern {
184 /// Static prefix of the import path.
185 pub prefix: String,
186 /// Static suffix, if any.
187 pub suffix: Option<String>,
188 /// Byte offset of the span start.
189 pub span_start: u32,
190 /// Byte offset of the span end.
191 pub span_end: u32,
192}