mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(resolver): make Resolution::full_path not owned
This commit is contained in:
parent
91fd375a3b
commit
7c5ff9e27d
13 changed files with 37 additions and 46 deletions
|
|
@ -34,8 +34,8 @@ impl Resolution {
|
|||
}
|
||||
|
||||
/// Returns the full path with query and fragment
|
||||
pub fn full_path(self) -> PathBuf {
|
||||
let mut path = self.path.into_os_string();
|
||||
pub fn full_path(&self) -> PathBuf {
|
||||
let mut path = self.path.clone().into_os_string();
|
||||
if let Some(query) = &self.query {
|
||||
path.push(query);
|
||||
}
|
||||
|
|
@ -56,6 +56,6 @@ fn test() {
|
|||
assert_eq!(resolution.path(), Path::new("foo"));
|
||||
assert_eq!(resolution.query(), Some("?query"));
|
||||
assert_eq!(resolution.fragment(), Some("#fragment"));
|
||||
assert_eq!(resolution.clone().full_path(), PathBuf::from("foo?query#fragment"));
|
||||
assert_eq!(resolution.full_path(), PathBuf::from("foo?query#fragment"));
|
||||
assert_eq!(resolution.into_path_buf(), PathBuf::from("foo"));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::{AliasValue, Resolution, ResolveError, ResolveOptions, Resolver, ResolverGeneric};
|
||||
use crate::{AliasValue, ResolveError, ResolveOptions, Resolver, ResolverGeneric};
|
||||
|
||||
use super::memory_fs::MemoryFS;
|
||||
|
||||
|
|
@ -91,7 +91,7 @@ fn alias() {
|
|||
];
|
||||
|
||||
for (comment, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(f, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(f, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(PathBuf::from(expected)), "{comment} {request}");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{Resolution, ResolveError, ResolveOptions, Resolver};
|
||||
use crate::{ResolveError, ResolveOptions, Resolver};
|
||||
|
||||
fn fixture() -> PathBuf {
|
||||
super::fixture().join("browser-module")
|
||||
|
|
@ -59,7 +59,7 @@ fn replace_file() {
|
|||
];
|
||||
|
||||
for (comment, path, request, expected) in data {
|
||||
let resolved_path = resolver.resolve(&path, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&path, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,7 @@
|
|||
//!
|
||||
//! The huge exports field test cases are at the bottom of this file.
|
||||
|
||||
use crate::{
|
||||
ExportsField, PathUtil, Resolution, ResolveContext, ResolveError, ResolveOptions, Resolver,
|
||||
};
|
||||
use crate::{ExportsField, PathUtil, ResolveContext, ResolveError, ResolveOptions, Resolver};
|
||||
use serde_json::json;
|
||||
use std::path::Path;
|
||||
|
||||
|
|
@ -51,7 +49,7 @@ fn test() {
|
|||
// * should log the correct info
|
||||
|
||||
for (comment, path, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(&path, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&path, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
|
||||
}
|
||||
|
||||
|
|
@ -90,8 +88,7 @@ fn exports_not_browser_field1() {
|
|||
..ResolveOptions::default()
|
||||
});
|
||||
|
||||
let resolved_path =
|
||||
resolver.resolve(&f, "exports-field/dist/main.js").map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&f, "exports-field/dist/main.js").map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(f.join("node_modules/exports-field/lib/lib2/main.js")));
|
||||
}
|
||||
|
||||
|
|
@ -107,8 +104,7 @@ fn exports_not_browser_field2() {
|
|||
..ResolveOptions::default()
|
||||
});
|
||||
|
||||
let resolved_path =
|
||||
resolver.resolve(&f2, "exports-field/dist/main.js").map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&f2, "exports-field/dist/main.js").map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(f2.join("node_modules/exports-field/lib/browser.js")));
|
||||
}
|
||||
|
||||
|
|
@ -124,7 +120,7 @@ fn extension_without_fully_specified() {
|
|||
});
|
||||
|
||||
let resolved_path =
|
||||
commonjs_resolver.resolve(&f2, "exports-field/dist/main").map(Resolution::full_path);
|
||||
commonjs_resolver.resolve(&f2, "exports-field/dist/main").map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(f2.join("node_modules/exports-field/lib/lib2/main.js")));
|
||||
}
|
||||
|
||||
|
|
@ -151,7 +147,7 @@ fn extension_alias_1_2() {
|
|||
];
|
||||
|
||||
for (comment, path, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(&path, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&path, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
|
||||
}
|
||||
}
|
||||
|
|
@ -177,7 +173,7 @@ fn extension_alias_3() {
|
|||
];
|
||||
|
||||
for (comment, path, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(&path, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&path, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{Resolution, ResolveError, ResolveOptions, Resolver};
|
||||
use crate::{ResolveError, ResolveOptions, Resolver};
|
||||
|
||||
fn fixture() -> PathBuf {
|
||||
super::fixture().join("extension-alias")
|
||||
|
|
@ -31,7 +31,7 @@ fn extension_alias() {
|
|||
];
|
||||
|
||||
for (comment, path, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(&path, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&path, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
|
||||
}
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ fn not_apply_to_extension_nor_main_files() {
|
|||
];
|
||||
|
||||
for (comment, path, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(&path, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&path, request).map(|r| r.full_path());
|
||||
let expected = f.join(expected);
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ fn extensions() {
|
|||
];
|
||||
|
||||
for (comment, request, expected_path) in pass {
|
||||
let resolved_path = resolver.resolve(&f, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&f, request).map(|r| r.full_path());
|
||||
let expected = f.join(expected_path);
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {request} {expected_path}");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::{AliasValue, Resolution, ResolveError, ResolveOptions, ResolverGeneric};
|
||||
use crate::{AliasValue, ResolveError, ResolveOptions, ResolverGeneric};
|
||||
|
||||
use super::memory_fs::MemoryFS;
|
||||
|
||||
|
|
@ -84,7 +84,7 @@ fn fallback() {
|
|||
];
|
||||
|
||||
for (comment, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(f, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(f, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(PathBuf::from(expected)), "{comment} {request}");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ fn file_system() -> MemoryFS {
|
|||
#[test]
|
||||
#[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows.
|
||||
fn test() {
|
||||
use crate::Resolution;
|
||||
let file_system = file_system();
|
||||
|
||||
let resolver = ResolverGeneric::<MemoryFS>::new_with_file_system(
|
||||
|
|
@ -70,7 +69,7 @@ fn test() {
|
|||
];
|
||||
|
||||
for (comment, request, expected) in successful_resolves {
|
||||
let resolution = resolver.resolve("/a", request).map(Resolution::full_path);
|
||||
let resolution = resolver.resolve("/a", request).map(|r| r.full_path());
|
||||
assert_eq!(resolution, Ok(PathBuf::from(expected)), "{comment} {request}");
|
||||
}
|
||||
}
|
||||
|
|
@ -78,8 +77,6 @@ fn test() {
|
|||
#[test]
|
||||
#[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows.
|
||||
fn resolve_to_context() {
|
||||
use crate::Resolution;
|
||||
|
||||
let file_system = file_system();
|
||||
|
||||
let resolver = ResolverGeneric::<MemoryFS>::new_with_file_system(
|
||||
|
|
@ -108,7 +105,7 @@ fn resolve_to_context() {
|
|||
];
|
||||
|
||||
for (comment, request, expected) in successful_resolves {
|
||||
let resolution = resolver.resolve("/a", request).map(Resolution::full_path);
|
||||
let resolution = resolver.resolve("/a", request).map(|r| r.full_path());
|
||||
assert_eq!(resolution, Ok(PathBuf::from(expected)), "{comment} {request}");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,9 +4,7 @@
|
|||
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{
|
||||
MatchObject, PathUtil, Resolution, ResolveContext, ResolveError, ResolveOptions, Resolver,
|
||||
};
|
||||
use crate::{MatchObject, PathUtil, ResolveContext, ResolveError, ResolveOptions, Resolver};
|
||||
use std::path::Path;
|
||||
|
||||
#[test]
|
||||
|
|
@ -32,7 +30,7 @@ fn test() {
|
|||
];
|
||||
|
||||
for (comment, path, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(&path, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&path, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
//! <https://github.com/webpack/enhanced-resolve/blob/main/test/resolve.test.js>
|
||||
|
||||
use crate::{Resolution, ResolveOptions, Resolver};
|
||||
use crate::{ResolveOptions, Resolver};
|
||||
|
||||
#[test]
|
||||
fn resolve() {
|
||||
|
|
@ -43,7 +43,7 @@ fn resolve() {
|
|||
];
|
||||
|
||||
for (comment, path, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(&path, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&path, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
|
||||
}
|
||||
}
|
||||
|
|
@ -65,7 +65,7 @@ fn prefer_relative() {
|
|||
];
|
||||
|
||||
for (comment, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(&f, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&f, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {request}");
|
||||
}
|
||||
}
|
||||
|
|
@ -85,7 +85,7 @@ fn resolve_context() {
|
|||
];
|
||||
|
||||
for (comment, path, request, expected) in data {
|
||||
let resolved_path = resolver.resolve(&path, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&path, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use std::{env, path::PathBuf};
|
||||
|
||||
use crate::{AliasValue, Resolution, ResolveError, ResolveOptions, Resolver};
|
||||
use crate::{AliasValue, ResolveError, ResolveOptions, Resolver};
|
||||
|
||||
fn dirname() -> PathBuf {
|
||||
env::current_dir().unwrap().join("tests/enhanced_resolve/test")
|
||||
|
|
@ -29,7 +29,7 @@ fn roots() {
|
|||
];
|
||||
|
||||
for (comment, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(&f, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&f, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {request}");
|
||||
}
|
||||
|
||||
|
|
@ -52,7 +52,7 @@ fn resolve_to_context() {
|
|||
resolve_to_context: true,
|
||||
..ResolveOptions::default()
|
||||
});
|
||||
let resolved_path = resolver.resolve(&f, "/fixtures/lib").map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&f, "/fixtures/lib").map(|r| r.full_path());
|
||||
let expected = f.join("lib");
|
||||
assert_eq!(resolved_path, Ok(expected));
|
||||
}
|
||||
|
|
@ -75,7 +75,7 @@ fn prefer_absolute() {
|
|||
];
|
||||
|
||||
for (comment, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(&f, &request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&f, &request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {request}");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{Resolution, ResolveOptions, Resolver};
|
||||
use crate::{ResolveOptions, Resolver};
|
||||
|
||||
fn fixture() -> PathBuf {
|
||||
super::fixture().join("scoped")
|
||||
|
|
@ -25,7 +25,7 @@ fn scoped_packages() {
|
|||
];
|
||||
|
||||
for (comment, path, request, expected) in pass {
|
||||
let resolved_path = resolver.resolve(&f, request).map(Resolution::full_path);
|
||||
let resolved_path = resolver.resolve(&f, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use std::{env, fs, io, path::Path};
|
||||
|
||||
use crate::{Resolution, ResolveOptions, Resolver};
|
||||
use crate::{ResolveOptions, Resolver};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum FileType {
|
||||
|
|
@ -113,12 +113,12 @@ fn test() -> io::Result<()> {
|
|||
|err| {
|
||||
panic!("{err:?} {comment} {path:?} {request}");
|
||||
},
|
||||
Resolution::full_path,
|
||||
|r| r.full_path(),
|
||||
);
|
||||
assert_eq!(filename, root.join("lib/index.js"));
|
||||
|
||||
let resolved_path =
|
||||
resolver_without_symlinks.resolve(&path, request).map(Resolution::full_path);
|
||||
resolver_without_symlinks.resolve(&path, request).map(|r| r.full_path());
|
||||
assert_eq!(resolved_path, Ok(path.join(request)));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue