From f82b9ba329856b9b52c831b456e4823fad62f858 Mon Sep 17 00:00:00 2001 From: Giorgi Rostomashvili Date: Mon, 6 Apr 2020 22:52:25 +0200 Subject: [PATCH] fix: fetch reference types for JS files (#4652) Fixes #4000 and fixes #4476. Now always tries to fetch reference types for JS files. Does not throw if it fails, since Typescript compiler will complain if the file is not there(it will try to fetch it again first) and people who just use JS should not be bothered by this error. Not sure about my test, it passes and catches the bug but maybe there is a better way to express it. --- cli/global_state.rs | 12 ++++++++++++ cli/tests/integration_tests.rs | 7 +++++++ cli/tests/type_directives_js_main.js | 3 +++ cli/tests/type_directives_js_main.js.out | 3 +++ 4 files changed, 25 insertions(+) create mode 100644 cli/tests/type_directives_js_main.js create mode 100644 cli/tests/type_directives_js_main.js.out diff --git a/cli/global_state.rs b/cli/global_state.rs index 129ab276a8..c9383bd881 100644 --- a/cli/global_state.rs +++ b/cli/global_state.rs @@ -138,6 +138,18 @@ impl GlobalState { .compile(state1.clone(), &out, target_lib) .await } else { + if let Some(types_url) = out.types_url.clone() { + let types_specifier = ModuleSpecifier::from(types_url); + state1 + .file_fetcher + .fetch_source_file( + &types_specifier, + Some(module_specifier.clone()), + ) + .await + .ok(); + }; + state1.js_compiler.compile(out).await } } diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 227566432e..bef4c8d566 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -1415,6 +1415,13 @@ itest!(type_directives_02 { output: "type_directives_02.ts.out", }); +itest!(type_directives_js_main { + args: "run --reload -L debug type_directives_js_main.js", + output: "type_directives_js_main.js.out", + check_stderr: true, + exit_code: 0, +}); + itest!(types { args: "types", output: "types.out", diff --git a/cli/tests/type_directives_js_main.js b/cli/tests/type_directives_js_main.js new file mode 100644 index 0000000000..f7274bf26d --- /dev/null +++ b/cli/tests/type_directives_js_main.js @@ -0,0 +1,3 @@ +import * as foo from "./subdir/type_reference.js"; + +console.log(foo.foo); diff --git a/cli/tests/type_directives_js_main.js.out b/cli/tests/type_directives_js_main.js.out new file mode 100644 index 0000000000..714dbd0b77 --- /dev/null +++ b/cli/tests/type_directives_js_main.js.out @@ -0,0 +1,3 @@ +[WILDCARD] +fetch_source_file specifier: file:[WILDCARD]cli/tests/subdir/type_reference.d.ts +[WILDCARD] \ No newline at end of file