1use std::time::{SystemTime, UNIX_EPOCH};
11
12use oxc_span::Span;
13
14use crate::ExportName;
15use fallow_types::extract::{NamespaceObjectAlias, VisibilityTag};
16
17#[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#[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#[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 _ => 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 destructured_names: r.destructured_names.clone(),
154 local_name: r.local_name.clone(),
155 })
156 .collect();
157
158 let dynamic_import_patterns = cached
159 .dynamic_import_patterns
160 .iter()
161 .map(|p| crate::DynamicImportPattern {
162 prefix: p.prefix.clone(),
163 suffix: p.suffix.clone(),
164 span: Span::new(p.span_start, p.span_end),
165 })
166 .collect();
167
168 let suppressions = cached
169 .suppressions
170 .iter()
171 .map(|s| crate::suppress::Suppression {
172 line: s.line,
173 comment_line: s.comment_line,
174 kind: if s.kind == 0 {
175 None
176 } else {
177 crate::suppress::IssueKind::from_discriminant(s.kind)
178 },
179 })
180 .collect();
181
182 let unknown_suppression_kinds = cached
183 .unknown_suppression_kinds
184 .iter()
185 .map(|u| fallow_types::suppress::UnknownSuppressionKind {
186 comment_line: u.comment_line,
187 is_file_level: u.is_file_level,
188 token: u.token.clone(),
189 })
190 .collect();
191
192 ModuleInfo {
193 file_id,
194 exports,
195 imports,
196 re_exports,
197 dynamic_imports,
198 dynamic_import_patterns,
199 require_calls,
200 member_accesses: cached.member_accesses.clone(),
201 whole_object_uses: cached.whole_object_uses.clone(),
202 has_cjs_exports: cached.has_cjs_exports,
203 has_angular_component_template_url: cached.has_angular_component_template_url,
204 content_hash: cached.content_hash,
205 suppressions,
206 unknown_suppression_kinds,
207 unused_import_bindings: cached.unused_import_bindings.clone(),
208 type_referenced_import_bindings: cached.type_referenced_import_bindings.clone(),
209 value_referenced_import_bindings: cached.value_referenced_import_bindings.clone(),
210 line_offsets: cached.line_offsets.clone(),
211 complexity: if need_complexity {
212 cached.complexity.clone()
213 } else {
214 Vec::new()
215 },
216 flag_uses: cached.flag_uses.clone(),
217 class_heritage: cached.class_heritage.clone(),
218 injection_tokens: cached.injection_tokens.clone(),
219 local_type_declarations: cached
220 .local_type_declarations
221 .iter()
222 .map(|decl| LocalTypeDeclaration {
223 name: decl.name.clone(),
224 span: Span::new(decl.span_start, decl.span_end),
225 })
226 .collect(),
227 public_signature_type_references: cached
228 .public_signature_type_references
229 .iter()
230 .map(|reference| PublicSignatureTypeReference {
231 export_name: reference.export_name.clone(),
232 type_name: reference.type_name.clone(),
233 span: Span::new(reference.span_start, reference.span_end),
234 })
235 .collect(),
236 namespace_object_aliases: cached
237 .namespace_object_aliases
238 .iter()
239 .map(|alias| NamespaceObjectAlias {
240 via_export_name: alias.via_export_name.clone(),
241 suffix: alias.suffix.clone(),
242 namespace_local: alias.namespace_local.clone(),
243 })
244 .collect(),
245 iconify_prefixes: cached.iconify_prefixes.clone(),
246 auto_import_candidates: cached.auto_import_candidates.clone(),
247 directives: cached.directives.clone(),
248 security_sinks: cached.security_sinks.clone(),
249 security_sinks_skipped: cached.security_sinks_skipped,
250 tainted_bindings: cached.tainted_bindings.clone(),
251 sanitized_sink_args: cached.sanitized_sink_args.clone(),
252 }
253}
254
255#[must_use]
261#[expect(
262 clippy::too_many_lines,
263 reason = "single flat field-by-field serialization; splitting it harms readability"
264)]
265pub fn module_to_cached(
266 module: &crate::ModuleInfo,
267 mtime_secs: u64,
268 file_size: u64,
269) -> CachedModule {
270 CachedModule {
271 content_hash: module.content_hash,
272 mtime_secs,
273 file_size,
274 last_access_secs: current_unix_seconds(),
275 exports: module
276 .exports
277 .iter()
278 .map(|e| CachedExport {
279 name: match &e.name {
280 ExportName::Named(n) => n.clone(),
281 ExportName::Default => String::new(),
282 },
283 is_default: matches!(e.name, ExportName::Default),
284 is_type_only: e.is_type_only,
285 is_side_effect_used: e.is_side_effect_used,
286 visibility: e.visibility as u8,
287 local_name: e.local_name.clone(),
288 span_start: e.span.start,
289 span_end: e.span.end,
290 members: e
291 .members
292 .iter()
293 .map(|m| CachedMember {
294 name: m.name.clone(),
295 kind: m.kind,
296 span_start: m.span.start,
297 span_end: m.span.end,
298 has_decorator: m.has_decorator,
299 decorator_names: m.decorator_names.clone(),
300 is_instance_returning_static: m.is_instance_returning_static,
301 is_self_returning: m.is_self_returning,
302 })
303 .collect(),
304 super_class: e.super_class.clone(),
305 })
306 .collect(),
307 imports: module
308 .imports
309 .iter()
310 .map(|i| {
311 let (kind, imported_name) = match &i.imported_name {
312 crate::ImportedName::Named(n) => (IMPORT_KIND_NAMED, n.clone()),
313 crate::ImportedName::Default => (IMPORT_KIND_DEFAULT, String::new()),
314 crate::ImportedName::Namespace => (IMPORT_KIND_NAMESPACE, String::new()),
315 crate::ImportedName::SideEffect => (IMPORT_KIND_SIDE_EFFECT, String::new()),
316 };
317 CachedImport {
318 source: i.source.clone(),
319 imported_name,
320 local_name: i.local_name.clone(),
321 is_type_only: i.is_type_only,
322 from_style: i.from_style,
323 kind,
324 span_start: i.span.start,
325 span_end: i.span.end,
326 source_span_start: i.source_span.start,
327 source_span_end: i.source_span.end,
328 }
329 })
330 .collect(),
331 re_exports: module
332 .re_exports
333 .iter()
334 .map(|r| CachedReExport {
335 source: r.source.clone(),
336 imported_name: r.imported_name.clone(),
337 exported_name: r.exported_name.clone(),
338 is_type_only: r.is_type_only,
339 span_start: r.span.start,
340 span_end: r.span.end,
341 })
342 .collect(),
343 dynamic_imports: module
344 .dynamic_imports
345 .iter()
346 .map(|d| CachedDynamicImport {
347 source: d.source.clone(),
348 span_start: d.span.start,
349 span_end: d.span.end,
350 destructured_names: d.destructured_names.clone(),
351 local_name: d.local_name.clone(),
352 is_speculative: d.is_speculative,
353 })
354 .collect(),
355 require_calls: module
356 .require_calls
357 .iter()
358 .map(|r| CachedRequireCall {
359 source: r.source.clone(),
360 span_start: r.span.start,
361 span_end: r.span.end,
362 destructured_names: r.destructured_names.clone(),
363 local_name: r.local_name.clone(),
364 })
365 .collect(),
366 member_accesses: module.member_accesses.clone(),
367 whole_object_uses: module.whole_object_uses.clone(),
368 dynamic_import_patterns: module
369 .dynamic_import_patterns
370 .iter()
371 .map(|p| CachedDynamicImportPattern {
372 prefix: p.prefix.clone(),
373 suffix: p.suffix.clone(),
374 span_start: p.span.start,
375 span_end: p.span.end,
376 })
377 .collect(),
378 has_cjs_exports: module.has_cjs_exports,
379 has_angular_component_template_url: module.has_angular_component_template_url,
380 unused_import_bindings: module.unused_import_bindings.clone(),
381 type_referenced_import_bindings: module.type_referenced_import_bindings.clone(),
382 value_referenced_import_bindings: module.value_referenced_import_bindings.clone(),
383 suppressions: module
384 .suppressions
385 .iter()
386 .map(|s| CachedSuppression {
387 line: s.line,
388 comment_line: s.comment_line,
389 kind: s
390 .kind
391 .map_or(0, crate::suppress::IssueKind::to_discriminant),
392 })
393 .collect(),
394 unknown_suppression_kinds: module
395 .unknown_suppression_kinds
396 .iter()
397 .map(|u| CachedUnknownSuppressionKind {
398 comment_line: u.comment_line,
399 is_file_level: u.is_file_level,
400 token: u.token.clone(),
401 })
402 .collect(),
403 line_offsets: module.line_offsets.clone(),
404 complexity: module.complexity.clone(),
405 flag_uses: module.flag_uses.clone(),
406 class_heritage: module.class_heritage.clone(),
407 injection_tokens: module.injection_tokens.clone(),
408 local_type_declarations: module
409 .local_type_declarations
410 .iter()
411 .map(|decl| CachedLocalTypeDeclaration {
412 name: decl.name.clone(),
413 span_start: decl.span.start,
414 span_end: decl.span.end,
415 })
416 .collect(),
417 public_signature_type_references: module
418 .public_signature_type_references
419 .iter()
420 .map(|reference| CachedPublicSignatureTypeReference {
421 export_name: reference.export_name.clone(),
422 type_name: reference.type_name.clone(),
423 span_start: reference.span.start,
424 span_end: reference.span.end,
425 })
426 .collect(),
427 namespace_object_aliases: module
428 .namespace_object_aliases
429 .iter()
430 .map(|alias| CachedNamespaceObjectAlias {
431 via_export_name: alias.via_export_name.clone(),
432 suffix: alias.suffix.clone(),
433 namespace_local: alias.namespace_local.clone(),
434 })
435 .collect(),
436 iconify_prefixes: module.iconify_prefixes.clone(),
437 auto_import_candidates: module.auto_import_candidates.clone(),
438 directives: module.directives.clone(),
439 security_sinks: module.security_sinks.clone(),
440 security_sinks_skipped: module.security_sinks_skipped,
441 tainted_bindings: module.tainted_bindings.clone(),
442 sanitized_sink_args: module.sanitized_sink_args.clone(),
443 }
444}