1#![warn(missing_docs)]
8
9pub mod astro;
10pub mod cache;
11pub(crate) mod complexity;
12pub mod css;
13pub mod html;
14pub mod mdx;
15mod parse;
16pub mod sfc;
17mod sfc_template;
18pub mod suppress;
19mod template_usage;
20pub mod visitor;
21
22use std::path::Path;
23
24use rayon::prelude::*;
25
26use cache::CacheStore;
27use fallow_types::discover::{DiscoveredFile, FileId};
28
29pub use fallow_types::extract::{
31 DynamicImportInfo, DynamicImportPattern, ExportInfo, ExportName, ImportInfo, ImportedName,
32 MemberAccess, MemberInfo, MemberKind, ModuleInfo, ParseResult, ReExportInfo, RequireCallInfo,
33 compute_line_offsets,
34};
35
36pub use astro::extract_astro_frontmatter;
38pub use css::extract_css_module_exports;
39pub use mdx::extract_mdx_statements;
40pub use sfc::{extract_sfc_scripts, is_sfc_file};
41
42use parse::parse_source_to_module;
43
44pub fn parse_all_files(
51 files: &[DiscoveredFile],
52 cache: Option<&CacheStore>,
53 need_complexity: bool,
54) -> ParseResult {
55 use std::sync::atomic::{AtomicUsize, Ordering};
56 let cache_hits = AtomicUsize::new(0);
57 let cache_misses = AtomicUsize::new(0);
58
59 let modules: Vec<ModuleInfo> = files
60 .par_iter()
61 .filter_map(|file| {
62 parse_single_file_cached(file, cache, &cache_hits, &cache_misses, need_complexity)
63 })
64 .collect();
65
66 let hits = cache_hits.load(Ordering::Relaxed);
67 let misses = cache_misses.load(Ordering::Relaxed);
68 if hits > 0 || misses > 0 {
69 tracing::info!(
70 cache_hits = hits,
71 cache_misses = misses,
72 "incremental cache stats"
73 );
74 }
75
76 ParseResult {
77 modules,
78 cache_hits: hits,
79 cache_misses: misses,
80 }
81}
82
83fn mtime_secs(metadata: &std::fs::Metadata) -> u64 {
86 metadata
87 .modified()
88 .ok()
89 .and_then(|t| t.duration_since(std::time::SystemTime::UNIX_EPOCH).ok())
90 .map_or(0, |d| d.as_secs())
91}
92
93fn parse_single_file_cached(
102 file: &DiscoveredFile,
103 cache: Option<&CacheStore>,
104 cache_hits: &std::sync::atomic::AtomicUsize,
105 cache_misses: &std::sync::atomic::AtomicUsize,
106 need_complexity: bool,
107) -> Option<ModuleInfo> {
108 use std::sync::atomic::Ordering;
109
110 if let Some(store) = cache
113 && let Ok(metadata) = std::fs::metadata(&file.path)
114 {
115 let mt = mtime_secs(&metadata);
116 let sz = metadata.len();
117 if let Some(cached) = store.get_by_metadata(&file.path, mt, sz) {
118 if !need_complexity || !cached.complexity.is_empty() {
121 cache_hits.fetch_add(1, Ordering::Relaxed);
122 return Some(cache::cached_to_module(cached, file.id));
123 }
124 }
125 }
126
127 let source = std::fs::read_to_string(&file.path).ok()?;
129 let content_hash = xxhash_rust::xxh3::xxh3_64(source.as_bytes());
130
131 if let Some(store) = cache
133 && let Some(cached) = store.get(&file.path, content_hash)
134 && (!need_complexity || !cached.complexity.is_empty())
135 {
136 cache_hits.fetch_add(1, Ordering::Relaxed);
137 return Some(cache::cached_to_module(cached, file.id));
138 }
139 cache_misses.fetch_add(1, Ordering::Relaxed);
140
141 Some(parse_source_to_module(
143 file.id,
144 &file.path,
145 &source,
146 content_hash,
147 need_complexity,
148 ))
149}
150
151#[must_use]
153pub fn parse_single_file(file: &DiscoveredFile) -> Option<ModuleInfo> {
154 let source = std::fs::read_to_string(&file.path).ok()?;
155 let content_hash = xxhash_rust::xxh3::xxh3_64(source.as_bytes());
156 Some(parse_source_to_module(
157 file.id,
158 &file.path,
159 &source,
160 content_hash,
161 false,
162 ))
163}
164
165#[must_use]
167pub fn parse_from_content(file_id: FileId, path: &Path, content: &str) -> ModuleInfo {
168 let content_hash = xxhash_rust::xxh3::xxh3_64(content.as_bytes());
169 parse_source_to_module(file_id, path, content, content_hash, true)
170}
171
172#[cfg(all(test, not(miri)))]
175mod tests;