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.
This commit is contained in:
Satya Rohith 2021-02-18 22:01:32 +05:30 committed by GitHub
parent c0f10e72ee
commit d9b1f96897
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 179 additions and 44 deletions

View file

@ -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,