deno/cli/fmt.rs

272 lines
7.6 KiB
Rust
Raw Normal View History

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
//! This module provides file formating utilities using
//! [`dprint`](https://github.com/dsherret/dprint).
//!
//! At the moment it is only consumed using CLI but in
//! the future it can be easily extended to provide
//! the same functions as ops available in JS runtime.
use crate::fs::files_in_subtree;
use crate::op_error::OpError;
use deno_core::ErrBox;
use dprint_plugin_typescript as dprint;
use std::fs;
use std::io::stdin;
use std::io::stdout;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
2020-04-23 23:01:15 +00:00
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
/// Format JavaScript/TypeScript files.
///
/// First argument supports globs, and if it is `None`
/// then the current directory is recursively walked.
pub async fn format(args: Vec<String>, check: bool) -> Result<(), ErrBox> {
if args.len() == 1 && args[0] == "-" {
return format_stdin(check);
}
let mut target_files: Vec<PathBuf> = vec![];
if args.is_empty() {
target_files.extend(files_in_subtree(
std::env::current_dir().unwrap(),
is_supported,
));
} else {
for arg in args {
let p = PathBuf::from(arg);
if p.is_dir() {
target_files.extend(files_in_subtree(p, is_supported));
} else {
target_files.push(p);
};
}
}
let config = get_config();
if check {
check_source_files(config, target_files).await
} else {
format_source_files(config, target_files).await
}
2020-03-25 21:24:26 +00:00
}
2020-04-23 23:01:15 +00:00
async fn check_source_files(
2020-02-03 20:52:32 +00:00
config: dprint::configuration::Configuration,
paths: Vec<PathBuf>,
) -> Result<(), ErrBox> {
2020-04-23 23:01:15 +00:00
let not_formatted_files_count = Arc::new(AtomicUsize::new(0));
let formatter = Arc::new(dprint::Formatter::new(config));
let output_lock = Arc::new(Mutex::new(0)); // prevent threads outputting at the same time
run_parallelized(paths, {
let not_formatted_files_count = not_formatted_files_count.clone();
move |file_path| {
let file_contents = fs::read_to_string(&file_path)?;
2020-04-28 19:17:40 +00:00
let r = formatter.format_text(&file_path, &file_contents);
2020-04-23 23:01:15 +00:00
match r {
Ok(formatted_text) => {
if formatted_text != file_contents {
not_formatted_files_count.fetch_add(1, Ordering::SeqCst);
}
}
Err(e) => {
2020-04-24 09:14:18 +00:00
let _g = output_lock.lock().unwrap();
2020-04-28 19:17:40 +00:00
eprintln!("Error checking: {}", file_path.to_string_lossy());
2020-04-23 23:01:15 +00:00
eprintln!(" {}", e);
}
}
2020-04-23 23:01:15 +00:00
Ok(())
}
2020-04-23 23:01:15 +00:00
})
.await?;
2020-04-23 23:01:15 +00:00
let not_formatted_files_count =
not_formatted_files_count.load(Ordering::SeqCst);
if not_formatted_files_count == 0 {
Ok(())
} else {
Err(
OpError::other(format!(
"Found {} not formatted {}",
2020-04-23 23:01:15 +00:00
not_formatted_files_count,
files_str(not_formatted_files_count),
))
.into(),
)
}
}
2020-04-23 23:01:15 +00:00
async fn format_source_files(
2020-02-03 20:52:32 +00:00
config: dprint::configuration::Configuration,
paths: Vec<PathBuf>,
) -> Result<(), ErrBox> {
2020-04-23 23:01:15 +00:00
let formatted_files_count = Arc::new(AtomicUsize::new(0));
let formatter = Arc::new(dprint::Formatter::new(config));
let output_lock = Arc::new(Mutex::new(0)); // prevent threads outputting at the same time
run_parallelized(paths, {
let formatted_files_count = formatted_files_count.clone();
move |file_path| {
let file_contents = fs::read_to_string(&file_path)?;
2020-04-28 19:17:40 +00:00
let r = formatter.format_text(&file_path, &file_contents);
2020-04-23 23:01:15 +00:00
match r {
Ok(formatted_text) => {
if formatted_text != file_contents {
fs::write(&file_path, formatted_text)?;
formatted_files_count.fetch_add(1, Ordering::SeqCst);
2020-04-24 09:14:18 +00:00
let _g = output_lock.lock().unwrap();
2020-04-28 19:17:40 +00:00
println!("{}", file_path.to_string_lossy());
2020-04-23 23:01:15 +00:00
}
}
Err(e) => {
2020-04-24 09:14:18 +00:00
let _g = output_lock.lock().unwrap();
2020-04-28 19:17:40 +00:00
eprintln!("Error formatting: {}", file_path.to_string_lossy());
2020-04-23 23:01:15 +00:00
eprintln!(" {}", e);
}
}
2020-04-23 23:01:15 +00:00
Ok(())
}
2020-04-23 23:01:15 +00:00
})
.await?;
let formatted_files_count = formatted_files_count.load(Ordering::SeqCst);
debug!(
"Formatted {} {}",
2020-04-23 23:01:15 +00:00
formatted_files_count,
files_str(formatted_files_count),
);
Ok(())
}
/// Format stdin and write result to stdout.
/// Treats input as TypeScript.
/// Compatible with `--check` flag.
fn format_stdin(check: bool) -> Result<(), ErrBox> {
let mut source = String::new();
if stdin().read_to_string(&mut source).is_err() {
return Err(OpError::other("Failed to read from stdin".to_string()).into());
}
2020-04-19 11:26:17 +00:00
let formatter = dprint::Formatter::new(get_config());
2020-04-28 19:17:40 +00:00
// dprint will fallback to jsx parsing if parsing this as a .ts file doesn't work
match formatter.format_text(&PathBuf::from("_stdin.ts"), &source) {
2020-04-19 11:26:17 +00:00
Ok(formatted_text) => {
if check {
if formatted_text != source {
println!("Not formatted stdin");
}
} else {
stdout().write_all(formatted_text.as_bytes())?;
}
}
Err(e) => {
return Err(OpError::other(e).into());
}
}
Ok(())
}
/// Formats the given source text
pub fn format_text(source: &str) -> Result<String, ErrBox> {
dprint::Formatter::new(get_config())
.format_text(&PathBuf::from("_tmp.ts"), &source)
.map_err(|e| OpError::other(e).into())
}
fn files_str(len: usize) -> &'static str {
if len == 1 {
"file"
} else {
"files"
}
}
fn is_supported(path: &Path) -> bool {
let lowercase_ext = path
.extension()
.and_then(|e| e.to_str())
.map(|e| e.to_lowercase());
if let Some(ext) = lowercase_ext {
ext == "ts" || ext == "tsx" || ext == "js" || ext == "jsx"
} else {
false
}
}
fn get_config() -> dprint::configuration::Configuration {
use dprint::configuration::*;
ConfigurationBuilder::new().deno().build()
}
2020-04-23 23:01:15 +00:00
async fn run_parallelized<F>(
file_paths: Vec<PathBuf>,
f: F,
) -> Result<(), ErrBox>
where
F: FnOnce(PathBuf) -> Result<(), ErrBox> + Send + 'static + Clone,
{
let handles = file_paths.iter().map(|file_path| {
let f = f.clone();
let file_path = file_path.clone();
tokio::task::spawn_blocking(move || f(file_path))
});
let join_results = futures::future::join_all(handles).await;
// find the tasks that panicked and let the user know which files
let panic_file_paths = join_results
.iter()
.enumerate()
.filter_map(|(i, join_result)| {
join_result
.as_ref()
.err()
.map(|_| file_paths[i].to_string_lossy())
})
.collect::<Vec<_>>();
if !panic_file_paths.is_empty() {
panic!("Panic formatting: {}", panic_file_paths.join(", "))
}
// check for any errors and if so return the first one
let mut errors = join_results.into_iter().filter_map(|join_result| {
join_result
.ok()
.map(|handle_result| handle_result.err())
.flatten()
});
if let Some(e) = errors.next() {
Err(e)
} else {
Ok(())
}
}
#[test]
fn test_is_supported() {
assert!(!is_supported(Path::new("tests/subdir/redirects")));
assert!(!is_supported(Path::new("README.md")));
2020-04-19 11:26:17 +00:00
assert!(is_supported(Path::new("lib/typescript.d.ts")));
assert!(is_supported(Path::new("cli/tests/001_hello.js")));
assert!(is_supported(Path::new("cli/tests/002_hello.ts")));
assert!(is_supported(Path::new("foo.jsx")));
assert!(is_supported(Path::new("foo.tsx")));
2020-04-28 19:17:40 +00:00
assert!(is_supported(Path::new("foo.TS")));
assert!(is_supported(Path::new("foo.TSX")));
assert!(is_supported(Path::new("foo.JS")));
assert!(is_supported(Path::new("foo.JSX")));
}
2020-04-23 23:01:15 +00:00
#[tokio::test]
async fn check_tests_dir() {
// Because of cli/tests/error_syntax.js the following should fail but not
// crash.
2020-04-23 23:01:15 +00:00
let r = format(vec!["./tests".to_string()], true).await;
assert!(r.is_err());
}