mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 04:48:52 -05:00
chore(lsp): remove recursion in recurse_dependents (#23153)
Was investigating a separate stack overflow (that I've now found in the node resolution code) and came across this. We should avoid recursion (this is very old code).
This commit is contained in:
parent
eb6f6ff33d
commit
db89ce33f4
1 changed files with 12 additions and 10 deletions
|
@ -648,16 +648,20 @@ pub fn to_lsp_range(range: &deno_graph::Range) -> lsp::Range {
|
||||||
fn recurse_dependents(
|
fn recurse_dependents(
|
||||||
specifier: &ModuleSpecifier,
|
specifier: &ModuleSpecifier,
|
||||||
map: &HashMap<ModuleSpecifier, HashSet<ModuleSpecifier>>,
|
map: &HashMap<ModuleSpecifier, HashSet<ModuleSpecifier>>,
|
||||||
dependents: &mut HashSet<ModuleSpecifier>,
|
) -> Vec<ModuleSpecifier> {
|
||||||
) {
|
let mut dependents = HashSet::new();
|
||||||
if let Some(deps) = map.get(specifier) {
|
let mut pending = VecDeque::new();
|
||||||
for dep in deps {
|
pending.push_front(specifier);
|
||||||
if !dependents.contains(dep) {
|
while let Some(specifier) = pending.pop_front() {
|
||||||
dependents.insert(dep.clone());
|
if let Some(deps) = map.get(specifier) {
|
||||||
recurse_dependents(dep, map, dependents);
|
for dep in deps {
|
||||||
|
if dependents.insert(dep) {
|
||||||
|
pending.push_front(dep);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
dependents.into_iter().cloned().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -1106,10 +1110,8 @@ impl Documents {
|
||||||
specifier: &ModuleSpecifier,
|
specifier: &ModuleSpecifier,
|
||||||
) -> Vec<ModuleSpecifier> {
|
) -> Vec<ModuleSpecifier> {
|
||||||
self.calculate_dependents_if_dirty();
|
self.calculate_dependents_if_dirty();
|
||||||
let mut dependents = HashSet::new();
|
|
||||||
if let Some(specifier) = self.resolve_specifier(specifier) {
|
if let Some(specifier) = self.resolve_specifier(specifier) {
|
||||||
recurse_dependents(&specifier, &self.dependents_map, &mut dependents);
|
recurse_dependents(&specifier, &self.dependents_map)
|
||||||
dependents.into_iter().collect()
|
|
||||||
} else {
|
} else {
|
||||||
vec![]
|
vec![]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue