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, LocalTypeDeclaration, MemberAccess, MemberInfo, MemberKind, ModuleInfo,
37 ParseResult, PublicSignatureTypeReference, ReExportInfo, RequireCallInfo, VisibilityTag,
38 compute_line_offsets,
39};
40
41pub use astro::extract_astro_frontmatter;
43pub use css::extract_css_module_exports;
44pub use mdx::extract_mdx_statements;
45pub use sfc::{extract_sfc_scripts, is_sfc_file};
46pub use sfc_template::angular::ANGULAR_TPL_SENTINEL;
47
48pub const INSTANCE_EXPORT_SENTINEL: &str = "__fallow_instance_export__:";
54
55use parse::parse_source_to_module;
56
57pub fn parse_all_files(
64 files: &[DiscoveredFile],
65 cache: Option<&CacheStore>,
66 need_complexity: bool,
67) -> ParseResult {
68 use std::sync::atomic::{AtomicUsize, Ordering};
69 let cache_hits = AtomicUsize::new(0);
70 let cache_misses = AtomicUsize::new(0);
71
72 let modules: Vec<ModuleInfo> = files
73 .par_iter()
74 .filter_map(|file| {
75 parse_single_file_cached(file, cache, &cache_hits, &cache_misses, need_complexity)
76 })
77 .collect();
78
79 let hits = cache_hits.load(Ordering::Relaxed);
80 let misses = cache_misses.load(Ordering::Relaxed);
81 if hits > 0 || misses > 0 {
82 tracing::info!(
83 cache_hits = hits,
84 cache_misses = misses,
85 "incremental cache stats"
86 );
87 }
88
89 ParseResult {
90 modules,
91 cache_hits: hits,
92 cache_misses: misses,
93 }
94}
95
96fn mtime_secs(metadata: &std::fs::Metadata) -> u64 {
99 metadata
100 .modified()
101 .ok()
102 .and_then(|t| t.duration_since(std::time::SystemTime::UNIX_EPOCH).ok())
103 .map_or(0, |d| d.as_secs())
104}
105
106fn parse_single_file_cached(
115 file: &DiscoveredFile,
116 cache: Option<&CacheStore>,
117 cache_hits: &std::sync::atomic::AtomicUsize,
118 cache_misses: &std::sync::atomic::AtomicUsize,
119 need_complexity: bool,
120) -> Option<ModuleInfo> {
121 use std::sync::atomic::Ordering;
122
123 if let Some(store) = cache
126 && let Ok(metadata) = std::fs::metadata(&file.path)
127 {
128 let mt = mtime_secs(&metadata);
129 let sz = metadata.len();
130 if let Some(cached) = store.get_by_metadata(&file.path, mt, sz) {
131 if !need_complexity || !cached.complexity.is_empty() {
134 cache_hits.fetch_add(1, Ordering::Relaxed);
135 return Some(cache::cached_to_module(cached, file.id));
136 }
137 }
138 }
139
140 let source = std::fs::read_to_string(&file.path).ok()?;
142 let content_hash = xxhash_rust::xxh3::xxh3_64(source.as_bytes());
143
144 if let Some(store) = cache
146 && let Some(cached) = store.get(&file.path, content_hash)
147 && (!need_complexity || !cached.complexity.is_empty())
148 {
149 cache_hits.fetch_add(1, Ordering::Relaxed);
150 return Some(cache::cached_to_module(cached, file.id));
151 }
152 cache_misses.fetch_add(1, Ordering::Relaxed);
153
154 Some(parse_source_to_module(
156 file.id,
157 &file.path,
158 &source,
159 content_hash,
160 need_complexity,
161 ))
162}
163
164#[must_use]
166pub fn parse_single_file(file: &DiscoveredFile) -> Option<ModuleInfo> {
167 let source = std::fs::read_to_string(&file.path).ok()?;
168 let content_hash = xxhash_rust::xxh3::xxh3_64(source.as_bytes());
169 Some(parse_source_to_module(
170 file.id,
171 &file.path,
172 &source,
173 content_hash,
174 false,
175 ))
176}
177
178#[must_use]
180pub fn parse_from_content(file_id: FileId, path: &Path, content: &str) -> ModuleInfo {
181 let content_hash = xxhash_rust::xxh3::xxh3_64(content.as_bytes());
182 parse_source_to_module(file_id, path, content, content_hash, true)
183}
184
185#[cfg(all(test, not(miri)))]
188mod tests;