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(resolver): resolution with ResolutionMode::Types should be attempted #304

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 33 additions & 1 deletion src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,14 @@ impl Resolution {
self.ok().map(|r| &r.specifier)
}

pub fn maybe_range(&self) -> Option<&Range> {
match self {
Resolution::None => None,
Resolution::Ok(r) => Some(&r.range),
Resolution::Err(e) => Some(e.range()),
}
}

pub fn ok(&self) -> Option<&ResolutionResolved> {
if let Resolution::Ok(resolved) = self {
Some(&**resolved)
Expand Down Expand Up @@ -2054,7 +2062,31 @@ pub(crate) fn parse_esm_module_from_module_info(
maybe_npm_resolver,
)
} else {
Resolution::None
let range = Range::from_position_range(
module.specifier.clone(),
desc.specifier_range.clone(),
);
// only check if the code resolution is for the same range
if Some(&range) == dep.maybe_code.maybe_range() {
let types_resolution = resolve(
&desc.specifier,
range,
ResolutionMode::Types,
maybe_resolver,
maybe_npm_resolver,
);
// only bother setting if the resolved specifier
// does not match the code specifier
if types_resolution.maybe_specifier()
!= dep.maybe_code.maybe_specifier()
{
types_resolution
} else {
Resolution::None
}
} else {
Resolution::None
}
};
dep.maybe_type = maybe_type
}
Expand Down
127 changes: 127 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3647,4 +3647,131 @@ export function a(a: A): B {
assert!(iterator.next().is_none());
}
}

#[tokio::test]
async fn test_resolver_execution_and_types_resolution() {
#[derive(Debug)]
struct ExtResolver;

impl crate::source::Resolver for ExtResolver {
fn resolve(
&self,
specifier_text: &str,
referrer: &ModuleSpecifier,
mode: ResolutionMode,
) -> Result<ModuleSpecifier, crate::source::ResolveError> {
let specifier_text = match mode {
ResolutionMode::Types => format!("{}.d.ts", specifier_text),
ResolutionMode::Execution => format!("{}.js", specifier_text),
};
Ok(resolve_import(&specifier_text, referrer)?)
}
}

let mut loader = setup(
vec![
(
"file:///a/test01.ts",
Source::Module {
specifier: "file:///a/test01.ts",
maybe_headers: None,
content: r#"
import a from "./a";
Copy link
Member Author

@dsherret dsherret Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice how the specifier is "./a" here, but it resolves to a .js files for the code resolution and a .d.ts file for the types resolution because of how the resolver is implemented.

This will allow resolving something like "chalk" to its declaration file.

"#,
},
),
(
"file:///a/a.js",
Source::Module {
specifier: "file:///a/a.js",
maybe_headers: None,
content: r#"export default 5;"#,
},
),
(
"file:///a/a.d.ts",
Source::Module {
specifier: "file:///a/a.d.ts",
maybe_headers: None,
content: r#"export default 5;"#,
},
),
],
vec![],
);
let root_specifier =
ModuleSpecifier::parse("file:///a/test01.ts").expect("bad url");
let mut graph = ModuleGraph::new(GraphKind::All);
let resolver = ExtResolver;
graph
.build(
vec![root_specifier.clone()],
&mut loader,
BuildOptions {
resolver: Some(&resolver),
..Default::default()
},
)
.await;
assert_eq!(
json!(graph),
json!({
"roots": [
"file:///a/test01.ts"
],
"modules": [
{
"kind": "esm",
"size": 17,
"mediaType": "Dts",
"specifier": "file:///a/a.d.ts"
},
{
"kind": "esm",
"size": 17,
"mediaType": "JavaScript",
"specifier": "file:///a/a.js"
},
{
"dependencies": [
{
"specifier": "./a",
"code": {
"specifier": "file:///a/a.js",
"span": {
"start": {
"line": 1,
"character": 26
},
"end": {
"line": 1,
"character": 31
}
}
},
"type": {
"specifier": "file:///a/a.d.ts",
"span": {
"start": {
"line": 1,
"character": 26
},
"end": {
"line": 1,
"character": 31
}
}
},
}
],
"kind": "esm",
"size": 46,
"mediaType": "TypeScript",
"specifier": "file:///a/test01.ts"
}
],
"redirects": {}
})
);
}
}
Loading