Skip to content

Commit

Permalink
fix(lsp): better handling of data: urls (denoland#18527)
Browse files Browse the repository at this point in the history
1. Log instead of error when the referrer can't be found
2. Fixes typescript to resolve data urls correctly. Properly documented
here:
https://github.com/denoland/TypeScript/pull/4/files#diff-180da7c288743d11d8590d30f0c07c48e5dcf291aa671bbea0dd520a9a1359d2

Closes denoland#18524
  • Loading branch information
dsherret committed Mar 31, 2023
1 parent aa9b94a commit 87ccd4b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
10 changes: 5 additions & 5 deletions cli/lsp/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2779,13 +2779,13 @@ fn op_resolve(
.collect(),
)
}
None => Err(custom_error(
"NotFound",
format!(
None => {
lsp_warn!(
"Error resolving. Referring specifier \"{}\" was not found.",
args.base
),
)),
);
Ok(vec![None; args.specifiers.len()])
}
};

state.performance.measure(mark);
Expand Down
74 changes: 74 additions & 0 deletions cli/tests/integration/lsp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7417,3 +7417,77 @@ fn lsp_closed_file_find_references() {

client.shutdown();
}

#[test]
fn lsp_data_urls_with_jsx_compiler_option() {
let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir();
temp_dir.write(
"deno.json",
r#"{ "compilerOptions": { "jsx": "react-jsx" } }"#,
);

let mut client = context.new_lsp_command().build();
client.initialize_default();

let uri = Url::from_file_path(temp_dir.path().join("main.ts")).unwrap();

let diagnostics = client.did_open(json!({
"textDocument": {
"uri": uri,
"languageId": "typescript",
"version": 1,
"text": "import a from \"data:application/typescript,export default 5;\";\na;"
}
})).viewed();

// there will be a diagnostic about not having cached the data url
assert_eq!(diagnostics.len(), 1);
assert_eq!(
diagnostics[0].code,
Some(lsp::NumberOrString::String("no-cache-data".to_string()))
);

// so cache it
client.write_request(
"deno/cache",
json!({
"referrer": {
"uri": uri,
},
"uris": [],
}),
);

let res = client.write_request(
"textDocument/references",
json!({
"textDocument": {
"uri": uri
},
"position": { "line": 1, "character": 1 },
"context": {
"includeDeclaration": false
}
}),
);

assert_eq!(
res,
json!([{
"uri": uri,
"range": {
"start": { "line": 0, "character": 7 },
"end": { "line": 0, "character": 8 }
}
}, {
"uri": uri,
"range": {
"start": { "line": 1, "character": 0 },
"end": { "line": 1, "character": 1 }
}
}])
);

client.shutdown();
}
6 changes: 6 additions & 0 deletions cli/tsc/00_typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -6712,6 +6712,9 @@ ${lanes.join("\n")}
}
return ~path.length;
}
if (path.startsWith("data:")) {
return ~path.length;
}
return 0;
}
function getRootLength(path) {
Expand Down Expand Up @@ -6870,6 +6873,9 @@ ${lanes.join("\n")}
}
function ensureTrailingDirectorySeparator(path) {
if (!hasTrailingDirectorySeparator(path)) {
if (path.startsWith("data:")) {
return path;
}
return path + directorySeparator;
}
return path;
Expand Down

0 comments on commit 87ccd4b

Please sign in to comment.