diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-10-21 11:20:18 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-21 15:20:18 +0000 |
commit | bcfe279fba865763c87f9cd8d5a2d0b2cbf451be (patch) | |
tree | 68e4d1bc52e261df50279f9ecea14795d1c46f6c /cli/tests | |
parent | 0e1a71fec6fff5fe62d7e6b2bfffb7ab877d7b71 (diff) |
feat(unstable/npm): initial type checking of npm specifiers (#16332)
Diffstat (limited to 'cli/tests')
36 files changed, 608 insertions, 17 deletions
diff --git a/cli/tests/integration/check_tests.rs b/cli/tests/integration/check_tests.rs index ab96670ef..f42cd4a7a 100644 --- a/cli/tests/integration/check_tests.rs +++ b/cli/tests/integration/check_tests.rs @@ -50,6 +50,13 @@ itest!(declaration_header_file_with_no_exports { output_str: Some(""), }); +itest!(check_npm_install_diagnostics { + args: "check --quiet check/npm_install_diagnostics/main.ts", + output: "check/npm_install_diagnostics/main.out", + envs: vec![("NO_COLOR".to_string(), "1".to_string())], + exit_code: 1, +}); + #[test] fn cache_switching_config_then_no_config() { let deno_dir = util::new_deno_dir(); diff --git a/cli/tests/integration/lsp_tests.rs b/cli/tests/integration/lsp_tests.rs index 8390b0f6f..9a0d407df 100644 --- a/cli/tests/integration/lsp_tests.rs +++ b/cli/tests/integration/lsp_tests.rs @@ -3347,6 +3347,37 @@ fn lsp_code_actions_deno_cache() { } #[test] +fn lsp_code_actions_deno_cache_npm() { + let mut session = TestSession::from_file("initialize_params.json"); + let diagnostics = session.did_open(json!({ + "textDocument": { + "uri": "file:///a/file.ts", + "languageId": "typescript", + "version": 1, + "text": "import chalk from \"npm:chalk\";\n\nconsole.log(chalk.green);\n" + } + })); + assert_eq!( + diagnostics.with_source("deno"), + load_fixture_as("code_actions/cache_npm/diagnostics.json") + ); + + let (maybe_res, maybe_err) = session + .client + .write_request( + "textDocument/codeAction", + load_fixture("code_actions/cache_npm/cache_action.json"), + ) + .unwrap(); + assert!(maybe_err.is_none()); + assert_eq!( + maybe_res, + Some(load_fixture("code_actions/cache_npm/cache_response.json")) + ); + session.shutdown_and_exit(); +} + +#[test] fn lsp_code_actions_imports() { let mut session = TestSession::from_file("initialize_params.json"); session.did_open(json!({ @@ -4047,6 +4078,169 @@ fn lsp_completions_no_snippet() { } #[test] +fn lsp_completions_npm() { + let _g = http_server(); + let mut client = init("initialize_params.json"); + did_open( + &mut client, + json!({ + "textDocument": { + "uri": "file:///a/file.ts", + "languageId": "typescript", + "version": 1, + "text": "import cjsDefault from 'npm:@denotest/cjs-default-export';import chalk from 'npm:chalk';\n\n", + } + }), + ); + let (maybe_res, maybe_err) = client + .write_request::<_, _, Value>( + "deno/cache", + json!({ + "referrer": { + "uri": "file:///a/file.ts", + }, + "uris": [ + { + "uri": "npm:@denotest/cjs-default-export", + }, + { + "uri": "npm:chalk", + } + ] + }), + ) + .unwrap(); + assert!(maybe_err.is_none()); + assert!(maybe_res.is_some()); + + // check importing a cjs default import + client + .write_notification( + "textDocument/didChange", + json!({ + "textDocument": { + "uri": "file:///a/file.ts", + "version": 2 + }, + "contentChanges": [ + { + "range": { + "start": { + "line": 2, + "character": 0 + }, + "end": { + "line": 2, + "character": 0 + } + }, + "text": "cjsDefault." + } + ] + }), + ) + .unwrap(); + read_diagnostics(&mut client); + + let (maybe_res, maybe_err) = client + .write_request( + "textDocument/completion", + json!({ + "textDocument": { + "uri": "file:///a/file.ts" + }, + "position": { + "line": 2, + "character": 11 + }, + "context": { + "triggerKind": 2, + "triggerCharacter": "." + } + }), + ) + .unwrap(); + assert!(maybe_err.is_none()); + if let Some(lsp::CompletionResponse::List(list)) = maybe_res { + assert!(!list.is_incomplete); + assert_eq!(list.items.len(), 3); + assert!(list.items.iter().any(|i| i.label == "default")); + assert!(list.items.iter().any(|i| i.label == "MyClass")); + } else { + panic!("unexpected response"); + } + let (maybe_res, maybe_err) = client + .write_request( + "completionItem/resolve", + load_fixture("completions/npm/resolve_params.json"), + ) + .unwrap(); + assert!(maybe_err.is_none()); + assert_eq!( + maybe_res, + Some(load_fixture("completions/npm/resolve_response.json")) + ); + + // now check chalk, which is esm + client + .write_notification( + "textDocument/didChange", + json!({ + "textDocument": { + "uri": "file:///a/file.ts", + "version": 3 + }, + "contentChanges": [ + { + "range": { + "start": { + "line": 2, + "character": 0 + }, + "end": { + "line": 2, + "character": 11 + } + }, + "text": "chalk." + } + ] + }), + ) + .unwrap(); + read_diagnostics(&mut client); + + let (maybe_res, maybe_err) = client + .write_request( + "textDocument/completion", + json!({ + "textDocument": { + "uri": "file:///a/file.ts" + }, + "position": { + "line": 2, + "character": 6 + }, + "context": { + "triggerKind": 2, + "triggerCharacter": "." + } + }), + ) + .unwrap(); + assert!(maybe_err.is_none()); + if let Some(lsp::CompletionResponse::List(list)) = maybe_res { + assert!(!list.is_incomplete); + assert!(list.items.iter().any(|i| i.label == "green")); + assert!(list.items.iter().any(|i| i.label == "red")); + } else { + panic!("unexpected response"); + } + + shutdown(&mut client); +} + +#[test] fn lsp_completions_registry() { let _g = http_server(); let mut client = init("initialize_params_registry.json"); diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index bc19c613d..9fc817141 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -1,6 +1,5 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -use deno_core::url::Url; use std::process::Stdio; use test_util as util; use util::assert_contains; @@ -34,7 +33,7 @@ itest!(esm_module_deno_test { }); itest!(esm_import_cjs_default { - args: "run --allow-read --allow-env --unstable --quiet npm/esm_import_cjs_default/main.js", + args: "run --allow-read --allow-env --unstable --quiet --check=all npm/esm_import_cjs_default/main.ts", output: "npm/esm_import_cjs_default/main.out", envs: env_vars(), http_server: true, @@ -84,7 +83,7 @@ itest!(translate_cjs_to_esm { }); itest!(compare_globals { - args: "run --allow-read --unstable npm/compare_globals/main.js", + args: "run --allow-read --unstable --check=all npm/compare_globals/main.ts", output: "npm/compare_globals/main.out", envs: env_vars(), http_server: true, @@ -210,6 +209,38 @@ itest!(deno_cache { http_server: true, }); +itest!(check_all { + args: "check --unstable --remote npm/check_errors/main.ts", + output: "npm/check_errors/main_all.out", + envs: env_vars(), + http_server: true, + exit_code: 1, +}); + +itest!(check_local { + args: "check --unstable npm/check_errors/main.ts", + output: "npm/check_errors/main_local.out", + envs: env_vars(), + http_server: true, + exit_code: 1, +}); + +itest!(types_ambient_module { + args: "check --unstable --quiet npm/types_ambient_module/main.ts", + output: "npm/types_ambient_module/main.out", + envs: env_vars(), + http_server: true, + exit_code: 1, +}); + +itest!(types_ambient_module_import_map { + args: "check --unstable --quiet --import-map=npm/types_ambient_module/import_map.json npm/types_ambient_module/main_import_map.ts", + output: "npm/types_ambient_module/main_import_map.out", + envs: env_vars(), + http_server: true, + exit_code: 1, +}); + #[test] fn parallel_downloading() { let (out, _err) = util::run_and_collect_output_with_args( @@ -672,18 +703,10 @@ fn ensure_registry_files_local() { } } -fn std_file_url() -> String { - let u = Url::from_directory_path(util::std_path()).unwrap(); - u.to_string() -} - fn env_vars_no_sync_download() -> Vec<(String, String)> { vec![ - ("DENO_NODE_COMPAT_URL".to_string(), std_file_url()), - ( - "DENO_NPM_REGISTRY".to_string(), - "http://localhost:4545/npm/registry/".to_string(), - ), + ("DENO_NODE_COMPAT_URL".to_string(), util::std_file_url()), + ("DENO_NPM_REGISTRY".to_string(), util::npm_registry_url()), ("NO_COLOR".to_string(), "1".to_string()), ] } diff --git a/cli/tests/testdata/check/npm_install_diagnostics/main.out b/cli/tests/testdata/check/npm_install_diagnostics/main.out new file mode 100644 index 000000000..fe46f0e42 --- /dev/null +++ b/cli/tests/testdata/check/npm_install_diagnostics/main.out @@ -0,0 +1,11 @@ +error: TS2581 [ERROR]: Cannot find name '$'. Did you mean to import jQuery? Try adding `import $ from "npm:jquery";`. +$; +^ + at file:///[WILDCARD]/npm_install_diagnostics/main.ts:1:1 + +TS2580 [ERROR]: Cannot find name 'process'. +process; +~~~~~~~ + at file:///[WILDCARD]/npm_install_diagnostics/main.ts:2:1 + +Found 2 errors. diff --git a/cli/tests/testdata/check/npm_install_diagnostics/main.ts b/cli/tests/testdata/check/npm_install_diagnostics/main.ts new file mode 100644 index 000000000..62c0c5619 --- /dev/null +++ b/cli/tests/testdata/check/npm_install_diagnostics/main.ts @@ -0,0 +1,2 @@ +$; +process; diff --git a/cli/tests/testdata/lsp/code_actions/cache_npm/cache_action.json b/cli/tests/testdata/lsp/code_actions/cache_npm/cache_action.json new file mode 100644 index 000000000..b698df3bd --- /dev/null +++ b/cli/tests/testdata/lsp/code_actions/cache_npm/cache_action.json @@ -0,0 +1,41 @@ +{ + "textDocument": { + "uri": "file:///a/file.ts" + }, + "range": { + "start": { + "line": 0, + "character": 18 + }, + "end": { + "line": 0, + "character": 29 + } + }, + "context": { + "diagnostics": [ + { + "range": { + "start": { + "line": 0, + "character": 18 + }, + "end": { + "line": 0, + "character": 29 + } + }, + "severity": 1, + "code": "no-cache-npm", + "source": "deno", + "message": "Uncached or missing npm package: \"chalk\".", + "data": { + "specifier": "npm:chalk" + } + } + ], + "only": [ + "quickfix" + ] + } +} diff --git a/cli/tests/testdata/lsp/code_actions/cache_npm/cache_response.json b/cli/tests/testdata/lsp/code_actions/cache_npm/cache_response.json new file mode 100644 index 000000000..1b41babcb --- /dev/null +++ b/cli/tests/testdata/lsp/code_actions/cache_npm/cache_response.json @@ -0,0 +1,36 @@ +[ + { + "title": "Cache \"npm:chalk\" and its dependencies.", + "kind": "quickfix", + "diagnostics": [ + { + "range": { + "start": { + "line": 0, + "character": 18 + }, + "end": { + "line": 0, + "character": 29 + } + }, + "severity": 1, + "code": "no-cache-npm", + "source": "deno", + "message": "Uncached or missing npm package: \"chalk\".", + "data": { + "specifier": "npm:chalk" + } + } + ], + "command": { + "title": "", + "command": "deno.cache", + "arguments": [ + [ + "npm:chalk" + ] + ] + } + } +] diff --git a/cli/tests/testdata/lsp/code_actions/cache_npm/diagnostics.json b/cli/tests/testdata/lsp/code_actions/cache_npm/diagnostics.json new file mode 100644 index 000000000..63c9d0029 --- /dev/null +++ b/cli/tests/testdata/lsp/code_actions/cache_npm/diagnostics.json @@ -0,0 +1,25 @@ +{ + "uri": "file:///a/file.ts", + "diagnostics": [ + { + "range": { + "start": { + "line": 0, + "character": 18 + }, + "end": { + "line": 0, + "character": 29 + } + }, + "severity": 1, + "code": "no-cache-npm", + "source": "deno", + "message": "Uncached or missing npm package: \"chalk\".", + "data": { + "specifier": "npm:chalk" + } + } + ], + "version": 1 +} diff --git a/cli/tests/testdata/lsp/completions/npm/resolve_params.json b/cli/tests/testdata/lsp/completions/npm/resolve_params.json new file mode 100644 index 000000000..c83b8ce49 --- /dev/null +++ b/cli/tests/testdata/lsp/completions/npm/resolve_params.json @@ -0,0 +1,14 @@ +{ + "label": "MyClass", + "kind": 6, + "sortText": "1", + "insertTextFormat": 1, + "data": { + "tsc": { + "specifier": "file:///a/file.ts", + "position": 69, + "name": "MyClass", + "useCodeSnippet": false + } + } +} diff --git a/cli/tests/testdata/lsp/completions/npm/resolve_response.json b/cli/tests/testdata/lsp/completions/npm/resolve_response.json new file mode 100644 index 000000000..c83b8ce49 --- /dev/null +++ b/cli/tests/testdata/lsp/completions/npm/resolve_response.json @@ -0,0 +1,14 @@ +{ + "label": "MyClass", + "kind": 6, + "sortText": "1", + "insertTextFormat": 1, + "data": { + "tsc": { + "specifier": "file:///a/file.ts", + "position": 69, + "name": "MyClass", + "useCodeSnippet": false + } + } +} diff --git a/cli/tests/testdata/npm/check_errors/main.ts b/cli/tests/testdata/npm/check_errors/main.ts new file mode 100644 index 000000000..4b8684195 --- /dev/null +++ b/cli/tests/testdata/npm/check_errors/main.ts @@ -0,0 +1,3 @@ +import * as test from "npm:@denotest/check-error"; + +console.log(test.Asdf); // should error diff --git a/cli/tests/testdata/npm/check_errors/main_all.out b/cli/tests/testdata/npm/check_errors/main_all.out new file mode 100644 index 000000000..96f16d0b9 --- /dev/null +++ b/cli/tests/testdata/npm/check_errors/main_all.out @@ -0,0 +1,19 @@ +Download http://localhost:4545/npm/registry/@denotest/check-error +Download http://localhost:4545/npm/registry/@denotest/check-error/1.0.0.tgz +Check file:///[WILDCARD]/check_errors/main.ts +error: TS2506 [ERROR]: 'Class1' is referenced directly or indirectly in its own base expression. +export class Class1 extends Class2 { + ~~~~~~ + at file:///[WILDCARD]/check-error/1.0.0/index.d.ts:2:14 + +TS2506 [ERROR]: 'Class2' is referenced directly or indirectly in its own base expression. +export class Class2 extends Class1 { + ~~~~~~ + at file:///[WILDCARD]/check-error/1.0.0/index.d.ts:5:14 + +TS2339 [ERROR]: Property 'Asdf' does not exist on type 'typeof import("file:///[WILDCARD]/@denotest/check-error/1.0.0/index.d.ts")'. +console.log(test.Asdf); // should error + ~~~~ + at file:///[WILDCARD]/check_errors/main.ts:3:18 + +Found 3 errors. diff --git a/cli/tests/testdata/npm/check_errors/main_local.out b/cli/tests/testdata/npm/check_errors/main_local.out new file mode 100644 index 000000000..1624b98bc --- /dev/null +++ b/cli/tests/testdata/npm/check_errors/main_local.out @@ -0,0 +1,7 @@ +Download http://localhost:4545/npm/registry/@denotest/check-error +Download http://localhost:4545/npm/registry/@denotest/check-error/1.0.0.tgz +Check file:///[WILDCARD]/check_errors/main.ts +error: TS2339 [ERROR]: Property 'Asdf' does not exist on type 'typeof import("file:///[WILDCARD]/@denotest/check-error/1.0.0/index.d.ts")'. +console.log(test.Asdf); // should error + ~~~~ + at file:///[WILDCARD]/npm/check_errors/main.ts:3:18 diff --git a/cli/tests/testdata/npm/compare_globals/main.js b/cli/tests/testdata/npm/compare_globals/main.js deleted file mode 100644 index ce43e32b1..000000000 --- a/cli/tests/testdata/npm/compare_globals/main.js +++ /dev/null @@ -1,2 +0,0 @@ -import * as globals from "npm:@denotest/globals"; -console.log(globals.global === globals.globalThis); diff --git a/cli/tests/testdata/npm/compare_globals/main.out b/cli/tests/testdata/npm/compare_globals/main.out index a1a5c0e8f..1b22fd318 100644 --- a/cli/tests/testdata/npm/compare_globals/main.out +++ b/cli/tests/testdata/npm/compare_globals/main.out @@ -1,3 +1,8 @@ Download http://localhost:4545/npm/registry/@denotest/globals +Download http://localhost:4545/npm/registry/@types/node Download http://localhost:4545/npm/registry/@denotest/globals/1.0.0.tgz +Download http://localhost:4545/npm/registry/@types/node/node-18.8.2.tgz +Check file:///[WILDCARD]/npm/compare_globals/main.ts +Check file:///[WILDCARD]/std/node/module_all.ts true +[] diff --git a/cli/tests/testdata/npm/compare_globals/main.ts b/cli/tests/testdata/npm/compare_globals/main.ts new file mode 100644 index 000000000..5710d0bd5 --- /dev/null +++ b/cli/tests/testdata/npm/compare_globals/main.ts @@ -0,0 +1,14 @@ +/// <reference types="npm:@types/node" /> + +import * as globals from "npm:@denotest/globals"; +console.log(globals.global === globals.globalThis); +console.log(globals.process.execArgv); + +type AssertTrue<T extends true> = never; +type _TestNoProcessGlobal = AssertTrue< + typeof globalThis extends { process: any } ? false : true +>; +type _TestHasNodeJsGlobal = NodeJS.Architecture; + +const controller = new AbortController(); +controller.abort("reason"); // in the NodeJS declaration it doesn't have a reason diff --git a/cli/tests/testdata/npm/esm_import_cjs_default/main.js b/cli/tests/testdata/npm/esm_import_cjs_default/main.ts index f405a5899..f9c3280e5 100644 --- a/cli/tests/testdata/npm/esm_import_cjs_default/main.js +++ b/cli/tests/testdata/npm/esm_import_cjs_default/main.ts @@ -1,3 +1,4 @@ +// @ts-check import cjsDefault, { MyClass as MyCjsClass, } from "npm:@denotest/cjs-default-export"; diff --git a/cli/tests/testdata/npm/registry/@denotest/check-error/1.0.0/index.d.ts b/cli/tests/testdata/npm/registry/@denotest/check-error/1.0.0/index.d.ts new file mode 100644 index 000000000..673c0035e --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/check-error/1.0.0/index.d.ts @@ -0,0 +1,6 @@ +// intentional type checking errors +export class Class1 extends Class2 { +} + +export class Class2 extends Class1 { +} diff --git a/cli/tests/testdata/npm/registry/@denotest/check-error/1.0.0/index.js b/cli/tests/testdata/npm/registry/@denotest/check-error/1.0.0/index.js new file mode 100644 index 000000000..7eb6b784d --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/check-error/1.0.0/index.js @@ -0,0 +1,6 @@ +module.exports = { + Class1: class { + }, + Class2: class { + }, +}; diff --git a/cli/tests/testdata/npm/registry/@denotest/check-error/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/check-error/1.0.0/package.json new file mode 100644 index 000000000..295920a8f --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/check-error/1.0.0/package.json @@ -0,0 +1,5 @@ +{ + "name": "@denotest/check-error", + "version": "1.0.0", + "types": "./index.d.ts" +} diff --git a/cli/tests/testdata/npm/registry/@denotest/cjs-default-export/1.0.0/index.d.ts b/cli/tests/testdata/npm/registry/@denotest/cjs-default-export/1.0.0/index.d.ts new file mode 100644 index 000000000..90fdfe5f6 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/cjs-default-export/1.0.0/index.d.ts @@ -0,0 +1,6 @@ +export default function (): number; +export declare function named(): number; +declare class MyClass { + static someStaticMethod(): string; +} +export { MyClass }; diff --git a/cli/tests/testdata/npm/registry/@denotest/cjs-default-export/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/cjs-default-export/1.0.0/package.json index 4765d25d2..8da28b919 100644 --- a/cli/tests/testdata/npm/registry/@denotest/cjs-default-export/1.0.0/package.json +++ b/cli/tests/testdata/npm/registry/@denotest/cjs-default-export/1.0.0/package.json @@ -1,4 +1,5 @@ { "name": "@denotest/cjs-default-export", - "version": "1.0.0" + "version": "1.0.0", + "types": "./index.d.ts" } diff --git a/cli/tests/testdata/npm/registry/@denotest/esm-import-cjs-default/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/esm-import-cjs-default/1.0.0/package.json index 184076799..f757a08fb 100644 --- a/cli/tests/testdata/npm/registry/@denotest/esm-import-cjs-default/1.0.0/package.json +++ b/cli/tests/testdata/npm/registry/@denotest/esm-import-cjs-default/1.0.0/package.json @@ -1,6 +1,7 @@ { "name": "@denotest/esm-import-cjs-default", "version": "1.0.0", + "main": "index.mjs", "dependencies": { "@denotest/cjs-default-export": "^1.0.0" } diff --git a/cli/tests/testdata/npm/registry/@denotest/globals/1.0.0/index.d.ts b/cli/tests/testdata/npm/registry/@denotest/globals/1.0.0/index.d.ts new file mode 100644 index 000000000..ee03712dd --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/globals/1.0.0/index.d.ts @@ -0,0 +1,13 @@ +declare const tempGlobalThis: typeof globalThis; +declare const tempGlobal: typeof global; +declare const tempProcess: typeof process; +export { + tempGlobalThis as globalThis, + tempGlobal as global, + tempProcess as process, +}; + +type AssertTrue<T extends true> = never; +type _TestHasProcessGlobal = AssertTrue< + typeof globalThis extends { process: any } ? true : false +>; diff --git a/cli/tests/testdata/npm/registry/@denotest/globals/1.0.0/index.js b/cli/tests/testdata/npm/registry/@denotest/globals/1.0.0/index.js index be5e6e5ac..50d2d3d2a 100644 --- a/cli/tests/testdata/npm/registry/@denotest/globals/1.0.0/index.js +++ b/cli/tests/testdata/npm/registry/@denotest/globals/1.0.0/index.js @@ -1,2 +1,3 @@ exports.globalThis = globalThis; exports.global = global; +exports.process = process; diff --git a/cli/tests/testdata/npm/registry/@denotest/globals/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/globals/1.0.0/package.json index cba0742c1..1ce42ded4 100644 --- a/cli/tests/testdata/npm/registry/@denotest/globals/1.0.0/package.json +++ b/cli/tests/testdata/npm/registry/@denotest/globals/1.0.0/package.json @@ -1,4 +1,5 @@ { "name": "@denotest/globals", - "version": "1.0.0" + "version": "1.0.0", + "types": "index.d.ts" } diff --git a/cli/tests/testdata/npm/registry/@denotest/types-ambient/1.0.0/index.d.ts b/cli/tests/testdata/npm/registry/@denotest/types-ambient/1.0.0/index.d.ts new file mode 100644 index 000000000..fc2199884 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-ambient/1.0.0/index.d.ts @@ -0,0 +1,10 @@ +// Some packages do this. It's really not ideal because instead of allowing +// the package to be resolved at any specifier, it instead expects the package +// to be resolved via a "@denotest/types-ambient" specifier. To make this work, +// we've currently modified the typescript compiler to check for any "<package-name>" +// ambient modules when resolving an npm specifier at "npm:<package-name>" +declare module "@denotest/types-ambient" { + class Test { + prop: number; + } +} diff --git a/cli/tests/testdata/npm/registry/@denotest/types-ambient/1.0.0/index.js b/cli/tests/testdata/npm/registry/@denotest/types-ambient/1.0.0/index.js new file mode 100644 index 000000000..47ff7adb2 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-ambient/1.0.0/index.js @@ -0,0 +1,3 @@ +export class Test { + prop = 5; +} diff --git a/cli/tests/testdata/npm/registry/@denotest/types-ambient/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/types-ambient/1.0.0/package.json new file mode 100644 index 000000000..ef927cbe3 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-ambient/1.0.0/package.json @@ -0,0 +1,5 @@ +{ + "name": "@denotest/types-ambient", + "version": "1.0.0", + "types": "./index.d.ts" +} diff --git a/cli/tests/testdata/npm/registry/@types/node/node-18.8.2.tgz b/cli/tests/testdata/npm/registry/@types/node/node-18.8.2.tgz Binary files differnew file mode 100644 index 000000000..8afc9d21d --- /dev/null +++ b/cli/tests/testdata/npm/registry/@types/node/node-18.8.2.tgz diff --git a/cli/tests/testdata/npm/registry/@types/node/registry.json b/cli/tests/testdata/npm/registry/@types/node/registry.json new file mode 100644 index 000000000..3fff1578e --- /dev/null +++ b/cli/tests/testdata/npm/registry/@types/node/registry.json @@ -0,0 +1,73 @@ +{ + "_id": "@types/node", + "_rev": "8944-025a921c7561ec7339c70b87995cb358", + "name": "@types/node", + "description": "TypeScript definitions for Node.js", + "dist-tags": { + "latest": "18.8.2" + }, + "versions": { + "18.8.2": { + "name": "@types/node", + "version": "18.8.2", + "description": "TypeScript definitions for Node.js", + "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node", + "license": "MIT", + "contributors": [ + ], + "main": "", + "types": "index.d.ts", + "typesVersions": { "<4.9.0-0": { "*": ["ts4.8/*"] } }, + "repository": { + "type": "git", + "url": "https://github.com/DefinitelyTyped/DefinitelyTyped.git", + "directory": "types/node" + }, + "scripts": {}, + "dependencies": {}, + "typesPublisherContentHash": "034172ea945b66afc6502e6be34d6fb957c596091e39cf43672e8aca563a8c66", + "typeScriptVersion": "4.1", + "_id": "@types/node@18.8.2", + "dist": { + "integrity": "sha512-cRMwIgdDN43GO4xMWAfJAecYn8wV4JbsOGHNfNUIDiuYkUYAR5ec4Rj7IO2SAhFPEfpPtLtUTbbny/TCT7aDwA==", + "shasum": "17d42c6322d917764dd3d2d3a10d7884925de067", + "tarball": "http://localhost:4545/npm/registry/@types/node/node-18.8.2.tgz", + "fileCount": 124, + "unpackedSize": 3524549, + "signatures": [ + { + "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA", + "sig": "MEYCIQCAqI3XibndhBD647C/13AFb58Fhmg4WmfCoGrIYrgtzwIhAIB0b0D58Tigwb3qKaOVsKnuYOOr0strAmprZSCi/+oq" + } + ], + "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjPFItACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrKAg/+IwaUWPgePlO4IxW7CVhFEEFiyhjEH3FHe0ogC3YmreoBFv/A\r\nPwQrwObdskbGWrBzsAOVFvhzYktzP3kc857HtU2ia9FXeaEYvsSQBqh6jZfA\r\njR1+h+jn+W5JnmbnwRGfNn2riCo/un4tYoZ4o/bKiMdNd9WrdIs0Oii1Dd4N\r\nnsBXPb05UPPP4Uu8cz68u1bj+QQ6aslr6keGNyNeILf8bJsEfcfVkEO3l4cu\r\njyTIrxiD+tM8jrUE9CDeodF8CZNuvLh3hqdMPJeH3U47qkDtPDKEOvZTbyYm\r\ngodto6mcnuBr8F9vmikAQfGiXV0U2cg2XRjWc1lI8HT4X0kESTIo+nzNuliD\r\niTpfjyZHdKBGGIuHP1Ou9dVvx4t5XZ1JsK9EK5WTilvAlu/qZrynxXxAV3Rc\r\nvL9UhIacISprMWB3Sohl9ZtfcmGnV/KMRpM+yPZOWp39gQQCKaKF/j2f/mir\r\n8YFbFUnySZkXKzxgsgjrSsh9QiK2dYAzcqHlazITeFN9jOYRzYUjAFj3qOFm\r\n7o1eJpW0qM5vgR+fPq30WxcdcQ4PaWgHSlb/ll8hiwQG1ZUihO/1RU7FtDoc\r\n1KwcfrGOAJPL6vBSLPReUkhDIUTSBwfmvfMxzqD1aDp6YV5WX7h03B0dWbPJ\r\nmPJmJZjjZje4Trk9jBJ5/ZLpB8/zkrDKvhE=\r\n=LPWa\r\n-----END PGP SIGNATURE-----\r\n" + }, + "_npmUser": { "name": "types", "email": "ts-npm-types@microsoft.com" }, + "directories": {}, + "maintainers": [ + { "name": "types", "email": "ts-npm-types@microsoft.com" } + ], + "_npmOperationalInternal": { + "host": "s3://npm-registry-packages", + "tmp": "tmp/node_18.8.2_1664897581729_0.9746861340465625" + }, + "_hasShrinkwrap": false + } + }, + "readme": "[object Object]", + "maintainers": [{ "name": "types", "email": "ts-npm-types@microsoft.com" }], + "time": { + "18.8.2": "2022-10-04T15:33:01.913Z" + }, + "license": "MIT", + "readmeFilename": "", + "repository": { + "type": "git", + "url": "https://github.com/DefinitelyTyped/DefinitelyTyped.git", + "directory": "types/node" + }, + "users": { + }, + "contributors": [], + "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node" +} diff --git a/cli/tests/testdata/npm/types_ambient_module/import_map.json b/cli/tests/testdata/npm/types_ambient_module/import_map.json new file mode 100644 index 000000000..f61d99b47 --- /dev/null +++ b/cli/tests/testdata/npm/types_ambient_module/import_map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "types-ambient": "npm:@denotest/types-ambient" + } +} diff --git a/cli/tests/testdata/npm/types_ambient_module/main.out b/cli/tests/testdata/npm/types_ambient_module/main.out new file mode 100644 index 000000000..c84130707 --- /dev/null +++ b/cli/tests/testdata/npm/types_ambient_module/main.out @@ -0,0 +1,21 @@ +error: TS2551 [ERROR]: Property 'Test2' does not exist on type 'typeof import("@denotest/types-ambient")'. Did you mean 'Test'? +console.log(import1.Test2); // should error + ~~~~~ + at file:///[WILDCARD]/types_ambient_module/main.ts:5:21 + + 'Test' is declared here. + class Test { + ~~~~ + at file:///[WILDCARD]/@denotest/types-ambient/1.0.0/index.d.ts:7:9 + +TS2551 [ERROR]: Property 'Test2' does not exist on type 'typeof import("@denotest/types-ambient")'. Did you mean 'Test'? +console.log(import2.Test2); // should error + ~~~~~ + at file:///[WILDCARD]/types_ambient_module/main.ts:7:21 + + 'Test' is declared here. + class Test { + ~~~~ + at file:///[WILDCARD]/@denotest/types-ambient/1.0.0/index.d.ts:7:9 + +Found 2 errors. diff --git a/cli/tests/testdata/npm/types_ambient_module/main.ts b/cli/tests/testdata/npm/types_ambient_module/main.ts new file mode 100644 index 000000000..8f77cabe8 --- /dev/null +++ b/cli/tests/testdata/npm/types_ambient_module/main.ts @@ -0,0 +1,7 @@ +import * as import1 from "npm:@denotest/types-ambient"; +import * as import2 from "npm:@denotest/types-ambient@1"; + +console.log(import1.Test); +console.log(import1.Test2); // should error +console.log(import2.Test); +console.log(import2.Test2); // should error diff --git a/cli/tests/testdata/npm/types_ambient_module/main_import_map.out b/cli/tests/testdata/npm/types_ambient_module/main_import_map.out new file mode 100644 index 000000000..548f9b479 --- /dev/null +++ b/cli/tests/testdata/npm/types_ambient_module/main_import_map.out @@ -0,0 +1,9 @@ +error: TS2551 [ERROR]: Property 'Test2' does not exist on type 'typeof import("@denotest/types-ambient")'. Did you mean 'Test'? +console.log(mod.Test2); // should error + ~~~~~ + at file:///[WILDCARD]/main_import_map.ts:4:17 + + 'Test' is declared here. + class Test { + ~~~~ + at file:///[WILDCARD]/@denotest/types-ambient/1.0.0/index.d.ts:7:9 diff --git a/cli/tests/testdata/npm/types_ambient_module/main_import_map.ts b/cli/tests/testdata/npm/types_ambient_module/main_import_map.ts new file mode 100644 index 000000000..2694c94b7 --- /dev/null +++ b/cli/tests/testdata/npm/types_ambient_module/main_import_map.ts @@ -0,0 +1,4 @@ +import * as mod from "npm:@denotest/types-ambient"; + +console.log(mod.Test); +console.log(mod.Test2); // should error |