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