diff options
-rw-r--r-- | Cargo.lock | 4 | ||||
-rw-r--r-- | cli/Cargo.toml | 2 | ||||
-rw-r--r-- | cli/fmt.rs | 62 | ||||
-rw-r--r-- | cli/lib.rs | 2 |
4 files changed, 34 insertions, 36 deletions
diff --git a/Cargo.lock b/Cargo.lock index b3d5f4814..00d199c12 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -576,9 +576,9 @@ dependencies = [ [[package]] name = "dprint-plugin-typescript" -version = "0.6.1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35436beb5b5c902deae6063f3e47a62ba94703828c527381cb05d48be8027cfe" +checksum = "9606967ab3af22b75547a3040349c4f32429189a86626f5b64f1cfb704a25d38" dependencies = [ "dprint-core", "serde", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index f09f8b6e4..976d87cfa 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -33,7 +33,7 @@ byteorder = "1.3.2" clap = "2.33.0" dirs = "2.0.2" dlopen = "0.1.8" -dprint-plugin-typescript = "0.6.1" +dprint-plugin-typescript = "0.7.0" futures = { version = "0.3.1", features = [ "compat", "io-compat" ] } glob = "0.3.0" http = "0.2.0" diff --git a/cli/fmt.rs b/cli/fmt.rs index 44afac527..571e27648 100644 --- a/cli/fmt.rs +++ b/cli/fmt.rs @@ -17,7 +17,6 @@ use std::io::Read; use std::io::Write; use std::path::Path; use std::path::PathBuf; -use std::time::Instant; fn is_supported(path: &Path) -> bool { if let Some(ext) = path.extension() { @@ -52,7 +51,6 @@ fn check_source_files( config: dprint::configuration::Configuration, paths: Vec<PathBuf>, ) -> Result<(), ErrBox> { - let start = Instant::now(); let mut not_formatted_files = vec![]; for file_path in paths { @@ -74,8 +72,6 @@ fn check_source_files( } } - let duration = Instant::now() - start; - if not_formatted_files.is_empty() { Ok(()) } else { @@ -86,10 +82,9 @@ fn check_source_files( }; Err( crate::op_error::OpError::other(format!( - "Found {} not formatted {} in {:?}", + "Found {} not formatted {}", not_formatted_files.len(), f, - duration )) .into(), ) @@ -99,50 +94,53 @@ fn check_source_files( fn format_source_files( config: dprint::configuration::Configuration, paths: Vec<PathBuf>, -) { - let start = Instant::now(); +) -> Result<(), ErrBox> { let mut not_formatted_files = vec![]; for file_path in paths { let file_path_str = file_path.to_string_lossy(); - let file_contents = fs::read_to_string(&file_path).unwrap(); - match dprint::format_text(&file_path_str, &file_contents, &config) { - Ok(None) => { - // nothing to format, pass - } - Ok(Some(formatted_text)) => { - if formatted_text != file_contents { - println!("Formatting {}", file_path_str); - fs::write(&file_path, formatted_text).unwrap(); - not_formatted_files.push(file_path); + let file_contents = fs::read_to_string(&file_path)?; + // TODO(ry) dprint seems to panic unnecessarally sometimes. Until it matures + // we'll use a catch_unwind to avoid passing it on to our users. + let catch_unwind_result = std::panic::catch_unwind(|| { + dprint::format_text(&file_path_str, &file_contents, &config) + }); + if let Ok(dprint_result) = catch_unwind_result { + match dprint_result { + Ok(None) => { + // nothing to format, pass + } + Ok(Some(formatted_text)) => { + if formatted_text != file_contents { + println!("{}", file_path_str); + fs::write(&file_path, formatted_text)?; + not_formatted_files.push(file_path); + } + } + Err(e) => { + eprintln!("Error formatting: {}", &file_path_str); + eprintln!(" {}", e); } } - Err(e) => { - eprintln!("Error formatting: {}", &file_path_str); - eprintln!(" {}", e); - } + } else { + eprintln!("dprint panic {}", file_path_str); } } - let duration = Instant::now() - start; let f = if not_formatted_files.len() == 1 { "file" } else { "files" }; - eprintln!( - "Formatted {} {} in {:?}", - not_formatted_files.len(), - f, - duration - ); + debug!("Formatted {} {}", not_formatted_files.len(), f); + Ok(()) } /// Format JavaScript/TypeScript files. /// /// First argument supports globs, and if it is `None` /// then the current directory is recursively walked. -pub fn format_files(args: Vec<String>, check: bool) -> Result<(), ErrBox> { +pub fn format(args: Vec<String>, check: bool) -> Result<(), ErrBox> { if args.len() == 1 && args[0] == "-" { format_stdin(check); return Ok(()); @@ -169,7 +167,7 @@ pub fn format_files(args: Vec<String>, check: bool) -> Result<(), ErrBox> { if check { check_source_files(config, target_files)?; } else { - format_source_files(config, target_files); + format_source_files(config, target_files)?; } Ok(()) } @@ -218,6 +216,6 @@ fn test_is_supported() { fn check_tests_dir() { // Because of cli/tests/error_syntax.js the following should fail but not // crash. - let r = format_files(vec!["./tests".to_string()], true); + let r = format(vec!["./tests".to_string()], true); assert!(r.is_err()); } diff --git a/cli/lib.rs b/cli/lib.rs index 3de40e1cd..3fa019d3e 100644 --- a/cli/lib.rs +++ b/cli/lib.rs @@ -417,7 +417,7 @@ pub fn main() { fetch_command(flags, files).boxed_local() } DenoSubcommand::Fmt { check, files } => { - async move { fmt::format_files(files, check) }.boxed_local() + async move { fmt::format(files, check) }.boxed_local() } DenoSubcommand::Info { file } => info_command(flags, file).boxed_local(), DenoSubcommand::Install { |