Fix stdin not formatting JSX (#4971)

This commit is contained in:
David Sherret 2020-04-28 15:17:40 -04:00 committed by GitHub
parent 6ee00e4da3
commit f899d76667
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 17 deletions

9
Cargo.lock generated
View file

@ -607,21 +607,22 @@ checksum = "52ba6eb47c2131e784a38b726eb54c1e1484904f013e576a25354d0124161af6"
[[package]]
name = "dprint-core"
version = "0.14.0"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93213397182b20fc4565e6ad93cc30cb1825a276d11bcac969cd06c63006898c"
checksum = "2fe2ae2e02c20dcb77d422c6326db6c2a11a3dd37eb012b1cf69703685d272fb"
dependencies = [
"serde",
]
[[package]]
name = "dprint-plugin-typescript"
version = "0.13.1"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdb73fe3655b530e17c5606b950cc55b0a05e7bdda50be11578f8eddade7ef96"
checksum = "677d80078f5e36e7e697f5f1cc175cacb4cb206fd5f95c918023778b0a944b33"
dependencies = [
"dprint-core",
"serde",
"serde_json",
"swc_common",
"swc_ecma_ast",
"swc_ecma_parser",

View file

@ -33,7 +33,7 @@ byteorder = "1.3.4"
clap = "2.33.0"
dirs = "2.0.2"
dlopen = "0.1.8"
dprint-plugin-typescript = "0.13.1"
dprint-plugin-typescript = "0.14.0"
futures = { version = "0.3.4", features = ["compat", "io-compat"] }
glob = "0.3.0"
http = "0.2.1"

View file

@ -22,7 +22,11 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
fn is_supported(path: &Path) -> bool {
if let Some(ext) = path.extension() {
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
@ -45,9 +49,8 @@ async fn check_source_files(
run_parallelized(paths, {
let not_formatted_files_count = not_formatted_files_count.clone();
move |file_path| {
let file_path_str = file_path.to_string_lossy();
let file_contents = fs::read_to_string(&file_path)?;
let r = formatter.format_text(&file_path_str, &file_contents);
let r = formatter.format_text(&file_path, &file_contents);
match r {
Ok(formatted_text) => {
if formatted_text != file_contents {
@ -56,7 +59,7 @@ async fn check_source_files(
}
Err(e) => {
let _g = output_lock.lock().unwrap();
eprintln!("Error checking: {}", &file_path_str);
eprintln!("Error checking: {}", file_path.to_string_lossy());
eprintln!(" {}", e);
}
}
@ -100,21 +103,20 @@ async fn format_source_files(
run_parallelized(paths, {
let formatted_files_count = formatted_files_count.clone();
move |file_path| {
let file_path_str = file_path.to_string_lossy();
let file_contents = fs::read_to_string(&file_path)?;
let r = formatter.format_text(&file_path_str, &file_contents);
let r = formatter.format_text(&file_path, &file_contents);
match r {
Ok(formatted_text) => {
if formatted_text != file_contents {
fs::write(&file_path, formatted_text)?;
formatted_files_count.fetch_add(1, Ordering::SeqCst);
let _g = output_lock.lock().unwrap();
println!("{}", file_path_str);
println!("{}", file_path.to_string_lossy());
}
}
Err(e) => {
let _g = output_lock.lock().unwrap();
eprintln!("Error formatting: {}", &file_path_str);
eprintln!("Error formatting: {}", file_path.to_string_lossy());
eprintln!(" {}", e);
}
}
@ -160,11 +162,10 @@ pub async fn format(args: Vec<String>, check: bool) -> Result<(), ErrBox> {
}
let config = get_config();
if check {
check_source_files(config, target_files).await?;
check_source_files(config, target_files).await
} else {
format_source_files(config, target_files).await?;
format_source_files(config, target_files).await
}
Ok(())
}
/// Format stdin and write result to stdout.
@ -177,7 +178,8 @@ fn format_stdin(check: bool) -> Result<(), ErrBox> {
}
let formatter = dprint::Formatter::new(get_config());
match formatter.format_text("_stdin.ts", &source) {
// 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) {
Ok(formatted_text) => {
if check {
if formatted_text != source {
@ -247,6 +249,10 @@ fn test_is_supported() {
assert!(is_supported(Path::new("cli/tests/002_hello.ts")));
assert!(is_supported(Path::new("foo.jsx")));
assert!(is_supported(Path::new("foo.tsx")));
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")));
}
#[tokio::test]