summaryrefslogtreecommitdiff
path: root/cli/tools/fmt.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tools/fmt.rs')
-rw-r--r--cli/tools/fmt.rs60
1 files changed, 24 insertions, 36 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index 9a16afeca..869403f07 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -152,6 +152,22 @@ fn format_json(file_text: &str) -> Result<String, String> {
dprint_plugin_json::format_text(&file_text, &json_config)
}
+/// Formats a single TS, TSX, JS, JSX, JSONC, JSON, or MD file.
+pub fn format_file(
+ file_path: &Path,
+ file_text: &str,
+ config: dprint_plugin_typescript::configuration::Configuration,
+) -> Result<String, String> {
+ let ext = get_extension(file_path).unwrap_or_else(String::new);
+ if ext == "md" {
+ format_markdown(&file_text, config)
+ } else if matches!(ext.as_str(), "json" | "jsonc") {
+ format_json(&file_text)
+ } else {
+ dprint_plugin_typescript::format_text(&file_path, &file_text, &config)
+ }
+}
+
async fn check_source_files(
config: dprint_plugin_typescript::configuration::Configuration,
paths: Vec<PathBuf>,
@@ -168,15 +184,8 @@ async fn check_source_files(
move |file_path| {
checked_files_count.fetch_add(1, Ordering::Relaxed);
let file_text = read_file_contents(&file_path)?.text;
- let ext = get_extension(&file_path).unwrap_or_else(String::new);
- let r = if ext == "md" {
- format_markdown(&file_text, config.clone())
- } else if matches!(ext.as_str(), "json" | "jsonc") {
- format_json(&file_text)
- } else {
- dprint_plugin_typescript::format_text(&file_path, &file_text, &config)
- };
- match r {
+
+ match format_file(&file_path, &file_text, config) {
Ok(formatted_text) => {
if formatted_text != file_text {
not_formatted_files_count.fetch_add(1, Ordering::Relaxed);
@@ -229,19 +238,8 @@ async fn format_source_files(
move |file_path| {
checked_files_count.fetch_add(1, Ordering::Relaxed);
let file_contents = read_file_contents(&file_path)?;
- let ext = get_extension(&file_path).unwrap_or_else(String::new);
- let r = if ext == "md" {
- format_markdown(&file_contents.text, config.clone())
- } else if matches!(ext.as_str(), "json" | "jsonc") {
- format_json(&file_contents.text)
- } else {
- dprint_plugin_typescript::format_text(
- &file_path,
- &file_contents.text,
- &config,
- )
- };
- match r {
+
+ match format_file(&file_path, &file_contents.text, config) {
Ok(formatted_text) => {
if formatted_text != file_contents.text {
write_file_contents(
@@ -293,19 +291,9 @@ pub fn format_stdin(check: bool, ext: String) -> Result<(), AnyError> {
return Err(generic_error("Failed to read from stdin"));
}
let config = get_typescript_config();
- let r = if ext.as_str() == "md" {
- format_markdown(&source, config)
- } else if matches!(ext.as_str(), "json" | "jsonc") {
- format_json(&source)
- } else {
- // dprint will fallback to jsx parsing if parsing this as a .ts file doesn't work
- dprint_plugin_typescript::format_text(
- &PathBuf::from("_stdin.ts"),
- &source,
- &config,
- )
- };
- match r {
+ let file_path = PathBuf::from(format!("_stdin.{}", ext));
+
+ match format_file(&file_path, &source, config) {
Ok(formatted_text) => {
if check {
if formatted_text != source {
@@ -330,7 +318,7 @@ fn files_str(len: usize) -> &'static str {
}
}
-fn get_typescript_config(
+pub fn get_typescript_config(
) -> dprint_plugin_typescript::configuration::Configuration {
dprint_plugin_typescript::configuration::ConfigurationBuilder::new()
.deno()