diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-08-04 16:08:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-04 16:08:41 +0200 |
commit | 55ea9c7e856cae5471fea309d9dc40444581c3ae (patch) | |
tree | fff7bb6729e0b0c137909a6533563ea8658fd50f | |
parent | 2cd1fe8edf1b9ce89f12e70738b5bcae8b177d0b (diff) |
refactor: remove repeated code in main.rs (#6954)
-rw-r--r-- | cli/main.rs | 75 |
1 files changed, 26 insertions, 49 deletions
diff --git a/cli/main.rs b/cli/main.rs index 191355a0c..97f3161df 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -286,22 +286,18 @@ async fn print_file_info( } fn get_types(unstable: bool) -> String { + let mut types = format!( + "{}\n{}\n{}", + crate::js::DENO_NS_LIB, + crate::js::SHARED_GLOBALS_LIB, + crate::js::WINDOW_LIB, + ); + if unstable { - format!( - "{}\n{}\n{}\n{}", - crate::js::DENO_NS_LIB, - crate::js::SHARED_GLOBALS_LIB, - crate::js::WINDOW_LIB, - crate::js::UNSTABLE_NS_LIB, - ) - } else { - format!( - "{}\n{}\n{}", - crate::js::DENO_NS_LIB, - crate::js::SHARED_GLOBALS_LIB, - crate::js::WINDOW_LIB, - ) + types.push_str(&format!("\n{}", crate::js::UNSTABLE_NS_LIB,)); } + + types } async fn info_command( @@ -348,20 +344,9 @@ async fn lint_command( files: Vec<String>, list_rules: bool, ) -> Result<(), ErrBox> { - let global_state = GlobalState::new(flags)?; - - // TODO(bartlomieju): refactor, it's non-sense to create - // state just to perform unstable check... - use crate::state::State; - let state = State::new( - global_state, - None, - ModuleSpecifier::resolve_url("file:///dummy.ts").unwrap(), - None, - true, - )?; - - state.check_unstable("lint"); + if !flags.unstable { + exit_unstable("lint"); + } if list_rules { lint::print_rules_list(); @@ -396,13 +381,14 @@ async fn eval_command( let main_module = ModuleSpecifier::resolve_url_or_path("./__$deno$eval.ts").unwrap(); let global_state = GlobalState::new(flags)?; - let mut worker = MainWorker::create(global_state, main_module.clone())?; + let mut worker = + MainWorker::create(global_state.clone(), main_module.clone())?; let main_module_url = main_module.as_url().to_owned(); // Create a dummy source file. let source_code = if print { - "console.log(".to_string() + &code + ")" + format!("console.log({})", code) } else { - code.clone() + code } .into_bytes(); @@ -418,11 +404,8 @@ async fn eval_command( source_code: TextDocument::new(source_code, Some("utf-8")), }; // Save our fake file into file fetcher cache - // to allow module access by TS compiler (e.g. op_fetch_source_files) - worker - .state - .borrow() - .global_state + // to allow module access by TS compiler. + global_state .file_fetcher .save_source_file_in_cache(&main_module, source_file); debug!("main_module {}", &main_module); @@ -520,11 +503,11 @@ async fn doc_command( &self, specifier: &str, ) -> Pin<Box<dyn Future<Output = Result<String, OpError>>>> { - let specifier = - ModuleSpecifier::resolve_url_or_path(specifier).expect("Bad specifier"); let fetcher = self.clone(); - + let specifier = specifier.to_string(); async move { + let specifier = ModuleSpecifier::resolve_url_or_path(&specifier) + .map_err(OpError::from)?; let source_file = fetcher .fetch_source_file(&specifier, None, Permissions::allow_all()) .await?; @@ -606,11 +589,8 @@ async fn run_command(flags: Flags, script: String) -> Result<(), ErrBox> { source_code: source.into(), }; // Save our fake file into file fetcher cache - // to allow module access by TS compiler (e.g. op_fetch_source_files) - worker - .state - .borrow() - .global_state + // to allow module access by TS compiler + global_state .file_fetcher .save_source_file_in_cache(&main_module, source_file); }; @@ -665,11 +645,8 @@ async fn test_command( ), }; // Save our fake file into file fetcher cache - // to allow module access by TS compiler (e.g. op_fetch_source_files) - worker - .state - .borrow() - .global_state + // to allow module access by TS compiler + global_state .file_fetcher .save_source_file_in_cache(&main_module, source_file); let execute_result = worker.execute_module(&main_module).await; |