From c7dabc99eed50fa20cdcafd7c0175ab615da3d50 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 17 Feb 2021 13:47:18 -0500 Subject: Make ModuleSpecifier a type alias, not wrapper struct (#9531) --- cli/lsp/analysis.rs | 21 +++++-------- cli/lsp/diagnostics.rs | 4 +-- cli/lsp/documents.rs | 10 +++---- cli/lsp/language_server.rs | 22 +++++++------- cli/lsp/sources.rs | 74 ++++++++++++++++++---------------------------- cli/lsp/tsc.rs | 54 ++++++++++++++------------------- cli/lsp/utils.rs | 20 +++++-------- 7 files changed, 85 insertions(+), 120 deletions(-) (limited to 'cli/lsp') diff --git a/cli/lsp/analysis.rs b/cli/lsp/analysis.rs index 1824e5298..8a1c56537 100644 --- a/cli/lsp/analysis.rs +++ b/cli/lsp/analysis.rs @@ -211,7 +211,7 @@ pub fn resolve_import( let specifier = if let Some(remapped) = maybe_mapped { remapped } else { - match ModuleSpecifier::resolve_import(specifier, referrer.as_str()) { + match deno_core::resolve_import(specifier, referrer.as_str()) { Ok(resolved) => resolved, Err(err) => { return ResolvedDependency::Err( @@ -220,8 +220,8 @@ pub fn resolve_import( } } }; - let referrer_scheme = referrer.as_url().scheme(); - let specifier_scheme = specifier.as_url().scheme(); + let referrer_scheme = referrer.scheme(); + let specifier_scheme = specifier.scheme(); if referrer_scheme == "https" && specifier_scheme == "http" { return ResolvedDependency::Err(ResolvedDependencyErr::InvalidDowngrade); } @@ -647,6 +647,7 @@ impl CodeActionCollection { #[cfg(test)] mod tests { use super::*; + use deno_core::resolve_url; #[test] fn test_as_lsp_range() { @@ -680,8 +681,7 @@ mod tests { #[test] fn test_analyze_dependencies() { - let specifier = - ModuleSpecifier::resolve_url("file:///a.ts").expect("bad specifier"); + let specifier = resolve_url("file:///a.ts").expect("bad specifier"); let source = r#"import { Application, Context, @@ -703,14 +703,10 @@ mod tests { Some(Dependency { is_dynamic: false, maybe_code: Some(ResolvedDependency::Resolved( - ModuleSpecifier::resolve_url("https://cdn.skypack.dev/react") - .unwrap() + resolve_url("https://cdn.skypack.dev/react").unwrap() )), maybe_type: Some(ResolvedDependency::Resolved( - ModuleSpecifier::resolve_url( - "https://deno.land/x/types/react/index.d.ts" - ) - .unwrap() + resolve_url("https://deno.land/x/types/react/index.d.ts").unwrap() )), maybe_code_specifier_range: Some(Range { start: Position { @@ -729,8 +725,7 @@ mod tests { Some(Dependency { is_dynamic: false, maybe_code: Some(ResolvedDependency::Resolved( - ModuleSpecifier::resolve_url("https://deno.land/x/oak@v6.3.2/mod.ts") - .unwrap() + resolve_url("https://deno.land/x/oak@v6.3.2/mod.ts").unwrap() )), maybe_type: None, maybe_code_specifier_range: Some(Range { diff --git a/cli/lsp/diagnostics.rs b/cli/lsp/diagnostics.rs index 1237ff7a3..8017d7e28 100644 --- a/cli/lsp/diagnostics.rs +++ b/cli/lsp/diagnostics.rs @@ -248,7 +248,7 @@ pub async fn generate_ts_diagnostics( let res = ts_server.request(state_snapshot.clone(), req).await?; let ts_diagnostic_map: TsDiagnostics = serde_json::from_value(res)?; for (specifier_str, ts_diagnostics) in ts_diagnostic_map.iter() { - let specifier = ModuleSpecifier::resolve_url(specifier_str)?; + let specifier = deno_core::resolve_url(specifier_str)?; let version = state_snapshot.documents.version(&specifier); diagnostics.push(( specifier, @@ -295,7 +295,7 @@ pub async fn generate_dependency_diagnostics( } ResolvedDependency::Resolved(specifier) => { if !(state_snapshot.documents.contains_key(&specifier) || sources.contains_key(&specifier)) { - let is_local = specifier.as_url().scheme() == "file"; + let is_local = specifier.scheme() == "file"; let (code, message) = if is_local { (Some(lsp::NumberOrString::String("no-local".to_string())), format!("Unable to load a local module: \"{}\".\n Please check the file path.", specifier)) } else { diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs index 2a9906958..ef9061004 100644 --- a/cli/lsp/documents.rs +++ b/cli/lsp/documents.rs @@ -204,14 +204,14 @@ impl DocumentCache { #[cfg(test)] mod tests { use super::*; + use deno_core::resolve_url; use lspower::lsp; #[test] fn test_document_cache_contains() { let mut document_cache = DocumentCache::default(); - let specifier = ModuleSpecifier::resolve_url("file:///a/b.ts").unwrap(); - let missing_specifier = - ModuleSpecifier::resolve_url("file:///a/c.ts").unwrap(); + let specifier = resolve_url("file:///a/b.ts").unwrap(); + let missing_specifier = resolve_url("file:///a/c.ts").unwrap(); document_cache.open(specifier.clone(), 1, "console.log(\"Hello Deno\");\n"); assert!(document_cache.contains_key(&specifier)); assert!(!document_cache.contains_key(&missing_specifier)); @@ -220,7 +220,7 @@ mod tests { #[test] fn test_document_cache_change() { let mut document_cache = DocumentCache::default(); - let specifier = ModuleSpecifier::resolve_url("file:///a/b.ts").unwrap(); + let specifier = resolve_url("file:///a/b.ts").unwrap(); document_cache.open(specifier.clone(), 1, "console.log(\"Hello deno\");\n"); document_cache .change( @@ -251,7 +251,7 @@ mod tests { #[test] fn test_document_cache_change_utf16() { let mut document_cache = DocumentCache::default(); - let specifier = ModuleSpecifier::resolve_url("file:///a/b.ts").unwrap(); + let specifier = resolve_url("file:///a/b.ts").unwrap(); document_cache.open(specifier.clone(), 1, "console.log(\"Hello 🦕\");\n"); document_cache .change( diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index 1501249e8..8de90607f 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -2,6 +2,7 @@ use deno_core::error::anyhow; use deno_core::error::AnyError; +use deno_core::resolve_url; use deno_core::serde::Deserialize; use deno_core::serde::Serialize; use deno_core::serde_json; @@ -172,7 +173,7 @@ impl Inner { specifier: ModuleSpecifier, ) -> Result { let mark = self.performance.mark("get_line_index"); - let result = if specifier.as_url().scheme() == "asset" { + let result = if specifier.scheme() == "asset" { if let Some(asset) = self.get_asset(&specifier).await? { Ok(asset.line_index) } else { @@ -196,7 +197,7 @@ impl Inner { specifier: &ModuleSpecifier, ) -> Option { let mark = self.performance.mark("get_line_index_sync"); - let maybe_line_index = if specifier.as_url().scheme() == "asset" { + let maybe_line_index = if specifier.scheme() == "asset" { if let Some(Some(asset)) = self.assets.get(specifier) { Some(asset.line_index.clone()) } else { @@ -374,7 +375,7 @@ impl Inner { .cloned(), ); } - let uri = specifier.as_url().clone(); + let uri = specifier.clone(); let version = self.documents.version(&specifier); self .client @@ -1200,7 +1201,7 @@ impl Inner { if let Some(implementations) = maybe_implementations { let mut locations = Vec::new(); for implementation in implementations { - let implementation_specifier = ModuleSpecifier::resolve_url( + let implementation_specifier = resolve_url( &implementation.document_span.file_name, ) .map_err(|err| { @@ -1285,7 +1286,7 @@ impl Inner { if reference.is_definition { continue; } - let reference_specifier = ModuleSpecifier::resolve_url( + let reference_specifier = resolve_url( &reference.document_span.file_name, ) .map_err(|err| { @@ -1439,8 +1440,7 @@ impl Inner { continue; } let reference_specifier = - ModuleSpecifier::resolve_url(&reference.document_span.file_name) - .unwrap(); + resolve_url(&reference.document_span.file_name).unwrap(); // TODO(lucacasonato): handle error correctly let line_index = self.get_line_index(reference_specifier).await.unwrap(); @@ -1585,8 +1585,7 @@ impl Inner { let mut results = Vec::new(); for impl_ in implementations { let document_span = impl_.document_span; - let impl_specifier = - ModuleSpecifier::resolve_url(&document_span.file_name).unwrap(); + let impl_specifier = resolve_url(&document_span.file_name).unwrap(); let impl_line_index = &self.get_line_index(impl_specifier).await.unwrap(); if let Some(link) = document_span.to_link(impl_line_index, self).await { @@ -1981,8 +1980,7 @@ impl Inner { ) -> LspResult> { let mark = self.performance.mark("virtual_text_document"); let specifier = utils::normalize_url(params.text_document.uri); - let url = specifier.as_url(); - let contents = if url.as_str() == "deno:/status.md" { + let contents = if specifier.as_str() == "deno:/status.md" { let mut contents = String::new(); contents.push_str(&format!( @@ -2001,7 +1999,7 @@ impl Inner { } Some(contents) } else { - match url.scheme() { + match specifier.scheme() { "asset" => { if let Some(asset) = self .get_asset(&specifier) diff --git a/cli/lsp/sources.rs b/cli/lsp/sources.rs index 265491895..0cd7b5876 100644 --- a/cli/lsp/sources.rs +++ b/cli/lsp/sources.rs @@ -55,12 +55,12 @@ fn resolve_remote_specifier( http_cache: &HttpCache, redirect_limit: isize, ) -> Option { - let cache_filename = http_cache.get_cache_filename(specifier.as_url())?; + let cache_filename = http_cache.get_cache_filename(specifier)?; if redirect_limit >= 0 && cache_filename.is_file() { let headers = get_remote_headers(&cache_filename)?; if let Some(location) = headers.get("location") { let redirect = - ModuleSpecifier::resolve_import(location, specifier.as_str()).ok()?; + deno_core::resolve_import(location, specifier.as_str()).ok()?; resolve_remote_specifier(&redirect, http_cache, redirect_limit - 1) } else { Some(specifier.clone()) @@ -75,7 +75,7 @@ fn resolve_specifier( redirects: &mut HashMap, http_cache: &HttpCache, ) -> Option { - let scheme = specifier.as_url().scheme(); + let scheme = specifier.scheme(); if !SUPPORTED_SCHEMES.contains(&scheme) { return None; } @@ -83,7 +83,7 @@ fn resolve_specifier( if scheme == "data" { Some(specifier.clone()) } else if scheme == "file" { - let path = specifier.as_url().to_file_path().ok()?; + let path = specifier.to_file_path().ok()?; if path.is_file() { Some(specifier.clone()) } else { @@ -295,15 +295,14 @@ impl Inner { let version = self.calculate_script_version(specifier)?; let path = self.get_path(specifier)?; let bytes = fs::read(path).ok()?; - let scheme = specifier.as_url().scheme(); + let scheme = specifier.scheme(); let (source, media_type, maybe_types) = if scheme == "file" { let maybe_charset = Some(text_encoding::detect_charset(&bytes).to_string()); let source = get_source_from_bytes(bytes, maybe_charset).ok()?; (source, MediaType::from(specifier), None) } else { - let cache_filename = - self.http_cache.get_cache_filename(specifier.as_url())?; + let cache_filename = self.http_cache.get_cache_filename(specifier)?; let headers = get_remote_headers(&cache_filename)?; let maybe_content_type = headers.get("content-type").cloned(); let (media_type, maybe_charset) = @@ -329,12 +328,12 @@ impl Inner { } fn get_path(&mut self, specifier: &ModuleSpecifier) -> Option { - if specifier.as_url().scheme() == "file" { - specifier.as_url().to_file_path().ok() + if specifier.scheme() == "file" { + specifier.to_file_path().ok() } else if let Some(path) = self.remotes.get(&specifier) { Some(path.clone()) } else { - let path = self.http_cache.get_cache_filename(&specifier.as_url())?; + let path = self.http_cache.get_cache_filename(&specifier)?; if path.is_file() { self.remotes.insert(specifier.clone(), path.clone()); Some(path) @@ -436,6 +435,8 @@ impl Inner { #[cfg(test)] mod tests { use super::*; + use deno_core::resolve_path; + use deno_core::resolve_url; use std::env; use tempfile::TempDir; @@ -451,10 +452,8 @@ mod tests { let (sources, _) = setup(); let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); let tests = c.join("tests"); - let specifier = ModuleSpecifier::resolve_path( - &tests.join("001_hello.js").to_string_lossy(), - ) - .unwrap(); + let specifier = + resolve_path(&tests.join("001_hello.js").to_string_lossy()).unwrap(); let actual = sources.get_script_version(&specifier); assert!(actual.is_some()); } @@ -464,10 +463,8 @@ mod tests { let (sources, _) = setup(); let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); let tests = c.join("tests"); - let specifier = ModuleSpecifier::resolve_path( - &tests.join("001_hello.js").to_string_lossy(), - ) - .unwrap(); + let specifier = + resolve_path(&tests.join("001_hello.js").to_string_lossy()).unwrap(); let actual = sources.get_source(&specifier); assert!(actual.is_some()); let actual = actual.unwrap(); @@ -479,10 +476,8 @@ mod tests { let (sources, _) = setup(); let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); let tests = c.join("tests"); - let specifier = ModuleSpecifier::resolve_path( - &tests.join("001_hello.js").to_string_lossy(), - ) - .unwrap(); + let specifier = + resolve_path(&tests.join("001_hello.js").to_string_lossy()).unwrap(); let actual = sources.get_length_utf16(&specifier); assert!(actual.is_some()); let actual = actual.unwrap(); @@ -493,32 +488,25 @@ mod tests { fn test_resolve_dependency_types() { let (sources, location) = setup(); let cache = HttpCache::new(&location); - let specifier_dep = - ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap(); + let specifier_dep = resolve_url("https://deno.land/x/mod.ts").unwrap(); cache .set( - specifier_dep.as_url(), + &specifier_dep, Default::default(), b"export * from \"https://deno.land/x/lib.js\";", ) .unwrap(); - let specifier_code = - ModuleSpecifier::resolve_url("https://deno.land/x/lib.js").unwrap(); + let specifier_code = resolve_url("https://deno.land/x/lib.js").unwrap(); let mut headers_code = HashMap::new(); headers_code .insert("x-typescript-types".to_string(), "./lib.d.ts".to_string()); cache - .set( - specifier_code.as_url(), - headers_code, - b"export const a = 1;", - ) + .set(&specifier_code, headers_code, b"export const a = 1;") .unwrap(); - let specifier_type = - ModuleSpecifier::resolve_url("https://deno.land/x/lib.d.ts").unwrap(); + let specifier_type = resolve_url("https://deno.land/x/lib.d.ts").unwrap(); cache .set( - specifier_type.as_url(), + &specifier_type, Default::default(), b"export const a: number;", ) @@ -532,19 +520,15 @@ mod tests { fn test_resolve_dependency_evil_redirect() { let (sources, location) = setup(); let cache = HttpCache::new(&location); - let evil_specifier = - ModuleSpecifier::resolve_url("https://deno.land/x/evil.ts").unwrap(); + let evil_specifier = resolve_url("https://deno.land/x/evil.ts").unwrap(); let mut evil_headers = HashMap::new(); evil_headers .insert("location".to_string(), "file:///etc/passwd".to_string()); - cache - .set(evil_specifier.as_url(), evil_headers, b"") - .unwrap(); - let remote_specifier = - ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap(); + cache.set(&evil_specifier, evil_headers, b"").unwrap(); + let remote_specifier = resolve_url("https://deno.land/x/mod.ts").unwrap(); cache .set( - remote_specifier.as_url(), + &remote_specifier, Default::default(), b"export * from \"./evil.ts\";", ) @@ -556,8 +540,8 @@ mod tests { #[test] fn test_sources_resolve_specifier_non_supported_schema() { let (sources, _) = setup(); - let specifier = ModuleSpecifier::resolve_url("foo://a/b/c.ts") - .expect("could not create specifier"); + let specifier = + resolve_url("foo://a/b/c.ts").expect("could not create specifier"); let sources = sources.0.lock().unwrap(); let mut redirects = sources.redirects.clone(); let http_cache = sources.http_cache.clone(); diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index 5dc7200b7..9a8dc7b21 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -19,6 +19,7 @@ use deno_core::error::anyhow; use deno_core::error::custom_error; use deno_core::error::AnyError; use deno_core::json_op_sync; +use deno_core::resolve_url; use deno_core::serde::Deserialize; use deno_core::serde::Serialize; use deno_core::serde_json; @@ -113,7 +114,7 @@ impl Default for Assets { .iter() .map(|(k, v)| { let url_str = format!("asset:///{}", k); - let specifier = ModuleSpecifier::resolve_url(&url_str).unwrap(); + let specifier = resolve_url(&url_str).unwrap(); let asset = AssetDocument::new(v); (specifier, Some(asset)) }) @@ -478,8 +479,7 @@ impl DocumentSpan { line_index: &LineIndex, language_server: &mut language_server::Inner, ) -> Option { - let target_specifier = - ModuleSpecifier::resolve_url(&self.file_name).unwrap(); + let target_specifier = resolve_url(&self.file_name).unwrap(); if let Ok(target_line_index) = language_server.get_line_index(target_specifier).await { @@ -615,8 +615,7 @@ impl RenameLocations { HashMap::new(); for location in self.locations.iter() { let uri = utils::normalize_file_name(&location.document_span.file_name)?; - let specifier = - ModuleSpecifier::resolve_url(&location.document_span.file_name)?; + let specifier = resolve_url(&location.document_span.file_name)?; // ensure TextDocumentEdit for `location.file_name`. if text_document_edit_map.get(&uri).is_none() { @@ -778,7 +777,7 @@ impl FileTextChanges { &self, language_server: &mut language_server::Inner, ) -> Result { - let specifier = ModuleSpecifier::resolve_url(&self.file_name)?; + let specifier = resolve_url(&self.file_name)?; let line_index = language_server.get_line_index(specifier.clone()).await?; let edits = self .text_changes @@ -787,7 +786,7 @@ impl FileTextChanges { .collect(); Ok(lsp::TextDocumentEdit { text_document: lsp::OptionalVersionedTextDocumentIdentifier { - uri: specifier.as_url().clone(), + uri: specifier.clone(), version: language_server.document_version(specifier), }, edits, @@ -1063,7 +1062,7 @@ fn cache_snapshot( .snapshots .contains_key(&(specifier.clone().into(), version.clone().into())) { - let s = ModuleSpecifier::resolve_url(&specifier)?; + let s = resolve_url(&specifier)?; let content = state.state_snapshot.documents.content(&s)?.unwrap(); state .snapshots @@ -1156,7 +1155,7 @@ fn get_change_range(state: &mut State, args: Value) -> Result { fn get_length(state: &mut State, args: Value) -> Result { let mark = state.state_snapshot.performance.mark("op_get_length"); let v: SourceSnapshotArgs = serde_json::from_value(args)?; - let specifier = ModuleSpecifier::resolve_url(&v.specifier)?; + let specifier = resolve_url(&v.specifier)?; if let Some(Some(asset)) = state.state_snapshot.assets.get(&specifier) { Ok(json!(asset.length)) } else if state.state_snapshot.documents.contains_key(&specifier) { @@ -1186,7 +1185,7 @@ struct GetTextArgs { fn get_text(state: &mut State, args: Value) -> Result { let mark = state.state_snapshot.performance.mark("op_get_text"); let v: GetTextArgs = serde_json::from_value(args)?; - let specifier = ModuleSpecifier::resolve_url(&v.specifier)?; + let specifier = resolve_url(&v.specifier)?; let content = if let Some(Some(content)) = state.state_snapshot.assets.get(&specifier) { content.text.clone() @@ -1208,7 +1207,7 @@ fn resolve(state: &mut State, args: Value) -> Result { let mark = state.state_snapshot.performance.mark("op_resolve"); let v: ResolveArgs = serde_json::from_value(args)?; let mut resolved = Vec::>::new(); - let referrer = ModuleSpecifier::resolve_url(&v.base)?; + let referrer = resolve_url(&v.base)?; let sources = &mut state.state_snapshot.sources; if state.state_snapshot.documents.contains_key(&referrer) { @@ -1311,8 +1310,8 @@ struct ScriptVersionArgs { fn script_version(state: &mut State, args: Value) -> Result { let mark = state.state_snapshot.performance.mark("op_script_version"); let v: ScriptVersionArgs = serde_json::from_value(args)?; - let specifier = ModuleSpecifier::resolve_url(&v.specifier)?; - if specifier.as_url().scheme() == "asset" { + let specifier = resolve_url(&v.specifier)?; + if specifier.scheme() == "asset" { return if state.state_snapshot.assets.contains_key(&specifier) { Ok(json!("1")) } else { @@ -1673,8 +1672,8 @@ mod tests { fn mock_state_snapshot(sources: Vec<(&str, &str, i32)>) -> StateSnapshot { let mut documents = DocumentCache::default(); for (specifier, content, version) in sources { - let specifier = ModuleSpecifier::resolve_url(specifier) - .expect("failed to create specifier"); + let specifier = + resolve_url(specifier).expect("failed to create specifier"); documents.open(specifier, version, content); } StateSnapshot { @@ -1769,8 +1768,7 @@ mod tests { }), vec![("file:///a.ts", r#"console.log("hello deno");"#, 1)], ); - let specifier = ModuleSpecifier::resolve_url("file:///a.ts") - .expect("could not resolve url"); + let specifier = resolve_url("file:///a.ts").expect("could not resolve url"); let result = request( &mut runtime, state_snapshot, @@ -1815,8 +1813,7 @@ mod tests { }), vec![("file:///a.ts", r#"console.log(document.location);"#, 1)], ); - let specifier = ModuleSpecifier::resolve_url("file:///a.ts") - .expect("could not resolve url"); + let specifier = resolve_url("file:///a.ts").expect("could not resolve url"); let result = request( &mut runtime, state_snapshot, @@ -1849,8 +1846,7 @@ mod tests { 1, )], ); - let specifier = ModuleSpecifier::resolve_url("file:///a.ts") - .expect("could not resolve url"); + let specifier = resolve_url("file:///a.ts").expect("could not resolve url"); let result = request( &mut runtime, state_snapshot, @@ -1879,8 +1875,7 @@ mod tests { 1, )], ); - let specifier = ModuleSpecifier::resolve_url("file:///a.ts") - .expect("could not resolve url"); + let specifier = resolve_url("file:///a.ts").expect("could not resolve url"); let result = request( &mut runtime, state_snapshot, @@ -1933,8 +1928,7 @@ mod tests { 1, )], ); - let specifier = ModuleSpecifier::resolve_url("file:///a.ts") - .expect("could not resolve url"); + let specifier = resolve_url("file:///a.ts").expect("could not resolve url"); let result = request( &mut runtime, state_snapshot, @@ -1970,8 +1964,7 @@ mod tests { 1, )], ); - let specifier = ModuleSpecifier::resolve_url("file:///a.ts") - .expect("could not resolve url"); + let specifier = resolve_url("file:///a.ts").expect("could not resolve url"); let result = request( &mut runtime, state_snapshot, @@ -2028,8 +2021,7 @@ mod tests { }), vec![("file:///a.ts", r#"const url = new URL("b.js", import."#, 1)], ); - let specifier = ModuleSpecifier::resolve_url("file:///a.ts") - .expect("could not resolve url"); + let specifier = resolve_url("file:///a.ts").expect("could not resolve url"); let result = request( &mut runtime, state_snapshot, @@ -2052,8 +2044,8 @@ mod tests { }), vec![], ); - let specifier = ModuleSpecifier::resolve_url("asset:///lib.esnext.d.ts") - .expect("could not resolve url"); + let specifier = + resolve_url("asset:///lib.esnext.d.ts").expect("could not resolve url"); let result = request( &mut runtime, state_snapshot, diff --git a/cli/lsp/utils.rs b/cli/lsp/utils.rs index 8f4de9c05..c7eae3147 100644 --- a/cli/lsp/utils.rs +++ b/cli/lsp/utils.rs @@ -1,6 +1,7 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use deno_core::error::AnyError; +use deno_core::resolve_url; use deno_core::url::Position; use deno_core::url::Url; use deno_core::ModuleSpecifier; @@ -19,12 +20,11 @@ pub fn normalize_file_name(file_name: &str) -> Result { pub fn normalize_specifier( specifier: &ModuleSpecifier, ) -> Result { - let url = specifier.as_url(); - if url.scheme() == "file" { - Ok(url.clone()) + if specifier.scheme() == "file" { + Ok(specifier.clone()) } else { let specifier_str = - format!("deno:///{}", url.as_str().replacen("://", "/", 1)); + format!("deno:///{}", specifier.as_str().replacen("://", "/", 1)); Url::parse(&specifier_str).map_err(|err| err.into()) } } @@ -41,12 +41,12 @@ pub fn normalize_url(url: Url) -> ModuleSpecifier { if let Ok(specifier) = percent_encoding::percent_decode_str(&specifier_str).decode_utf8() { - if let Ok(specifier) = ModuleSpecifier::resolve_url(&specifier) { + if let Ok(specifier) = resolve_url(&specifier) { return specifier; } } } - ModuleSpecifier::from(url) + url } #[cfg(test)] @@ -63,8 +63,7 @@ mod tests { #[test] fn test_normalize_specifier() { - let fixture = - ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap(); + let fixture = resolve_url("https://deno.land/x/mod.ts").unwrap(); let actual = normalize_specifier(&fixture).unwrap(); let expected = Url::parse("deno:///https/deno.land/x/mod.ts").unwrap(); assert_eq!(actual, expected); @@ -74,9 +73,6 @@ mod tests { fn test_normalize_url() { let fixture = Url::parse("deno:///https/deno.land/x/mod.ts").unwrap(); let actual = normalize_url(fixture); - assert_eq!( - actual, - ModuleSpecifier::resolve_url("https://deno.land/x/mod.ts").unwrap() - ); + assert_eq!(actual, resolve_url("https://deno.land/x/mod.ts").unwrap()); } } -- cgit v1.2.3