fix(lsp): include JSON modules in local import completions (#20536)

This commit is contained in:
Nayeem Rahman 2023-09-17 07:50:30 +01:00 committed by GitHub
parent 3b2e553b05
commit fa18878f54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 31 deletions

View file

@ -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!(),
}

View file

@ -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
}

View file

@ -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()

View file

@ -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
}

View file

@ -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]