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