diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-07-10 14:46:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-10 14:46:25 -0400 |
commit | a49d0bd10ba2a4745c291f3f413d97396213e4ec (patch) | |
tree | 0c63c25304f465e969d2bcfb8bd71df8575c4033 /tests | |
parent | 4d2d764816d266e42f3b2251248b100abb667c83 (diff) |
fix(check): CJS types importing dual ESM/CJS package should prefer CJS types (#24492)
Closes #16370
Diffstat (limited to 'tests')
10 files changed, 92 insertions, 2 deletions
diff --git a/tests/integration/lsp_tests.rs b/tests/integration/lsp_tests.rs index d42bff4bd..2e2e00942 100644 --- a/tests/integration/lsp_tests.rs +++ b/tests/integration/lsp_tests.rs @@ -14501,6 +14501,55 @@ fn lsp_cjs_internal_types_default_export() { } #[test] +fn lsp_cjs_import_dual() { + let context = TestContextBuilder::new() + .use_http_server() + .use_temp_cwd() + .add_npm_env_vars() + .env("DENO_FUTURE", "1") + .build(); + let temp_dir = context.temp_dir(); + temp_dir.write("deno.json", r#"{}"#); + temp_dir.write( + "package.json", + r#"{ + "dependencies": { + "@denotest/cjs-import-dual": "1" + } +}"#, + ); + context.run_npm("install"); + + let mut client = context.new_lsp_command().build(); + client.initialize_default(); + let main_url = temp_dir.path().join("main.ts").uri_file(); + let diagnostics = client.did_open( + json!({ + "textDocument": { + "uri": main_url, + "languageId": "typescript", + "version": 1, + // getKind() should resolve as "cjs" and cause a type checker error + "text": "import { getKind } from 'npm:@denotest/cjs-import-dual@1';\nconst kind: 'esm' = getKind(); console.log(kind);", + } + }), + ); + assert_eq!( + json!(diagnostics.all()), + json!([{ + "range": { + "start": { "line": 1, "character": 6, }, + "end": { "line": 1, "character": 10, }, + }, + "severity": 1, + "code": 2322, + "source": "deno-ts", + "message": "Type '\"cjs\"' is not assignable to type '\"esm\"'.", + }]) + ); +} + +#[test] fn lsp_ts_code_fix_any_param() { let context = TestContextBuilder::new().use_temp_cwd().build(); let temp_dir = context.temp_dir(); diff --git a/tests/registry/npm/@denotest/cjs-import-dual/1.0.0/index.d.ts b/tests/registry/npm/@denotest/cjs-import-dual/1.0.0/index.d.ts new file mode 100644 index 000000000..55aaea82e --- /dev/null +++ b/tests/registry/npm/@denotest/cjs-import-dual/1.0.0/index.d.ts @@ -0,0 +1,2 @@ +// it should pick up the cjs types +export { getKind } from "@denotest/dual-cjs-esm"; diff --git a/tests/registry/npm/@denotest/cjs-import-dual/1.0.0/index.js b/tests/registry/npm/@denotest/cjs-import-dual/1.0.0/index.js new file mode 100644 index 000000000..45afec736 --- /dev/null +++ b/tests/registry/npm/@denotest/cjs-import-dual/1.0.0/index.js @@ -0,0 +1 @@ +module.exports.getKind = require("@denotest/dual-cjs-esm").getKind; diff --git a/tests/registry/npm/@denotest/cjs-import-dual/1.0.0/package.json b/tests/registry/npm/@denotest/cjs-import-dual/1.0.0/package.json new file mode 100644 index 000000000..78eb8d515 --- /dev/null +++ b/tests/registry/npm/@denotest/cjs-import-dual/1.0.0/package.json @@ -0,0 +1,7 @@ +{ + "name": "@denotest/cjs-import-dual", + "version": "1.0.0", + "dependencies": { + "@denotest/dual-cjs-esm": "1" + } +}
\ No newline at end of file diff --git a/tests/registry/npm/@denotest/dual-cjs-esm/1.0.0/main.d.cts b/tests/registry/npm/@denotest/dual-cjs-esm/1.0.0/main.d.cts index f969ba996..3b231fa31 100644 --- a/tests/registry/npm/@denotest/dual-cjs-esm/1.0.0/main.d.cts +++ b/tests/registry/npm/@denotest/dual-cjs-esm/1.0.0/main.d.cts @@ -1 +1 @@ -export function getKind(): string; +export function getKind(): "cjs"; diff --git a/tests/registry/npm/@denotest/dual-cjs-esm/1.0.0/main.d.mts b/tests/registry/npm/@denotest/dual-cjs-esm/1.0.0/main.d.mts index f969ba996..ef69c885d 100644 --- a/tests/registry/npm/@denotest/dual-cjs-esm/1.0.0/main.d.mts +++ b/tests/registry/npm/@denotest/dual-cjs-esm/1.0.0/main.d.mts @@ -1 +1 @@ -export function getKind(): string; +export function getKind(): "esm"; diff --git a/tests/specs/npm/cjs_import_dual/__test__.jsonc b/tests/specs/npm/cjs_import_dual/__test__.jsonc new file mode 100644 index 000000000..83fbd15cc --- /dev/null +++ b/tests/specs/npm/cjs_import_dual/__test__.jsonc @@ -0,0 +1,13 @@ +{ + "tests": { + "run": { + "args": "run main.ts", + "output": "run.out" + }, + "check": { + "args": "check --all main.ts", + "exitCode": 1, + "output": "check.out" + } + } +} diff --git a/tests/specs/npm/cjs_import_dual/check.out b/tests/specs/npm/cjs_import_dual/check.out new file mode 100644 index 000000000..be1fe86a6 --- /dev/null +++ b/tests/specs/npm/cjs_import_dual/check.out @@ -0,0 +1,9 @@ +Download http://localhost:4260/@denotest/cjs-import-dual +Download http://localhost:4260/@denotest/dual-cjs-esm +Download http://localhost:4260/@denotest/cjs-import-dual/1.0.0.tgz +Download http://localhost:4260/@denotest/dual-cjs-esm/1.0.0.tgz +Check file:///[WILDLINE]/cjs_import_dual/main.ts +error: TS2322 [ERROR]: Type '"cjs"' is not assignable to type '"esm"'. +const kind: "esm" = getKind(); // should cause a type error + ~~~~ + at file:///[WILDLINE]/cjs_import_dual/main.ts:3:7 diff --git a/tests/specs/npm/cjs_import_dual/main.ts b/tests/specs/npm/cjs_import_dual/main.ts new file mode 100644 index 000000000..1a1bd4aed --- /dev/null +++ b/tests/specs/npm/cjs_import_dual/main.ts @@ -0,0 +1,4 @@ +import { getKind } from "npm:@denotest/cjs-import-dual@1"; + +const kind: "esm" = getKind(); // should cause a type error +console.log(kind); diff --git a/tests/specs/npm/cjs_import_dual/run.out b/tests/specs/npm/cjs_import_dual/run.out new file mode 100644 index 000000000..2c05e125e --- /dev/null +++ b/tests/specs/npm/cjs_import_dual/run.out @@ -0,0 +1,5 @@ +Download http://localhost:4260/@denotest/cjs-import-dual +Download http://localhost:4260/@denotest/dual-cjs-esm +Download http://localhost:4260/@denotest/cjs-import-dual/1.0.0.tgz +Download http://localhost:4260/@denotest/dual-cjs-esm/1.0.0.tgz +cjs |