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