1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

upgrade: dprint 0.7.0 (#4130)

* upgrade: dprint 0.7.0

Also make deno fmt less verbose (like cargo fmt)
This commit is contained in:
Ryan Dahl 2020-02-26 05:50:53 -05:00 committed by GitHub
parent 3d03d7e83b
commit 3eebef39c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 36 deletions

4
Cargo.lock generated
View file

@ -576,9 +576,9 @@ dependencies = [
[[package]] [[package]]
name = "dprint-plugin-typescript" name = "dprint-plugin-typescript"
version = "0.6.1" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35436beb5b5c902deae6063f3e47a62ba94703828c527381cb05d48be8027cfe" checksum = "9606967ab3af22b75547a3040349c4f32429189a86626f5b64f1cfb704a25d38"
dependencies = [ dependencies = [
"dprint-core", "dprint-core",
"serde", "serde",

View file

@ -33,7 +33,7 @@ byteorder = "1.3.2"
clap = "2.33.0" clap = "2.33.0"
dirs = "2.0.2" dirs = "2.0.2"
dlopen = "0.1.8" 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" ] } futures = { version = "0.3.1", features = [ "compat", "io-compat" ] }
glob = "0.3.0" glob = "0.3.0"
http = "0.2.0" http = "0.2.0"

View file

@ -17,7 +17,6 @@ use std::io::Read;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::Instant;
fn is_supported(path: &Path) -> bool { fn is_supported(path: &Path) -> bool {
if let Some(ext) = path.extension() { if let Some(ext) = path.extension() {
@ -52,7 +51,6 @@ fn check_source_files(
config: dprint::configuration::Configuration, config: dprint::configuration::Configuration,
paths: Vec<PathBuf>, paths: Vec<PathBuf>,
) -> Result<(), ErrBox> { ) -> Result<(), ErrBox> {
let start = Instant::now();
let mut not_formatted_files = vec![]; let mut not_formatted_files = vec![];
for file_path in paths { for file_path in paths {
@ -74,8 +72,6 @@ fn check_source_files(
} }
} }
let duration = Instant::now() - start;
if not_formatted_files.is_empty() { if not_formatted_files.is_empty() {
Ok(()) Ok(())
} else { } else {
@ -86,10 +82,9 @@ fn check_source_files(
}; };
Err( Err(
crate::op_error::OpError::other(format!( crate::op_error::OpError::other(format!(
"Found {} not formatted {} in {:?}", "Found {} not formatted {}",
not_formatted_files.len(), not_formatted_files.len(),
f, f,
duration
)) ))
.into(), .into(),
) )
@ -99,50 +94,53 @@ fn check_source_files(
fn format_source_files( fn format_source_files(
config: dprint::configuration::Configuration, config: dprint::configuration::Configuration,
paths: Vec<PathBuf>, paths: Vec<PathBuf>,
) { ) -> Result<(), ErrBox> {
let start = Instant::now();
let mut not_formatted_files = vec![]; let mut not_formatted_files = vec![];
for file_path in paths { for file_path in paths {
let file_path_str = file_path.to_string_lossy(); let file_path_str = file_path.to_string_lossy();
let file_contents = fs::read_to_string(&file_path).unwrap(); let file_contents = fs::read_to_string(&file_path)?;
match dprint::format_text(&file_path_str, &file_contents, &config) { // TODO(ry) dprint seems to panic unnecessarally sometimes. Until it matures
Ok(None) => { // we'll use a catch_unwind to avoid passing it on to our users.
// nothing to format, pass let catch_unwind_result = std::panic::catch_unwind(|| {
} dprint::format_text(&file_path_str, &file_contents, &config)
Ok(Some(formatted_text)) => { });
if formatted_text != file_contents { if let Ok(dprint_result) = catch_unwind_result {
println!("Formatting {}", file_path_str); match dprint_result {
fs::write(&file_path, formatted_text).unwrap(); Ok(None) => {
not_formatted_files.push(file_path); // 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) => { } else {
eprintln!("Error formatting: {}", &file_path_str); eprintln!("dprint panic {}", file_path_str);
eprintln!(" {}", e);
}
} }
} }
let duration = Instant::now() - start;
let f = if not_formatted_files.len() == 1 { let f = if not_formatted_files.len() == 1 {
"file" "file"
} else { } else {
"files" "files"
}; };
eprintln!( debug!("Formatted {} {}", not_formatted_files.len(), f);
"Formatted {} {} in {:?}", Ok(())
not_formatted_files.len(),
f,
duration
);
} }
/// Format JavaScript/TypeScript files. /// Format JavaScript/TypeScript files.
/// ///
/// First argument supports globs, and if it is `None` /// First argument supports globs, and if it is `None`
/// then the current directory is recursively walked. /// 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] == "-" { if args.len() == 1 && args[0] == "-" {
format_stdin(check); format_stdin(check);
return Ok(()); return Ok(());
@ -169,7 +167,7 @@ pub fn format_files(args: Vec<String>, check: bool) -> Result<(), ErrBox> {
if check { if check {
check_source_files(config, target_files)?; check_source_files(config, target_files)?;
} else { } else {
format_source_files(config, target_files); format_source_files(config, target_files)?;
} }
Ok(()) Ok(())
} }
@ -218,6 +216,6 @@ fn test_is_supported() {
fn check_tests_dir() { fn check_tests_dir() {
// Because of cli/tests/error_syntax.js the following should fail but not // Because of cli/tests/error_syntax.js the following should fail but not
// crash. // crash.
let r = format_files(vec!["./tests".to_string()], true); let r = format(vec!["./tests".to_string()], true);
assert!(r.is_err()); assert!(r.is_err());
} }

View file

@ -417,7 +417,7 @@ pub fn main() {
fetch_command(flags, files).boxed_local() fetch_command(flags, files).boxed_local()
} }
DenoSubcommand::Fmt { check, files } => { 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::Info { file } => info_command(flags, file).boxed_local(),
DenoSubcommand::Install { DenoSubcommand::Install {