diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2024-07-23 19:39:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-23 19:39:14 +0100 |
commit | a45a40533eb25c5a12df98189338320f8aa6dcc4 (patch) | |
tree | 5cfa841deb39f933ace9336f76e1dce4d8c7d2ff /tests/integration/lsp_tests.rs | |
parent | 9806064ac22680c732f64f990e672a58e946a58a (diff) |
fix(lsp): rewrite import for 'infer return type' action (#24685)
Diffstat (limited to 'tests/integration/lsp_tests.rs')
-rw-r--r-- | tests/integration/lsp_tests.rs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/tests/integration/lsp_tests.rs b/tests/integration/lsp_tests.rs index 8034bb683..cfcf72ddc 100644 --- a/tests/integration/lsp_tests.rs +++ b/tests/integration/lsp_tests.rs @@ -7373,6 +7373,104 @@ fn lsp_npm_completions_auto_import_and_quick_fix_no_import_map() { client.shutdown(); } +#[test] +fn lsp_infer_return_type() { + let context = TestContextBuilder::new().use_temp_cwd().build(); + let temp_dir = context.temp_dir(); + temp_dir.write("deno.json", json!({}).to_string()); + let types_file = source_file( + temp_dir.path().join("types.d.ts"), + r#" + export interface SomeInterface { + someField: number; + } + declare global { + export function someFunction(): SomeInterface; + } + "#, + ); + let file = source_file( + temp_dir.path().join("file.ts"), + r#" + function foo() { + return someFunction(); + } + foo(); + "#, + ); + let mut client = context.new_lsp_command().build(); + client.initialize_default(); + let res = client.write_request( + "textDocument/codeAction", + json!({ + "textDocument": { "uri": file.uri() }, + "range": { + "start": { "line": 1, "character": 15 }, + "end": { "line": 1, "character": 18 }, + }, + "context": { + "diagnostics": [], + "only": ["refactor.rewrite.function.returnType"], + } + }), + ); + assert_eq!( + &res, + &json!([ + { + "title": "Infer function return type", + "kind": "refactor.rewrite.function.returnType", + "isPreferred": false, + "data": { + "specifier": file.uri(), + "range": { + "start": { "line": 1, "character": 15 }, + "end": { "line": 1, "character": 18 }, + }, + "refactorName": "Infer function return type", + "actionName": "Infer function return type", + }, + } + ]), + ); + let code_action = res.as_array().unwrap().first().unwrap(); + let res = client.write_request("codeAction/resolve", code_action); + assert_eq!( + &res, + &json!({ + "title": "Infer function return type", + "kind": "refactor.rewrite.function.returnType", + "isPreferred": false, + "data": { + "specifier": file.uri(), + "range": { + "start": { "line": 1, "character": 15 }, + "end": { "line": 1, "character": 18 }, + }, + "refactorName": "Infer function return type", + "actionName": "Infer function return type", + }, + "edit": { + "documentChanges": [ + { + "textDocument": { "uri": file.uri(), "version": null }, + "edits": [ + { + "range": { + "start": { "line": 1, "character": 20 }, + "end": { "line": 1, "character": 20 }, + }, + "newText": format!(": import(\"{}\").SomeInterface", types_file.uri()), + }, + ], + }, + ], + }, + }), + ); + client.shutdown(); +} + // Regression test for https://github.com/denoland/deno/issues/23895. #[test] fn lsp_npm_types_nested_js_dts() { |