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 +++ cli/tests/testdata/import_maps/config.json | 15 ++++ .../testdata/import_maps/import_map_invalid.json | 7 ++ .../testdata/lsp/deno.embedded_import_map.jsonc | 5 ++ .../testdata/run/033_import_map_in_config_file.out | 8 ++ .../run/033_import_map_in_flag_has_precedence.out | 1 + 7 files changed, 134 insertions(+) create mode 100644 cli/tests/testdata/import_maps/config.json create mode 100644 cli/tests/testdata/import_maps/import_map_invalid.json create mode 100644 cli/tests/testdata/lsp/deno.embedded_import_map.jsonc create mode 100644 cli/tests/testdata/run/033_import_map_in_config_file.out create mode 100644 cli/tests/testdata/run/033_import_map_in_flag_has_precedence.out (limited to 'cli/tests') 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", diff --git a/cli/tests/testdata/import_maps/config.json b/cli/tests/testdata/import_maps/config.json new file mode 100644 index 000000000..b296a63c7 --- /dev/null +++ b/cli/tests/testdata/import_maps/config.json @@ -0,0 +1,15 @@ +{ + "importMap": "./import_map.json", + "imports": { + "moment": "./moment/moment.ts", + "moment/": "./moment/", + "lodash": "./lodash/lodash.ts", + "lodash/": "./lodash/", + "https://www.unpkg.com/vue/dist/vue.runtime.esm.js": "./vue.ts" + }, + "scopes": { + "scope/": { + "moment": "./scoped_moment.ts" + } + } +} diff --git a/cli/tests/testdata/import_maps/import_map_invalid.json b/cli/tests/testdata/import_maps/import_map_invalid.json new file mode 100644 index 000000000..a09d280c5 --- /dev/null +++ b/cli/tests/testdata/import_maps/import_map_invalid.json @@ -0,0 +1,7 @@ +{ + "imports": { + "https://www.unpkg.com/vue/dist/vue.runtime.esm.js": "./vue.ts" + }, + "scopes": { + } +} diff --git a/cli/tests/testdata/lsp/deno.embedded_import_map.jsonc b/cli/tests/testdata/lsp/deno.embedded_import_map.jsonc new file mode 100644 index 000000000..75d5d0849 --- /dev/null +++ b/cli/tests/testdata/lsp/deno.embedded_import_map.jsonc @@ -0,0 +1,5 @@ +{ + "imports": { + "/~/": "./lib/" + } +} diff --git a/cli/tests/testdata/run/033_import_map_in_config_file.out b/cli/tests/testdata/run/033_import_map_in_config_file.out new file mode 100644 index 000000000..72df124a2 --- /dev/null +++ b/cli/tests/testdata/run/033_import_map_in_config_file.out @@ -0,0 +1,8 @@ +Warning "importMap" setting is ignored when "imports" or "scopes" are specified in the config file. +Hello from remapped moment! +Hello from remapped moment dir! +Hello from remapped lodash! +Hello from remapped lodash dir! +Hello from remapped Vue! +Hello from scoped moment! +Hello from scoped! diff --git a/cli/tests/testdata/run/033_import_map_in_flag_has_precedence.out b/cli/tests/testdata/run/033_import_map_in_flag_has_precedence.out new file mode 100644 index 000000000..e9b183ee6 --- /dev/null +++ b/cli/tests/testdata/run/033_import_map_in_flag_has_precedence.out @@ -0,0 +1 @@ +error: Relative import path [WILDCARD] not prefixed with / or ./ or ../ and not in import map [WILDCARD] -- cgit v1.2.3