mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
feat(resolver): check for directory before loading a directory (#590)
This commit is contained in:
parent
53242c0e51
commit
c0d06c5598
4 changed files with 32 additions and 7 deletions
|
|
@ -26,9 +26,16 @@ impl<Fs: FileSystem> Cache<Fs> {
|
|||
}
|
||||
|
||||
pub fn is_file(&self, path: &Path) -> bool {
|
||||
self.cache_value(path).meta(&self.fs).is_some_and(|m| m.is_file)
|
||||
self.cache_value(path).is_file(&self.fs)
|
||||
}
|
||||
|
||||
pub fn is_dir(&self, path: &Path) -> bool {
|
||||
self.cache_value(path).is_dir(&self.fs)
|
||||
}
|
||||
|
||||
/// # Panics
|
||||
///
|
||||
/// * Path is file but does not have a parent
|
||||
pub fn dirname(&self, path: &Path) -> PathBuf {
|
||||
(if self.is_file(path) { path.parent().unwrap() } else { path }).to_path_buf()
|
||||
}
|
||||
|
|
@ -97,6 +104,14 @@ impl CacheValue {
|
|||
*self.meta.get_or_init(|| fs.metadata(&self.path).ok())
|
||||
}
|
||||
|
||||
fn is_file<Fs: FileSystem>(&self, fs: &Fs) -> bool {
|
||||
self.meta(fs).is_some_and(|meta| meta.is_file)
|
||||
}
|
||||
|
||||
fn is_dir<Fs: FileSystem>(&self, fs: &Fs) -> bool {
|
||||
self.meta(fs).is_some_and(|meta| meta.is_dir)
|
||||
}
|
||||
|
||||
fn symlink<Fs: FileSystem>(&self, fs: &Fs) -> Option<PathBuf> {
|
||||
self.symlink.get_or_init(|| fs.canonicalize(&self.path).ok()).clone()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,11 +37,18 @@ pub trait FileSystem: Default + Send + Sync {
|
|||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct FileMetadata {
|
||||
pub(crate) is_file: bool,
|
||||
pub(crate) is_dir: bool,
|
||||
}
|
||||
|
||||
impl FileMetadata {
|
||||
pub fn new(is_file: bool) -> Self {
|
||||
Self { is_file }
|
||||
pub fn new(is_file: bool, is_dir: bool) -> Self {
|
||||
Self { is_file, is_dir }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<fs::Metadata> for FileMetadata {
|
||||
fn from(metadata: fs::Metadata) -> Self {
|
||||
Self::new(metadata.is_file(), metadata.is_dir())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -55,7 +62,7 @@ impl FileSystem for FileSystemOs {
|
|||
}
|
||||
|
||||
fn metadata<P: AsRef<Path>>(&self, path: P) -> io::Result<FileMetadata> {
|
||||
fs::metadata(path).map(|metadata| FileMetadata { is_file: metadata.is_file() })
|
||||
fs::metadata(path).map(FileMetadata::from)
|
||||
}
|
||||
|
||||
fn canonicalize<P: AsRef<Path>>(&self, path: P) -> io::Result<PathBuf> {
|
||||
|
|
|
|||
|
|
@ -268,8 +268,10 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
|
|||
}
|
||||
}
|
||||
// c. LOAD_AS_DIRECTORY(DIR/X)
|
||||
if let Some(path) = self.load_as_directory(&node_module_file)? {
|
||||
return Ok(Some(path));
|
||||
if self.cache.is_dir(&node_module_file) {
|
||||
if let Some(path) = self.load_as_directory(&node_module_file)? {
|
||||
return Ok(Some(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
|
|
|
|||
|
|
@ -58,7 +58,8 @@ impl FileSystem for MemoryFS {
|
|||
.metadata(path.as_ref().to_string_lossy().as_ref())
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::NotFound, err))?;
|
||||
let is_file = metadata.file_type == vfs::VfsFileType::File;
|
||||
Ok(FileMetadata::new(is_file))
|
||||
let is_dir = metadata.file_type == vfs::VfsFileType::Directory;
|
||||
Ok(FileMetadata::new(is_file, is_dir))
|
||||
}
|
||||
|
||||
fn canonicalize<P: AsRef<Path>>(&self, path: P) -> io::Result<PathBuf> {
|
||||
|
|
|
|||
Loading…
Reference in a new issue