0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

perf(lsp): use null types instead of stub modules (#21541)

This commit is contained in:
Nayeem Rahman 2023-12-12 10:26:27 +00:00 committed by GitHub
parent 49a6daaa83
commit 7d88e48296
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 78 deletions

View file

@ -3877,11 +3877,7 @@ fn op_load<'s>(
let specifier = state.specifier_map.normalize(specifier)?; let specifier = state.specifier_map.normalize(specifier)?;
let maybe_load_response = let maybe_load_response =
if specifier.as_str() == "internal:///missing_dependency.d.ts" { if specifier.as_str() == "internal:///missing_dependency.d.ts" {
Some(LoadResponse { None
data: Arc::from("declare const __: any;\nexport = __;\n"),
script_kind: crate::tsc::as_ts_script_kind(MediaType::Dts),
version: Some("1".to_string()),
})
} else { } else {
let asset_or_document = state.get_asset_or_document(&specifier); let asset_or_document = state.get_asset_or_document(&specifier);
asset_or_document.map(|doc| LoadResponse { asset_or_document.map(|doc| LoadResponse {

View file

@ -8116,57 +8116,6 @@ fn lsp_ts_diagnostics_refresh_on_lsp_version_reset() {
client.shutdown(); client.shutdown();
} }
#[test]
fn lsp_npm_missing_type_imports_diagnostics() {
let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build();
client.initialize_default();
client.did_open(json!({
"textDocument": {
"uri": "file:///a/file.ts",
"languageId": "typescript",
"version": 1,
"text": r#"
import colorName, { type RGB } from 'npm:color-name';
const color: RGB = colorName.black;
console.log(color);
"#,
},
}));
client.write_request(
"workspace/executeCommand",
json!({
"command": "deno.cache",
"arguments": [[], "file:///a/file.ts"],
}),
);
let diagnostics = client.read_diagnostics();
assert_eq!(
json!(
diagnostics.messages_with_file_and_source("file:///a/file.ts", "deno-ts")
),
json!({
"uri": "file:///a/file.ts",
"diagnostics": [
{
"range": {
"start": { "line": 1, "character": 33 },
"end": { "line": 1, "character": 36 },
},
"severity": 1,
"code": 2305,
"source": "deno-ts",
"message": "Module '\"npm:color-name\"' has no exported member 'RGB'.",
},
],
"version": 1,
})
);
}
#[test] #[test]
fn lsp_jupyter_diagnostics() { fn lsp_jupyter_diagnostics() {
let context = TestContextBuilder::new().use_temp_cwd().build(); let context = TestContextBuilder::new().use_temp_cwd().build();

View file

@ -528,7 +528,7 @@ delete Object.prototype.__proto__;
if (logDebug) { if (logDebug) {
debug(`host.readFile("${specifier}")`); debug(`host.readFile("${specifier}")`);
} }
return ops.op_load(specifier).data; return ops.op_load(specifier)?.data;
}, },
getCancellationToken() { getCancellationToken() {
// createLanguageService will call this immediately and cache it // createLanguageService will call this immediately and cache it

View file

@ -441,7 +441,7 @@ pub fn as_ts_script_kind(media_type: MediaType) -> i32 {
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct LoadResponse { struct LoadResponse {
data: Option<String>, data: String,
version: Option<String>, version: Option<String>,
script_kind: i32, script_kind: i32,
} }
@ -451,7 +451,7 @@ struct LoadResponse {
fn op_load( fn op_load(
state: &mut OpState, state: &mut OpState,
#[string] load_specifier: &str, #[string] load_specifier: &str,
) -> Result<LoadResponse, AnyError> { ) -> Result<Option<LoadResponse>, AnyError> {
let state = state.borrow_mut::<State>(); let state = state.borrow_mut::<State>();
let specifier = normalize_specifier(load_specifier, &state.current_dir) let specifier = normalize_specifier(load_specifier, &state.current_dir)
@ -466,9 +466,7 @@ fn op_load(
// in certain situations we return a "blank" module to tsc and we need to // in certain situations we return a "blank" module to tsc and we need to
// handle the request for that module here. // handle the request for that module here.
} else if load_specifier == "internal:///missing_dependency.d.ts" { } else if load_specifier == "internal:///missing_dependency.d.ts" {
hash = Some("1".to_string()); None
media_type = MediaType::Dts;
Some(Cow::Borrowed("declare const __: any;\nexport = __;\n"))
} else if let Some(name) = load_specifier.strip_prefix("asset:///") { } else if let Some(name) = load_specifier.strip_prefix("asset:///") {
let maybe_source = get_lazily_loaded_asset(name); let maybe_source = get_lazily_loaded_asset(name);
hash = get_maybe_hash(maybe_source, state.hash_data); hash = get_maybe_hash(maybe_source, state.hash_data);
@ -521,18 +519,19 @@ fn op_load(
.with_context(|| format!("Unable to load {}", file_path.display()))?; .with_context(|| format!("Unable to load {}", file_path.display()))?;
Some(Cow::Owned(code)) Some(Cow::Owned(code))
} else { } else {
media_type = MediaType::Unknown;
None None
}; };
hash = get_maybe_hash(maybe_source.as_deref(), state.hash_data); hash = get_maybe_hash(maybe_source.as_deref(), state.hash_data);
maybe_source maybe_source
}; };
let Some(data) = data else {
Ok(LoadResponse { return Ok(None);
data: data.map(String::from), };
Ok(Some(LoadResponse {
data: data.into_owned(),
version: hash, version: hash,
script_kind: as_ts_script_kind(media_type), script_kind: as_ts_script_kind(media_type),
}) }))
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
@ -1079,9 +1078,10 @@ mod tests {
) )
.await; .await;
let actual = op_load::call(&mut state, "asset:///lib.dom.d.ts") let actual = op_load::call(&mut state, "asset:///lib.dom.d.ts")
.expect("should have invoked op"); .expect("should have invoked op")
.expect("load should have succeeded");
let expected = get_lazily_loaded_asset("lib.dom.d.ts").unwrap(); let expected = get_lazily_loaded_asset("lib.dom.d.ts").unwrap();
assert_eq!(actual.data.unwrap(), expected); assert_eq!(actual.data, expected);
assert!(actual.version.is_some()); assert!(actual.version.is_some());
assert_eq!(actual.script_kind, 3); assert_eq!(actual.script_kind, 3);
} }
@ -1095,7 +1095,8 @@ mod tests {
) )
.await; .await;
let actual = op_load::call(&mut state, "internal:///.tsbuildinfo") let actual = op_load::call(&mut state, "internal:///.tsbuildinfo")
.expect("should have invoked op"); .expect("should have invoked op")
.expect("load should have succeeded");
assert_eq!( assert_eq!(
serde_json::to_value(actual).unwrap(), serde_json::to_value(actual).unwrap(),
json!({ json!({
@ -1111,14 +1112,7 @@ mod tests {
let mut state = setup(None, None, None).await; let mut state = setup(None, None, None).await;
let actual = op_load::call(&mut state, "https://deno.land/x/mod.ts") let actual = op_load::call(&mut state, "https://deno.land/x/mod.ts")
.expect("should have invoked op"); .expect("should have invoked op");
assert_eq!( assert_eq!(serde_json::to_value(actual).unwrap(), json!(null));
serde_json::to_value(actual).unwrap(),
json!({
"data": null,
"version": null,
"scriptKind": 0,
})
)
} }
#[tokio::test] #[tokio::test]