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::sync::Arc;
11use std::time::{SystemTime, UNIX_EPOCH};
12
13use oxc_span::Span;
14
15use crate::ExportName;
16use fallow_types::extract::{NamespaceObjectAlias, VisibilityTag};
17use fallow_types::suppress::{PolicyRuleSuppression, SuppressionTarget};
18
19/// Seconds-since-Unix-epoch from the wall clock, saturating to 0 if the
20/// system clock is set before the epoch. Used as the LRU bookkeeping
21/// timestamp on `CachedModule.last_access_secs`. Wall-clock (not monotonic)
22/// is the right source here because the value persists across process
23/// invocations.
24#[must_use]
25pub fn current_unix_seconds() -> u64 {
26    SystemTime::now()
27        .duration_since(UNIX_EPOCH)
28        .map_or(0, |d| d.as_secs())
29}
30
31use super::types::{
32    CachedDynamicImport, CachedDynamicImportPattern, CachedExport, CachedImport,
33    CachedLocalTypeDeclaration, CachedMember, CachedModule, CachedNamespaceObjectAlias,
34    CachedPublicSignatureTypeReference, CachedReExport, CachedRequireCall, CachedSuppression,
35    CachedUnknownSuppressionKind, IMPORT_KIND_DEFAULT, IMPORT_KIND_NAMED, IMPORT_KIND_NAMESPACE,
36    IMPORT_KIND_SIDE_EFFECT,
37};
38
39/// Reconstruct a [`ModuleInfo`](crate::ModuleInfo) from a [`CachedModule`].
40#[must_use]
41pub fn cached_to_module(
42    cached: &CachedModule,
43    file_id: fallow_types::discover::FileId,
44) -> crate::ModuleInfo {
45    cached_to_module_opts(cached, file_id, true)
46}
47
48fn cached_exports_to_module(exports: &[CachedExport]) -> Vec<crate::ExportInfo> {
49    exports
50        .iter()
51        .map(|export| crate::ExportInfo {
52            name: if export.is_default {
53                ExportName::Default
54            } else {
55                ExportName::Named(export.name.clone())
56            },
57            local_name: export.local_name.clone(),
58            is_type_only: export.is_type_only,
59            is_side_effect_used: export.is_side_effect_used,
60            visibility: match export.visibility {
61                1 => VisibilityTag::Public,
62                2 => VisibilityTag::Internal,
63                3 => VisibilityTag::Beta,
64                4 => VisibilityTag::Alpha,
65                5 => VisibilityTag::ExpectedUnused,
66                _ => VisibilityTag::None,
67            },
68            expected_unused_reason: export.expected_unused_reason.clone(),
69            deprecated: export.deprecated,
70            deprecated_reason: export.deprecated_reason.clone(),
71            span: Span::new(export.span_start, export.span_end),
72            members: export
73                .members
74                .iter()
75                .map(|member| crate::MemberInfo {
76                    name: member.name.clone(),
77                    kind: member.kind,
78                    span: Span::new(member.span_start, member.span_end),
79                    has_decorator: member.has_decorator,
80                    decorator_names: member.decorator_names.clone(),
81                    is_instance_returning_static: member.is_instance_returning_static,
82                    is_self_returning: member.is_self_returning,
83                })
84                .collect(),
85            super_class: export.super_class.clone(),
86        })
87        .collect()
88}
89
90fn cached_imports_to_module(imports: &[CachedImport]) -> Vec<crate::ImportInfo> {
91    imports
92        .iter()
93        .map(|import| crate::ImportInfo {
94            source: import.source.clone(),
95            imported_name: match import.kind {
96                IMPORT_KIND_DEFAULT => crate::ImportedName::Default,
97                IMPORT_KIND_NAMESPACE => crate::ImportedName::Namespace,
98                IMPORT_KIND_SIDE_EFFECT => crate::ImportedName::SideEffect,
99                _ => crate::ImportedName::Named(import.imported_name.clone()),
100            },
101            local_name: import.local_name.clone(),
102            is_type_only: import.is_type_only,
103            is_type_only_star: import.is_type_only_star,
104            from_style: import.from_style,
105            span: Span::new(import.span_start, import.span_end),
106            source_span: Span::new(import.source_span_start, import.source_span_end),
107        })
108        .collect()
109}
110
111fn cached_re_exports_to_module(re_exports: &[CachedReExport]) -> Vec<crate::ReExportInfo> {
112    re_exports
113        .iter()
114        .map(|re_export| crate::ReExportInfo {
115            source: re_export.source.clone(),
116            imported_name: re_export.imported_name.clone(),
117            exported_name: re_export.exported_name.clone(),
118            is_type_only: re_export.is_type_only,
119            span: Span::new(re_export.span_start, re_export.span_end),
120            statement_span: Span::new(re_export.statement_span_start, re_export.statement_span_end),
121            source_span: Span::new(re_export.source_span_start, re_export.source_span_end),
122        })
123        .collect()
124}
125
126fn cached_dynamic_imports_to_module(
127    dynamic_imports: &[CachedDynamicImport],
128) -> Vec<crate::DynamicImportInfo> {
129    dynamic_imports
130        .iter()
131        .map(|dynamic_import| crate::DynamicImportInfo {
132            source: dynamic_import.source.clone(),
133            span: Span::new(dynamic_import.span_start, dynamic_import.span_end),
134            destructured_names: dynamic_import.destructured_names.clone(),
135            local_name: dynamic_import.local_name.clone(),
136            is_speculative: dynamic_import.is_speculative,
137        })
138        .collect()
139}
140
141fn cached_require_calls_to_module(
142    require_calls: &[CachedRequireCall],
143) -> Vec<crate::RequireCallInfo> {
144    require_calls
145        .iter()
146        .map(|require_call| crate::RequireCallInfo {
147            source: require_call.source.clone(),
148            span: Span::new(require_call.span_start, require_call.span_end),
149            source_span: Span::new(require_call.source_span_start, require_call.source_span_end),
150            destructured_names: require_call.destructured_names.clone(),
151            local_name: require_call.local_name.clone(),
152            is_type_only: require_call.is_type_only,
153        })
154        .collect()
155}
156
157fn cached_dynamic_patterns_to_module(
158    dynamic_import_patterns: &[CachedDynamicImportPattern],
159) -> Vec<crate::DynamicImportPattern> {
160    dynamic_import_patterns
161        .iter()
162        .map(|pattern| crate::DynamicImportPattern {
163            prefix: pattern.prefix.clone(),
164            suffix: pattern.suffix.clone(),
165            span: Span::new(pattern.span_start, pattern.span_end),
166            mechanism: pattern.mechanism,
167        })
168        .collect()
169}
170
171fn cached_suppressions_to_module(
172    suppressions: &[CachedSuppression],
173) -> Vec<crate::suppress::Suppression> {
174    suppressions
175        .iter()
176        .map(|suppression| {
177            let target = if suppression.kind == 0 {
178                None
179            } else if suppression.kind
180                == crate::suppress::IssueKind::PolicyViolation.to_discriminant()
181                && !suppression.policy_pack.is_empty()
182                && !suppression.policy_rule_id.is_empty()
183            {
184                Some(SuppressionTarget::PolicyRule(PolicyRuleSuppression::new(
185                    suppression.policy_pack.clone(),
186                    suppression.policy_rule_id.clone(),
187                )))
188            } else {
189                crate::suppress::IssueKind::from_discriminant(suppression.kind)
190                    .map(SuppressionTarget::Issue)
191            };
192            crate::suppress::Suppression {
193                line: suppression.line,
194                comment_line: suppression.comment_line,
195                target,
196                reason: suppression.reason.clone(),
197            }
198        })
199        .collect()
200}
201
202fn cached_unknown_suppressions_to_module(
203    unknown_suppression_kinds: &[CachedUnknownSuppressionKind],
204) -> Vec<fallow_types::suppress::UnknownSuppressionKind> {
205    unknown_suppression_kinds
206        .iter()
207        .map(|unknown| fallow_types::suppress::UnknownSuppressionKind {
208            comment_line: unknown.comment_line,
209            is_file_level: unknown.is_file_level,
210            token: unknown.token.clone(),
211            reason: unknown.reason.clone(),
212        })
213        .collect()
214}
215
216fn cached_local_types_to_module(
217    local_type_declarations: &[CachedLocalTypeDeclaration],
218) -> Vec<crate::LocalTypeDeclaration> {
219    local_type_declarations
220        .iter()
221        .map(|declaration| crate::LocalTypeDeclaration {
222            name: declaration.name.clone(),
223            span: Span::new(declaration.span_start, declaration.span_end),
224        })
225        .collect()
226}
227
228fn cached_signature_refs_to_module(
229    public_signature_type_references: &[CachedPublicSignatureTypeReference],
230) -> Vec<crate::PublicSignatureTypeReference> {
231    public_signature_type_references
232        .iter()
233        .map(|reference| crate::PublicSignatureTypeReference {
234            export_name: reference.export_name.clone(),
235            type_name: reference.type_name.clone(),
236            span: Span::new(reference.span_start, reference.span_end),
237        })
238        .collect()
239}
240
241fn cached_namespace_aliases_to_module(
242    namespace_object_aliases: &[CachedNamespaceObjectAlias],
243) -> Vec<NamespaceObjectAlias> {
244    namespace_object_aliases
245        .iter()
246        .map(|alias| NamespaceObjectAlias {
247            via_export_name: alias.via_export_name.clone(),
248            suffix: alias.suffix.clone(),
249            namespace_local: alias.namespace_local.clone(),
250        })
251        .collect()
252}
253
254fn module_exports_to_cached(exports: &[crate::ExportInfo]) -> Vec<CachedExport> {
255    exports
256        .iter()
257        .map(|export| CachedExport {
258            name: match &export.name {
259                ExportName::Named(name) => name.clone(),
260                ExportName::Default => String::new(),
261            },
262            is_default: matches!(export.name, ExportName::Default),
263            is_type_only: export.is_type_only,
264            is_side_effect_used: export.is_side_effect_used,
265            visibility: export.visibility as u8,
266            expected_unused_reason: export.expected_unused_reason.clone(),
267            deprecated: export.deprecated,
268            deprecated_reason: export.deprecated_reason.clone(),
269            local_name: export.local_name.clone(),
270            span_start: export.span.start,
271            span_end: export.span.end,
272            members: export
273                .members
274                .iter()
275                .map(|member| CachedMember {
276                    name: member.name.clone(),
277                    kind: member.kind,
278                    span_start: member.span.start,
279                    span_end: member.span.end,
280                    has_decorator: member.has_decorator,
281                    decorator_names: member.decorator_names.clone(),
282                    is_instance_returning_static: member.is_instance_returning_static,
283                    is_self_returning: member.is_self_returning,
284                })
285                .collect(),
286            super_class: export.super_class.clone(),
287        })
288        .collect()
289}
290
291fn module_imports_to_cached(imports: &[crate::ImportInfo]) -> Vec<CachedImport> {
292    imports
293        .iter()
294        .map(|import| {
295            let (kind, imported_name) = match &import.imported_name {
296                crate::ImportedName::Named(name) => (IMPORT_KIND_NAMED, name.clone()),
297                crate::ImportedName::Default => (IMPORT_KIND_DEFAULT, String::new()),
298                crate::ImportedName::Namespace => (IMPORT_KIND_NAMESPACE, String::new()),
299                crate::ImportedName::SideEffect => (IMPORT_KIND_SIDE_EFFECT, String::new()),
300            };
301            CachedImport {
302                source: import.source.clone(),
303                imported_name,
304                local_name: import.local_name.clone(),
305                is_type_only: import.is_type_only,
306                is_type_only_star: import.is_type_only_star,
307                from_style: import.from_style,
308                kind,
309                span_start: import.span.start,
310                span_end: import.span.end,
311                source_span_start: import.source_span.start,
312                source_span_end: import.source_span.end,
313            }
314        })
315        .collect()
316}
317
318fn module_re_exports_to_cached(re_exports: &[crate::ReExportInfo]) -> Vec<CachedReExport> {
319    re_exports
320        .iter()
321        .map(|re_export| CachedReExport {
322            source: re_export.source.clone(),
323            imported_name: re_export.imported_name.clone(),
324            exported_name: re_export.exported_name.clone(),
325            is_type_only: re_export.is_type_only,
326            span_start: re_export.span.start,
327            span_end: re_export.span.end,
328            statement_span_start: re_export.statement_span.start,
329            statement_span_end: re_export.statement_span.end,
330            source_span_start: re_export.source_span.start,
331            source_span_end: re_export.source_span.end,
332        })
333        .collect()
334}
335
336fn module_dynamic_imports_to_cached(
337    dynamic_imports: &[crate::DynamicImportInfo],
338) -> Vec<CachedDynamicImport> {
339    dynamic_imports
340        .iter()
341        .map(|dynamic_import| CachedDynamicImport {
342            source: dynamic_import.source.clone(),
343            span_start: dynamic_import.span.start,
344            span_end: dynamic_import.span.end,
345            destructured_names: dynamic_import.destructured_names.clone(),
346            local_name: dynamic_import.local_name.clone(),
347            is_speculative: dynamic_import.is_speculative,
348        })
349        .collect()
350}
351
352fn module_require_calls_to_cached(
353    require_calls: &[crate::RequireCallInfo],
354) -> Vec<CachedRequireCall> {
355    require_calls
356        .iter()
357        .map(|require_call| CachedRequireCall {
358            source: require_call.source.clone(),
359            span_start: require_call.span.start,
360            span_end: require_call.span.end,
361            source_span_start: require_call.source_span.start,
362            source_span_end: require_call.source_span.end,
363            destructured_names: require_call.destructured_names.clone(),
364            local_name: require_call.local_name.clone(),
365            is_type_only: require_call.is_type_only,
366        })
367        .collect()
368}
369
370fn module_dynamic_patterns_to_cached(
371    dynamic_import_patterns: &[crate::DynamicImportPattern],
372) -> Vec<CachedDynamicImportPattern> {
373    dynamic_import_patterns
374        .iter()
375        .map(|pattern| CachedDynamicImportPattern {
376            prefix: pattern.prefix.clone(),
377            suffix: pattern.suffix.clone(),
378            span_start: pattern.span.start,
379            span_end: pattern.span.end,
380            mechanism: pattern.mechanism,
381        })
382        .collect()
383}
384
385fn module_suppressions_to_cached(
386    suppressions: &[crate::suppress::Suppression],
387) -> Vec<CachedSuppression> {
388    suppressions
389        .iter()
390        .map(|suppression| {
391            let (kind, policy_pack, policy_rule_id) = match &suppression.target {
392                None => (0, String::new(), String::new()),
393                Some(SuppressionTarget::Issue(kind)) => {
394                    (kind.to_discriminant(), String::new(), String::new())
395                }
396                Some(SuppressionTarget::PolicyRule(target)) => (
397                    crate::suppress::IssueKind::PolicyViolation.to_discriminant(),
398                    target.pack.clone(),
399                    target.rule_id.clone(),
400                ),
401            };
402            CachedSuppression {
403                line: suppression.line,
404                comment_line: suppression.comment_line,
405                kind,
406                policy_pack,
407                policy_rule_id,
408                reason: suppression.reason.clone(),
409            }
410        })
411        .collect()
412}
413
414fn module_unknown_suppressions_to_cached(
415    unknown_suppression_kinds: &[fallow_types::suppress::UnknownSuppressionKind],
416) -> Vec<CachedUnknownSuppressionKind> {
417    unknown_suppression_kinds
418        .iter()
419        .map(|unknown| CachedUnknownSuppressionKind {
420            comment_line: unknown.comment_line,
421            is_file_level: unknown.is_file_level,
422            token: unknown.token.clone(),
423            reason: unknown.reason.clone(),
424        })
425        .collect()
426}
427
428fn module_local_types_to_cached(
429    local_type_declarations: &[crate::LocalTypeDeclaration],
430) -> Vec<CachedLocalTypeDeclaration> {
431    local_type_declarations
432        .iter()
433        .map(|declaration| CachedLocalTypeDeclaration {
434            name: declaration.name.clone(),
435            span_start: declaration.span.start,
436            span_end: declaration.span.end,
437        })
438        .collect()
439}
440
441fn module_signature_refs_to_cached(
442    public_signature_type_references: &[crate::PublicSignatureTypeReference],
443) -> Vec<CachedPublicSignatureTypeReference> {
444    public_signature_type_references
445        .iter()
446        .map(|reference| CachedPublicSignatureTypeReference {
447            export_name: reference.export_name.clone(),
448            type_name: reference.type_name.clone(),
449            span_start: reference.span.start,
450            span_end: reference.span.end,
451        })
452        .collect()
453}
454
455fn module_namespace_aliases_to_cached(
456    namespace_object_aliases: &[NamespaceObjectAlias],
457) -> Vec<CachedNamespaceObjectAlias> {
458    namespace_object_aliases
459        .iter()
460        .map(|alias| CachedNamespaceObjectAlias {
461            via_export_name: alias.via_export_name.clone(),
462            suffix: alias.suffix.clone(),
463            namespace_local: alias.namespace_local.clone(),
464        })
465        .collect()
466}
467
468/// Rehydrate an optional cached slice into the shared `Arc<[T]>` module form.
469fn cached_opt_to_arc<T: Clone>(cached: Option<&[T]>) -> Arc<[T]> {
470    cached.map(Arc::from).unwrap_or_default()
471}
472
473/// Reconstruct a [`ModuleInfo`](crate::ModuleInfo) from a [`CachedModule`], skipping
474/// the per-function complexity vec when `need_complexity` is `false`. Avoids the
475/// `Vec<FunctionComplexity>` clone on warm runs of commands (e.g. `fallow dead-code`)
476/// that don't consume complexity, which adds up across tens of thousands of files.
477#[must_use]
478pub fn cached_to_module_opts(
479    cached: &CachedModule,
480    file_id: fallow_types::discover::FileId,
481    need_complexity: bool,
482) -> crate::ModuleInfo {
483    crate::ModuleInfo {
484        file_id,
485        exports: cached_exports_to_module(&cached.exports).into(),
486        imports: cached_imports_to_module(&cached.imports),
487        re_exports: cached_re_exports_to_module(&cached.re_exports),
488        dynamic_imports: cached_dynamic_imports_to_module(&cached.dynamic_imports),
489        dynamic_import_patterns: cached_dynamic_patterns_to_module(&cached.dynamic_import_patterns),
490        require_calls: cached_require_calls_to_module(&cached.require_calls),
491        package_path_references: cached.package_path_references.clone(),
492        member_accesses: cached.member_accesses.clone().into(),
493        semantic_facts: cached_opt_to_arc(cached.semantic_facts.as_deref()),
494        whole_object_uses: Arc::from(&*cached.whole_object_uses),
495        has_cjs_exports: cached.has_cjs_exports,
496        has_angular_component_template_url: cached.has_angular_component_template_url,
497        content_hash: cached.content_hash,
498        parse_error_count: cached.parse_error_count,
499        parse_panicked: cached.parse_panicked,
500        suppressions: cached_suppressions_to_module(&cached.suppressions),
501        unknown_suppression_kinds: cached_unknown_suppressions_to_module(
502            &cached.unknown_suppression_kinds,
503        ),
504        unused_import_bindings: cached.unused_import_bindings.clone(),
505        type_referenced_import_bindings: cached.type_referenced_import_bindings.clone(),
506        value_referenced_import_bindings: cached.value_referenced_import_bindings.clone(),
507        line_offsets: cached.line_offsets.clone(),
508        complexity: if need_complexity {
509            cached.complexity.clone()
510        } else {
511            Vec::new()
512        },
513        flag_uses: cached.flag_uses.clone(),
514        flag_registry_facts: cached.flag_registry_facts.clone(),
515        class_heritage: cached.class_heritage.clone(),
516        exported_factory_returns: cached_opt_to_arc(cached.exported_factory_returns.as_deref()),
517        exported_factory_return_object_shapes: cached_opt_to_arc(
518            cached.exported_factory_return_object_shapes.as_deref(),
519        ),
520        type_member_types: cached_opt_to_arc(cached.type_member_types.as_deref()),
521        injection_tokens: cached.injection_tokens.clone(),
522        local_type_declarations: cached_local_types_to_module(&cached.local_type_declarations),
523        public_signature_type_references: cached_signature_refs_to_module(
524            &cached.public_signature_type_references,
525        ),
526        namespace_object_aliases: cached_namespace_aliases_to_module(
527            &cached.namespace_object_aliases,
528        ),
529        iconify_prefixes: cached.iconify_prefixes.clone(),
530        iconify_icon_names: cached.iconify_icon_names.clone(),
531        auto_import_candidates: cached.auto_import_candidates.clone(),
532        directives: cached.directives.clone(),
533        client_only_dynamic_import_spans: cached.client_only_dynamic_import_spans.clone(),
534        security_sinks: cached.security_sinks.clone(),
535        security_sinks_skipped: cached.security_sinks_skipped,
536        security_unresolved_callee_sites: cached.security_unresolved_callee_sites.clone(),
537        tainted_bindings: cached.tainted_bindings.clone(),
538        sanitized_sink_args: cached.sanitized_sink_args.clone(),
539        security_control_sites: cached.security_control_sites.clone(),
540        callee_uses: cached.callee_uses.clone(),
541        misplaced_directives: cached.misplaced_directives.clone(),
542        inline_server_action_exports: cached.inline_server_action_exports.clone(),
543        di_key_sites: cached.di_key_sites.clone(),
544        has_dynamic_provide: cached.has_dynamic_provide,
545        // Derived in `release_resolution_payload` from `imports` + `unused_import_bindings`
546        // (both cached); never persisted, so the cache-load path leaves it empty.
547        referenced_import_bindings: Vec::new(),
548        component_props: cached.component_props.clone(),
549        has_props_attrs_fallthrough: cached.has_props_attrs_fallthrough,
550        has_define_expose: cached.has_define_expose,
551        has_define_model: cached.has_define_model,
552        has_unharvestable_props: cached.has_unharvestable_props,
553        component_emits: cached.component_emits.clone(),
554        angular_inputs: cached.angular_inputs.clone(),
555        angular_outputs: cached.angular_outputs.clone(),
556        angular_component_selectors: cached.angular_component_selectors.clone(),
557        registered_custom_elements: cached.registered_custom_elements.clone(),
558        used_custom_element_tags: cached.used_custom_element_tags.clone(),
559        angular_used_selectors: cached.angular_used_selectors.clone(),
560        angular_entry_component_refs: cached.angular_entry_component_refs.clone(),
561        has_dynamic_component_render: cached.has_dynamic_component_render,
562        has_unharvestable_emits: cached.has_unharvestable_emits,
563        has_dynamic_emit: cached.has_dynamic_emit,
564        has_emit_whole_object_use: cached.has_emit_whole_object_use,
565        load_return_keys: cached.load_return_keys.clone(),
566        has_unharvestable_load: cached.has_unharvestable_load,
567        has_load_data_whole_use: cached.has_load_data_whole_use,
568        // Derived in `release_resolution_payload` from `whole_object_uses`.
569        has_page_data_store_whole_use: false,
570        // Derived in `release_resolution_payload` from `whole_object_uses`.
571        has_route_loader_data_whole_use: false,
572        component_functions: cached.component_functions.clone(),
573        react_props: cached.react_props.clone(),
574        hook_uses: cached.hook_uses.clone(),
575        render_edges: cached.render_edges.clone(),
576        svelte_dispatched_events: cached.svelte_dispatched_events.clone(),
577        svelte_listened_events: cached.svelte_listened_events.clone(),
578        has_dynamic_dispatch: cached.has_dynamic_dispatch,
579    }
580}
581
582/// Convert a [`ModuleInfo`](crate::ModuleInfo) to a [`CachedModule`] for storage.
583///
584/// The [`SourceFingerprint`](fallow_types::source_fingerprint::SourceFingerprint)
585/// comes from `std::fs::metadata()` at parse time and enables fast cache
586/// validation on subsequent runs.
587#[must_use]
588pub fn module_to_cached(
589    module: &crate::ModuleInfo,
590    fingerprint: fallow_types::source_fingerprint::SourceFingerprint,
591    complexity_extracted: bool,
592) -> CachedModule {
593    CachedModule {
594        content_hash: module.content_hash,
595        mtime_ns: fingerprint.mtime_ns,
596        ctime_ns: fingerprint.ctime_ns,
597        file_size: fingerprint.file_size,
598        last_access_secs: current_unix_seconds(),
599        exports: module_exports_to_cached(&module.exports),
600        imports: module_imports_to_cached(&module.imports),
601        re_exports: module_re_exports_to_cached(&module.re_exports),
602        dynamic_imports: module_dynamic_imports_to_cached(&module.dynamic_imports),
603        require_calls: module_require_calls_to_cached(&module.require_calls),
604        package_path_references: module.package_path_references.clone(),
605        member_accesses: module.member_accesses.to_vec(),
606        semantic_facts: (!module.semantic_facts.is_empty())
607            .then(|| Box::from(&*module.semantic_facts)),
608        whole_object_uses: Box::from(&*module.whole_object_uses),
609        dynamic_import_patterns: module_dynamic_patterns_to_cached(&module.dynamic_import_patterns),
610        parse_error_count: module.parse_error_count,
611        parse_panicked: module.parse_panicked,
612        has_cjs_exports: module.has_cjs_exports,
613        has_angular_component_template_url: module.has_angular_component_template_url,
614        unused_import_bindings: module.unused_import_bindings.clone(),
615        type_referenced_import_bindings: module.type_referenced_import_bindings.clone(),
616        value_referenced_import_bindings: module.value_referenced_import_bindings.clone(),
617        suppressions: module_suppressions_to_cached(&module.suppressions),
618        unknown_suppression_kinds: module_unknown_suppressions_to_cached(
619            &module.unknown_suppression_kinds,
620        ),
621        line_offsets: module.line_offsets.clone(),
622        complexity: module.complexity.clone(),
623        complexity_extracted,
624        flag_uses: module.flag_uses.clone(),
625        flag_registry_facts: module.flag_registry_facts.clone(),
626        class_heritage: module.class_heritage.clone(),
627        exported_factory_returns: (!module.exported_factory_returns.is_empty())
628            .then(|| Box::from(&*module.exported_factory_returns)),
629        exported_factory_return_object_shapes: (!module
630            .exported_factory_return_object_shapes
631            .is_empty())
632        .then(|| Box::from(&*module.exported_factory_return_object_shapes)),
633        type_member_types: (!module.type_member_types.is_empty())
634            .then(|| Box::from(&*module.type_member_types)),
635        injection_tokens: module.injection_tokens.clone(),
636        local_type_declarations: module_local_types_to_cached(&module.local_type_declarations),
637        public_signature_type_references: module_signature_refs_to_cached(
638            &module.public_signature_type_references,
639        ),
640        namespace_object_aliases: module_namespace_aliases_to_cached(
641            &module.namespace_object_aliases,
642        ),
643        iconify_prefixes: module.iconify_prefixes.clone(),
644        iconify_icon_names: module.iconify_icon_names.clone(),
645        auto_import_candidates: module.auto_import_candidates.clone(),
646        directives: module.directives.clone(),
647        client_only_dynamic_import_spans: module.client_only_dynamic_import_spans.clone(),
648        security_sinks: module.security_sinks.clone(),
649        security_sinks_skipped: module.security_sinks_skipped,
650        security_unresolved_callee_sites: module.security_unresolved_callee_sites.clone(),
651        tainted_bindings: module.tainted_bindings.clone(),
652        sanitized_sink_args: module.sanitized_sink_args.clone(),
653        security_control_sites: module.security_control_sites.clone(),
654        callee_uses: module.callee_uses.clone(),
655        misplaced_directives: module.misplaced_directives.clone(),
656        inline_server_action_exports: module.inline_server_action_exports.clone(),
657        di_key_sites: module.di_key_sites.clone(),
658        has_dynamic_provide: module.has_dynamic_provide,
659        component_props: module.component_props.clone(),
660        has_props_attrs_fallthrough: module.has_props_attrs_fallthrough,
661        has_define_expose: module.has_define_expose,
662        has_define_model: module.has_define_model,
663        has_unharvestable_props: module.has_unharvestable_props,
664        component_emits: module.component_emits.clone(),
665        angular_inputs: module.angular_inputs.clone(),
666        angular_outputs: module.angular_outputs.clone(),
667        angular_component_selectors: module.angular_component_selectors.clone(),
668        registered_custom_elements: module.registered_custom_elements.clone(),
669        used_custom_element_tags: module.used_custom_element_tags.clone(),
670        angular_used_selectors: module.angular_used_selectors.clone(),
671        angular_entry_component_refs: module.angular_entry_component_refs.clone(),
672        has_dynamic_component_render: module.has_dynamic_component_render,
673        has_unharvestable_emits: module.has_unharvestable_emits,
674        has_dynamic_emit: module.has_dynamic_emit,
675        has_emit_whole_object_use: module.has_emit_whole_object_use,
676        load_return_keys: module.load_return_keys.clone(),
677        has_unharvestable_load: module.has_unharvestable_load,
678        has_load_data_whole_use: module.has_load_data_whole_use,
679        component_functions: module.component_functions.clone(),
680        react_props: module.react_props.clone(),
681        hook_uses: module.hook_uses.clone(),
682        render_edges: module.render_edges.clone(),
683        svelte_dispatched_events: module.svelte_dispatched_events.clone(),
684        svelte_listened_events: module.svelte_listened_events.clone(),
685        has_dynamic_dispatch: module.has_dynamic_dispatch,
686    }
687}
688
689/// Convert a module to a cache entry from explicit source-fingerprint parts.
690///
691/// Kept for tests that need literal invalidation inputs without filesystem
692/// metadata.
693#[cfg(test)]
694#[must_use]
695pub fn module_to_cached_from_parts(
696    module: &crate::ModuleInfo,
697    mtime_ns: u64,
698    file_size: u64,
699) -> CachedModule {
700    module_to_cached(
701        module,
702        fallow_types::source_fingerprint::SourceFingerprint::new(mtime_ns, file_size),
703        !module.complexity.is_empty(),
704    )
705}