1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

fix(lsp): strip .js before probing for valid import fix (#24188)

This commit is contained in:
Nayeem Rahman 2024-06-12 22:22:57 +01:00 committed by Nathan Whitaker
parent bf9e6c4df9
commit 5bc78de808
No known key found for this signature in database
2 changed files with 77 additions and 1 deletions

View file

@ -76,7 +76,10 @@ static PREFERRED_FIXES: Lazy<HashMap<&'static str, (u32, bool)>> =
static IMPORT_SPECIFIER_RE: Lazy<Regex> = static IMPORT_SPECIFIER_RE: Lazy<Regex> =
lazy_regex::lazy_regex!(r#"\sfrom\s+["']([^"']*)["']"#); lazy_regex::lazy_regex!(r#"\sfrom\s+["']([^"']*)["']"#);
const SUPPORTED_EXTENSIONS: &[&str] = &[".ts", ".tsx", ".js", ".jsx", ".mjs"]; const SUPPORTED_EXTENSIONS: &[&str] = &[
".ts", ".tsx", ".js", ".jsx", ".mjs", ".mts", ".cjs", ".cts", ".d.ts",
".d.mts", ".d.cts",
];
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DataQuickFixChange { pub struct DataQuickFixChange {
@ -436,6 +439,7 @@ impl<'a> TsResponseImportMapper<'a> {
return Some(specifier); return Some(specifier);
} }
} }
let specifier = specifier.strip_suffix(".js").unwrap_or(specifier);
for ext in SUPPORTED_EXTENSIONS { for ext in SUPPORTED_EXTENSIONS {
let specifier_with_ext = format!("{specifier}{ext}"); let specifier_with_ext = format!("{specifier}{ext}");
if self if self

View file

@ -6071,6 +6071,78 @@ export class DuckConfig {
client.shutdown(); client.shutdown();
} }
#[test]
fn lsp_code_actions_imports_dts() {
let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir();
source_file(
temp_dir.path().join("decl.d.ts"),
"export type SomeType = 1;\n",
);
let mut client = context.new_lsp_command().build();
client.initialize_default();
let diagnostics = client.did_open(json!({
"textDocument": {
"uri": temp_dir.uri().join("file.ts").unwrap(),
"languageId": "typescript",
"version": 1,
"text": r#"
const a: SomeType = 1;
console.log(a);
"#,
}
}));
let res = client.write_request(
"textDocument/codeAction",
json!({
"textDocument": {
"uri": temp_dir.uri().join("file.ts").unwrap(),
},
"range": {
"start": { "line": 1, "character": 17 },
"end": { "line": 1, "character": 25 },
},
"context": {
"diagnostics": diagnostics.all(),
"only": ["quickfix"],
},
}),
);
assert_eq!(
res,
json!([{
"title": "Add import from \"./decl.d.ts\"",
"kind": "quickfix",
"diagnostics": [{
"range": {
"start": { "line": 1, "character": 17 },
"end": { "line": 1, "character": 25 },
},
"severity": 1,
"code": 2304,
"source": "deno-ts",
"message": "Cannot find name 'SomeType'.",
}],
"edit": {
"documentChanges": [{
"textDocument": {
"uri": temp_dir.uri().join("file.ts").unwrap(),
"version": 1,
},
"edits": [{
"range": {
"start": { "line": 0, "character": 0 },
"end": { "line": 0, "character": 0 },
},
"newText": "import { SomeType } from \"./decl.d.ts\";\n",
}],
}],
},
}])
);
client.shutdown();
}
#[test] #[test]
fn lsp_code_actions_refactor() { fn lsp_code_actions_refactor() {
let context = TestContextBuilder::new().use_temp_cwd().build(); let context = TestContextBuilder::new().use_temp_cwd().build();