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 glimmer;
16pub mod graphql;
17pub mod html;
18pub mod inventory;
19pub mod mdx;
20mod parse;
21pub mod sfc;
22mod sfc_template;
23pub mod suppress;
24pub(crate) mod template_complexity;
25mod template_usage;
26pub mod visitor;
27
28use std::path::Path;
29
30use rayon::prelude::*;
31
32use cache::CacheStore;
33use fallow_types::discover::{DiscoveredFile, FileId};
34
35pub use fallow_types::extract::{
37 ClassHeritageInfo, DynamicImportInfo, DynamicImportPattern, ExportInfo, ExportName, ImportInfo,
38 ImportedName, LocalTypeDeclaration, MemberAccess, MemberInfo, MemberKind, ModuleInfo,
39 ParseResult, PublicSignatureTypeReference, ReExportInfo, RequireCallInfo, VisibilityTag,
40 compute_line_offsets,
41};
42
43pub use astro::extract_astro_frontmatter;
45pub use css::extract_css_module_exports;
46pub use glimmer::{is_glimmer_file, strip_glimmer_templates};
47pub use mdx::extract_mdx_statements;
48pub use sfc::{extract_sfc_scripts, is_sfc_file};
49pub use sfc_template::angular::ANGULAR_TPL_SENTINEL;
50
51pub const INSTANCE_EXPORT_SENTINEL: &str = "__fallow_instance_export__:";
57
58pub const PLAYWRIGHT_FIXTURE_DEF_SENTINEL: &str = "__fallow_playwright_fixture_def__:";
64
65pub const PLAYWRIGHT_FIXTURE_USE_SENTINEL: &str = "__fallow_playwright_fixture_use__:";
71
72pub const FACTORY_CALL_SENTINEL: &str = "__fallow_factory_call__:";
81
82use parse::parse_source_to_module;
83
84pub fn parse_all_files(
91 files: &[DiscoveredFile],
92 cache: Option<&CacheStore>,
93 need_complexity: bool,
94) -> ParseResult {
95 use std::sync::atomic::{AtomicUsize, Ordering};
96 let cache_hits = AtomicUsize::new(0);
97 let cache_misses = AtomicUsize::new(0);
98
99 let modules: Vec<ModuleInfo> = files
100 .par_iter()
101 .filter_map(|file| {
102 parse_single_file_cached(file, cache, &cache_hits, &cache_misses, need_complexity)
103 })
104 .collect();
105
106 let hits = cache_hits.load(Ordering::Relaxed);
107 let misses = cache_misses.load(Ordering::Relaxed);
108 if hits > 0 || misses > 0 {
109 tracing::info!(
110 cache_hits = hits,
111 cache_misses = misses,
112 "incremental cache stats"
113 );
114 }
115
116 ParseResult {
117 modules,
118 cache_hits: hits,
119 cache_misses: misses,
120 }
121}
122
123fn parse_single_file_cached(
132 file: &DiscoveredFile,
133 cache: Option<&CacheStore>,
134 cache_hits: &std::sync::atomic::AtomicUsize,
135 cache_misses: &std::sync::atomic::AtomicUsize,
136 need_complexity: bool,
137) -> Option<ModuleInfo> {
138 use std::sync::atomic::Ordering;
139
140 if let Some(store) = cache
143 && let Ok(metadata) = std::fs::metadata(&file.path)
144 {
145 let mt = mtime_secs(&metadata);
146 let sz = metadata.len();
147 if let Some(cached) = store.get_by_metadata(&file.path, mt, sz) {
148 if !need_complexity || !cached.complexity.is_empty() {
151 cache_hits.fetch_add(1, Ordering::Relaxed);
152 return Some(cache::cached_to_module_opts(
153 cached,
154 file.id,
155 need_complexity,
156 ));
157 }
158 }
159 }
160
161 let source = std::fs::read_to_string(&file.path).ok()?;
163 let content_hash = xxhash_rust::xxh3::xxh3_64(source.as_bytes());
164
165 if let Some(store) = cache
167 && let Some(cached) = store.get(&file.path, content_hash)
168 && (!need_complexity || !cached.complexity.is_empty())
169 {
170 cache_hits.fetch_add(1, Ordering::Relaxed);
171 return Some(cache::cached_to_module_opts(
172 cached,
173 file.id,
174 need_complexity,
175 ));
176 }
177 cache_misses.fetch_add(1, Ordering::Relaxed);
178
179 Some(parse_source_to_module(
181 file.id,
182 &file.path,
183 &source,
184 content_hash,
185 need_complexity,
186 ))
187}
188
189fn mtime_secs(metadata: &std::fs::Metadata) -> u64 {
192 metadata
193 .modified()
194 .ok()
195 .and_then(|t| t.duration_since(std::time::SystemTime::UNIX_EPOCH).ok())
196 .map_or(0, |d| d.as_secs())
197}
198
199#[must_use]
201pub fn parse_single_file(file: &DiscoveredFile) -> Option<ModuleInfo> {
202 let source = std::fs::read_to_string(&file.path).ok()?;
203 let content_hash = xxhash_rust::xxh3::xxh3_64(source.as_bytes());
204 Some(parse_source_to_module(
205 file.id,
206 &file.path,
207 &source,
208 content_hash,
209 false,
210 ))
211}
212
213#[must_use]
215pub fn parse_from_content(file_id: FileId, path: &Path, content: &str) -> ModuleInfo {
216 let content_hash = xxhash_rust::xxh3::xxh3_64(content.as_bytes());
217 parse_source_to_module(file_id, path, content, content_hash, true)
218}
219
220#[cfg(all(test, not(miri)))]
223mod tests;