Skip to main content

fallow_graph/resolve/
types.rs

1//! Type definitions and constants for import resolution.
2
3use std::path::{Path, PathBuf};
4
5use oxc_resolver::Resolver;
6use rustc_hash::FxHashMap;
7
8use fallow_types::discover::FileId;
9
10/// Result of resolving an import specifier.
11#[derive(Debug, Clone)]
12pub enum ResolveResult {
13    /// Resolved to a file within the project.
14    InternalModule(FileId),
15    /// Resolved to a file outside the project (`node_modules`, `.json`, etc.).
16    ExternalFile(PathBuf),
17    /// Bare specifier — an npm package.
18    NpmPackage(String),
19    /// Could not resolve.
20    Unresolvable(String),
21}
22
23/// A resolved import with its target.
24#[derive(Debug, Clone)]
25pub struct ResolvedImport {
26    /// The original import information.
27    pub info: fallow_types::extract::ImportInfo,
28    /// Where the import resolved to.
29    pub target: ResolveResult,
30}
31
32/// A resolved re-export with its target.
33#[derive(Debug, Clone)]
34pub struct ResolvedReExport {
35    /// The original re-export information.
36    pub info: fallow_types::extract::ReExportInfo,
37    /// Where the re-export source resolved to.
38    pub target: ResolveResult,
39}
40
41/// Fully resolved module with all imports mapped to targets.
42#[derive(Debug)]
43pub struct ResolvedModule {
44    /// Unique file identifier.
45    pub file_id: FileId,
46    /// Absolute path to the module file.
47    pub path: PathBuf,
48    /// All export declarations in this module.
49    pub exports: Vec<fallow_types::extract::ExportInfo>,
50    /// All re-exports with resolved targets.
51    pub re_exports: Vec<ResolvedReExport>,
52    /// All static imports with resolved targets.
53    pub resolved_imports: Vec<ResolvedImport>,
54    /// All dynamic imports with resolved targets.
55    pub resolved_dynamic_imports: Vec<ResolvedImport>,
56    /// Dynamic import patterns matched against discovered files.
57    pub resolved_dynamic_patterns: Vec<(fallow_types::extract::DynamicImportPattern, Vec<FileId>)>,
58    /// Static member accesses (e.g., `Status.Active`).
59    pub member_accesses: Vec<fallow_types::extract::MemberAccess>,
60    /// Identifiers used as whole objects (Object.values, for..in, spread, etc.).
61    pub whole_object_uses: Vec<String>,
62    /// Whether this module uses `CommonJS` exports.
63    pub has_cjs_exports: bool,
64    /// Local names of import bindings that are never referenced in this file.
65    pub unused_import_bindings: Vec<String>,
66}
67
68/// Shared context for resolving import specifiers.
69///
70/// Groups the immutable lookup tables and caches that are shared across all
71/// `resolve_specifier` calls within a single `resolve_all_imports` invocation.
72pub(super) struct ResolveContext<'a> {
73    /// The oxc_resolver instance (configured once, shared across threads).
74    pub resolver: &'a Resolver,
75    /// Canonical path → FileId lookup.
76    pub path_to_id: &'a FxHashMap<&'a Path, FileId>,
77    /// Raw (non-canonical) path → FileId lookup.
78    pub raw_path_to_id: &'a FxHashMap<&'a Path, FileId>,
79    /// Workspace name → canonical root path.
80    pub workspace_roots: &'a FxHashMap<&'a str, &'a Path>,
81    /// Plugin-provided path aliases (prefix, replacement).
82    pub path_aliases: &'a [(String, String)],
83    /// Project root directory.
84    pub root: &'a Path,
85}
86
87/// Known output directory names that may appear in exports map targets.
88/// When an exports map points to `./dist/utils.js`, we try replacing these
89/// prefixes with `src/` (the conventional source directory) to find the tracked
90/// source file.
91pub const OUTPUT_DIRS: &[&str] = &["dist", "build", "out", "esm", "cjs"];
92
93/// Source extensions to try when mapping a built output file back to source.
94pub const SOURCE_EXTS: &[&str] = &["ts", "tsx", "mts", "cts", "js", "jsx", "mjs", "cjs"];
95
96/// React Native platform extension prefixes.
97/// Metro resolves platform-specific files (e.g., `./foo` -> `./foo.web.tsx` on web).
98pub const RN_PLATFORM_PREFIXES: &[&str] = &[".web", ".ios", ".android", ".native"];