8000 fix: takes paths and references into account at the same time · unrs/unrs-resolver@4ce4451 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ce4451

Browse files
committed
fix: takes paths and references into account at the same time
1 parent 93709b9 commit 4ce4451

File tree

4 files changed

+120
-13
lines changed

4 files changed

+120
-13
lines changed

examples/resolver.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ fn main() {
1111

1212
let specifier = env::args().nth(2).expect("specifier");
1313

14-
println!("path: {path:?}");
15-
println!("specifier: {specifier}");
16-
1714
let options = ResolveOptions {
1815
alias_fields: vec![vec!["browser".into()]],
1916
alias: vec![("asdf".into(), vec![AliasValue::from("./test.js")])],
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"extends": "../tsconfig.base.json",
3+
"compilerOptions": {
4+
"baseUrl": "./"
5+
},
6+
"references": [
7+
{
8+
"path": "../project_a/conf.json"
9+
},
10+
{
11+
"path": "../project_b"
12+
},
13+
{
14+
"path": "../project_c/tsconfig.json"
15+
},
16+
{
17+
"path": "../../paths_template_variable/tsconfig2.json"
18+
}
19+
]
20+
}

src/tests/tsconfig_project_references.rs

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use crate::{ResolveError, ResolveOptions, Resolver, TsconfigOptions, TsconfigRef
66
fn auto() {
77
let f = super::fixture_root().join("tsconfig/cases/project_references");
88

9+
// The following resolver's `config_file` has defined it's own paths alias which has higher priority
10+
// some cases will work without references
911
let resolver = Resolver::new(ResolveOptions {
1012
tsconfig: Some(TsconfigOptions {
1113
config_file: f.join("app"),
@@ -14,14 +16,23 @@ fn auto() {
1416
..ResolveOptions::default()
1517
});
1618

19+
// The following resolver's `config_file` has no `paths` alias with `references` enabled
20+
let no_paths_resolver = Resolver::new(ResolveOptions {
21+
tsconfig: Some(TsconfigOptions {
22+
config_file: f.join("app/tsconfig.nopaths.json"),
23+
references: TsconfigReferences::Auto,
24+
}),
25+
..ResolveOptions::default()
26+
});
27+
1728
#[rustfmt::skip]
1829
let pass = [
1930
// Test normal paths alias
2031
(f.join("app"), "@/index.ts", f.join("app/aliased/index.ts")),
2132
(f.join("app"), "@/../index.ts", f.join("app/index.ts")),
2233
// Test project reference
23-
(f.join("project_a"), "@/index.ts", f.join("project_a/aliased/index.ts")),
24-
(f.join("project_b/src"), "@/index.ts", f.join("project_b/src/aliased/index.ts")),
34+
(f.join("project_a"), "@/index.ts", f.join("app/aliased/index.ts")),
35+
(f.join("project_b/src"), "@/index.ts", f.join("app/aliased/index.ts")),
2536
// Does not have paths alias
2637
(f.join("project_a"), "./index.ts", f.join("project_a/index.ts")),
2738
(f.join("project_c"), "./index.ts", f.join("project_c/index.ts")),
@@ -36,12 +47,37 @@ fn auto() {
3647
let resolved_path = resolver.resolve(&path, request).map(|f| f.full_path());
3748
assert_eq!(resolved_path, Ok(expected), "{request} {path:?}");
3849
}
50+
51+
#[rustfmt::skip]
52+
let pass = [
53+
// Test normal paths alias
54+
(f.join("app"), "@/index.ts", Err(ResolveError::NotFound("@/index.ts".into()))),
55+
(f.join("app"), "@/../index.ts", Ok(f.join("app/index.ts"))),
56+
// Test project reference
57+
(f.join("project_a"), "@/index.ts", Ok(f.join("project_a/aliased/index.ts"))),
58+
(f.join("project_b/src"), "@/index.ts", Ok(f.join("project_b/src/aliased/index.ts"))),
59+
// Does not have paths alias
60+
(f.join("project_a"), "./index.ts", Ok(f.join("project_a/index.ts"))),
61+
(f.join("project_c"), "./index.ts", Ok(f.join("project_c/index.ts"))),
62+
// Template variable
63+
{
64+
let dir = f.parent().unwrap().join("paths_template_variable");
65+
(dir.clone(), "foo", Ok(dir.join("foo.js")))
66+
}
67+
];
68+
69+
for (path, request, expected) in pass {
70+
let resolved_path = no_paths_resolver.resolve(&path, request).map(|f| f.full_path());
71+
assert_eq!(resolved_path, expected, "{request} {path:?}");
72+
}
3973
}
4074

4175
#[test]
4276
fn disabled() {
4377
let f = super::fixture_root().join("tsconfig/cases/project_references");
4478

79+
// The following resolver's `config_file` has defined it's own paths alias which has higher priority
80+
// some cases will work without references
4581
let resolver = Resolver::new(ResolveOptions {
4682
tsconfig: Some(TsconfigOptions {
4783
config_file: f.join("app"),
@@ -50,12 +86,39 @@ fn disabled() {
5086
..ResolveOptions::default()
5187
});
5288

89+
// The following resolver's `config_file` has no `paths` alias with `references` enabled
90+
let no_paths_resolver = Resolver::new(ResolveOptions {
91+
tsconfig: Some(TsconfigOptions {
92+
config_file: f.join("app/tsconfig.nopaths.json"),
93+
references: TsconfigReferences::Disabled,
94+
}),
95+
..ResolveOptions::default()
96+
});
97+
5398
#[rustfmt::skip]
5499
let pass = [
55100
// Test normal paths alias
56101
(f.join("app"), "@/index.ts", Ok(f.join("app/aliased/index.ts"))),
57102
(f.join("app"), "@/../index.ts", Ok(f.join("app/index.ts"))),
58103
// Test project reference
104+
(f.join("project_a"), "@/index.ts", Ok(f.join("app/aliased/index.ts"))),
105+
(f.join("project_b/src"), "@/index.ts", Ok(f.join("app/aliased/index.ts"))),
106+
// Does not have paths alias
107+
(f.join("project_a"), "./index.ts", Ok(f.join("project_a/index.ts"))),
108+
(f.join("project_c"), "./index.ts", Ok(f.join("project_c/index.ts"))),
109+
];
110+
111+
for (path, request, expected) in pass {
112+
let resolved_path = resolver.resolve(&path, request).map(|f| f.full_path());
113+
assert_eq!(resolved_path, expected, "{request} {path:?}");
114+
}
115+
116+
#[rustfmt::skip]
117+
let pass = [
118+
// Test normal paths alias
119+
(f.join("app"), "@/index.ts", Err(ResolveError::NotFound("@/index.ts".into()))),
120+
(f.join("app"), "#/../index.ts", Ok(f.join("app/index.ts"))), // This works because "#/../index.ts" is resolved as "./index.ts"
121+
// Test project reference
59122
(f.join("project_a"), "@/index.ts", Err(ResolveError::NotFound("@/index.ts".into()))),
60123
(f.join("project_b/src"), "@/index.ts", Err(ResolveError::NotFound("@/index.ts".into()))),
61124
// Does not have paths alias
@@ -64,7 +127,7 @@ fn disabled() {
64127
];
65128

66129
for (path, request, expected) in pass {
67-
let resolved_path = resolver.resolve(&path, request).map(|f| f.full_path());
130+
let resolved_path = no_paths_resolver.resolve(&path, request).map(|f| f.full_path());
68131
assert_eq!(resolved_path, expected, "{request} {path:?}");
69132
}
70133
}
@@ -73,6 +136,8 @@ fn disabled() {
73136
fn manual() {
74137
let f = super::fixture_root().join("tsconfig/cases/project_references");
75138

139+
// The following resolver's `config_file` has defined it's own paths alias which has higher priority
140+
// some cases will work without references
76141
let resolver = Resolver::new(ResolveOptions {
77142
tsconfig: Some(TsconfigOptions {
78143
config_file: f.join("app"),
@@ -81,12 +146,39 @@ fn manual() {
81146
..ResolveOptions::default()
82147
});
83148

149+
// The following resolver's `config_file` has no `paths` alias with `references` enabled
150+
let no_paths_resolver = Resolver 10000 ::new(ResolveOptions {
151+
tsconfig: Some(TsconfigOptions {
152+
config_file: f.join("app/tsconfig.nopaths.json"),
153+
references: TsconfigReferences::Paths(vec!["../project_a/conf.json".into()]),
154+
}),
155+
..ResolveOptions::default()
156+
});
157+
84158
#[rustfmt::skip]
85159
let pass = [
86160
// Test normal paths alias
87161
(f.join("app"), "@/index.ts", Ok(f.join("app/aliased/index.ts"))),
88162
(f.join("app"), "@/../index.ts", Ok(f.join("app/index.ts"))),
89163
// Test project reference
164+
(f.join("project_a"), "@/index.ts", Ok(f.join("app/aliased/index.ts"))),
165+
(f.join("project_b/src"), "@/index.ts", Ok(f.join("app/aliased/index.ts"))),
166+
// Does not have paths alias
167+
(f.join("project_a"), "./index.ts", Ok(f.join("project_a/index.ts"))),
168+
(f.join("project_c"), "./index.ts", Ok(f.join("project_c/index.ts"))),
169+
];
170+
171+
for (path, request, expected) in pass {
172+
let resolved_path = resolver.resolve(&path, request).map(|f| f.full_path());
173+
assert_eq!(resolved_path, expected, "{request} {path:?}");
174+
}
175+
176+
#[rustfmt::skip]
177+
let pass = [
178+
// Test normal paths alias
179+
(f.join("app"), "@/index.ts", Err(ResolveError::NotFound("@/index.ts".into()))),
180+
(f.join("app"), "@/../index.ts", Ok(f.join("app/index.ts"))),
181+
// Test project reference
90182
(f.join("project_a"), "@/index.ts", Ok(f.join("project_a/aliased/index.ts"))),
91183
(f.join("project_b/src"), "@/index.ts", Err(ResolveError::NotFound("@/index.ts".into()))),
92184
// Does not have paths alias
@@ -95,7 +187,7 @@ fn manual() {
95187
];
96188

97189
for (path, request, expected) in pass {
98-
let resolved_path = resolver.resolve(&path, request).map(|f| f.full_path());
190+
let resolved_path = no_paths_resolver.resolve(&path, request).map(|f| f.full_path());
99191< DB5E /td>
assert_eq!(resolved_path, expected, "{request} {path:?}");
100192
}
101193
}

src/tsconfig.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,14 @@ impl TsConfig {
129129
}
130130

131131
pub fn resolve(&self, path: &Path, specifier: &str) -> Vec<PathBuf> {
132-
let paths = self.resolve_path_alias(specifier);
133-
if !paths.is_empty() {
134-
return paths;
135-
}
132+
let mut paths = self.resolve_path_alias(specifier);
136133
for tsconfig in self.references.iter().filter_map(|reference| reference.tsconfig.as_ref()) {
137134
if path.starts_with(tsconfig.base_path()) {
138-
return tsconfig.resolve_path_alias(specifier);
135+
paths = [paths, tsconfig.resolve_path_alias(specifier)].concat();
136+
break;
139137
}
140138
}
141-
vec![]
139+
paths
142140
}
143141

144142
// Copied from parcel

0 commit comments

Comments
 (0)
0