Skip to content

Commit b6d70f6

Browse files
committed
Merge remote-tracking branch 'oxc' into merge/oxc
2 parents 0995fea + b0224bf commit b6d70f6

File tree

10 files changed

+60
-4
lines changed

10 files changed

+60
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
>
1919
> We also sync with [oxc-resolver] and [rspack-resolver] regularly to keep up with the latest changes:
2020
>
21-
> - `oxc-resolver`: [#15](https://github.com/unrs/unrs-resolver/pull/15), [#49](https://github.com/unrs/unrs-resolver/pull/49), [#62](https://github.com/unrs/unrs-resolver/pull/62) and [#86](https://github.com/unrs/unrs-resolver/pull/86)
21+
> - `oxc-resolver`: [#15](https://github.com/unrs/unrs-resolver/pull/15), [#49](https://github.com/unrs/unrs-resolver/pull/49), [#62](https://github.com/unrs/unrs-resolver/pull/62), [#86](https://github.com/unrs/unrs-resolver/pull/86) and [#94](https://github.com/unrs/unrs-resolver/pull/94)
2222
> - `rspack-resolver`(planned): [#59](https://github.com/unrs/unrs-resolver/issues/59)
2323
>
2424
> Last but not least, we prepare some bug fix PRs first on our side and PR back into upstream projects, and we will keep doing this in the future:

fixtures/misc/package-json-with-bom/index.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@oxc-resolver/test-package-json-with-bom",
3+
"private": true,
4+
"version": "0.0.0",
5+
"main": "index.js",
6+
"type": "module"
7+
}

fixtures/tsconfig/cases/with-bom/bar.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"paths": {
4+
"foo": ["bar.js"]
5+
}
6+
}
7+
}

src/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ impl<C: Cache<Cp = FsCachedPath>> ResolverGeneric<C> {
513513
specifier: &str,
514514
ctx: &mut Ctx,
515515
) -> Result<C::Cp, ResolveError> {
516-
let (_, subpath) = Self::parse_package_specifier(specifier);
516+
let (package_name, subpath) = Self::parse_package_specifier(specifier);
517517
if subpath.is_empty() {
518518
ctx.with_fully_specified(false);
519519
}
@@ -522,7 +522,9 @@ impl<C: Cache<Cp = FsCachedPath>> ResolverGeneric<C> {
522522
return Ok(path);
523523
}
524524
// 6. LOAD_NODE_MODULES(X, dirname(Y))
525-
if let Some(path) = self.load_node_modules(cached_path, specifier, ctx)? {
525+
if let Some(path) =
526+
self.load_node_modules(cached_path, specifier, package_name, subpath, ctx)?
527+
{
526528
return Ok(path);
527529
}
528530
// 7. THROW "not found"
@@ -749,6 +751,8 @@ impl<C: Cache<Cp = FsCachedPath>> ResolverGeneric<C> {
749751
&self,
750752
cached_path: &C::Cp,
751753
specifier: &str,
754+
package_name: &str,
755+
subpath: &str,
752756
ctx: &mut Ctx,
753757
) -> ResolveResult<C::Cp> {
754758
#[cfg(feature = "yarn_pnp")]
@@ -758,7 +762,6 @@ impl<C: Cache<Cp = FsCachedPath>> ResolverGeneric<C> {
758762
}
759763
}
760764

761-
let (package_name, subpath) = Self::parse_package_specifier(specifier);
762765
// 1. let DIRS = NODE_MODULES_PATHS(START)
763766
// 2. for each DIR in DIRS:
764767
for module_name in &self.options.modules {

src/package_json_serde.rs

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ impl PackageJsonSerde {
134134
realpath: PathBuf,
135135
json: &str,
136136
) -> Result<Self, serde_json::Error> {
137+
let json = json.trim_start_matches("\u{feff}"); // strip bom
137138
let mut raw_json: JSONValue = serde_json::from_str(json)?;
138139
let mut package_json = Self::default();
139140

src/tests/tsconfig_paths.rs

+16
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ fn json_with_comments() {
9393
assert_eq!(resolved_path, Ok(f.join("bar.js")));
9494
}
9595

96+
#[test]
97+
fn with_bom() {
98+
let f = super::fixture_root().join("tsconfig/cases/with-bom");
99+
100+
let resolver = Resolver::new(ResolveOptions {
101+
tsconfig: Some(TsconfigOptions {
102+
config_file: f.join("tsconfig.json"),
103+
references: TsconfigReferences::Auto,
104+
}),
105+
..ResolveOptions::default()
106+
});
107+
108+
let resolved_path = resolver.resolve(&f, "foo").map(|f| f.full_path());
109+
assert_eq!(resolved_path, Ok(f.join("bar.js")));
110+
}
111+
96112
#[test]
97113
fn broken() {
98114
let f = super::fixture_root().join("tsconfig");

src/tsconfig_serde.rs

+10
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,20 @@ impl TsConfigSerde {
269269
///
270270
/// * Any error that can be returned by `serde_json::from_str()`.
271271
pub fn parse(root: bool, path: &Path, json: &mut str) -> Result<Self, serde_json::Error> {
272+
let json = trim_start_matches_mut(json, '\u{feff}'); // strip bom
272273
_ = json_strip_comments::strip(json);
273274
let mut tsconfig: Self = serde_json::from_str(json)?;
274275
tsconfig.root = root;
275276
tsconfig.path = path.to_path_buf();
276277
Ok(tsconfig)
277278
}
278279
}
280+
281+
fn trim_start_matches_mut(s: &mut str, pat: char) -> &mut str {
282+
if s.starts_with(pat) {
283+
// trim the prefix
284+
&mut s[pat.len_utf8()..]
285+
} else {
286+
s
287+
}
288+
}

tests/resolve_test.rs

+12
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,15 @@ fn windows_symlinked_longfilename() {
235235
Resolver::new(ResolveOptions::default()).resolve(&path, specifier).map(|r| r.full_path());
236236
assert_eq!(resolution, Ok(module_path));
237237
}
238+
239+
#[test]
240+
fn package_json_with_bom() {
241+
let dir = dir();
242+
let dir = dir.join("fixtures/misc");
243+
assert_eq!(
244+
Resolver::new(ResolveOptions::default())
245+
.resolve(&dir, "./package-json-with-bom")
246+
.map(Resolution::into_path_buf),
247+
Ok(dir.join("package-json-with-bom/index.js"))
248+
);
249+
}

0 commit comments

Comments
 (0)