Skip to main content

fallow_extract/cache/
conversion.rs

1//! Conversion between [`ModuleInfo`](crate::ModuleInfo) and [`CachedModule`].
2//!
3//! Both functions convert between borrowed source structs and owned target structs
4//! (`&CachedModule -> ModuleInfo`, `&ModuleInfo -> CachedModule`). All `String` clones
5//! are structurally necessary: the cache store retains ownership of `CachedModule`
6//! entries (for persistence), and `ModuleInfo` must outlive the cache for the
7//! analysis pipeline. Eliminating these clones would require shared ownership
8//! (`Arc<str>`) across the entire extraction + analysis pipeline.
9
10use std::time::{SystemTime, UNIX_EPOCH};
11
12use oxc_span::Span;
13
14use crate::ExportName;
15use fallow_types::extract::{NamespaceObjectAlias, VisibilityTag};
16
17/// Seconds-since-Unix-epoch from the wall clock, saturating to 0 if the
18/// system clock is set before the epoch. Used as the LRU bookkeeping
19/// timestamp on `CachedModule.last_access_secs`. Wall-clock (not monotonic)
20/// is the right source here because the value persists across process
21/// invocations.
22#[must_use]
23pub fn current_unix_seconds() -> u64 {
24    SystemTime::now()
25        .duration_since(UNIX_EPOCH)
26        .map_or(0, |d| d.as_secs())
27}
28
29use super::types::{
30    CachedDynamicImport, CachedDynamicImportPattern, CachedExport, CachedImport,
31    CachedLocalTypeDeclaration, CachedMember, CachedModule, CachedNamespaceObjectAlias,
32    CachedPublicSignatureTypeReference, CachedReExport, CachedRequireCall, CachedSuppression,
33    CachedUnknownSuppressionKind, IMPORT_KIND_DEFAULT, IMPORT_KIND_NAMED, IMPORT_KIND_NAMESPACE,
34    IMPORT_KIND_SIDE_EFFECT,
35};
36
37/// Reconstruct a [`ModuleInfo`](crate::ModuleInfo) from a [`CachedModule`].
38#[must_use]
39pub fn cached_to_module(
40    cached: &CachedModule,
41    file_id: fallow_types::discover::FileId,
42) -> crate::ModuleInfo {
43    cached_to_module_opts(cached, file_id, true)
44}
45
46/// Reconstruct a [`ModuleInfo`](crate::ModuleInfo) from a [`CachedModule`], skipping
47/// the per-function complexity vec when `need_complexity` is `false`. Avoids the
48/// `Vec<FunctionComplexity>` clone on warm runs of commands (e.g. `fallow dead-code`)
49/// that don't consume complexity, which adds up across tens of thousands of files.
50#[must_use]
51#[expect(
52    clippy::too_many_lines,
53    reason = "single flat field-by-field deserialization; splitting it harms readability"
54)]
55pub fn cached_to_module_opts(
56    cached: &CachedModule,
57    file_id: fallow_types::discover::FileId,
58    need_complexity: bool,
59) -> crate::ModuleInfo {
60    use crate::{
61        DynamicImportInfo, ExportInfo, ImportInfo, ImportedName, LocalTypeDeclaration, MemberInfo,
62        ModuleInfo, PublicSignatureTypeReference, ReExportInfo, RequireCallInfo,
63    };
64
65    let exports = cached
66        .exports
67        .iter()
68        .map(|e| ExportInfo {
69            name: if e.is_default {
70                ExportName::Default
71            } else {
72                ExportName::Named(e.name.clone())
73            },
74            local_name: e.local_name.clone(),
75            is_type_only: e.is_type_only,
76            is_side_effect_used: e.is_side_effect_used,
77            visibility: match e.visibility {
78                1 => VisibilityTag::Public,
79                2 => VisibilityTag::Internal,
80                3 => VisibilityTag::Beta,
81                4 => VisibilityTag::Alpha,
82                5 => VisibilityTag::ExpectedUnused,
83                _ => VisibilityTag::None,
84            },
85            span: Span::new(e.span_start, e.span_end),
86            members: e
87                .members
88                .iter()
89                .map(|m| MemberInfo {
90                    name: m.name.clone(),
91                    kind: m.kind,
92                    span: Span::new(m.span_start, m.span_end),
93                    has_decorator: m.has_decorator,
94                    decorator_names: m.decorator_names.clone(),
95                    is_instance_returning_static: m.is_instance_returning_static,
96                    is_self_returning: m.is_self_returning,
97                })
98                .collect(),
99            super_class: e.super_class.clone(),
100        })
101        .collect();
102
103    let imports = cached
104        .imports
105        .iter()
106        .map(|i| ImportInfo {
107            source: i.source.clone(),
108            imported_name: match i.kind {
109                IMPORT_KIND_DEFAULT => ImportedName::Default,
110                IMPORT_KIND_NAMESPACE => ImportedName::Namespace,
111                IMPORT_KIND_SIDE_EFFECT => ImportedName::SideEffect,
112                // IMPORT_KIND_NAMED (0) and any unknown value default to Named
113                _ => ImportedName::Named(i.imported_name.clone()),
114            },
115            local_name: i.local_name.clone(),
116            is_type_only: i.is_type_only,
117            from_style: i.from_style,
118            span: Span::new(i.span_start, i.span_end),
119            source_span: Span::new(i.source_span_start, i.source_span_end),
120        })
121        .collect();
122
123    let re_exports = cached
124        .re_exports
125        .iter()
126        .map(|r| ReExportInfo {
127            source: r.source.clone(),
128            imported_name: r.imported_name.clone(),
129            exported_name: r.exported_name.clone(),
130            is_type_only: r.is_type_only,
131            span: Span::new(r.span_start, r.span_end),
132        })
133        .collect();
134
135    let dynamic_imports = cached
136        .dynamic_imports
137        .iter()
138        .map(|d| DynamicImportInfo {
139            source: d.source.clone(),
140            span: Span::new(d.span_start, d.span_end),
141            destructured_names: d.destructured_names.clone(),
142            local_name: d.local_name.clone(),
143            is_speculative: d.is_speculative,
144        })
145        .collect();
146
147    let require_calls = cached
148        .require_calls
149        .iter()
150        .map(|r| RequireCallInfo {
151            source: r.source.clone(),
152            span: Span::new(r.span_start, r.span_end),
153            source_span: Span::new(r.source_span_start, r.source_span_end),
154            destructured_names: r.destructured_names.clone(),
155            local_name: r.local_name.clone(),
156        })
157        .collect();
158
159    let dynamic_import_patterns = cached
160        .dynamic_import_patterns
161        .iter()
162        .map(|p| crate::DynamicImportPattern {
163            prefix: p.prefix.clone(),
164            suffix: p.suffix.clone(),
165            span: Span::new(p.span_start, p.span_end),
166        })
167        .collect();
168
169    let suppressions = cached
170        .suppressions
171        .iter()
172        .map(|s| crate::suppress::Suppression {
173            line: s.line,
174            comment_line: s.comment_line,
175            kind: if s.kind == 0 {
176                None
177            } else {
178                crate::suppress::IssueKind::from_discriminant(s.kind)
179            },
180        })
181        .collect();
182
183    let unknown_suppression_kinds = cached
184        .unknown_suppression_kinds
185        .iter()
186        .map(|u| fallow_types::suppress::UnknownSuppressionKind {
187            comment_line: u.comment_line,
188            is_file_level: u.is_file_level,
189            token: u.token.clone(),
190        })
191        .collect();
192
193    ModuleInfo {
194        file_id,
195        exports,
196        imports,
197        re_exports,
198        dynamic_imports,
199        dynamic_import_patterns,
200        require_calls,
201        package_path_references: cached.package_path_references.clone(),
202        member_accesses: cached.member_accesses.clone(),
203        whole_object_uses: cached.whole_object_uses.clone(),
204        has_cjs_exports: cached.has_cjs_exports,
205        has_angular_component_template_url: cached.has_angular_component_template_url,
206        content_hash: cached.content_hash,
207        suppressions,
208        unknown_suppression_kinds,
209        unused_import_bindings: cached.unused_import_bindings.clone(),
210        type_referenced_import_bindings: cached.type_referenced_import_bindings.clone(),
211        value_referenced_import_bindings: cached.value_referenced_import_bindings.clone(),
212        line_offsets: cached.line_offsets.clone(),
213        complexity: if need_complexity {
214            cached.complexity.clone()
215        } else {
216            Vec::new()
217        },
218        flag_uses: cached.flag_uses.clone(),
219        class_heritage: cached.class_heritage.clone(),
220        injection_tokens: cached.injection_tokens.clone(),
221        local_type_declarations: cached
222            .local_type_declarations
223            .iter()
224            .map(|decl| LocalTypeDeclaration {
225                name: decl.name.clone(),
226                span: Span::new(decl.span_start, decl.span_end),
227            })
228            .collect(),
229        public_signature_type_references: cached
230            .public_signature_type_references
231            .iter()
232            .map(|reference| PublicSignatureTypeReference {
233                export_name: reference.export_name.clone(),
234                type_name: reference.type_name.clone(),
235                span: Span::new(reference.span_start, reference.span_end),
236            })
237            .collect(),
238        namespace_object_aliases: cached
239            .namespace_object_aliases
240            .iter()
241            .map(|alias| NamespaceObjectAlias {
242                via_export_name: alias.via_export_name.clone(),
243                suffix: alias.suffix.clone(),
244                namespace_local: alias.namespace_local.clone(),
245            })
246            .collect(),
247        iconify_prefixes: cached.iconify_prefixes.clone(),
248        iconify_icon_names: cached.iconify_icon_names.clone(),
249        auto_import_candidates: cached.auto_import_candidates.clone(),
250        directives: cached.directives.clone(),
251        security_sinks: cached.security_sinks.clone(),
252        security_sinks_skipped: cached.security_sinks_skipped,
253        tainted_bindings: cached.tainted_bindings.clone(),
254        sanitized_sink_args: cached.sanitized_sink_args.clone(),
255        security_control_sites: cached.security_control_sites.clone(),
256    }
257}
258
259/// Convert a [`ModuleInfo`](crate::ModuleInfo) to a [`CachedModule`] for storage.
260///
261/// `mtime_secs` and `file_size` come from `std::fs::metadata()` at parse time
262/// and enable fast cache validation on subsequent runs (skip file read when
263/// mtime+size match).
264#[must_use]
265#[expect(
266    clippy::too_many_lines,
267    reason = "single flat field-by-field serialization; splitting it harms readability"
268)]
269pub fn module_to_cached(
270    module: &crate::ModuleInfo,
271    mtime_secs: u64,
272    file_size: u64,
273) -> CachedModule {
274    CachedModule {
275        content_hash: module.content_hash,
276        mtime_secs,
277        file_size,
278        last_access_secs: current_unix_seconds(),
279        exports: module
280            .exports
281            .iter()
282            .map(|e| CachedExport {
283                name: match &e.name {
284                    ExportName::Named(n) => n.clone(),
285                    ExportName::Default => String::new(),
286                },
287                is_default: matches!(e.name, ExportName::Default),
288                is_type_only: e.is_type_only,
289                is_side_effect_used: e.is_side_effect_used,
290                visibility: e.visibility as u8,
291                local_name: e.local_name.clone(),
292                span_start: e.span.start,
293                span_end: e.span.end,
294                members: e
295                    .members
296                    .iter()
297                    .map(|m| CachedMember {
298                        name: m.name.clone(),
299                        kind: m.kind,
300                        span_start: m.span.start,
301                        span_end: m.span.end,
302                        has_decorator: m.has_decorator,
303                        decorator_names: m.decorator_names.clone(),
304                        is_instance_returning_static: m.is_instance_returning_static,
305                        is_self_returning: m.is_self_returning,
306                    })
307                    .collect(),
308                super_class: e.super_class.clone(),
309            })
310            .collect(),
311        imports: module
312            .imports
313            .iter()
314            .map(|i| {
315                let (kind, imported_name) = match &i.imported_name {
316                    crate::ImportedName::Named(n) => (IMPORT_KIND_NAMED, n.clone()),
317                    crate::ImportedName::Default => (IMPORT_KIND_DEFAULT, String::new()),
318                    crate::ImportedName::Namespace => (IMPORT_KIND_NAMESPACE, String::new()),
319                    crate::ImportedName::SideEffect => (IMPORT_KIND_SIDE_EFFECT, String::new()),
320                };
321                CachedImport {
322                    source: i.source.clone(),
323                    imported_name,
324                    local_name: i.local_name.clone(),
325                    is_type_only: i.is_type_only,
326                    from_style: i.from_style,
327                    kind,
328                    span_start: i.span.start,
329                    span_end: i.span.end,
330                    source_span_start: i.source_span.start,
331                    source_span_end: i.source_span.end,
332                }
333            })
334            .collect(),
335        re_exports: module
336            .re_exports
337            .iter()
338            .map(|r| CachedReExport {
339                source: r.source.clone(),
340                imported_name: r.imported_name.clone(),
341                exported_name: r.exported_name.clone(),
342                is_type_only: r.is_type_only,
343                span_start: r.span.start,
344                span_end: r.span.end,
345            })
346            .collect(),
347        dynamic_imports: module
348            .dynamic_imports
349            .iter()
350            .map(|d| CachedDynamicImport {
351                source: d.source.clone(),
352                span_start: d.span.start,
353                span_end: d.span.end,
354                destructured_names: d.destructured_names.clone(),
355                local_name: d.local_name.clone(),
356                is_speculative: d.is_speculative,
357            })
358            .collect(),
359        require_calls: module
360            .require_calls
361            .iter()
362            .map(|r| CachedRequireCall {
363                source: r.source.clone(),
364                span_start: r.span.start,
365                span_end: r.span.end,
366                source_span_start: r.source_span.start,
367                source_span_end: r.source_span.end,
368                destructured_names: r.destructured_names.clone(),
369                local_name: r.local_name.clone(),
370            })
371            .collect(),
372        package_path_references: module.package_path_references.clone(),
373        member_accesses: module.member_accesses.clone(),
374        whole_object_uses: module.whole_object_uses.clone(),
375        dynamic_import_patterns: module
376            .dynamic_import_patterns
377            .iter()
378            .map(|p| CachedDynamicImportPattern {
379                prefix: p.prefix.clone(),
380                suffix: p.suffix.clone(),
381                span_start: p.span.start,
382                span_end: p.span.end,
383            })
384            .collect(),
385        has_cjs_exports: module.has_cjs_exports,
386        has_angular_component_template_url: module.has_angular_component_template_url,
387        unused_import_bindings: module.unused_import_bindings.clone(),
388        type_referenced_import_bindings: module.type_referenced_import_bindings.clone(),
389        value_referenced_import_bindings: module.value_referenced_import_bindings.clone(),
390        suppressions: module
391            .suppressions
392            .iter()
393            .map(|s| CachedSuppression {
394                line: s.line,
395                comment_line: s.comment_line,
396                kind: s
397                    .kind
398                    .map_or(0, crate::suppress::IssueKind::to_discriminant),
399            })
400            .collect(),
401        unknown_suppression_kinds: module
402            .unknown_suppression_kinds
403            .iter()
404            .map(|u| CachedUnknownSuppressionKind {
405                comment_line: u.comment_line,
406                is_file_level: u.is_file_level,
407                token: u.token.clone(),
408            })
409            .collect(),
410        line_offsets: module.line_offsets.clone(),
411        complexity: module.complexity.clone(),
412        flag_uses: module.flag_uses.clone(),
413        class_heritage: module.class_heritage.clone(),
414        injection_tokens: module.injection_tokens.clone(),
415        local_type_declarations: module
416            .local_type_declarations
417            .iter()
418            .map(|decl| CachedLocalTypeDeclaration {
419                name: decl.name.clone(),
420                span_start: decl.span.start,
421                span_end: decl.span.end,
422            })
423            .collect(),
424        public_signature_type_references: module
425            .public_signature_type_references
426            .iter()
427            .map(|reference| CachedPublicSignatureTypeReference {
428                export_name: reference.export_name.clone(),
429                type_name: reference.type_name.clone(),
430                span_start: reference.span.start,
431                span_end: reference.span.end,
432            })
433            .collect(),
434        namespace_object_aliases: module
435            .namespace_object_aliases
436            .iter()
437            .map(|alias| CachedNamespaceObjectAlias {
438                via_export_name: alias.via_export_name.clone(),
439                suffix: alias.suffix.clone(),
440                namespace_local: alias.namespace_local.clone(),
441            })
442            .collect(),
443        iconify_prefixes: module.iconify_prefixes.clone(),
444        iconify_icon_names: module.iconify_icon_names.clone(),
445        auto_import_candidates: module.auto_import_candidates.clone(),
446        directives: module.directives.clone(),
447        security_sinks: module.security_sinks.clone(),
448        security_sinks_skipped: module.security_sinks_skipped,
449        tainted_bindings: module.tainted_bindings.clone(),
450        sanitized_sink_args: module.sanitized_sink_args.clone(),
451        security_control_sites: module.security_control_sites.clone(),
452    }
453}