Skip to main content

fallow_graph/resolve/
types.rs

1//! Type definitions and constants for import resolution.
2
3use std::path::{Path, PathBuf};
4use std::sync::Mutex;
5
6use oxc_resolver::Resolver;
7use rustc_hash::{FxHashMap, FxHashSet};
8
9use fallow_types::discover::FileId;
10
11/// Result of resolving an import specifier.
12#[derive(Debug, Clone)]
13pub enum ResolveResult {
14    /// Resolved to a file within the project.
15    InternalModule(FileId),
16    /// Resolved to a workspace or self package source file while preserving
17    /// dependency usage for package accounting.
18    InternalPackageModule {
19        /// Internal source file reached by the package map.
20        file_id: FileId,
21        /// Package name that was used in the import specifier.
22        package_name: String,
23    },
24    /// Resolved to a file outside the project (`node_modules`, `.json`, etc.).
25    ExternalFile(PathBuf),
26    /// Bare specifier — an npm package.
27    NpmPackage(String),
28    /// Could not resolve.
29    Unresolvable(String),
30}
31
32impl ResolveResult {
33    /// Return the target file for any project-internal result.
34    #[must_use]
35    pub const fn internal_file_id(&self) -> Option<FileId> {
36        match self {
37            Self::InternalModule(file_id) | Self::InternalPackageModule { file_id, .. } => {
38                Some(*file_id)
39            }
40            Self::ExternalFile(_) | Self::NpmPackage(_) | Self::Unresolvable(_) => None,
41        }
42    }
43
44    /// Return the package name that should receive dependency usage credit.
45    #[must_use]
46    pub fn package_usage_name(&self) -> Option<&str> {
47        match self {
48            Self::InternalPackageModule { package_name, .. } | Self::NpmPackage(package_name) => {
49                Some(package_name)
50            }
51            Self::InternalModule(_) | Self::ExternalFile(_) | Self::Unresolvable(_) => None,
52        }
53    }
54}
55
56/// A resolved import with its target.
57#[derive(Debug, Clone)]
58pub struct ResolvedImport {
59    /// The original import information.
60    pub info: fallow_types::extract::ImportInfo,
61    /// Where the import resolved to.
62    pub target: ResolveResult,
63}
64
65/// A resolved re-export with its target.
66#[derive(Debug, Clone)]
67pub struct ResolvedReExport {
68    /// The original re-export information.
69    pub info: fallow_types::extract::ReExportInfo,
70    /// Where the re-export source resolved to.
71    pub target: ResolveResult,
72}
73
74/// Fully resolved module with all imports mapped to targets.
75#[derive(Debug)]
76pub struct ResolvedModule {
77    /// Unique file identifier.
78    pub file_id: FileId,
79    /// Absolute path to the module file.
80    pub path: PathBuf,
81    /// All export declarations in this module.
82    pub exports: Vec<fallow_types::extract::ExportInfo>,
83    /// All re-exports with resolved targets.
84    pub re_exports: Vec<ResolvedReExport>,
85    /// All static imports with resolved targets.
86    pub resolved_imports: Vec<ResolvedImport>,
87    /// All dynamic imports with resolved targets.
88    pub resolved_dynamic_imports: Vec<ResolvedImport>,
89    /// Dynamic import patterns matched against discovered files.
90    pub resolved_dynamic_patterns: Vec<(fallow_types::extract::DynamicImportPattern, Vec<FileId>)>,
91    /// Static member accesses (e.g., `Status.Active`).
92    pub member_accesses: Vec<fallow_types::extract::MemberAccess>,
93    /// Identifiers used as whole objects (Object.values, for..in, spread, etc.).
94    pub whole_object_uses: Vec<String>,
95    /// Whether this module uses `CommonJS` exports.
96    pub has_cjs_exports: bool,
97    /// Whether this module declares at least one Angular `@Component({
98    /// templateUrl: ... })` decorator. Mirrors `ModuleInfo.has_angular_component_template_url`;
99    /// see that field for the contract this gate enforces.
100    pub has_angular_component_template_url: bool,
101    /// Local names of import bindings that are never referenced in this file.
102    pub unused_import_bindings: FxHashSet<String>,
103    /// Local import bindings referenced from type positions.
104    pub type_referenced_import_bindings: Vec<String>,
105    /// Local import bindings referenced from runtime/value positions.
106    pub value_referenced_import_bindings: Vec<String>,
107    /// Namespace-import aliases re-exported through an object literal.
108    /// See `fallow_types::extract::NamespaceObjectAlias` for the shape.
109    pub namespace_object_aliases: Vec<fallow_types::extract::NamespaceObjectAlias>,
110}
111
112impl Default for ResolvedModule {
113    fn default() -> Self {
114        Self {
115            file_id: FileId(0),
116            path: PathBuf::new(),
117            exports: vec![],
118            re_exports: vec![],
119            resolved_imports: vec![],
120            resolved_dynamic_imports: vec![],
121            resolved_dynamic_patterns: vec![],
122            member_accesses: vec![],
123            whole_object_uses: vec![],
124            has_cjs_exports: false,
125            has_angular_component_template_url: false,
126            unused_import_bindings: FxHashSet::default(),
127            type_referenced_import_bindings: vec![],
128            value_referenced_import_bindings: vec![],
129            namespace_object_aliases: vec![],
130        }
131    }
132}
133
134impl ResolvedModule {
135    /// Iterate over all concrete resolved imports in source order buckets.
136    ///
137    /// Includes static `import`/`require` edges and literal dynamic `import()`
138    /// edges. Dynamic import patterns are intentionally excluded because they
139    /// resolve to sets of files rather than single import specifiers.
140    pub fn all_resolved_imports(&self) -> impl Iterator<Item = &ResolvedImport> {
141        self.resolved_imports
142            .iter()
143            .chain(self.resolved_dynamic_imports.iter())
144    }
145}
146
147/// Shared context for resolving import specifiers.
148///
149/// Groups the immutable lookup tables and caches that are shared across all
150/// `resolve_specifier` calls within a single `resolve_all_imports` invocation.
151pub(super) struct ResolveContext<'a> {
152    /// The oxc_resolver instance (configured once, shared across threads).
153    pub resolver: &'a Resolver,
154    /// CSS-only resolver with the package.json `style` condition enabled.
155    /// Used only for stylesheet package subpaths so JS/TS imports do not
156    /// accidentally prefer CSS export branches.
157    pub style_resolver: &'a Resolver,
158    /// Ordered extension list used by the resolver.
159    pub extensions: &'a [String],
160    /// Canonical path → FileId lookup (raw paths when root is canonical).
161    pub path_to_id: &'a FxHashMap<&'a Path, FileId>,
162    /// Raw (non-canonical) path → FileId lookup.
163    pub raw_path_to_id: &'a FxHashMap<&'a Path, FileId>,
164    /// Workspace name → canonical root path.
165    pub workspace_roots: &'a FxHashMap<&'a str, &'a Path>,
166    /// Package manifests for the root package and workspace packages.
167    pub package_manifests: &'a [PackageManifestInfo],
168    /// Ordered package condition names matching the resolver configuration.
169    pub condition_names: &'a [String],
170    /// Plugin-provided path aliases (prefix, replacement).
171    pub path_aliases: &'a [(String, String)],
172    /// Absolute directories to search when resolving bare SCSS/Sass
173    /// `@import` / `@use` specifiers. Populated from Angular's
174    /// `stylePreprocessorOptions.includePaths` and equivalent settings.
175    pub scss_include_paths: &'a [PathBuf],
176    /// Project root directory.
177    pub root: &'a Path,
178    /// Lazy canonical path → FileId fallback for intra-project symlinks.
179    /// Only initialized on first miss when root is canonical. `None` when
180    /// path_to_id already uses canonical paths (root is not canonical).
181    pub canonical_fallback: Option<&'a CanonicalFallback<'a>>,
182    /// Dedup set for broken-tsconfig warnings. Emits one `tracing::warn!`
183    /// per unique error message instead of spamming the log with one
184    /// warning per affected file. Shared across all parallel resolver
185    /// threads via `Mutex`. Empty and unused when no tsconfig errors occur.
186    pub tsconfig_warned: &'a Mutex<FxHashSet<String>>,
187}
188
189/// Package manifest data used by source fallbacks.
190#[derive(Debug, Clone)]
191pub(super) struct PackageManifestInfo {
192    /// Package root path as discovered from the workspace tree.
193    pub root: PathBuf,
194    /// Canonical package root path for node_modules symlink comparisons.
195    pub canonical_root: PathBuf,
196    /// Parsed package name.
197    pub name: Option<String>,
198    /// Parsed package.json fields.
199    pub package_json: fallow_config::PackageJson,
200}
201
202/// Thread-safe lazy canonical path index, built on first access.
203pub(super) struct CanonicalFallback<'a> {
204    files: &'a [fallow_types::discover::DiscoveredFile],
205    map: std::sync::OnceLock<FxHashMap<std::path::PathBuf, FileId>>,
206}
207
208impl<'a> CanonicalFallback<'a> {
209    pub const fn new(files: &'a [fallow_types::discover::DiscoveredFile]) -> Self {
210        Self {
211            files,
212            map: std::sync::OnceLock::new(),
213        }
214    }
215
216    /// Look up a canonical path, lazily building the index on first call.
217    pub fn get(&self, canonical: &Path) -> Option<FileId> {
218        let map = self.map.get_or_init(|| {
219            tracing::debug!(
220                "intra-project symlinks detected, building canonical path index ({} files)",
221                self.files.len()
222            );
223            self.files
224                .iter()
225                .filter_map(|f| {
226                    dunce::canonicalize(&f.path)
227                        .ok()
228                        .map(|canonical| (canonical, f.id))
229                })
230                .collect()
231        });
232        map.get(canonical).copied()
233    }
234}
235
236#[cfg(all(test, not(miri)))]
237mod tests {
238    use super::*;
239    use fallow_types::discover::DiscoveredFile;
240
241    #[test]
242    fn canonical_fallback_returns_none_for_empty_files() {
243        let files: Vec<DiscoveredFile> = vec![];
244        let fallback = CanonicalFallback::new(&files);
245        assert!(fallback.get(Path::new("/nonexistent")).is_none());
246    }
247
248    #[test]
249    fn canonical_fallback_finds_existing_file() {
250        let temp = std::env::temp_dir().join("fallow-test-canonical-fallback");
251        let _ = std::fs::create_dir_all(&temp);
252        let test_file = temp.join("test.ts");
253        std::fs::write(&test_file, "").unwrap();
254
255        let files = vec![DiscoveredFile {
256            id: FileId(42),
257            path: test_file.clone(),
258            size_bytes: 0,
259        }];
260        let fallback = CanonicalFallback::new(&files);
261
262        let canonical = dunce::canonicalize(&test_file).unwrap();
263        assert_eq!(fallback.get(&canonical), Some(FileId(42)));
264
265        // Second call uses cached map (OnceLock)
266        assert_eq!(fallback.get(&canonical), Some(FileId(42)));
267
268        let _ = std::fs::remove_dir_all(&temp);
269    }
270
271    #[test]
272    fn canonical_fallback_returns_none_for_missing_path() {
273        let temp = std::env::temp_dir().join("fallow-test-canonical-miss");
274        let _ = std::fs::create_dir_all(&temp);
275        let test_file = temp.join("exists.ts");
276        std::fs::write(&test_file, "").unwrap();
277
278        let files = vec![DiscoveredFile {
279            id: FileId(1),
280            path: test_file,
281            size_bytes: 0,
282        }];
283        let fallback = CanonicalFallback::new(&files);
284        assert!(fallback.get(Path::new("/nonexistent/file.ts")).is_none());
285
286        let _ = std::fs::remove_dir_all(&temp);
287    }
288}
289
290/// Known output directory names that may appear in exports map targets.
291/// When an exports map points to `./dist/utils.js`, we try replacing these
292/// prefixes with `src/` (the conventional source directory) to find the tracked
293/// source file.
294pub const OUTPUT_DIRS: &[&str] = &["dist", "build", "out", "esm", "cjs"];
295
296/// Source extensions to try when mapping a built output file back to source.
297pub const SOURCE_EXTS: &[&str] = &["ts", "tsx", "mts", "cts", "js", "jsx", "mjs", "cjs"];
298
299/// React Native platform extension prefixes.
300/// Metro resolves platform-specific files (e.g., `./foo` -> `./foo.web.tsx` on web).
301pub const RN_PLATFORM_PREFIXES: &[&str] = &[".web", ".ios", ".android", ".native"];