From c6c8c91a6e4b7a2b6eed02d3e2f5db25c124d9a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 25 Jan 2023 21:13:40 +0100 Subject: feat: embed import map in the config file (#17478) This commit changes handling of config file to enable specifying "imports" and "scopes" objects effectively making the configuration file an import map. "imports" and "scopes" take precedence over "importMap" configuration, but have lower priority than "--importmap" CLI flag. Co-authored-by: David Sherret Co-authored-by: David Sherret --- cli/tests/integration/lsp_tests.rs | 87 ++++++++++++++++++++++++++++++++++++++ cli/tests/integration/run_tests.rs | 11 +++++ 2 files changed, 98 insertions(+) (limited to 'cli/tests/integration') diff --git a/cli/tests/integration/lsp_tests.rs b/cli/tests/integration/lsp_tests.rs index f7c0e6137..be280bfa7 100644 --- a/cli/tests/integration/lsp_tests.rs +++ b/cli/tests/integration/lsp_tests.rs @@ -582,6 +582,93 @@ fn lsp_import_map_config_file() { shutdown(&mut client); } +#[test] +fn lsp_import_map_embedded_in_config_file() { + let temp_dir = TempDir::new(); + let mut params: lsp::InitializeParams = + serde_json::from_value(load_fixture("initialize_params.json")).unwrap(); + + let deno_import_map_jsonc = serde_json::to_string_pretty(&load_fixture( + "deno.embedded_import_map.jsonc", + )) + .unwrap(); + temp_dir.write("deno.embedded_import_map.jsonc", deno_import_map_jsonc); + + params.root_uri = Some(Url::from_file_path(temp_dir.path()).unwrap()); + if let Some(Value::Object(mut map)) = params.initialization_options { + map.insert( + "config".to_string(), + json!("./deno.embedded_import_map.jsonc"), + ); + params.initialization_options = Some(Value::Object(map)); + } + fs::create_dir(temp_dir.path().join("lib")).unwrap(); + temp_dir.write("lib/b.ts", r#"export const b = "b";"#); + + let deno_exe = deno_exe_path(); + let mut client = LspClient::new(&deno_exe, false).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_deno_task() { let temp_dir = TempDir::new(); diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index fd6e6588c..62bfe136f 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -164,6 +164,17 @@ itest!(_033_import_map { output: "run/033_import_map.out", }); +itest!(_033_import_map_in_config_file { + args: "run --reload --config=import_maps/config.json import_maps/test.ts", + output: "run/033_import_map_in_config_file.out", +}); + +itest!(_033_import_map_in_flag_has_precedence { + args: "run --quiet --reload --import-map=import_maps/import_map_invalid.json --config=import_maps/config.json import_maps/test.ts", + output: "run/033_import_map_in_flag_has_precedence.out", + exit_code: 1, +}); + itest!(_033_import_map_remote { args: "run --quiet --reload --import-map=http://127.0.0.1:4545/import_maps/import_map_remote.json --unstable import_maps/test_remote.ts", -- cgit v1.2.3