diff --git a/cli/emit.rs b/cli/emit.rs index 924af49c4d..c7a31fc0f9 100644 --- a/cli/emit.rs +++ b/cli/emit.rs @@ -199,50 +199,37 @@ pub fn get_ts_config_for_emit( /// Transform the graph into root specifiers that we can feed `tsc`. We have to /// provide the media type for root modules because `tsc` does not "resolve" the /// media type like other modules, as well as a root specifier needs any -/// redirects resolved. If we aren't checking JavaScript, we need to include all -/// the emittable files in the roots, so they get type checked and optionally -/// emitted, otherwise they would be ignored if only imported into JavaScript. +/// redirects resolved. We need to include all the emittable files in +/// the roots, so they get type checked and optionally emitted, +/// otherwise they would be ignored if only imported into JavaScript. fn get_tsc_roots( - roots: &[(ModuleSpecifier, ModuleKind)], graph_data: &GraphData, check_js: bool, ) -> Vec<(ModuleSpecifier, MediaType)> { - if !check_js { - graph_data - .entries() - .into_iter() - .filter_map(|(specifier, module_entry)| match module_entry { - ModuleEntry::Module { - media_type, - ts_check, - .. - } => match &media_type { - MediaType::TypeScript - | MediaType::Tsx - | MediaType::Mts - | MediaType::Cts - | MediaType::Jsx => Some((specifier.clone(), *media_type)), - MediaType::JavaScript | MediaType::Mjs | MediaType::Cjs - if check_js || *ts_check => - { - Some((specifier.clone(), *media_type)) - } - _ => None, - }, - _ => None, - }) - .collect() - } else { - roots - .iter() - .filter_map(|(specifier, _)| match graph_data.get(specifier) { - Some(ModuleEntry::Module { media_type, .. }) => { + graph_data + .entries() + .into_iter() + .filter_map(|(specifier, module_entry)| match module_entry { + ModuleEntry::Module { + media_type, + ts_check, + .. + } => match &media_type { + MediaType::TypeScript + | MediaType::Tsx + | MediaType::Mts + | MediaType::Cts + | MediaType::Jsx => Some((specifier.clone(), *media_type)), + MediaType::JavaScript | MediaType::Mjs | MediaType::Cjs + if check_js || *ts_check => + { Some((specifier.clone(), *media_type)) } _ => None, - }) - .collect() - } + }, + _ => None, + }) + .collect() } /// A hashing function that takes the source code, version and optionally a @@ -328,7 +315,7 @@ pub fn check( return Ok(Default::default()); } - let root_names = get_tsc_roots(roots, &segment_graph_data, check_js); + let root_names = get_tsc_roots(&segment_graph_data, check_js); if options.log_checks { for (root, _) in roots { let root_str = root.to_string(); diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index b450d0d447..bb46fe1b5c 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -2701,6 +2701,13 @@ fn check_local_then_remote() { assert_contains!(stderr, "Type 'string' is not assignable to type 'number'."); } +// Regression test for https://github.com/denoland/deno/issues/15163 +itest!(check_js_points_to_ts { + args: "run --quiet --check --config checkjs.tsconfig.json run/check_js_points_to_ts/test.js", + output: "run/check_js_points_to_ts/test.js.out", + exit_code: 1, +}); + itest!(no_prompt_flag { args: "run --quiet --unstable --no-prompt no_prompt.ts", output_str: Some(""), diff --git a/cli/tests/testdata/run/check_js_points_to_ts/bar.ts b/cli/tests/testdata/run/check_js_points_to_ts/bar.ts new file mode 100644 index 0000000000..026cd2f1e0 --- /dev/null +++ b/cli/tests/testdata/run/check_js_points_to_ts/bar.ts @@ -0,0 +1,3 @@ +export function bar(): string { + return 42; +} diff --git a/cli/tests/testdata/run/check_js_points_to_ts/foo.d.ts b/cli/tests/testdata/run/check_js_points_to_ts/foo.d.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/cli/tests/testdata/run/check_js_points_to_ts/foo.js b/cli/tests/testdata/run/check_js_points_to_ts/foo.js new file mode 100644 index 0000000000..9ac1a14ff9 --- /dev/null +++ b/cli/tests/testdata/run/check_js_points_to_ts/foo.js @@ -0,0 +1,4 @@ +import { bar } from "./bar.ts"; +export function foo() { + bar(); +} diff --git a/cli/tests/testdata/run/check_js_points_to_ts/test.js b/cli/tests/testdata/run/check_js_points_to_ts/test.js new file mode 100644 index 0000000000..00d894451f --- /dev/null +++ b/cli/tests/testdata/run/check_js_points_to_ts/test.js @@ -0,0 +1,3 @@ +// @deno-types="./foo.d.ts" +import { foo } from "./foo.js"; +foo(); diff --git a/cli/tests/testdata/run/check_js_points_to_ts/test.js.out b/cli/tests/testdata/run/check_js_points_to_ts/test.js.out new file mode 100644 index 0000000000..c052a7c3d9 --- /dev/null +++ b/cli/tests/testdata/run/check_js_points_to_ts/test.js.out @@ -0,0 +1,4 @@ +error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. + return 42; + ~~~~~~~~~~ + at [WILDCARD]