diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2021-07-25 15:33:42 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-25 15:33:42 +1000 |
commit | 72ac9c3ae0c5db99af1e1f5d2191fb82dc092acd (patch) | |
tree | d1120201acc158155ea9a76d4b7896221489c9e9 /cli/tests | |
parent | 74c7559d2029539eb6ab7459c06061c00b3e0c1a (diff) |
fix(lsp): handle importmaps properly (#11496)
Fixes: #11146
Fixes: #11456
Fixes: #10439
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/integration/lsp_tests.rs | 85 | ||||
-rw-r--r-- | cli/tests/lsp/import-map.json | 5 |
2 files changed, 90 insertions, 0 deletions
diff --git a/cli/tests/integration/lsp_tests.rs b/cli/tests/integration/lsp_tests.rs index 81eb64b7a..7f4f4e591 100644 --- a/cli/tests/integration/lsp_tests.rs +++ b/cli/tests/integration/lsp_tests.rs @@ -232,6 +232,91 @@ fn lsp_triple_slash_types() { } #[test] +fn lsp_import_map() { + let temp_dir = TempDir::new().expect("could not create temp dir"); + let mut params: lsp::InitializeParams = + serde_json::from_value(load_fixture("initialize_params.json")).unwrap(); + let import_map = + serde_json::to_vec_pretty(&load_fixture("import-map.json")).unwrap(); + fs::write(temp_dir.path().join("import-map.json"), import_map).unwrap(); + fs::create_dir(temp_dir.path().join("lib")).unwrap(); + fs::write( + temp_dir.path().join("lib").join("b.ts"), + r#"export const b = "b";"#, + ) + .unwrap(); + + params.root_uri = Some(Url::from_file_path(temp_dir.path()).unwrap()); + if let Some(Value::Object(mut map)) = params.initialization_options { + map.insert("importMap".to_string(), json!("import-map.json")); + params.initialization_options = Some(Value::Object(map)); + } + + let deno_exe = deno_exe_path(); + let mut client = LspClient::new(&deno_exe).unwrap(); + client + .write_request::<_, _, Value>("initialize", params) + .unwrap(); + + client.write_notification("initialized", json!({})).unwrap(); + let uri = Url::from_file_path(temp_dir.path().join("a.ts")).unwrap(); + + let diagnostics = did_open( + &mut client, + json!({ + "textDocument": { + "uri": uri, + "languageId": "typescript", + "version": 1, + "text": "import { b } from \"/~/b.ts\";\n\nconsole.log(b);\n" + } + }), + ); + + let diagnostics = diagnostics.into_iter().flat_map(|x| x.diagnostics); + assert_eq!(diagnostics.count(), 0); + + let (maybe_res, maybe_err) = client + .write_request::<_, _, Value>( + "textDocument/hover", + json!({ + "textDocument": { + "uri": uri + }, + "position": { + "line": 2, + "character": 12 + } + }), + ) + .unwrap(); + assert!(maybe_err.is_none()); + assert_eq!( + maybe_res, + Some(json!({ + "contents": [ + { + "language": "typescript", + "value":"(alias) const b: \"b\"\nimport b" + }, + "" + ], + "range": { + "start": { + "line": 2, + "character": 12 + }, + "end": { + "line": 2, + "character": 13 + } + } + })) + ); + shutdown(&mut client); +} + +#[test] fn lsp_hover() { let mut client = init("initialize_params.json"); did_open( diff --git a/cli/tests/lsp/import-map.json b/cli/tests/lsp/import-map.json new file mode 100644 index 000000000..75d5d0849 --- /dev/null +++ b/cli/tests/lsp/import-map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "/~/": "./lib/" + } +} |