Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check all import ranges in Dependency::includes() #271

Merged
merged 3 commits into from
Apr 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,16 @@ impl Dependency {
/// entry for the dependency, returning a reference to the range if true,
/// otherwise none.
pub fn includes(&self, position: &Position) -> Option<&Range> {
let code = self.maybe_code.includes(position);
if code.is_none() {
self.maybe_type.includes(position)
} else {
code
for import in &self.imports {
if import.range.includes(position) {
return Some(&import.range);
}
}
// `@deno-types` directives won't be associated with an import.
if let Some(range) = self.maybe_type.includes(position) {
return Some(range);
}
None
}
}

Expand Down Expand Up @@ -2786,7 +2790,8 @@ mod tests {
fn test_module_dependency_includes() {
let specifier = ModuleSpecifier::parse("file:///a.ts").unwrap();
let module_analyzer = DefaultModuleAnalyzer::default();
let content = r#"import * as b from "./b.ts";"#;
let content =
"import * as b from \"./b.ts\";\nimport * as c from \"./b.ts\"";
let module = parse_module(
&specifier,
None,
Expand All @@ -2800,15 +2805,8 @@ mod tests {
)
.unwrap();
let module = module.esm().unwrap();
let maybe_dependency = module.dependencies.values().find_map(|d| {
d.includes(&Position {
line: 0,
character: 21,
})
.map(|r| (d, r))
});
assert!(maybe_dependency.is_some());
let (dependency, range) = maybe_dependency.unwrap();
assert_eq!(module.dependencies.len(), 1);
let dependency = module.dependencies.first().unwrap().1;
assert_eq!(
dependency.maybe_code,
Resolution::Ok(Box::new(ResolutionResolved {
Expand All @@ -2827,9 +2825,12 @@ mod tests {
}))
);
assert_eq!(
range,
&Range {
specifier,
dependency.includes(&Position {
line: 0,
character: 21,
}),
Some(&Range {
specifier: specifier.clone(),
start: Position {
line: 0,
character: 19
Expand All @@ -2838,16 +2839,32 @@ mod tests {
line: 0,
character: 27
},
}
})
);

let maybe_dependency = module.dependencies.values().find_map(|d| {
d.includes(&Position {
assert_eq!(
dependency.includes(&Position {
line: 1,
character: 21,
}),
Some(&Range {
specifier,
start: Position {
line: 1,
character: 19
},
end: Position {
line: 1,
character: 27
},
})
);
assert_eq!(
dependency.includes(&Position {
line: 0,
character: 18,
})
});
assert!(maybe_dependency.is_none());
}),
None,
);
}

#[tokio::test]
Expand Down