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