1use std::path::{Path, PathBuf};
4use std::sync::Mutex;
5
6use oxc_resolver::Resolver;
7use rustc_hash::{FxHashMap, FxHashSet};
8use serde_json::Value;
9
10use fallow_types::discover::FileId;
11
12#[derive(Debug, Clone)]
14pub enum ResolveResult {
15 InternalModule(FileId),
17 SyntheticAutoImport(FileId),
19 InternalPackageModule {
22 file_id: FileId,
24 package_name: String,
26 },
27 ExternalFile(PathBuf),
29 NpmPackage(String),
31 Unresolvable(String),
33}
34
35impl ResolveResult {
36 #[must_use]
38 pub const fn internal_file_id(&self) -> Option<FileId> {
39 match self {
40 Self::InternalModule(file_id)
41 | Self::SyntheticAutoImport(file_id)
42 | Self::InternalPackageModule { file_id, .. } => Some(*file_id),
43 Self::ExternalFile(_) | Self::NpmPackage(_) | Self::Unresolvable(_) => None,
44 }
45 }
46
47 #[must_use]
49 pub const fn is_synthetic_auto_import(&self) -> bool {
50 matches!(self, Self::SyntheticAutoImport(_))
51 }
52
53 #[must_use]
55 pub fn package_usage_name(&self) -> Option<&str> {
56 match self {
57 Self::InternalPackageModule { package_name, .. } | Self::NpmPackage(package_name) => {
58 Some(package_name)
59 }
60 Self::InternalModule(_)
61 | Self::SyntheticAutoImport(_)
62 | Self::ExternalFile(_)
63 | Self::Unresolvable(_) => None,
64 }
65 }
66}
67
68#[derive(Debug, Clone)]
70pub struct ResolvedImport {
71 pub info: fallow_types::extract::ImportInfo,
73 pub target: ResolveResult,
75}
76
77#[derive(Debug, Clone)]
79pub struct ResolvedReExport {
80 pub info: fallow_types::extract::ReExportInfo,
82 pub target: ResolveResult,
84}
85
86pub enum ResolvedSourceEdge<'a> {
88 Import(&'a ResolvedImport),
90 ReExport(&'a ResolvedReExport),
92}
93
94impl<'a> ResolvedSourceEdge<'a> {
95 #[must_use]
97 pub fn source_specifier(&self) -> &'a str {
98 match self {
99 Self::Import(import) => &import.info.source,
100 Self::ReExport(re_export) => &re_export.info.source,
101 }
102 }
103
104 #[must_use]
106 pub const fn target(&self) -> &'a ResolveResult {
107 match self {
108 Self::Import(import) => &import.target,
109 Self::ReExport(re_export) => &re_export.target,
110 }
111 }
112
113 #[must_use]
115 pub const fn is_type_only(&self) -> bool {
116 match self {
117 Self::Import(import) => import.info.is_type_only,
118 Self::ReExport(re_export) => re_export.info.is_type_only,
119 }
120 }
121
122 #[must_use]
124 pub const fn span(&self) -> oxc_span::Span {
125 match self {
126 Self::Import(import) => import.info.span,
127 Self::ReExport(re_export) => re_export.info.span,
128 }
129 }
130
131 #[must_use]
133 pub const fn source_span(&self) -> oxc_span::Span {
134 match self {
135 Self::Import(import) => import.info.source_span,
136 Self::ReExport(_) => oxc_span::Span::new(0, 0),
137 }
138 }
139}
140
141#[derive(Debug)]
143pub struct ResolvedModule {
144 pub file_id: FileId,
146 pub path: PathBuf,
148 pub exports: Vec<fallow_types::extract::ExportInfo>,
150 pub re_exports: Vec<ResolvedReExport>,
152 pub resolved_imports: Vec<ResolvedImport>,
154 pub resolved_dynamic_imports: Vec<ResolvedImport>,
156 pub resolved_dynamic_patterns: Vec<(fallow_types::extract::DynamicImportPattern, Vec<FileId>)>,
158 pub member_accesses: Vec<fallow_types::extract::MemberAccess>,
160 pub semantic_facts: Box<[fallow_types::extract::SemanticFact]>,
162 pub whole_object_uses: Box<[String]>,
164 pub has_cjs_exports: bool,
166 pub has_angular_component_template_url: bool,
170 pub unused_import_bindings: FxHashSet<String>,
172 pub type_referenced_import_bindings: Vec<String>,
174 pub value_referenced_import_bindings: Vec<String>,
176 pub namespace_object_aliases: Vec<fallow_types::extract::NamespaceObjectAlias>,
179 pub exported_factory_returns: Box<[fallow_types::extract::FactoryReturnExport]>,
182}
183
184impl Default for ResolvedModule {
185 fn default() -> Self {
186 Self {
187 file_id: FileId(0),
188 path: PathBuf::new(),
189 exports: vec![],
190 re_exports: vec![],
191 resolved_imports: vec![],
192 resolved_dynamic_imports: vec![],
193 resolved_dynamic_patterns: vec![],
194 member_accesses: vec![],
195 semantic_facts: Box::default(),
196 whole_object_uses: Box::default(),
197 has_cjs_exports: false,
198 has_angular_component_template_url: false,
199 unused_import_bindings: FxHashSet::default(),
200 type_referenced_import_bindings: vec![],
201 value_referenced_import_bindings: vec![],
202 namespace_object_aliases: vec![],
203 exported_factory_returns: Box::default(),
204 }
205 }
206}
207
208impl ResolvedModule {
209 pub fn all_resolved_imports(&self) -> impl Iterator<Item = &ResolvedImport> {
215 self.resolved_imports
216 .iter()
217 .chain(self.resolved_dynamic_imports.iter())
218 }
219
220 pub fn all_resolved_source_edges(&self) -> impl Iterator<Item = ResolvedSourceEdge<'_>> {
226 self.resolved_imports
227 .iter()
228 .map(ResolvedSourceEdge::Import)
229 .chain(
230 self.resolved_dynamic_imports
231 .iter()
232 .map(ResolvedSourceEdge::Import),
233 )
234 .chain(self.re_exports.iter().map(ResolvedSourceEdge::ReExport))
235 }
236}
237
238pub(super) struct ResolveContext<'a> {
243 pub resolver: &'a Resolver,
245 pub style_resolver: &'a Resolver,
249 pub extensions: &'a [String],
251 pub path_to_id: &'a FxHashMap<&'a Path, FileId>,
253 pub raw_path_to_id: &'a FxHashMap<&'a Path, FileId>,
255 pub workspace_roots: &'a FxHashMap<&'a str, &'a Path>,
257 pub package_manifests: &'a [PackageManifestInfo],
259 pub condition_names: &'a [String],
261 pub path_aliases: &'a [(String, String)],
263 pub scss_include_paths: &'a [PathBuf],
267 pub static_dir_mappings: &'a [(PathBuf, String)],
270 pub root: &'a Path,
272 pub canonical_fallback: Option<&'a CanonicalFallback<'a>>,
276 pub tsconfig_warned: &'a Mutex<FxHashSet<String>>,
281 pub tsconfig_cache: &'a TsconfigCache,
286}
287
288#[derive(Default)]
290pub(super) struct TsconfigCache {
291 json: Mutex<FxHashMap<PathBuf, Option<Value>>>,
292 chains: Mutex<FxHashMap<PathBuf, Vec<PathBuf>>>,
293}
294
295impl TsconfigCache {
296 pub fn json(&self, path: &Path, load: impl FnOnce(&Path) -> Option<Value>) -> Option<Value> {
298 if let Ok(cache) = self.json.lock()
299 && let Some(value) = cache.get(path)
300 {
301 return value.clone();
302 }
303
304 let value = load(path);
305 if let Ok(mut cache) = self.json.lock() {
306 cache.insert(path.to_path_buf(), value.clone());
307 }
308 value
309 }
310
311 pub fn chain(&self, from_file: &Path) -> Option<Vec<PathBuf>> {
313 self.chains
314 .lock()
315 .ok()
316 .and_then(|cache| cache.get(from_file).cloned())
317 }
318
319 pub fn store_chain(&self, from_file: &Path, chain: Vec<PathBuf>) {
321 if let Ok(mut cache) = self.chains.lock() {
322 cache.insert(from_file.to_path_buf(), chain);
323 }
324 }
325}
326
327#[derive(Debug, Clone)]
329pub(super) struct PackageManifestInfo {
330 pub root: PathBuf,
332 pub canonical_root: PathBuf,
334 pub name: Option<String>,
336 pub package_json: fallow_config::PackageJson,
338}
339
340pub(super) struct CanonicalFallback<'a> {
342 files: &'a [fallow_types::discover::DiscoveredFile],
343 map: std::sync::OnceLock<FxHashMap<std::path::PathBuf, FileId>>,
344}
345
346impl<'a> CanonicalFallback<'a> {
347 pub const fn new(files: &'a [fallow_types::discover::DiscoveredFile]) -> Self {
348 Self {
349 files,
350 map: std::sync::OnceLock::new(),
351 }
352 }
353
354 pub fn get(&self, canonical: &Path) -> Option<FileId> {
356 let map = self.map.get_or_init(|| {
357 tracing::debug!(
358 "intra-project symlinks detected, building canonical path index ({} files)",
359 self.files.len()
360 );
361 self.files
362 .iter()
363 .filter_map(|f| {
364 dunce::canonicalize(&f.path)
365 .ok()
366 .map(|canonical| (canonical, f.id))
367 })
368 .collect()
369 });
370 map.get(canonical).copied()
371 }
372}
373
374#[cfg(all(test, not(miri)))]
375mod tests {
376 use super::*;
377 use fallow_types::discover::DiscoveredFile;
378
379 #[test]
380 fn canonical_fallback_returns_none_for_empty_files() {
381 let files: Vec<DiscoveredFile> = vec![];
382 let fallback = CanonicalFallback::new(&files);
383 assert!(fallback.get(Path::new("/nonexistent")).is_none());
384 }
385
386 #[test]
387 fn canonical_fallback_finds_existing_file() {
388 let temp = std::env::temp_dir().join("fallow-test-canonical-fallback");
389 let _ = std::fs::create_dir_all(&temp);
390 let test_file = temp.join("test.ts");
391 std::fs::write(&test_file, "").unwrap();
392
393 let files = vec![DiscoveredFile {
394 id: FileId(42),
395 path: test_file.clone(),
396 size_bytes: 0,
397 }];
398 let fallback = CanonicalFallback::new(&files);
399
400 let canonical = dunce::canonicalize(&test_file).unwrap();
401 assert_eq!(fallback.get(&canonical), Some(FileId(42)));
402
403 assert_eq!(fallback.get(&canonical), Some(FileId(42)));
404
405 let _ = std::fs::remove_dir_all(&temp);
406 }
407
408 #[test]
409 fn canonical_fallback_returns_none_for_missing_path() {
410 let temp = std::env::temp_dir().join("fallow-test-canonical-miss");
411 let _ = std::fs::create_dir_all(&temp);
412 let test_file = temp.join("exists.ts");
413 std::fs::write(&test_file, "").unwrap();
414
415 let files = vec![DiscoveredFile {
416 id: FileId(1),
417 path: test_file,
418 size_bytes: 0,
419 }];
420 let fallback = CanonicalFallback::new(&files);
421 assert!(fallback.get(Path::new("/nonexistent/file.ts")).is_none());
422
423 let _ = std::fs::remove_dir_all(&temp);
424 }
425}
426
427pub const OUTPUT_DIRS: &[&str] = &["dist", "build", "out", "esm", "cjs"];
432
433pub const SOURCE_EXTS: &[&str] = &["ts", "tsx", "mts", "cts", "js", "jsx", "mjs", "cjs"];
435
436pub const RN_PLATFORM_PREFIXES: &[&str] = &[".web", ".ios", ".android", ".native"];