mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -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:
parent
3d03d7e83b
commit
3eebef39c5
4 changed files with 34 additions and 36 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -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",
|
||||
|
|
|
@ -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"
|
||||
|
|
42
cli/fmt.rs
42
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,21 +94,26 @@ 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) {
|
||||
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!("Formatting {}", file_path_str);
|
||||
fs::write(&file_path, formatted_text).unwrap();
|
||||
println!("{}", file_path_str);
|
||||
fs::write(&file_path, formatted_text)?;
|
||||
not_formatted_files.push(file_path);
|
||||
}
|
||||
}
|
||||
|
@ -122,27 +122,25 @@ fn format_source_files(
|
|||
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());
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue