diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-07-25 09:07:59 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-25 09:07:59 -0400 |
commit | 763f05e74dfd0032b238603f625893a52e363591 (patch) | |
tree | c6a71559472755919358afa53eecac206cad80a9 /tests | |
parent | ef78d317f084ffe633253acd138a48a425113fa7 (diff) |
fix(unstable): move sloppy-import warnings to lint rule (#24710)
Adds a new `no-sloppy-imports` lint rule and cleans up the lint code.
Closes #22844
Closes https://github.com/denoland/deno_lint/issues/1293
Diffstat (limited to 'tests')
42 files changed, 377 insertions, 190 deletions
diff --git a/tests/integration/check_tests.rs b/tests/integration/check_tests.rs index 78ab859f9..b560b352e 100644 --- a/tests/integration/check_tests.rs +++ b/tests/integration/check_tests.rs @@ -392,105 +392,3 @@ fn npm_module_check_then_error() { .assert_matches_text("Check [WILDCARD]main.ts\nerror: TS2305[WILDCARD]has no exported member 'oldName'[WILDCARD]") .assert_exit_code(1); } - -#[test] -fn test_unstable_sloppy_imports_dts_files() { - let context = TestContextBuilder::new().use_temp_cwd().build(); - let temp_dir = context.temp_dir(); - temp_dir.write("a.ts", "export class A {}"); // resolves this - temp_dir.write("a.d.ts", "export class A2 {}"); - - temp_dir.write("b.js", "export class B {}"); - temp_dir.write("b.d.ts", "export class B2 {}"); // this - - temp_dir.write("c.mts", "export class C {}"); // this - temp_dir.write("c.d.mts", "export class C2 {}"); - - temp_dir.write("d.mjs", "export class D {}"); - temp_dir.write("d.d.mts", "export class D2 {}"); // this - - let temp_dir = temp_dir.path(); - - let dir = temp_dir.join("dir_ts"); - dir.create_dir_all(); - dir.join("index.ts").write("export class Dir {}"); // this - dir.join("index.d.ts").write("export class Dir2 {}"); - - let dir = temp_dir.join("dir_js"); - dir.create_dir_all(); - dir.join("index.js").write("export class Dir {}"); - dir.join("index.d.ts").write("export class Dir2 {}"); // this - - let dir = temp_dir.join("dir_mts"); - dir.create_dir_all(); - dir.join("index.mts").write("export class Dir {}"); // this - dir.join("index.d.ts").write("export class Dir2 {}"); - - let dir = temp_dir.join("dir_mjs"); - dir.create_dir_all(); - dir.join("index.mjs").write("export class Dir {}"); - dir.join("index.d.ts").write("export class Dir2 {}"); // this - - temp_dir.join("main.ts").write( - r#"import * as a from "./a.js"; -import * as b from "./b.js"; -import * as c from "./c.mjs"; -import * as d from "./d.mjs"; - -console.log(a.A); -console.log(b.B2); -console.log(c.C); -console.log(d.D2); - -import * as a2 from "./a"; -import * as b2 from "./b"; -import * as c2 from "./c"; -import * as d2 from "./d"; - -console.log(a2.A); -console.log(b2.B2); -console.log(c2.C); -console.log(d2.D2); - -import * as dirTs from "./dir_ts"; -import * as dirJs from "./dir_js"; -import * as dirMts from "./dir_mts"; -import * as dirMjs from "./dir_mjs"; - -console.log(dirTs.Dir); -console.log(dirJs.Dir2); -console.log(dirMts.Dir); -console.log(dirMjs.Dir2); -"#, - ); - - context - .new_command() - .args("check --unstable-sloppy-imports main.ts") - .run() - .assert_matches_text( - r#"Warning Sloppy module resolution (hint: update .js extension to .ts) - at file:///[WILDCARD]/main.ts:1:20 -Warning Sloppy module resolution (hint: update .mjs extension to .mts) - at file:///[WILDCARD]/main.ts:3:20 -Warning Sloppy module resolution (hint: add .ts extension) - at file:///[WILDCARD]/main.ts:11:21 -Warning Sloppy module resolution (hint: add .js extension) - at file:///[WILDCARD]/main.ts:12:21 -Warning Sloppy module resolution (hint: add .mts extension) - at file:///[WILDCARD]/main.ts:13:21 -Warning Sloppy module resolution (hint: add .mjs extension) - at file:///[WILDCARD]/main.ts:14:21 -Warning Sloppy module resolution (hint: specify path to index.ts file in directory instead) - at file:///[WILDCARD]/main.ts:21:24 -Warning Sloppy module resolution (hint: specify path to index.js file in directory instead) - at file:///[WILDCARD]/main.ts:22:24 -Warning Sloppy module resolution (hint: specify path to index.mts file in directory instead) - at file:///[WILDCARD]/main.ts:23:25 -Warning Sloppy module resolution (hint: specify path to index.mjs file in directory instead) - at file:///[WILDCARD]/main.ts:24:25 -Check [WILDCARD]main.ts -"#, - ) - .assert_exit_code(0); -} diff --git a/tests/integration/lsp_tests.rs b/tests/integration/lsp_tests.rs index e8904942d..cbc175ec6 100644 --- a/tests/integration/lsp_tests.rs +++ b/tests/integration/lsp_tests.rs @@ -14459,7 +14459,67 @@ fn lsp_sloppy_imports() { }, })); - assert_eq!(json!(diagnostics.all()), json!([])); + assert_eq!( + json!(diagnostics.all()), + json!([{ + "range": { + "start": { "line": 0, "character": 19 }, + "end": { "line": 0, "character": 24 } + }, + "severity": 2, + "code": "no-sloppy-imports", + "source": "deno-lint", + "message": "Sloppy imports are not allowed.", + "data": [{ + "description": "Add a '.ts' extension.", + "changes": [{ + "range": { + "start": { "line": 0, "character": 19 }, + "end": { "line": 0, "character": 24 }, + }, + "new_text": "'./a.ts'" + }] + }] + }, { + "range": { + "start": { "line": 1, "character": 19 }, + "end": { "line": 1, "character": 27 } + }, + "severity": 2, + "code": "no-sloppy-imports", + "source": "deno-lint", + "message": "Sloppy imports are not allowed.", + "data": [{ + "description": "Change the extension to '.ts'.", + "changes": [{ + "range": { + "start": { "line": 1, "character": 19 }, + "end": { "line": 1, "character": 27 }, + }, + "new_text": "'./b.ts'" + }] + }] + }, { + "range": { + "start": { "line": 2, "character": 19 }, + "end": { "line": 2, "character": 27 } + }, + "severity": 2, + "code": "no-sloppy-imports", + "source": "deno-lint", + "message": "Sloppy imports are not allowed.", + "data": [{ + "description": "Change the extension to '.d.ts'.", + "changes": [{ + "range": { + "start": { "line": 2, "character": 19 }, + "end": { "line": 2, "character": 27 }, + }, + "new_text": "'./c.d.ts'" + }] + }] + }]) + ); client.shutdown(); } @@ -14488,11 +14548,33 @@ fn lsp_sloppy_imports_prefers_dts() { "import { foo } from './a.js';\nconsole.log(foo);", ); let diagnostics = client.did_open_file(&file); - // no warnings because "a.js" exists - assert_eq!(diagnostics.all().len(), 0); + // no other warnings because "a.js" exists + assert_eq!( + json!(diagnostics.all()), + json!([{ + "range": { + "start": { "line": 0, "character": 20 }, + "end": { "line": 0, "character": 28 } + }, + "severity": 2, + "code": "no-sloppy-imports", + "source": "deno-lint", + "message": "Sloppy imports are not allowed.", + "data": [{ + "description": "Change the extension to '.d.ts'.", + "changes": [{ + "range": { + "start": { "line": 0, "character": 20 }, + "end": { "line": 0, "character": 28 }, + }, + "new_text": "'./a.d.ts'" + }] + }] + }]) + ); let diagnostics = client.did_open_file(&a_dts); - assert_eq!(diagnostics.all().len(), 0, "Got {:#?}", diagnostics.all()); + assert_eq!(json!(diagnostics.for_file(&a_dts.uri())), json!([])); let response = client.write_request( "textDocument/references", diff --git a/tests/integration/run_tests.rs b/tests/integration/run_tests.rs index 73d98aff5..7a6ee61cf 100644 --- a/tests/integration/run_tests.rs +++ b/tests/integration/run_tests.rs @@ -4851,86 +4851,6 @@ itest!(unsafe_proto_flag { 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("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); -"#, - ); - - // run without sloppy imports - context - .new_command() - .args("run main.ts") - .run() - .assert_matches_text(r#"error: Module not found "file:///[WILDCARD]/a.js". Maybe change the extension to '.ts' or run with --unstable-sloppy-imports - at file:///[WILDCARD]/main.ts:1:20 -"#) - .assert_exit_code(1); - - // now run with sloppy imports - temp_dir.write("deno.json", r#"{ "unstable": ["sloppy-imports"] }"#); - context - .new_command() - .args("run main.ts") - .run() - .assert_matches_text( - "Warning Sloppy imports are not recommended and have a negative impact on performance. -Warning Sloppy module resolution (hint: update .js extension to .ts) - at file:///[WILDCARD]/main.ts:1:20 -Warning Sloppy module resolution (hint: add .js extension) - at file:///[WILDCARD]/main.ts:2:20 -Warning Sloppy module resolution (hint: add .mts extension) - at file:///[WILDCARD]/main.ts:3:20 -Warning Sloppy module resolution (hint: add .mjs extension) - at file:///[WILDCARD]/main.ts:4:20 -Warning Sloppy module resolution (hint: add .tsx extension) - at file:///[WILDCARD]/main.ts:5:20 -Warning Sloppy module resolution (hint: update .js extension to .tsx) - at file:///[WILDCARD]/main.ts:6:21 -Warning Sloppy module resolution (hint: add .jsx extension) - at file:///[WILDCARD]/main.ts:7:20 -Warning Sloppy module 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] -", - ); -} - itest!(unstable_temporal_api { args: "run --no-config --unstable-temporal --check run/unstable_temporal_api/main.ts", output: "run/unstable_temporal_api/main.out", diff --git a/tests/specs/lint/sloppy_imports_dts/__test__.jsonc b/tests/specs/lint/sloppy_imports_dts/__test__.jsonc new file mode 100644 index 000000000..2650d7ec2 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/__test__.jsonc @@ -0,0 +1,34 @@ +{ + "tests": { + "check": { + "args": "check --unstable-sloppy-imports main.ts", + "output": "check.out" + }, + "run": { + "args": "run --unstable-sloppy-imports main.ts", + "output": "run.out" + }, + "lint": { + "args": "lint --unstable-sloppy-imports", + "output": "lint.out", + "exitCode": 1 + }, + // try fixing the lint issues and then ensure deno check and run still work + "lint_fix": { + "tempDir": true, + "steps": [{ + "args": "lint --unstable-sloppy-imports --fix", + "output": "Checked 17 files\n" + }, { + "args": "lint --unstable-sloppy-imports", + "output": "Checked 17 files\n" + }, { + "args": "check --unstable-sloppy-imports main.ts", + "output": "check.out" + }, { + "args": "run --unstable-sloppy-imports main.ts", + "output": "run.out" + }] + } + } +} diff --git a/tests/specs/lint/sloppy_imports_dts/a.d.ts b/tests/specs/lint/sloppy_imports_dts/a.d.ts new file mode 100644 index 000000000..b0c84214f --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/a.d.ts @@ -0,0 +1 @@ +export class A2 {} diff --git a/tests/specs/lint/sloppy_imports_dts/a.ts b/tests/specs/lint/sloppy_imports_dts/a.ts new file mode 100644 index 000000000..1e14df544 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/a.ts @@ -0,0 +1 @@ +export class A {} diff --git a/tests/specs/lint/sloppy_imports_dts/b.d.ts b/tests/specs/lint/sloppy_imports_dts/b.d.ts new file mode 100644 index 000000000..522d6784d --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/b.d.ts @@ -0,0 +1 @@ +export class B2 {} diff --git a/tests/specs/lint/sloppy_imports_dts/b.js b/tests/specs/lint/sloppy_imports_dts/b.js new file mode 100644 index 000000000..1aa41a54a --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/b.js @@ -0,0 +1 @@ +export class B {} diff --git a/tests/specs/lint/sloppy_imports_dts/c.d.mts b/tests/specs/lint/sloppy_imports_dts/c.d.mts new file mode 100644 index 000000000..efd6dcc73 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/c.d.mts @@ -0,0 +1 @@ +export class C2 {} diff --git a/tests/specs/lint/sloppy_imports_dts/c.mts b/tests/specs/lint/sloppy_imports_dts/c.mts new file mode 100644 index 000000000..1ec0ebf40 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/c.mts @@ -0,0 +1 @@ +export class C {} diff --git a/tests/specs/lint/sloppy_imports_dts/check.out b/tests/specs/lint/sloppy_imports_dts/check.out new file mode 100644 index 000000000..1830c3186 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/check.out @@ -0,0 +1 @@ +Check file:///[WILDLINE]/main.ts diff --git a/tests/specs/lint/sloppy_imports_dts/d.d.mts b/tests/specs/lint/sloppy_imports_dts/d.d.mts new file mode 100644 index 000000000..629b29e30 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/d.d.mts @@ -0,0 +1 @@ +export class D2 {} diff --git a/tests/specs/lint/sloppy_imports_dts/d.mjs b/tests/specs/lint/sloppy_imports_dts/d.mjs new file mode 100644 index 000000000..01b958f66 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/d.mjs @@ -0,0 +1 @@ +export class D {} diff --git a/tests/specs/lint/sloppy_imports_dts/dir_js/index.d.ts b/tests/specs/lint/sloppy_imports_dts/dir_js/index.d.ts new file mode 100644 index 000000000..23cac8500 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/dir_js/index.d.ts @@ -0,0 +1 @@ +export class Dir2 {} diff --git a/tests/specs/lint/sloppy_imports_dts/dir_js/index.js b/tests/specs/lint/sloppy_imports_dts/dir_js/index.js new file mode 100644 index 000000000..f31e303be --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/dir_js/index.js @@ -0,0 +1 @@ +export class Dir {} diff --git a/tests/specs/lint/sloppy_imports_dts/dir_mjs/index.d.ts b/tests/specs/lint/sloppy_imports_dts/dir_mjs/index.d.ts new file mode 100644 index 000000000..23cac8500 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/dir_mjs/index.d.ts @@ -0,0 +1 @@ +export class Dir2 {} diff --git a/tests/specs/lint/sloppy_imports_dts/dir_mjs/index.mjs b/tests/specs/lint/sloppy_imports_dts/dir_mjs/index.mjs new file mode 100644 index 000000000..f31e303be --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/dir_mjs/index.mjs @@ -0,0 +1 @@ +export class Dir {} diff --git a/tests/specs/lint/sloppy_imports_dts/dir_mts/index.d.ts b/tests/specs/lint/sloppy_imports_dts/dir_mts/index.d.ts new file mode 100644 index 000000000..23cac8500 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/dir_mts/index.d.ts @@ -0,0 +1 @@ +export class Dir2 {} diff --git a/tests/specs/lint/sloppy_imports_dts/dir_mts/index.mts b/tests/specs/lint/sloppy_imports_dts/dir_mts/index.mts new file mode 100644 index 000000000..f31e303be --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/dir_mts/index.mts @@ -0,0 +1 @@ +export class Dir {} diff --git a/tests/specs/lint/sloppy_imports_dts/dir_ts/index.d.ts b/tests/specs/lint/sloppy_imports_dts/dir_ts/index.d.ts new file mode 100644 index 000000000..23cac8500 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/dir_ts/index.d.ts @@ -0,0 +1 @@ +export class Dir2 {} diff --git a/tests/specs/lint/sloppy_imports_dts/dir_ts/index.ts b/tests/specs/lint/sloppy_imports_dts/dir_ts/index.ts new file mode 100644 index 000000000..f31e303be --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/dir_ts/index.ts @@ -0,0 +1 @@ +export class Dir {} diff --git a/tests/specs/lint/sloppy_imports_dts/lint.out b/tests/specs/lint/sloppy_imports_dts/lint.out new file mode 100644 index 000000000..d891676ac --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/lint.out @@ -0,0 +1,110 @@ +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:1:20 + | +1 | import * as a from "./a.js"; + | ^^^^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:2:20 + | +2 | import * as b from "./b.js"; + | ^^^^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:3:20 + | +3 | import * as c from "./c.mjs"; + | ^^^^^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:4:20 + | +4 | import * as d from "./d.mjs"; + | ^^^^^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:11:21 + | +11 | import * as a2 from "./a"; + | ^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:12:21 + | +12 | import * as b2 from "./b"; + | ^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:13:21 + | +13 | import * as c2 from "./c"; + | ^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:14:21 + | +14 | import * as d2 from "./d"; + | ^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:21:24 + | +21 | import * as dirTs from "./dir_ts"; + | ^^^^^^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:22:24 + | +22 | import * as dirJs from "./dir_js"; + | ^^^^^^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:23:25 + | +23 | import * as dirMts from "./dir_mts"; + | ^^^^^^^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:24:25 + | +24 | import * as dirMjs from "./dir_mjs"; + | ^^^^^^^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +Found 12 problems (12 fixable via --fix) +Checked 17 files diff --git a/tests/specs/lint/sloppy_imports_dts/main.ts b/tests/specs/lint/sloppy_imports_dts/main.ts new file mode 100644 index 000000000..2261eae58 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/main.ts @@ -0,0 +1,29 @@ +import * as a from "./a.js"; +import * as b from "./b.js"; +import * as c from "./c.mjs"; +import * as d from "./d.mjs"; + +console.log(a.A); +console.log(b.B2); +console.log(c.C); +console.log(d.D2); + +import * as a2 from "./a"; +import * as b2 from "./b"; +import * as c2 from "./c"; +import * as d2 from "./d"; + +console.log(a2.A); +console.log(b2.B2); +console.log(c2.C); +console.log(d2.D2); + +import * as dirTs from "./dir_ts"; +import * as dirJs from "./dir_js"; +import * as dirMts from "./dir_mts"; +import * as dirMjs from "./dir_mjs"; + +console.log(dirTs.Dir); +console.log(dirJs.Dir2); +console.log(dirMts.Dir); +console.log(dirMjs.Dir2); diff --git a/tests/specs/lint/sloppy_imports_dts/run.out b/tests/specs/lint/sloppy_imports_dts/run.out new file mode 100644 index 000000000..665534c64 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_dts/run.out @@ -0,0 +1,12 @@ +[class A] +undefined +[class C] +undefined +[class A] +undefined +[class C] +undefined +[class Dir] +undefined +[class Dir] +undefined diff --git a/tests/specs/lint/sloppy_imports_no_incremental_cache/__test__.jsonc b/tests/specs/lint/sloppy_imports_no_incremental_cache/__test__.jsonc new file mode 100644 index 000000000..e4596fd21 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_no_incremental_cache/__test__.jsonc @@ -0,0 +1,20 @@ +{ + "tempDir": true, + "steps": [{ + "args": "lint main.ts", + "output": "Checked 1 file\n" + }, { + "args": "lint --unstable-sloppy-imports main.ts", + "output": "Checked 1 file\n" + }, { + "args": [ + "eval", + "Deno.renameSync('file.js', 'file.ts')" + ], + "output": "" + }, { + "args": "lint --unstable-sloppy-imports main.ts", + "output": "fail_js_to_ts.out", + "exitCode": 1 + }] +} diff --git a/tests/specs/lint/sloppy_imports_no_incremental_cache/fail_js_to_ts.out b/tests/specs/lint/sloppy_imports_no_incremental_cache/fail_js_to_ts.out new file mode 100644 index 000000000..7321fd184 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_no_incremental_cache/fail_js_to_ts.out @@ -0,0 +1,11 @@ +error[no-sloppy-imports]: Sloppy imports are not allowed. + --> [WILDLINE]main.ts:1:23 + | +1 | import * as file from "./file.js"; + | ^^^^^^^^^^^ + + docs: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports + + +Found 1 problem (1 fixable via --fix) +Checked 1 file diff --git a/tests/specs/lint/sloppy_imports_no_incremental_cache/file.js b/tests/specs/lint/sloppy_imports_no_incremental_cache/file.js new file mode 100644 index 000000000..23f12c5f6 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_no_incremental_cache/file.js @@ -0,0 +1 @@ +export class File {} diff --git a/tests/specs/lint/sloppy_imports_no_incremental_cache/main.ts b/tests/specs/lint/sloppy_imports_no_incremental_cache/main.ts new file mode 100644 index 000000000..873f4f483 --- /dev/null +++ b/tests/specs/lint/sloppy_imports_no_incremental_cache/main.ts @@ -0,0 +1,3 @@ +import * as file from "./file.js"; + +console.log(file); diff --git a/tests/specs/publish/sloppy_imports/sloppy_imports.out b/tests/specs/publish/sloppy_imports/sloppy_imports.out index bfa258b93..9371e9bb6 100644 --- a/tests/specs/publish/sloppy_imports/sloppy_imports.out +++ b/tests/specs/publish/sloppy_imports/sloppy_imports.out @@ -1,5 +1,3 @@ -Warning Sloppy module resolution (hint: specify path to index.ts file in directory instead) - at file:///[WILDCARD]/mod.ts:1:20 Check file:///[WILDCARD]/mod.ts Checking for slow types in the public API... Check file:///[WILDCARD]/mod.ts diff --git a/tests/specs/run/sloppy_imports/__test__.jsonc b/tests/specs/run/sloppy_imports/__test__.jsonc new file mode 100644 index 000000000..79aaaba69 --- /dev/null +++ b/tests/specs/run/sloppy_imports/__test__.jsonc @@ -0,0 +1,10 @@ +{ + "steps": [{ + "args": "run main.ts", + "output": "no_sloppy.out", + "exitCode": 1 + }, { + "args": "run --unstable-sloppy-imports main.ts", + "output": "sloppy.out" + }] +} diff --git a/tests/specs/run/sloppy_imports/a.ts b/tests/specs/run/sloppy_imports/a.ts new file mode 100644 index 000000000..1e14df544 --- /dev/null +++ b/tests/specs/run/sloppy_imports/a.ts @@ -0,0 +1 @@ +export class A {} diff --git a/tests/specs/run/sloppy_imports/b.js b/tests/specs/run/sloppy_imports/b.js new file mode 100644 index 000000000..1aa41a54a --- /dev/null +++ b/tests/specs/run/sloppy_imports/b.js @@ -0,0 +1 @@ +export class B {} diff --git a/tests/specs/run/sloppy_imports/c.mts b/tests/specs/run/sloppy_imports/c.mts new file mode 100644 index 000000000..1ec0ebf40 --- /dev/null +++ b/tests/specs/run/sloppy_imports/c.mts @@ -0,0 +1 @@ +export class C {} diff --git a/tests/specs/run/sloppy_imports/d.mjs b/tests/specs/run/sloppy_imports/d.mjs new file mode 100644 index 000000000..01b958f66 --- /dev/null +++ b/tests/specs/run/sloppy_imports/d.mjs @@ -0,0 +1 @@ +export class D {} diff --git a/tests/specs/run/sloppy_imports/dir/index.tsx b/tests/specs/run/sloppy_imports/dir/index.tsx new file mode 100644 index 000000000..d679ef9a9 --- /dev/null +++ b/tests/specs/run/sloppy_imports/dir/index.tsx @@ -0,0 +1 @@ +export class G {} diff --git a/tests/specs/run/sloppy_imports/e.tsx b/tests/specs/run/sloppy_imports/e.tsx new file mode 100644 index 000000000..70e8d4378 --- /dev/null +++ b/tests/specs/run/sloppy_imports/e.tsx @@ -0,0 +1 @@ +export class E {} diff --git a/tests/specs/run/sloppy_imports/f.jsx b/tests/specs/run/sloppy_imports/f.jsx new file mode 100644 index 000000000..cee3fd259 --- /dev/null +++ b/tests/specs/run/sloppy_imports/f.jsx @@ -0,0 +1 @@ +export class F {} diff --git a/tests/specs/run/sloppy_imports/main.ts b/tests/specs/run/sloppy_imports/main.ts new file mode 100644 index 000000000..3bdc3fe01 --- /dev/null +++ b/tests/specs/run/sloppy_imports/main.ts @@ -0,0 +1,16 @@ +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); diff --git a/tests/specs/run/sloppy_imports/no_sloppy.out b/tests/specs/run/sloppy_imports/no_sloppy.out new file mode 100644 index 000000000..d3a205e99 --- /dev/null +++ b/tests/specs/run/sloppy_imports/no_sloppy.out @@ -0,0 +1,2 @@ +error: Module not found "file:///[WILDCARD]/a.js". Maybe change the extension to '.ts' or run with --unstable-sloppy-imports + at file:///[WILDLINE]/main.ts:1:20 diff --git a/tests/specs/run/sloppy_imports/sloppy.out b/tests/specs/run/sloppy_imports/sloppy.out new file mode 100644 index 000000000..170a4bb16 --- /dev/null +++ b/tests/specs/run/sloppy_imports/sloppy.out @@ -0,0 +1,8 @@ +[class A] +[class B] +[class C] +[class D] +[class E] +[class E] +[class F] +[class G] diff --git a/tests/specs/task/invalid_unstable_feature/invalid_unstable_feature.out b/tests/specs/task/invalid_unstable_feature/invalid_unstable_feature.out index 78c4eaca9..901eea47a 100644 --- a/tests/specs/task/invalid_unstable_feature/invalid_unstable_feature.out +++ b/tests/specs/task/invalid_unstable_feature/invalid_unstable_feature.out @@ -1,5 +1,4 @@ Task start deno run index.js -Warning Sloppy imports are not recommended and have a negative impact on performance. Warning 'abc' isn't a valid unstable feature Warning 'cba' isn't a valid unstable feature Hello unstable features diff --git a/tests/util/server/src/lsp.rs b/tests/util/server/src/lsp.rs index f21035610..b615ed475 100644 --- a/tests/util/server/src/lsp.rs +++ b/tests/util/server/src/lsp.rs @@ -1230,6 +1230,16 @@ impl CollectedDiagnostics { .collect() } + pub fn for_file(&self, specifier: &Url) -> Vec<lsp::Diagnostic> { + self + .all_messages() + .iter() + .filter(|p| p.uri == *specifier) + .flat_map(|p| p.diagnostics.iter()) + .cloned() + .collect() + } + /// Gets the messages that the editor will see after all the publishes. pub fn all_messages(&self) -> Vec<lsp::PublishDiagnosticsParams> { self.0.clone() @@ -1245,7 +1255,7 @@ impl CollectedDiagnostics { .find(|p| { p.diagnostics .iter() - .any(|d| d.source == Some(source.to_string())) + .any(|d| d.source.as_deref() == Some(source)) }) .map(ToOwned::to_owned) .unwrap() |