diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2020-08-13 02:04:17 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-12 16:34:17 -0400 |
commit | ad4af23aba39e904ba9e2c284b6d06436b7b4f77 (patch) | |
tree | 184f6a25d39ab0ef637f3f447a378f2a6029109a /core/module_specifier.rs | |
parent | c4d9c6aa4baed5c9f4844dd5f28475e9ef2d87b0 (diff) |
unify path normalization utility (#6865)
Diffstat (limited to 'core/module_specifier.rs')
-rw-r--r-- | core/module_specifier.rs | 37 |
1 files changed, 2 insertions, 35 deletions
diff --git a/core/module_specifier.rs b/core/module_specifier.rs index 1ad4bcc93..e5a0413ca 100644 --- a/core/module_specifier.rs +++ b/core/module_specifier.rs @@ -1,8 +1,7 @@ +use crate::normalize_path; use std::env::current_dir; use std::error::Error; use std::fmt; -use std::path::Component; -use std::path::Path; use std::path::PathBuf; use url::ParseError; use url::Url; @@ -206,42 +205,10 @@ impl PartialEq<String> for ModuleSpecifier { } } -/// Normalize all itermediate components of the path (ie. remove "./" and "../" components). -/// Similar to `fs::canonicalize()` but doesn't resolve symlinks. -/// -/// Taken from Cargo -/// https://github.com/rust-lang/cargo/blob/af307a38c20a753ec60f0ad18be5abed3db3c9ac/src/cargo/util/paths.rs#L60-L85 -pub fn normalize_path(path: &Path) -> PathBuf { - let mut components = path.components().peekable(); - let mut ret = - if let Some(c @ Component::Prefix(..)) = components.peek().cloned() { - components.next(); - PathBuf::from(c.as_os_str()) - } else { - PathBuf::new() - }; - - for component in components { - match component { - Component::Prefix(..) => unreachable!(), - Component::RootDir => { - ret.push(component.as_os_str()); - } - Component::CurDir => {} - Component::ParentDir => { - ret.pop(); - } - Component::Normal(c) => { - ret.push(c); - } - } - } - ret -} - #[cfg(test)] mod tests { use super::*; + use std::path::Path; #[test] fn test_resolve_import() { |