diff options
-rw-r--r-- | cli/media_type.rs | 17 | ||||
-rw-r--r-- | cli/module_graph.rs | 21 | ||||
-rw-r--r-- | cli/tests/module_graph/file_tests-a.mjs | 3 | ||||
-rw-r--r-- | cli/tests/module_graph/file_tests-b.ts | 1 | ||||
-rw-r--r-- | cli/tsc.rs | 59 |
5 files changed, 90 insertions, 11 deletions
diff --git a/cli/media_type.rs b/cli/media_type.rs index 7be494a3a..8280c1180 100644 --- a/cli/media_type.rs +++ b/cli/media_type.rs @@ -94,19 +94,16 @@ impl MediaType { }, }, Some(os_str) => match os_str.to_str() { - Some("ts") => match path.file_stem() { - Some(os_str) => match os_str.to_str() { - Some(file_name) => { + Some("ts") => { + if let Some(os_str) = path.file_stem() { + if let Some(file_name) = os_str.to_str() { if file_name.ends_with(".d") { - MediaType::Dts - } else { - MediaType::TypeScript + return MediaType::Dts; } } - None => MediaType::TypeScript, - }, - None => MediaType::TypeScript, - }, + } + MediaType::TypeScript + } Some("tsx") => MediaType::TSX, Some("js") => MediaType::JavaScript, Some("jsx") => MediaType::JSX, diff --git a/cli/module_graph.rs b/cli/module_graph.rs index 493bc2dc4..bf3972904 100644 --- a/cli/module_graph.rs +++ b/cli/module_graph.rs @@ -2008,6 +2008,27 @@ pub mod tests { } #[tokio::test] + async fn fix_graph_check_mjs_root() { + let specifier = ModuleSpecifier::resolve_url_or_path("file:///tests/a.mjs") + .expect("could not resolve module"); + let (graph, handler) = setup(specifier).await; + let result_info = graph + .check(CheckOptions { + debug: false, + emit: true, + lib: TypeLib::DenoWindow, + maybe_config_path: None, + reload: false, + }) + .expect("should have checked"); + assert!(result_info.maybe_ignored_options.is_none()); + assert!(result_info.diagnostics.is_empty()); + let h = handler.borrow(); + assert_eq!(h.cache_calls.len(), 1); + assert_eq!(h.tsbuildinfo_calls.len(), 1); + } + + #[tokio::test] async fn fix_graph_check_types_root() { let specifier = ModuleSpecifier::resolve_url_or_path("file:///typesref.js") .expect("could not resolve module"); diff --git a/cli/tests/module_graph/file_tests-a.mjs b/cli/tests/module_graph/file_tests-a.mjs new file mode 100644 index 000000000..72b3a67bc --- /dev/null +++ b/cli/tests/module_graph/file_tests-a.mjs @@ -0,0 +1,3 @@ +import * as b from "./b.ts"; + +console.log(b); diff --git a/cli/tests/module_graph/file_tests-b.ts b/cli/tests/module_graph/file_tests-b.ts new file mode 100644 index 000000000..59d168993 --- /dev/null +++ b/cli/tests/module_graph/file_tests-b.ts @@ -0,0 +1 @@ +export const b = "b"; diff --git a/cli/tsc.rs b/cli/tsc.rs index 7f90dd7b2..bcaeaaeb6 100644 --- a/cli/tsc.rs +++ b/cli/tsc.rs @@ -22,6 +22,7 @@ use deno_core::Snapshot; use serde::Deserialize; use std::cell::RefCell; use std::collections::HashMap; +use std::path::PathBuf; use std::rc::Rc; /// Provide static assets that are not preloaded in the compiler snapshot. @@ -63,6 +64,41 @@ fn get_maybe_hash( } } +/// tsc only supports `.ts`, `.tsx`, `.d.ts`, `.js`, or `.jsx` as root modules +/// and so we have to detect the apparent media type based on extensions it +/// supports. +fn get_tsc_media_type(specifier: &ModuleSpecifier) -> MediaType { + let url = specifier.as_url(); + let path = if url.scheme() == "file" { + if let Ok(path) = url.to_file_path() { + path + } else { + PathBuf::from(url.path()) + } + } else { + PathBuf::from(url.path()) + }; + match path.extension() { + None => MediaType::Unknown, + Some(os_str) => match os_str.to_str() { + Some("ts") => { + if let Some(os_str) = path.file_stem() { + if let Some(file_name) = os_str.to_str() { + if file_name.ends_with(".d") { + return MediaType::Dts; + } + } + } + MediaType::TypeScript + } + Some("tsx") => MediaType::TSX, + Some("js") => MediaType::JavaScript, + Some("jsx") => MediaType::JSX, + _ => MediaType::Unknown, + }, + } +} + #[derive(Debug, Clone, Default, Eq, PartialEq)] pub struct EmittedFile { pub data: String, @@ -337,7 +373,7 @@ pub fn exec( .root_names .iter() .map(|(s, mt)| { - let ext_media_type = MediaType::from(&s.as_str().to_owned()); + let ext_media_type = get_tsc_media_type(s); if mt != &ext_media_type { let new_specifier = format!("{}{}", s, mt.as_ts_extension()); root_map.insert(new_specifier.clone(), s.clone()); @@ -490,6 +526,27 @@ mod tests { ); } + #[test] + fn test_get_tsc_media_type() { + let fixtures = vec![ + ("file:///a.ts", MediaType::TypeScript), + ("file:///a.tsx", MediaType::TSX), + ("file:///a.d.ts", MediaType::Dts), + ("file:///a.js", MediaType::JavaScript), + ("file:///a.jsx", MediaType::JSX), + ("file:///a.cjs", MediaType::Unknown), + ("file:///a.mjs", MediaType::Unknown), + ("file:///a.json", MediaType::Unknown), + ("file:///a.wasm", MediaType::Unknown), + ("file:///a.js.map", MediaType::Unknown), + ("file:///.tsbuildinfo", MediaType::Unknown), + ]; + for (specifier, media_type) in fixtures { + let specifier = ModuleSpecifier::resolve_url_or_path(specifier).unwrap(); + assert_eq!(get_tsc_media_type(&specifier), media_type); + } + } + #[tokio::test] async fn test_emit() { let mut state = setup(None, None, None).await; |