Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkirsz committed Jul 3, 2023
1 parent 4f8ad69 commit f5b8530
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 114 deletions.
104 changes: 60 additions & 44 deletions crates/turbo-tasks-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use turbo_tasks::{
};
use turbo_tasks_hash::hash_xxh3_hash64;
use util::{extract_disk_access, join_path, normalize_path, sys_to_unix, unix_to_sys};
pub use virtual_fs::VirtualFileSystemVc;

use self::{invalidation::WatchStart, json::UnparseableJson, mutex_map::MutexMap};
use crate::{
Expand Down Expand Up @@ -929,17 +930,57 @@ impl FileSystemPath {
/// Returns the final component of the FileSystemPath, or an empty string
/// for the root path.
pub fn file_name(&self) -> &str {
// rsplit will always give at least one item
self.path.rsplit('/').next().unwrap()
let (_, file_name) = self.split_file_name();
file_name
}

pub fn extension(&self) -> Option<&str> {
if let Some((_, ext)) = self.path.rsplit_once('.') {
if !ext.contains('/') {
return Some(ext);
let (_, extension) = self.split_extension();
extension
}

/// Splits the path into two components:
/// 1. The path without the extension;
/// 2. The extension, if any.
fn split_extension(&self) -> (&str, Option<&str>) {
if let Some((path_before_extension, extension)) = self.path.rsplit_once('.') {
if extension.contains('/') ||
// The file name begins with a `.` and has no other `.`s within.
path_before_extension.ends_with('/') || path_before_extension.is_empty()
{
(self.path.as_str(), None)
} else {
(path_before_extension, Some(extension))
}
} else {
(self.path.as_str(), None)
}
}

/// Splits the path into two components:
/// 1. The parent directory, if any;
/// 2. The file name;
fn split_file_name(&self) -> (Option<&str>, &str) {
// Since the path is normalized, we know `parent`, if any, must not be empty.
if let Some((parent, file_name)) = self.path.rsplit_once('/') {
(Some(parent), file_name)
} else {
(None, self.path.as_str())
}
}

/// Splits the path into three components:
/// 1. The parent directory, if any;
/// 2. The file stem;
/// 3. The extension, if any.
fn split_file_stem_extension(&self) -> (Option<&str>, &str, Option<&str>) {
let (path_before_extension, extension) = self.split_extension();

if let Some((parent, file_stem)) = path_before_extension.rsplit_once('/') {
(Some(parent), file_stem, extension)
} else {
(None, path_before_extension, extension)
}
None
}
}

Expand Down Expand Up @@ -1015,15 +1056,11 @@ impl FileSystemPathVc {
appending
)
}
if let Some((path, ext)) = this.path.rsplit_once('.') {
// check if `ext` is a real extension, and not a "." in a directory name or a
// .dotfile
if !(ext.contains('/') || (path.ends_with('/') && !path.is_empty())) {
return Ok(Self::new_normalized(
this.fs,
format!("{}{}.{}", path, appending, ext),
));
}
if let (path, Some(ext)) = this.split_extension() {
return Ok(Self::new_normalized(
this.fs,
format!("{}{}.{}", path, appending, ext),
));
}
Ok(Self::new_normalized(
this.fs,
Expand Down Expand Up @@ -1096,16 +1133,10 @@ impl FileSystemPathVc {
#[turbo_tasks::function]
pub async fn with_extension(self, extension: &str) -> Result<FileSystemPathVc> {
let this = self.await?;
let path_without_extension =
if let Some((path_without_extension, old_extension)) = this.path.rsplit_once('.') {
if old_extension.contains('/') {
this.path.as_str()
} else {
path_without_extension
}
} else {
this.path.as_str()
};
let path_without_extension = match this.split_extension() {
(path_without_extension, Some(_)) => path_without_extension,
(path_without_extension, None) => path_without_extension,
};
Ok(Self::new_normalized(
this.fs,
// Like `Path::with_extension` and `PathBuf::set_extension`, if the extension is empty,
Expand All @@ -1129,20 +1160,9 @@ impl FileSystemPathVc {
#[turbo_tasks::function]
pub async fn file_stem(self) -> Result<OptionStringVc> {
let this = self.await?;
if this.path.is_empty() {
return Ok(OptionStringVc::cell(None));
}
let file_name = if let Some((_prefix_path, file_name)) = this.path.rsplit_once('/') {
file_name
} else {
this.path.as_str()
};
let Some((file_stem, _extension)) = file_name.rsplit_once('.') else {
return Ok(OptionStringVc::cell(Some(file_name.to_string())));
};
let (_, file_stem, _) = this.split_file_stem_extension();
if file_stem.is_empty() {
// The file name begins with a `.` and has no other `.`s within.
return Ok(OptionStringVc::cell(Some(file_name.to_string())));
return Ok(OptionStringVc::cell(None));
}
Ok(OptionStringVc::cell(Some(file_stem.to_string())))
}
Expand Down Expand Up @@ -1270,12 +1290,8 @@ impl FileSystemPathVc {
Ok(FileSystemEntryTypeVc::cell(FileSystemEntryType::NotFound))
}
DirectoryContent::Entries(entries) => {
let basename = if let Some((_, basename)) = this.path.rsplit_once('/') {
basename
} else {
&this.path
};
if let Some(entry) = entries.get(basename) {
let (_, file_name) = this.split_file_name();
if let Some(entry) = entries.get(file_name) {
Ok(FileSystemEntryTypeVc::cell(entry.into()))
} else {
Ok(FileSystemEntryTypeVc::cell(FileSystemEntryType::NotFound))
Expand Down
9 changes: 7 additions & 2 deletions crates/turbo-tasks-fs/src/virtual_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ use super::{
#[turbo_tasks::value]
pub struct VirtualFileSystem;

#[turbo_tasks::value_impl]
impl VirtualFileSystemVc {
#[turbo_tasks::function]
/// Creates a new [`VirtualFileSystemVc`].
///
/// NOTE: This function is not a `turbo_tasks::function` to avoid instances
/// being equivalent identity-wise. This ensures that a [`FileSystemPathVc`]
/// created from this [`VirtualFileSystemVc`] will never be equivalent,
/// nor be interoperable, with a [`FileSystemPathVc`] created from another
/// [`VirtualFileSystemVc`].
pub fn new() -> Self {
Self::cell(VirtualFileSystem)
}
Expand Down
5 changes: 4 additions & 1 deletion crates/turbopack-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ pub mod target;
mod utils;
pub mod version;
pub mod virtual_asset;
pub mod virtual_fs;

pub mod virtual_fs {
pub use turbo_tasks_fs::VirtualFileSystemVc;
}

pub const PROJECT_FILESYSTEM_NAME: &str = "project";
pub const SOURCE_MAP_ROOT_NAME: &str = "turbopack";
Expand Down
67 changes: 0 additions & 67 deletions crates/turbopack-core/src/virtual_fs.rs

This file was deleted.

0 comments on commit f5b8530

Please sign in to comment.