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