diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2023-09-17 07:50:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-17 07:50:30 +0100 |
commit | fa18878f54c91c8335dd0abe911b1ee3d15c5607 (patch) | |
tree | 6eff8ab0920ee57e4b8382cc2ed4934500e7a559 /cli | |
parent | 3b2e553b05d84ea9de15d27b8e4ef5544e541cb1 (diff) |
fix(lsp): include JSON modules in local import completions (#20536)
Diffstat (limited to 'cli')
-rw-r--r-- | cli/lsp/completions.rs | 13 | ||||
-rw-r--r-- | cli/tools/bench/mod.rs | 4 | ||||
-rw-r--r-- | cli/tools/lint.rs | 4 | ||||
-rw-r--r-- | cli/tools/test/mod.rs | 4 | ||||
-rw-r--r-- | cli/util/path.rs | 72 |
5 files changed, 66 insertions, 31 deletions
diff --git a/cli/lsp/completions.rs b/cli/lsp/completions.rs index d459daee0..7f11daef2 100644 --- a/cli/lsp/completions.rs +++ b/cli/lsp/completions.rs @@ -10,7 +10,7 @@ use super::npm::NpmSearchApi; use super::registries::ModuleRegistry; use super::tsc; -use crate::util::path::is_supported_ext; +use crate::util::path::is_importable_ext; use crate::util::path::relative_specifier; use crate::util::path::specifier_to_file_path; @@ -420,7 +420,7 @@ fn get_local_completions( ..Default::default() }), Ok(file_type) if file_type.is_file() => { - if is_supported_ext(&de.path()) { + if is_importable_ext(&de.path()) { Some(lsp::CompletionItem { label, kind: Some(lsp::CompletionItemKind::FILE), @@ -743,6 +743,8 @@ mod tests { std::fs::write(file_e, b"").expect("could not create"); let file_f = dir_a.join("f.mjs"); std::fs::write(file_f, b"").expect("could not create"); + let file_g = dir_a.join("g.json"); + std::fs::write(file_g, b"").expect("could not create"); let specifier = ModuleSpecifier::from_file_path(file_c).expect("could not create"); let actual = get_local_completions( @@ -761,13 +763,12 @@ mod tests { ); assert!(actual.is_some()); let actual = actual.unwrap(); - assert_eq!(actual.len(), 2); + assert_eq!(actual.len(), 3); for item in actual { match item.text_edit { Some(lsp::CompletionTextEdit::Edit(text_edit)) => { - assert!( - text_edit.new_text == "./f.mjs" || text_edit.new_text == "./b" - ); + assert!(["./b", "./f.mjs", "./g.json"] + .contains(&text_edit.new_text.as_str())); } _ => unreachable!(), } diff --git a/cli/tools/bench/mod.rs b/cli/tools/bench/mod.rs index 732d889eb..1c44c8b3c 100644 --- a/cli/tools/bench/mod.rs +++ b/cli/tools/bench/mod.rs @@ -15,7 +15,7 @@ use crate::tools::test::format_test_error; use crate::tools::test::TestFilter; use crate::util::file_watcher; use crate::util::fs::collect_specifiers; -use crate::util::path::is_supported_ext; +use crate::util::path::is_script_ext; use crate::version::get_user_agent; use crate::worker::CliMainWorkerFactory; @@ -347,7 +347,7 @@ fn is_supported_bench_path(path: &Path) -> bool { (basename.ends_with("_bench") || basename.ends_with(".bench") || basename == "bench") - && is_supported_ext(path) + && is_script_ext(path) } else { false } diff --git a/cli/tools/lint.rs b/cli/tools/lint.rs index 835b91f60..6a308b599 100644 --- a/cli/tools/lint.rs +++ b/cli/tools/lint.rs @@ -13,7 +13,7 @@ use crate::factory::CliFactory; use crate::tools::fmt::run_parallelized; use crate::util::file_watcher; use crate::util::fs::FileCollector; -use crate::util::path::is_supported_ext; +use crate::util::path::is_script_ext; use crate::util::sync::AtomicFlag; use deno_ast::MediaType; use deno_core::anyhow::bail; @@ -195,7 +195,7 @@ async fn lint_files( } fn collect_lint_files(files: &FilesConfig) -> Result<Vec<PathBuf>, AnyError> { - FileCollector::new(is_supported_ext) + FileCollector::new(is_script_ext) .ignore_git_folder() .ignore_node_modules() .ignore_vendor_folder() diff --git a/cli/tools/test/mod.rs b/cli/tools/test/mod.rs index 4426caec2..550ec9f9b 100644 --- a/cli/tools/test/mod.rs +++ b/cli/tools/test/mod.rs @@ -18,7 +18,7 @@ use crate::ops; use crate::util::file_watcher; use crate::util::fs::collect_specifiers; use crate::util::path::get_extension; -use crate::util::path::is_supported_ext; +use crate::util::path::is_script_ext; use crate::util::path::mapped_specifier_for_tsc; use crate::worker::CliMainWorkerFactory; @@ -992,7 +992,7 @@ pub(crate) fn is_supported_test_path(path: &Path) -> bool { (basename.ends_with("_test") || basename.ends_with(".test") || basename == "test") - && is_supported_ext(path) + && is_script_ext(path) } else { false } diff --git a/cli/util/path.rs b/cli/util/path.rs index 2c37d84ef..c15a1ede1 100644 --- a/cli/util/path.rs +++ b/cli/util/path.rs @@ -9,8 +9,8 @@ use deno_ast::ModuleSpecifier; use deno_core::error::uri_error; use deno_core::error::AnyError; -/// Checks if the path has extension Deno supports. -pub fn is_supported_ext(path: &Path) -> bool { +/// Checks if the path has an extension Deno supports for script execution. +pub fn is_script_ext(path: &Path) -> bool { if let Some(ext) = get_extension(path) { matches!( ext.as_str(), @@ -21,6 +21,18 @@ pub fn is_supported_ext(path: &Path) -> bool { } } +/// Checks if the path has an extension Deno supports for importing. +pub fn is_importable_ext(path: &Path) -> bool { + if let Some(ext) = get_extension(path) { + matches!( + ext.as_str(), + "ts" | "tsx" | "js" | "jsx" | "mjs" | "mts" | "cjs" | "cts" | "json" + ) + } else { + false + } +} + /// Get the extension of a file in lowercase. pub fn get_extension(file_path: &Path) -> Option<String> { return file_path @@ -259,23 +271,45 @@ mod test { use super::*; #[test] - fn test_is_supported_ext() { - assert!(!is_supported_ext(Path::new("tests/subdir/redirects"))); - assert!(!is_supported_ext(Path::new("README.md"))); - assert!(is_supported_ext(Path::new("lib/typescript.d.ts"))); - assert!(is_supported_ext(Path::new("testdata/run/001_hello.js"))); - assert!(is_supported_ext(Path::new("testdata/run/002_hello.ts"))); - assert!(is_supported_ext(Path::new("foo.jsx"))); - assert!(is_supported_ext(Path::new("foo.tsx"))); - assert!(is_supported_ext(Path::new("foo.TS"))); - assert!(is_supported_ext(Path::new("foo.TSX"))); - assert!(is_supported_ext(Path::new("foo.JS"))); - assert!(is_supported_ext(Path::new("foo.JSX"))); - assert!(is_supported_ext(Path::new("foo.mjs"))); - assert!(is_supported_ext(Path::new("foo.mts"))); - assert!(is_supported_ext(Path::new("foo.cjs"))); - assert!(is_supported_ext(Path::new("foo.cts"))); - assert!(!is_supported_ext(Path::new("foo.mjsx"))); + fn test_is_script_ext() { + assert!(!is_script_ext(Path::new("tests/subdir/redirects"))); + assert!(!is_script_ext(Path::new("README.md"))); + assert!(is_script_ext(Path::new("lib/typescript.d.ts"))); + assert!(is_script_ext(Path::new("testdata/run/001_hello.js"))); + assert!(is_script_ext(Path::new("testdata/run/002_hello.ts"))); + assert!(is_script_ext(Path::new("foo.jsx"))); + assert!(is_script_ext(Path::new("foo.tsx"))); + assert!(is_script_ext(Path::new("foo.TS"))); + assert!(is_script_ext(Path::new("foo.TSX"))); + assert!(is_script_ext(Path::new("foo.JS"))); + assert!(is_script_ext(Path::new("foo.JSX"))); + assert!(is_script_ext(Path::new("foo.mjs"))); + assert!(is_script_ext(Path::new("foo.mts"))); + assert!(is_script_ext(Path::new("foo.cjs"))); + assert!(is_script_ext(Path::new("foo.cts"))); + assert!(!is_script_ext(Path::new("foo.json"))); + assert!(!is_script_ext(Path::new("foo.mjsx"))); + } + + #[test] + fn test_is_importable_ext() { + assert!(!is_importable_ext(Path::new("tests/subdir/redirects"))); + assert!(!is_importable_ext(Path::new("README.md"))); + assert!(is_importable_ext(Path::new("lib/typescript.d.ts"))); + assert!(is_importable_ext(Path::new("testdata/run/001_hello.js"))); + assert!(is_importable_ext(Path::new("testdata/run/002_hello.ts"))); + assert!(is_importable_ext(Path::new("foo.jsx"))); + assert!(is_importable_ext(Path::new("foo.tsx"))); + assert!(is_importable_ext(Path::new("foo.TS"))); + assert!(is_importable_ext(Path::new("foo.TSX"))); + assert!(is_importable_ext(Path::new("foo.JS"))); + assert!(is_importable_ext(Path::new("foo.JSX"))); + assert!(is_importable_ext(Path::new("foo.mjs"))); + assert!(is_importable_ext(Path::new("foo.mts"))); + assert!(is_importable_ext(Path::new("foo.cjs"))); + assert!(is_importable_ext(Path::new("foo.cts"))); + assert!(is_importable_ext(Path::new("foo.json"))); + assert!(!is_importable_ext(Path::new("foo.mjsx"))); } #[test] |