-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbarrel_resolution.rs
More file actions
189 lines (168 loc) · 6.3 KB
/
barrel_resolution.rs
File metadata and controls
189 lines (168 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! Shared barrel-file resolution logic.
//!
//! Both `edge_builder.rs` (napi-driven) and `import_edges.rs` (SQLite-driven)
//! need to recursively resolve a symbol through barrel reexport chains.
//! This module extracts the common algorithm so both callers share a single
//! implementation.
use std::collections::HashSet;
/// Minimal view of a single reexport entry, borrowed from the caller's data.
pub struct ReexportRef<'a> {
pub source: &'a str,
pub names: &'a [String],
pub wildcard_reexport: bool,
}
/// Trait that abstracts over the different context types in `edge_builder` and
/// `import_edges`. Each implementor provides access to its own reexport map
/// and definition index so the resolution algorithm stays generic.
pub trait BarrelContext {
/// Return the reexport entries for `barrel_path`, or `None` if the path
/// has no reexports.
fn reexports_for(&self, barrel_path: &str) -> Option<Vec<ReexportRef<'_>>>;
/// Return `true` if `file_path` contains a definition named `symbol`.
fn has_definition(&self, file_path: &str, symbol: &str) -> bool;
}
/// Recursively resolve a symbol through barrel reexport chains.
///
/// Mirrors `resolveBarrelExport()` in `resolve-imports.ts`.
/// The caller provides a `visited` set to prevent infinite loops on circular
/// reexport chains.
pub fn resolve_barrel_export<'a, C: BarrelContext>(
ctx: &'a C,
barrel_path: &str,
symbol_name: &str,
visited: &mut HashSet<String>,
) -> Option<String> {
if visited.contains(barrel_path) {
return None;
}
visited.insert(barrel_path.to_string());
let reexports = ctx.reexports_for(barrel_path)?;
for re in &reexports {
// Named reexports (non-wildcard)
if !re.names.is_empty() && !re.wildcard_reexport {
if re.names.iter().any(|n| n == symbol_name) {
if ctx.has_definition(re.source, symbol_name) {
return Some(re.source.to_string());
}
let deeper = resolve_barrel_export(ctx, re.source, symbol_name, visited);
if deeper.is_some() {
return deeper;
}
// Fallback: return source even if no definition found
return Some(re.source.to_string());
}
continue;
}
// Wildcard or empty-names reexports
if ctx.has_definition(re.source, symbol_name) {
return Some(re.source.to_string());
}
let deeper = resolve_barrel_export(ctx, re.source, symbol_name, visited);
if deeper.is_some() {
return deeper;
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
struct TestContext {
reexports: HashMap<String, Vec<(String, Vec<String>, bool)>>,
definitions: HashMap<String, HashSet<String>>,
}
impl BarrelContext for TestContext {
fn reexports_for(&self, barrel_path: &str) -> Option<Vec<ReexportRef<'_>>> {
self.reexports.get(barrel_path).map(|entries| {
entries
.iter()
.map(|(source, names, wildcard)| ReexportRef {
source: source.as_str(),
names: names.as_slice(),
wildcard_reexport: *wildcard,
})
.collect()
})
}
fn has_definition(&self, file_path: &str, symbol: &str) -> bool {
self.definitions
.get(file_path)
.map_or(false, |defs| defs.contains(symbol))
}
}
#[test]
fn resolves_named_reexport() {
let mut reexports = HashMap::new();
reexports.insert(
"src/index.ts".to_string(),
vec![("src/utils.ts".to_string(), vec!["foo".to_string()], false)],
);
let mut definitions = HashMap::new();
definitions.insert(
"src/utils.ts".to_string(),
HashSet::from(["foo".to_string()]),
);
let ctx = TestContext { reexports, definitions };
let mut visited = HashSet::new();
let result = resolve_barrel_export(&ctx, "src/index.ts", "foo", &mut visited);
assert_eq!(result.as_deref(), Some("src/utils.ts"));
}
#[test]
fn resolves_wildcard_reexport() {
let mut reexports = HashMap::new();
reexports.insert(
"src/index.ts".to_string(),
vec![("src/utils.ts".to_string(), vec![], true)],
);
let mut definitions = HashMap::new();
definitions.insert(
"src/utils.ts".to_string(),
HashSet::from(["bar".to_string()]),
);
let ctx = TestContext { reexports, definitions };
let mut visited = HashSet::new();
let result = resolve_barrel_export(&ctx, "src/index.ts", "bar", &mut visited);
assert_eq!(result.as_deref(), Some("src/utils.ts"));
}
#[test]
fn resolves_transitive_chain() {
let mut reexports = HashMap::new();
reexports.insert(
"src/index.ts".to_string(),
vec![("src/mid.ts".to_string(), vec![], true)],
);
reexports.insert(
"src/mid.ts".to_string(),
vec![("src/deep.ts".to_string(), vec!["baz".to_string()], false)],
);
let mut definitions = HashMap::new();
definitions.insert(
"src/deep.ts".to_string(),
HashSet::from(["baz".to_string()]),
);
let ctx = TestContext { reexports, definitions };
let mut visited = HashSet::new();
let result = resolve_barrel_export(&ctx, "src/index.ts", "baz", &mut visited);
assert_eq!(result.as_deref(), Some("src/deep.ts"));
}
#[test]
fn prevents_circular_reexport() {
let mut reexports = HashMap::new();
reexports.insert(
"src/a.ts".to_string(),
vec![("src/b.ts".to_string(), vec![], true)],
);
reexports.insert(
"src/b.ts".to_string(),
vec![("src/a.ts".to_string(), vec![], true)],
);
let ctx = TestContext {
reexports,
definitions: HashMap::new(),
};
let mut visited = HashSet::new();
let result = resolve_barrel_export(&ctx, "src/a.ts", "missing", &mut visited);
assert_eq!(result, None);
}
}