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

@ -0,0 +1,12 @@
{
"key1": "value1",
"key2": true,
"key3": ["value2", "value3", false],
"keys": {
"more": "values"
}
}

View file

@ -25,4 +25,22 @@ hello( "alice");
function foo(): number {
return 2;
}
```
```jsonc
{
// Comment in JSON
"key": "value",
"key2":
"value2",
}
```
```json
{
"numbers":
["1", "2"]
}
```

View file

@ -0,0 +1,8 @@
{
"key1": "value1",
"key2": true,
"key3": ["value2", "value3", false],
"keys": {
"more": "values"
}
}

View file

@ -21,3 +21,17 @@ function foo(): number {
return 2;
}
```
```jsonc
{
// Comment in JSON
"key": "value",
"key2": "value2"
}
```
```json
{
"numbers": ["1", "2"]
}
```

View file

@ -1 +1 @@
Checked 3 files
Checked 4 files

View file

@ -1 +1 @@
Checked 2 files
Checked 3 files

View file

@ -1,2 +1,2 @@
[WILDCARD]
error: Found 6 not formatted files in [WILDCARD] files
error: Found 7 not formatted files in [WILDCARD] files

View file

@ -0,0 +1,4 @@
{
// Comment
"key": "value"
}

View file

@ -496,30 +496,43 @@ mod integration {
fn fmt_test() {
let t = TempDir::new().expect("tempdir fail");
let fixed_js = util::root_path().join("cli/tests/badly_formatted_fixed.js");
let fixed_md = util::root_path().join("cli/tests/badly_formatted_fixed.md");
let badly_formatted_original_js =
util::root_path().join("cli/tests/badly_formatted.mjs");
let badly_formatted_original_md =
util::root_path().join("cli/tests/badly_formatted.md");
let badly_formatted_js = t.path().join("badly_formatted.js");
let badly_formatted_md = t.path().join("badly_formatted.md");
let badly_formatted_js_str = badly_formatted_js.to_str().unwrap();
let badly_formatted_md_str = badly_formatted_md.to_str().unwrap();
std::fs::copy(&badly_formatted_original_js, &badly_formatted_js)
.expect("Failed to copy file");
let fixed_md = util::root_path().join("cli/tests/badly_formatted_fixed.md");
let badly_formatted_original_md =
util::root_path().join("cli/tests/badly_formatted.md");
let badly_formatted_md = t.path().join("badly_formatted.md");
let badly_formatted_md_str = badly_formatted_md.to_str().unwrap();
std::fs::copy(&badly_formatted_original_md, &badly_formatted_md)
.expect("Failed to copy file");
let fixed_json =
util::root_path().join("cli/tests/badly_formatted_fixed.json");
let badly_formatted_original_json =
util::root_path().join("cli/tests/badly_formatted.json");
let badly_formatted_json = t.path().join("badly_formatted.json");
let badly_formatted_json_str = badly_formatted_json.to_str().unwrap();
std::fs::copy(&badly_formatted_original_json, &badly_formatted_json)
.expect("Failed to copy file");
// First, check formatting by ignoring the badly formatted file.
let status = util::deno_cmd()
.current_dir(util::root_path())
.arg("fmt")
.arg(format!(
"--ignore={},{}",
badly_formatted_js_str, badly_formatted_md_str
"--ignore={},{},{}",
badly_formatted_js_str,
badly_formatted_md_str,
badly_formatted_json_str
))
.arg("--check")
.arg(badly_formatted_js_str)
.arg(badly_formatted_md_str)
.arg(badly_formatted_json_str)
.spawn()
.expect("Failed to spawn script")
.wait()
@ -532,6 +545,7 @@ mod integration {
.arg("--check")
.arg(badly_formatted_js_str)
.arg(badly_formatted_md_str)
.arg(badly_formatted_json_str)
.spawn()
.expect("Failed to spawn script")
.wait()
@ -543,6 +557,7 @@ mod integration {
.arg("fmt")
.arg(badly_formatted_js_str)
.arg(badly_formatted_md_str)
.arg(badly_formatted_json_str)
.spawn()
.expect("Failed to spawn script")
.wait()
@ -550,10 +565,13 @@ mod integration {
assert!(status.success());
let expected_js = std::fs::read_to_string(fixed_js).unwrap();
let expected_md = std::fs::read_to_string(fixed_md).unwrap();
let expected_json = std::fs::read_to_string(fixed_json).unwrap();
let actual_js = std::fs::read_to_string(badly_formatted_js).unwrap();
let actual_md = std::fs::read_to_string(badly_formatted_md).unwrap();
let actual_json = std::fs::read_to_string(badly_formatted_json).unwrap();
assert_eq!(expected_js, actual_js);
assert_eq!(expected_md, actual_md);
assert_eq!(expected_json, actual_json);
}
mod file_watcher {
@ -2838,7 +2856,7 @@ console.log("finish");
});
itest!(fmt_check_tests_dir {
args: "fmt --check ./",
args: "fmt --check ./ --ignore=.test_coverage",
output: "fmt/expected_fmt_check_tests_dir.out",
exit_code: 1,
});
@ -2850,7 +2868,7 @@ console.log("finish");
});
itest!(fmt_check_formatted_files {
args: "fmt --check fmt/formatted1.js fmt/formatted2.ts fmt/formatted3.md",
args: "fmt --check fmt/formatted1.js fmt/formatted2.ts fmt/formatted3.md fmt/formatted4.jsonc",
output: "fmt/expected_fmt_check_formatted_files.out",
exit_code: 0,
});
@ -2875,6 +2893,12 @@ console.log("finish");
),
});
itest!(fmt_stdin_json {
args: "fmt --ext=json -",
input: Some("{ \"key\": \"value\"}"),
output_str: Some("{ \"key\": \"value\" }\n"),
});
itest!(fmt_stdin_check_formatted {
args: "fmt --check -",
input: Some("const a = 1;\n"),