mirror of
https://github.com/hanzoai/unrs-resolver.git
synced 2026-09-24 17:03:52 +00:00
fix: resolve parent base url correctly by normalizing as absolute path (#72)
This commit is contained in:
@@ -8,3 +8,6 @@ end_of_line=lf
|
||||
charset=utf-8
|
||||
trim_trailing_whitespace=true
|
||||
insert_final_newline=true
|
||||
|
||||
[*.rs]
|
||||
indent_size = 4
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
export const foo = 'bar'
|
||||
@@ -0,0 +1 @@
|
||||
export * from "index";
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "./tsconfigs/tsconfig.dev"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": "../src"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "./tsconfig.base"
|
||||
}
|
||||
@@ -297,6 +297,30 @@ fn test_paths_nested_base() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parent_base_url() {
|
||||
let f = super::fixture_root().join("tsconfig");
|
||||
let f2 = f.join("cases").join("parent-base-url");
|
||||
|
||||
#[rustfmt::skip]
|
||||
let pass = [
|
||||
(f2.join("test"), "tsconfig.json", ".", Err(ResolveError::NotFound(".".into()))),
|
||||
(f2.join("test"), "tsconfig.json", "index", Ok(f2.join("src/index.ts"))),
|
||||
];
|
||||
|
||||
for (dir, tsconfig, request, expected) in pass {
|
||||
let resolver = Resolver::new(ResolveOptions {
|
||||
tsconfig: Some(TsconfigOptions {
|
||||
config_file: dir.parent().unwrap().join(tsconfig),
|
||||
references: TsconfigReferences::Auto,
|
||||
}),
|
||||
..ResolveOptions::default().with_extension(String::from(".ts"))
|
||||
});
|
||||
let resolved_path = resolver.resolve(&dir, request).map(|f| f.full_path());
|
||||
assert_eq!(resolved_path, expected, "{request} {tsconfig} {dir:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows.
|
||||
mod windows_test {
|
||||
use std::{
|
||||
|
||||
+17
-4
@@ -81,16 +81,29 @@ pub trait TsConfig: Sized + Debug {
|
||||
fn extend_tsconfig(&mut self, tsconfig: &Self) {
|
||||
let compiler_options = self.compiler_options_mut();
|
||||
|
||||
let tsconfig_dir = tsconfig.directory();
|
||||
|
||||
if compiler_options.base_url().is_none() {
|
||||
if let Some(base_url) = tsconfig.compiler_options().base_url() {
|
||||
compiler_options.set_base_url(base_url.to_path_buf());
|
||||
compiler_options.set_base_url(if base_url.starts_with(TEMPLATE_VARIABLE) {
|
||||
base_url.to_path_buf()
|
||||
} else {
|
||||
tsconfig_dir.join(base_url).normalize()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if compiler_options.paths().is_none() {
|
||||
let paths_base = compiler_options
|
||||
.base_url()
|
||||
.map_or_else(|| tsconfig.directory().to_path_buf(), Path::to_path_buf);
|
||||
let paths_base = compiler_options.base_url().map_or_else(
|
||||
|| tsconfig_dir.to_path_buf(),
|
||||
|path| {
|
||||
if path.starts_with(TEMPLATE_VARIABLE) {
|
||||
path.to_path_buf()
|
||||
} else {
|
||||
tsconfig_dir.join(path).normalize()
|
||||
}
|
||||
},
|
||||
);
|
||||
compiler_options.set_paths_base(paths_base);
|
||||
compiler_options.set_paths(tsconfig.compiler_options().paths().cloned());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user