diff options
Diffstat (limited to 'cli/tools/fmt.rs')
-rw-r--r-- | cli/tools/fmt.rs | 76 |
1 files changed, 38 insertions, 38 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 4fba9ea13..3c5ab523e 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -14,9 +14,11 @@ use crate::config_file::ProseWrap; use crate::diff::diff; use crate::file_watcher; use crate::file_watcher::ResolutionResult; -use crate::flags::{Flags, FmtFlags}; +use crate::flags::Flags; +use crate::flags::FmtFlags; +use crate::fs_util::collect_files; +use crate::fs_util::get_extension; use crate::fs_util::specifier_to_file_path; -use crate::fs_util::{collect_files, get_extension}; use crate::text_encoding; use deno_ast::ParsedSource; use deno_core::anyhow::bail; @@ -24,9 +26,9 @@ use deno_core::anyhow::Context; use deno_core::error::generic_error; use deno_core::error::AnyError; use deno_core::futures; +use deno_core::parking_lot::Mutex; use log::debug; use log::info; -use std::borrow::Cow; use std::fs; use std::io::stdin; use std::io::stdout; @@ -34,8 +36,9 @@ use std::io::Read; use std::io::Write; use std::path::Path; use std::path::PathBuf; -use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::atomic::AtomicUsize; +use std::sync::atomic::Ordering; +use std::sync::Arc; /// Format JavaScript/TypeScript files. pub async fn format( @@ -167,7 +170,7 @@ pub async fn format( fn format_markdown( file_text: &str, fmt_options: &FmtOptionsConfig, -) -> Result<String, AnyError> { +) -> Result<Option<String>, AnyError> { let markdown_config = get_resolved_markdown_config(fmt_options); dprint_plugin_markdown::format_text( file_text, @@ -196,7 +199,7 @@ fn format_markdown( if matches!(extension, "json" | "jsonc") { let mut json_config = get_resolved_json_config(fmt_options); json_config.line_width = line_width; - dprint_plugin_json::format_text(text, &json_config).map(Cow::Owned) + dprint_plugin_json::format_text(text, &json_config) } else { let fake_filename = PathBuf::from(format!("deno_fmt_stdin.{}", extension)); @@ -208,10 +211,9 @@ fn format_markdown( text, &codeblock_config, ) - .map(Cow::Owned) } } else { - Ok(Cow::Borrowed(text)) + Ok(None) } }, ) @@ -223,7 +225,7 @@ fn format_markdown( pub fn format_json( file_text: &str, fmt_options: &FmtOptionsConfig, -) -> Result<String, AnyError> { +) -> Result<Option<String>, AnyError> { let config = get_resolved_json_config(fmt_options); dprint_plugin_json::format_text(file_text, &config) } @@ -233,7 +235,7 @@ pub fn format_file( file_path: &Path, file_text: &str, fmt_options: FmtOptionsConfig, -) -> Result<String, AnyError> { +) -> Result<Option<String>, AnyError> { let ext = get_extension(file_path).unwrap_or_default(); if matches!( ext.as_str(), @@ -251,7 +253,7 @@ pub fn format_file( pub fn format_parsed_source( parsed_source: &ParsedSource, fmt_options: FmtOptionsConfig, -) -> Result<String, AnyError> { +) -> Result<Option<String>, AnyError> { dprint_plugin_typescript::format_parsed_source( parsed_source, &get_resolved_typescript_config(&fmt_options), @@ -276,18 +278,17 @@ async fn check_source_files( let file_text = read_file_contents(&file_path)?.text; match format_file(&file_path, &file_text, fmt_options.clone()) { - Ok(formatted_text) => { - if formatted_text != file_text { - not_formatted_files_count.fetch_add(1, Ordering::Relaxed); - let _g = output_lock.lock().unwrap(); - let diff = diff(&file_text, &formatted_text); - info!(""); - info!("{} {}:", colors::bold("from"), file_path.display()); - info!("{}", diff); - } + Ok(Some(formatted_text)) => { + not_formatted_files_count.fetch_add(1, Ordering::Relaxed); + let _g = output_lock.lock(); + let diff = diff(&file_text, &formatted_text); + info!(""); + info!("{} {}:", colors::bold("from"), file_path.display()); + info!("{}", diff); } + Ok(None) => {} Err(e) => { - let _g = output_lock.lock().unwrap(); + let _g = output_lock.lock(); eprintln!("Error checking: {}", file_path.to_string_lossy()); eprintln!(" {}", e); } @@ -330,22 +331,21 @@ async fn format_source_files( let file_contents = read_file_contents(&file_path)?; match format_file(&file_path, &file_contents.text, fmt_options.clone()) { - Ok(formatted_text) => { - if formatted_text != file_contents.text { - write_file_contents( - &file_path, - FileContents { - had_bom: file_contents.had_bom, - text: formatted_text, - }, - )?; - formatted_files_count.fetch_add(1, Ordering::Relaxed); - let _g = output_lock.lock().unwrap(); - info!("{}", file_path.to_string_lossy()); - } + Ok(Some(formatted_text)) => { + write_file_contents( + &file_path, + FileContents { + had_bom: file_contents.had_bom, + text: formatted_text, + }, + )?; + formatted_files_count.fetch_add(1, Ordering::Relaxed); + let _g = output_lock.lock(); + info!("{}", file_path.to_string_lossy()); } + Ok(None) => {} Err(e) => { - let _g = output_lock.lock().unwrap(); + let _g = output_lock.lock(); eprintln!("Error formatting: {}", file_path.to_string_lossy()); eprintln!(" {}", e); } @@ -388,11 +388,11 @@ pub fn format_stdin( let formatted_text = format_file(&file_path, &source, fmt_options)?; if fmt_flags.check { - if formatted_text != source { + if formatted_text.is_some() { println!("Not formatted stdin"); } } else { - stdout().write_all(formatted_text.as_bytes())?; + stdout().write_all(formatted_text.unwrap_or(source).as_bytes())?; } Ok(()) } |