diff options
author | Satya Rohith <me@satyarohith.com> | 2021-02-18 22:01:32 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-18 17:31:32 +0100 |
commit | d9b1f96897239bf7de8cdfd11d40e3f99bb29a4a (patch) | |
tree | 4a1c60d4850111f738b485bbad5f43659bd9ed09 /cli/tools | |
parent | c0f10e72ee64f1512e6aff20a841f4696e774217 (diff) |
feat: add json(c) support to deno fmt (#9292)
This commit adds support for formatting JSON and JSONC
in "deno fmt".
New values "json" and "jsonc" are added to "--ext" flag for
standard input processing.
Diffstat (limited to 'cli/tools')
-rw-r--r-- | cli/tools/fmt.rs | 57 |
1 files changed, 45 insertions, 12 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 61875d172..d4002984a 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -10,7 +10,7 @@ use crate::colors; use crate::diff::diff; use crate::file_watcher; -use crate::fs_util::{collect_files, get_extension, is_supported_ext_md}; +use crate::fs_util::{collect_files, get_extension, is_supported_ext_fmt}; use crate::text_encoding; use deno_core::error::generic_error; use deno_core::error::AnyError; @@ -37,7 +37,7 @@ pub async fn format( ) -> Result<(), AnyError> { let target_file_resolver = || { // collect the files that are to be formatted - collect_files(&args, &ignore, is_supported_ext_md) + collect_files(&args, &ignore, is_supported_ext_fmt) }; let operation = |paths: Vec<PathBuf>| { let config = get_typescript_config(); @@ -75,7 +75,14 @@ fn format_markdown( let tag = tag.to_lowercase(); if matches!( tag.as_str(), - "ts" | "tsx" | "js" | "jsx" | "javascript" | "typescript" + "ts" + | "tsx" + | "js" + | "jsx" + | "javascript" + | "typescript" + | "json" + | "jsonc" ) { // It's important to tell dprint proper file extension, otherwise // it might parse the file twice. @@ -84,16 +91,22 @@ fn format_markdown( "typescript" => "ts", rest => rest, }; - let fake_filename = - PathBuf::from(format!("deno_fmt_stdin.{}", extension)); - let mut codeblock_config = ts_config.clone(); - codeblock_config.line_width = line_width; - dprint_plugin_typescript::format_text( - &fake_filename, - &text, - &codeblock_config, - ) + if matches!(extension, "json" | "jsonc") { + let mut json_config = get_json_config(); + json_config.line_width = line_width; + dprint_plugin_json::format_text(&text, &json_config) + } else { + let fake_filename = + PathBuf::from(format!("deno_fmt_stdin.{}", extension)); + let mut codeblock_config = ts_config.clone(); + codeblock_config.line_width = line_width; + dprint_plugin_typescript::format_text( + &fake_filename, + &text, + &codeblock_config, + ) + } } else { Ok(text.to_string()) } @@ -101,6 +114,14 @@ fn format_markdown( ) } +/// Formats JSON and JSONC using the rules provided by .deno() +/// of configuration builder of https://github.com/dprint/dprint-plugin-json. +/// See https://git.io/Jt4ht for configuration. +fn format_json(file_text: &str) -> Result<String, String> { + let json_config = get_json_config(); + dprint_plugin_json::format_text(&file_text, &json_config) +} + async fn check_source_files( config: dprint_plugin_typescript::configuration::Configuration, paths: Vec<PathBuf>, @@ -120,6 +141,8 @@ async fn check_source_files( 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) }; @@ -179,6 +202,8 @@ async fn format_source_files( 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, @@ -240,6 +265,8 @@ pub fn format_stdin(check: bool, ext: String) -> Result<(), AnyError> { 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( @@ -291,6 +318,12 @@ fn get_markdown_config() -> dprint_plugin_markdown::configuration::Configuration .build() } +fn get_json_config() -> dprint_plugin_json::configuration::Configuration { + dprint_plugin_json::configuration::ConfigurationBuilder::new() + .deno() + .build() +} + struct FileContents { text: String, had_bom: bool, |