From 2c0f6953bef7021a40bfcf0f479552bfbbb6a1b9 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Tue, 30 Jun 2020 17:29:08 +1000 Subject: [PATCH] fixups --- cli/global_state.rs | 2 +- cli/tsc.rs | 138 +++++++++++++++++++++++--------------------- 2 files changed, 72 insertions(+), 68 deletions(-) diff --git a/cli/global_state.rs b/cli/global_state.rs index d9db998c903532..36156040c8812f 100644 --- a/cli/global_state.rs +++ b/cli/global_state.rs @@ -155,7 +155,7 @@ impl GlobalState { if self.flags.no_check { self .ts_compiler - .transpile_module_graph(self.clone(), permissions, module_graph) + .transpile(self.clone(), permissions, module_graph) .await?; } else { self diff --git a/cli/tsc.rs b/cli/tsc.rs index 903829ef1f1de6..dbdd3e20c417c3 100644 --- a/cli/tsc.rs +++ b/cli/tsc.rs @@ -689,7 +689,7 @@ impl TsCompiler { Ok(output) } - pub async fn transpile_module_graph( + pub async fn transpile( &self, global_state: GlobalState, permissions: Permissions, @@ -699,8 +699,7 @@ impl TsCompiler { for (_, value) in module_graph.iter() { let url = Url::parse(&value.url).expect("Filename is not a valid url"); if !value.url.ends_with(".d.ts") - && (!self.use_disk_cache - || !self.has_compiled_source(&global_state.file_fetcher, &url)) + && (!self.use_disk_cache || !self.has_compiled_source(&url)) { source_files.push(TranspileSourceFile { source_code: value.source_code.clone(), @@ -740,7 +739,7 @@ impl TsCompiler { let msg = execute_in_same_thread(global_state.clone(), permissions, req_msg) - .await?; + .await?; let json_str = std::str::from_utf8(&msg).unwrap(); @@ -1506,7 +1505,6 @@ fn parse_deno_types(comment: &str) -> Option { mod tests { use super::*; use crate::deno_dir; - use crate::flags; use crate::fs as deno_fs; use crate::http_cache; use deno_core::ModuleSpecifier; @@ -1627,68 +1625,74 @@ mod tests { .starts_with("//# sourceMappingURL=data:application/json;base64")); } - // #[tokio::test] - // async fn test_transpile() { - // let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) - // .parent() - // .unwrap() - // .join("cli/tests/004_set_timeout.ts"); - // let specifier = - // ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap(); - // let out = SourceFile { - // url: specifier.as_url().clone(), - // filename: PathBuf::from(p.to_str().unwrap().to_string()), - // media_type: msg::MediaType::TypeScript, - // source_code: include_bytes!("./tests/004_set_timeout.ts").to_vec(), - // types_header: None, - // }; - // let mock_state = GlobalState::mock( - // vec![ - // String::from("deno"), - // String::from("run"), - // String::from("hello.ts"), - // ], - // Some(flags::Flags { - // reload: true, - // no_check: true, - // ..flags::Flags::default() - // }), - // ); - - // let mut module_graph_loader = ModuleGraphLoader::new( - // mock_state.file_fetcher.clone(), - // None, - // Permissions::allow_all(), - // false, - // false, - // ); - // module_graph_loader - // .add_to_graph(&specifier, None) - // .await - // .expect("Failed to create graph"); - // let module_graph = module_graph_loader.get_graph(); - - // let result = mock_state - // .ts_compiler - // .transpile_module_graph( - // mock_state.clone(), - // Permissions::allow_all(), - // module_graph, - // ) - // .await; - // assert!(result.is_ok()); - // let compiled_file = mock_state - // .ts_compiler - // .get_compiled_module(&out.url) - // .unwrap(); - // let source_code = compiled_file.code; - // assert!(source_code.as_bytes().starts_with(b"setTimeout(() => {")); - // let mut lines: Vec = - // source_code.split('\n').map(|s| s.to_string()).collect(); - // let last_line = lines.pop().unwrap(); - // assert!(last_line - // .starts_with("//# sourceMappingURL=data:application/json;base64")); - // } + #[tokio::test] + async fn test_transpile() { + let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .join("cli/tests/002_hello.ts"); + let specifier = + ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap(); + let out = SourceFile { + url: specifier.as_url().clone(), + filename: PathBuf::from(p.to_str().unwrap().to_string()), + media_type: msg::MediaType::TypeScript, + source_code: include_bytes!("./tests/002_hello.ts").to_vec(), + types_header: None, + }; + let dir = + deno_dir::DenoDir::new(Some(test_util::new_deno_dir().path().to_owned())) + .unwrap(); + let http_cache = http_cache::HttpCache::new(&dir.root.join("deps")); + let mock_state = GlobalState::mock( + vec![String::from("deno"), String::from("hello.ts")], + None, + ); + let file_fetcher = SourceFileFetcher::new( + http_cache, + true, + mock_state.flags.cache_blocklist.clone(), + false, + false, + None, + ) + .unwrap(); + + let mut module_graph_loader = ModuleGraphLoader::new( + file_fetcher.clone(), + None, + Permissions::allow_all(), + false, + false, + ); + module_graph_loader + .add_to_graph(&specifier, None) + .await + .expect("Failed to create graph"); + let module_graph = module_graph_loader.get_graph(); + + let ts_compiler = TsCompiler::new( + file_fetcher, + mock_state.flags.clone(), + dir.gen_cache.clone(), + ) + .unwrap(); + + let result = ts_compiler + .transpile(mock_state.clone(), Permissions::allow_all(), module_graph) + .await; + assert!(result.is_ok()); + let compiled_file = ts_compiler.get_compiled_module(&out.url).unwrap(); + let source_code = compiled_file.code; + assert!(source_code + .as_bytes() + .starts_with(b"console.log(\"Hello World\");")); + let mut lines: Vec = + source_code.split('\n').map(|s| s.to_string()).collect(); + let last_line = lines.pop().unwrap(); + assert!(last_line + .starts_with("//# sourceMappingURL=data:application/json;base64")); + } #[tokio::test] async fn test_bundle() {