1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

fix(cli): expand tsc roots when using checkJs (#15164)

A JS file can still point to a TS file, so we need to expand the roots
in the checkJs case too.

Fixes: #15163
This commit is contained in:
Rafael Ávila de Espíndola 2022-07-14 14:40:47 +00:00 committed by GitHub
parent 2423f83ca8
commit a34ed568e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 38 deletions

View file

@ -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();

View file

@ -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(""),

View file

@ -0,0 +1,3 @@
export function bar(): string {
return 42;
}

View file

View file

@ -0,0 +1,4 @@
import { bar } from "./bar.ts";
export function foo() {
bar();
}

View file

@ -0,0 +1,3 @@
// @deno-types="./foo.d.ts"
import { foo } from "./foo.js";
foo();

View file

@ -0,0 +1,4 @@
error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'.
return 42;
~~~~~~~~~~
at [WILDCARD]