summaryrefslogtreecommitdiff
path: root/cli/util/path.rs
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2023-09-17 07:50:30 +0100
committerGitHub <noreply@github.com>2023-09-17 07:50:30 +0100
commitfa18878f54c91c8335dd0abe911b1ee3d15c5607 (patch)
tree6eff8ab0920ee57e4b8382cc2ed4934500e7a559 /cli/util/path.rs
parent3b2e553b05d84ea9de15d27b8e4ef5544e541cb1 (diff)
fix(lsp): include JSON modules in local import completions (#20536)
Diffstat (limited to 'cli/util/path.rs')
-rw-r--r--cli/util/path.rs72
1 files changed, 53 insertions, 19 deletions
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]