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