Skip to content

Commit 4eff1e8

Browse files
authored
fix(lsp): import map lookup for jsr subpath auto import (#25025)
1 parent 3a3315c commit 4eff1e8

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

cli/lsp/analysis.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,16 @@ impl<'a> TsResponseImportMapper<'a> {
312312
if let Some(result) = import_map.lookup(&specifier, referrer) {
313313
return Some(result);
314314
}
315+
if let Some(req_ref_str) = specifier.as_str().strip_prefix("jsr:") {
316+
if !req_ref_str.starts_with('/') {
317+
let specifier_str = format!("jsr:/{req_ref_str}");
318+
if let Ok(specifier) = ModuleSpecifier::parse(&specifier_str) {
319+
if let Some(result) = import_map.lookup(&specifier, referrer) {
320+
return Some(result);
321+
}
322+
}
323+
}
324+
}
315325
}
316326
return Some(spec_str);
317327
}

tests/integration/lsp_tests.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5482,6 +5482,80 @@ fn lsp_jsr_auto_import_completion_import_map() {
54825482
client.shutdown();
54835483
}
54845484

5485+
#[test]
5486+
fn lsp_jsr_auto_import_completion_import_map_sub_path() {
5487+
let context = TestContextBuilder::new()
5488+
.use_http_server()
5489+
.use_temp_cwd()
5490+
.build();
5491+
let temp_dir = context.temp_dir();
5492+
temp_dir.write(
5493+
"deno.json",
5494+
json!({
5495+
"imports": {
5496+
"@std/path": "jsr:@std/path@^0.220.1",
5497+
},
5498+
})
5499+
.to_string(),
5500+
);
5501+
let file = source_file(
5502+
temp_dir.path().join("file.ts"),
5503+
r#"
5504+
// Adds jsr:@std/path@^0.220.1/normalize to the module graph.
5505+
import "jsr:@std/url@^0.220.1/normalize";
5506+
normalize
5507+
"#,
5508+
);
5509+
let mut client = context.new_lsp_command().build();
5510+
client.initialize_default();
5511+
client.write_request(
5512+
"workspace/executeCommand",
5513+
json!({
5514+
"command": "deno.cache",
5515+
"arguments": [[], file.uri()],
5516+
}),
5517+
);
5518+
client.read_diagnostics();
5519+
client.did_open_file(&file);
5520+
let list = client.get_completion_list(
5521+
file.uri(),
5522+
(3, 15),
5523+
json!({ "triggerKind": 1 }),
5524+
);
5525+
let item = list
5526+
.items
5527+
.iter()
5528+
.find(|i| {
5529+
i.label == "normalize"
5530+
&& json!(&i.label_details)
5531+
.to_string()
5532+
.contains("\"@std/path/posix/normalize\"")
5533+
})
5534+
.unwrap();
5535+
let res = client.write_request("completionItem/resolve", json!(item));
5536+
assert_eq!(
5537+
res,
5538+
json!({
5539+
"label": "normalize",
5540+
"labelDetails": { "description": "@std/path/posix/normalize" },
5541+
"kind": 3,
5542+
"detail": "function normalize(path: string): string",
5543+
"documentation": { "kind": "markdown", "value": "Normalize the `path`, resolving `'..'` and `'.'` segments.\nNote that resolving these segments does not necessarily mean that all will be eliminated.\nA `'..'` at the top-level will be preserved, and an empty path is canonically `'.'`.\n\n*@param* - path to be normalized" },
5544+
"sortText": "\u{ffff}16_0",
5545+
"additionalTextEdits": [
5546+
{
5547+
"range": {
5548+
"start": { "line": 2, "character": 6 },
5549+
"end": { "line": 2, "character": 6 },
5550+
},
5551+
"newText": "import { normalize } from \"@std/path/posix/normalize\";\n",
5552+
},
5553+
],
5554+
}),
5555+
);
5556+
client.shutdown();
5557+
}
5558+
54855559
#[test]
54865560
fn lsp_jsr_code_action_missing_declaration() {
54875561
let context = TestContextBuilder::new()

tests/registry/jsr/@std/url/0.220.1/join.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22
// This module is browser compatible.
33

4-
import { join as posixJoin } from "jsr:/@std/path@^0.220.1/posix/join";
4+
import { join as posixJoin } from "jsr:@std/path@^0.220.1/posix/join";
55

66
/**
77
* Join a base `URL` and a series of `paths`, then normalizes the resulting URL.

tests/registry/jsr/@std/url/0.220.1/normalize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22
// This module is browser compatible.
33

4-
import { normalize as posixNormalize } from "jsr:/@std/path@^0.220.1/posix/normalize";
4+
import { normalize as posixNormalize } from "jsr:@std/path@^0.220.1/posix/normalize";
55

66
/**
77
* Normalize the `URL`, resolving `'..'` and `'.'` segments and multiple

0 commit comments

Comments
 (0)