summaryrefslogtreecommitdiff
path: root/cli/tests
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tests')
-rw-r--r--cli/tests/integration/lsp_tests.rs166
-rw-r--r--cli/tests/integration/run_tests.rs68
2 files changed, 232 insertions, 2 deletions
diff --git a/cli/tests/integration/lsp_tests.rs b/cli/tests/integration/lsp_tests.rs
index 6dfb0a29a..711d0bcd8 100644
--- a/cli/tests/integration/lsp_tests.rs
+++ b/cli/tests/integration/lsp_tests.rs
@@ -1095,8 +1095,7 @@ fn lsp_import_attributes() {
"only": ["quickfix"]
}
}),
- )
- ;
+ );
assert_eq!(
res,
json!([{
@@ -10506,3 +10505,166 @@ fn lsp_jupyter_byonm_diagnostics() {
);
client.shutdown();
}
+
+#[test]
+fn lsp_sloppy_imports_warn() {
+ let context = TestContextBuilder::new().use_temp_cwd().build();
+ let temp_dir = context.temp_dir();
+ let temp_dir = temp_dir.path();
+ temp_dir
+ .join("deno.json")
+ .write(r#"{ "unstable": ["sloppy-imports"] }"#);
+ // should work when exists on the fs and when doesn't
+ temp_dir.join("a.ts").write("export class A {}");
+ let mut client = context.new_lsp_command().build();
+ client.initialize(|builder| {
+ builder.set_root_uri(temp_dir.uri_dir());
+ });
+ client.did_open(json!({
+ "textDocument": {
+ "uri": temp_dir.join("b.ts").uri_file(),
+ "languageId": "typescript",
+ "version": 1,
+ "text": "export class B {}",
+ },
+ }));
+ let diagnostics = client.did_open(json!({
+ "textDocument": {
+ "uri": temp_dir.join("file.ts").uri_file(),
+ "languageId": "typescript",
+ "version": 1,
+ "text": "import * as a from './a';\nimport * as b from './b.js';\nconsole.log(a)\nconsole.log(b);\n",
+ },
+ }));
+ assert_eq!(
+ diagnostics.messages_with_source("deno"),
+ lsp::PublishDiagnosticsParams {
+ uri: temp_dir.join("file.ts").uri_file(),
+ diagnostics: vec![
+ lsp::Diagnostic {
+ range: lsp::Range {
+ start: lsp::Position {
+ line: 0,
+ character: 19
+ },
+ end: lsp::Position {
+ line: 0,
+ character: 24
+ }
+ },
+ severity: Some(lsp::DiagnosticSeverity::INFORMATION),
+ code: Some(lsp::NumberOrString::String("redirect".to_string())),
+ source: Some("deno".to_string()),
+ message: format!(
+ "The import of \"{}\" was redirected to \"{}\".",
+ temp_dir.join("a").uri_file(),
+ temp_dir.join("a.ts").uri_file()
+ ),
+ data: Some(json!({
+ "specifier": temp_dir.join("a").uri_file(),
+ "redirect": temp_dir.join("a.ts").uri_file()
+ })),
+ ..Default::default()
+ },
+ lsp::Diagnostic {
+ range: lsp::Range {
+ start: lsp::Position {
+ line: 1,
+ character: 19
+ },
+ end: lsp::Position {
+ line: 1,
+ character: 27
+ }
+ },
+ severity: Some(lsp::DiagnosticSeverity::INFORMATION),
+ code: Some(lsp::NumberOrString::String("redirect".to_string())),
+ source: Some("deno".to_string()),
+ message: format!(
+ "The import of \"{}\" was redirected to \"{}\".",
+ temp_dir.join("b.js").uri_file(),
+ temp_dir.join("b.ts").uri_file()
+ ),
+ data: Some(json!({
+ "specifier": temp_dir.join("b.js").uri_file(),
+ "redirect": temp_dir.join("b.ts").uri_file()
+ })),
+ ..Default::default()
+ }
+ ],
+ version: Some(1),
+ }
+ );
+
+ let res = client.write_request(
+ "textDocument/codeAction",
+ json!({
+ "textDocument": {
+ "uri": temp_dir.join("file.ts").uri_file()
+ },
+ "range": {
+ "start": { "line": 0, "character": 19 },
+ "end": { "line": 0, "character": 24 }
+ },
+ "context": {
+ "diagnostics": [{
+ "range": {
+ "start": { "line": 0, "character": 19 },
+ "end": { "line": 0, "character": 24 }
+ },
+ "severity": 3,
+ "code": "redirect",
+ "source": "deno",
+ "message": format!(
+ "The import of \"{}\" was redirected to \"{}\".",
+ temp_dir.join("a").uri_file(),
+ temp_dir.join("a.ts").uri_file()
+ ),
+ "data": {
+ "specifier": temp_dir.join("a").uri_file(),
+ "redirect": temp_dir.join("a.ts").uri_file()
+ },
+ }],
+ "only": ["quickfix"]
+ }
+ }),
+ );
+ assert_eq!(
+ res,
+ json!([{
+ "title": "Update specifier to its redirected specifier.",
+ "kind": "quickfix",
+ "diagnostics": [{
+ "range": {
+ "start": { "line": 0, "character": 19 },
+ "end": { "line": 0, "character": 24 }
+ },
+ "severity": 3,
+ "code": "redirect",
+ "source": "deno",
+ "message": format!(
+ "The import of \"{}\" was redirected to \"{}\".",
+ temp_dir.join("a").uri_file(),
+ temp_dir.join("a.ts").uri_file()
+ ),
+ "data": {
+ "specifier": temp_dir.join("a").uri_file(),
+ "redirect": temp_dir.join("a.ts").uri_file()
+ },
+ }],
+ "edit": {
+ "changes": {
+ temp_dir.join("file.ts").uri_file(): [{
+ "range": {
+ "start": { "line": 0, "character": 19 },
+ "end": { "line": 0, "character": 24 }
+ },
+ "newText": "\"./a.ts\""
+ }]
+ }
+ }
+ }])
+ );
+
+ client.shutdown();
+}
diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs
index b3bd98098..4e0bbdfd2 100644
--- a/cli/tests/integration/run_tests.rs
+++ b/cli/tests/integration/run_tests.rs
@@ -4735,3 +4735,71 @@ itest!(unsafe_proto_flag {
http_server: false,
exit_code: 0,
});
+
+#[test]
+fn test_unstable_sloppy_imports() {
+ let context = TestContextBuilder::new().use_temp_cwd().build();
+ let temp_dir = context.temp_dir();
+ temp_dir.write("deno.json", r#"{ "unstable": ["sloppy-imports"] }"#);
+ temp_dir.write("a.ts", "export class A {}");
+ temp_dir.write("b.js", "export class B {}");
+ temp_dir.write("c.mts", "export class C {}");
+ temp_dir.write("d.mjs", "export class D {}");
+ temp_dir.write("e.tsx", "export class E {}");
+ temp_dir.write("f.jsx", "export class F {}");
+ let dir = temp_dir.path().join("dir");
+ dir.create_dir_all();
+ dir.join("index.tsx").write("export class G {}");
+ temp_dir.write(
+ "main.ts",
+ r#"import * as a from "./a.js";
+import * as b from "./b";
+import * as c from "./c";
+import * as d from "./d";
+import * as e from "./e";
+import * as e2 from "./e.js";
+import * as f from "./f";
+import * as g from "./dir";
+console.log(a.A);
+console.log(b.B);
+console.log(c.C);
+console.log(d.D);
+console.log(e.E);
+console.log(e2.E);
+console.log(f.F);
+console.log(g.G);
+"#,
+ );
+
+ context
+ .new_command()
+ .args("run main.ts")
+ .run()
+ .assert_matches_text(
+ "Warning Sloppy import resolution (hint: update .js extension to .ts)
+ at file:///[WILDCARD]/main.ts:1:20
+Warning Sloppy import resolution (hint: add .js extension)
+ at file:///[WILDCARD]/main.ts:2:20
+Warning Sloppy import resolution (hint: add .mts extension)
+ at file:///[WILDCARD]/main.ts:3:20
+Warning Sloppy import resolution (hint: add .mjs extension)
+ at file:///[WILDCARD]/main.ts:4:20
+Warning Sloppy import resolution (hint: add .tsx extension)
+ at file:///[WILDCARD]/main.ts:5:20
+Warning Sloppy import resolution (hint: update .js extension to .tsx)
+ at file:///[WILDCARD]/main.ts:6:21
+Warning Sloppy import resolution (hint: add .jsx extension)
+ at file:///[WILDCARD]/main.ts:7:20
+Warning Sloppy import resolution (hint: specify path to index.tsx file in directory instead)
+ at file:///[WILDCARD]/main.ts:8:20
+[class A]
+[class B]
+[class C]
+[class D]
+[class E]
+[class E]
+[class F]
+[class G]
+",
+ );
+}