mirror of
https://github.com/denoland/deno.git
synced 2025-08-04 10:59:13 +00: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
62
cli/fmt.rs
62
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());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue