diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-03-13 21:12:09 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-14 01:12:09 +0000 |
commit | 48ede89f1f192df28cc74822d7bb79b0b4bd0957 (patch) | |
tree | d6dd65cf03152fb0253aba551f7ed016e3b7b09f /core/module_specifier.rs | |
parent | c4771356f27b250e7fdbcede0de5682982720455 (diff) |
refactor(core): resolve_url_or_path and resolve_url_or_path_deprecated (#18170)
This commit changes current "deno_core::resolve_url_or_path" API to
"resolve_url_or_path_deprecated" and adds new "resolve_url_or_path"
API that requires to explicitly pass the directory from which paths
should be resolved to.
Some of the call sites were updated to use the new API, the reminder
of them will be updated in a follow up PR.
Towards landing https://github.com/denoland/deno/pull/15454
Diffstat (limited to 'core/module_specifier.rs')
-rw-r--r-- | core/module_specifier.rs | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/core/module_specifier.rs b/core/module_specifier.rs index 72d693723..6c6dbad95 100644 --- a/core/module_specifier.rs +++ b/core/module_specifier.rs @@ -123,7 +123,7 @@ pub fn resolve_url( /// e.g. 'http:' or 'file:' or 'git+ssh:'. If not, it's interpreted as a /// file path; if it is a relative path it's resolved relative to the current /// working directory. -pub fn resolve_url_or_path( +pub fn resolve_url_or_path_deprecated( specifier: &str, ) -> Result<ModuleSpecifier, ModuleResolutionError> { if specifier_has_uri_scheme(specifier) { @@ -135,9 +135,26 @@ pub fn resolve_url_or_path( } } +/// Takes a string representing either an absolute URL or a file path, +/// as it may be passed to deno as a command line argument. +/// The string is interpreted as a URL if it starts with a valid URI scheme, +/// e.g. 'http:' or 'file:' or 'git+ssh:'. If not, it's interpreted as a +/// file path; if it is a relative path it's resolved relative to passed +/// `current_dir`. +pub fn resolve_url_or_path( + specifier: &str, + current_dir: &Path, +) -> Result<ModuleSpecifier, ModuleResolutionError> { + if specifier_has_uri_scheme(specifier) { + resolve_url(specifier) + } else { + resolve_path(specifier, current_dir) + } +} + /// Converts a string representing a relative or absolute path into a -/// ModuleSpecifier. A relative path is considered relative to the current -/// working directory. +/// ModuleSpecifier. A relative path is considered relative to the passed +/// `current_dir`. pub fn resolve_path( path_str: &str, current_dir: &Path, @@ -344,7 +361,7 @@ mod tests { } #[test] - fn test_resolve_url_or_path() { + fn test_resolve_url_or_path_deprecated() { // Absolute URL. let mut tests: Vec<(&str, String)> = vec![ ( @@ -440,13 +457,15 @@ mod tests { } for (specifier, expected_url) in tests { - let url = resolve_url_or_path(specifier).unwrap().to_string(); + let url = resolve_url_or_path_deprecated(specifier) + .unwrap() + .to_string(); assert_eq!(url, expected_url); } } #[test] - fn test_resolve_url_or_path_error() { + fn test_resolve_url_or_path_deprecated_error() { use url::ParseError::*; use ModuleResolutionError::*; @@ -460,7 +479,7 @@ mod tests { } for (specifier, expected_err) in tests { - let err = resolve_url_or_path(specifier).unwrap_err(); + let err = resolve_url_or_path_deprecated(specifier).unwrap_err(); assert_eq!(err, expected_err); } } |