From b839bdfae9edf4d8f213edd924a8d3f660245867 Mon Sep 17 00:00:00 2001 From: Denis Bezrukov <6227442+denbezrukov@users.noreply.github.com> Date: Tue, 11 Apr 2023 18:34:33 +0300 Subject: [PATCH] fix(rome_json_formatter): JSON formatter does not respect lineWidth for arrays #4351 (#4367) fix(rome_json_formatter): JSON formatter does not respect lineWidth for arrays #4351 --- CHANGELOG.md | 1 + .../does_not_format_ignored_directories.snap | 9 ++- .../src/json/lists/array_element_list.rs | 11 +-- .../rome_json_formatter/tests/quick_test.rs | 19 +++-- .../tests/specs/json/array/fill_layout.json | 3 + .../specs/json/array/fill_layout.json.snap | 34 ++++++++ .../tests/specs/json/array/layout.json.snap | 10 ++- .../specs/json/array/multi_line.json.snap | 4 +- .../tests/specs/json/array/nested.json.snap | 7 +- .../specs/json/array/one_per_line_layout.json | 11 +++ .../json/array/one_per_line_layout.json.snap | 81 +++++++++++++++++++ 11 files changed, 168 insertions(+), 22 deletions(-) create mode 100644 crates/rome_json_formatter/tests/specs/json/array/fill_layout.json create mode 100644 crates/rome_json_formatter/tests/specs/json/array/fill_layout.json.snap create mode 100644 crates/rome_json_formatter/tests/specs/json/array/one_per_line_layout.json create mode 100644 crates/rome_json_formatter/tests/specs/json/array/one_per_line_layout.json.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bf7a3ab7f1..4364726e9c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - import "module" assert {} + import "module" with {} ``` +- Fix an issue where JSON formatter does not respect `lineWidth` for arrays [#4351](https://github.com/rome/tools/issues/4351) ### Linter #### Other changes diff --git a/crates/rome_cli/tests/snapshots/main_commands_format/does_not_format_ignored_directories.snap b/crates/rome_cli/tests/snapshots/main_commands_format/does_not_format_ignored_directories.snap index 5f28de72cda..00e1bd7e5df 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_format/does_not_format_ignored_directories.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_format/does_not_format_ignored_directories.snap @@ -8,8 +8,13 @@ expression: content { "formatter": { "ignore": [ - "test1.js", "./test2.js", "./test3/**/*", "/test4/**/*", "test5/**/*", - "**/test6/*.js", "*.test7.js" + "test1.js", + "./test2.js", + "./test3/**/*", + "/test4/**/*", + "test5/**/*", + "**/test6/*.js", + "*.test7.js" ] } } diff --git a/crates/rome_json_formatter/src/json/lists/array_element_list.rs b/crates/rome_json_formatter/src/json/lists/array_element_list.rs index a8f4b0cce2c..785d48296de 100644 --- a/crates/rome_json_formatter/src/json/lists/array_element_list.rs +++ b/crates/rome_json_formatter/src/json/lists/array_element_list.rs @@ -82,13 +82,6 @@ pub(crate) fn can_concisely_print_array_list(list: &JsonArrayElementList) -> boo return false; } - list.iter().all(|node| { - matches!( - node, - Ok(AnyJsonValue::JsonStringValue(_) - | AnyJsonValue::JsonBooleanValue(_) - | AnyJsonValue::JsonNullValue(_) - | AnyJsonValue::JsonNumberValue(_)) - ) - }) + list.iter() + .all(|node| matches!(node, Ok(AnyJsonValue::JsonNumberValue(_)))) } diff --git a/crates/rome_json_formatter/tests/quick_test.rs b/crates/rome_json_formatter/tests/quick_test.rs index 8be4bdd580e..7bd0a964782 100644 --- a/crates/rome_json_formatter/tests/quick_test.rs +++ b/crates/rome_json_formatter/tests/quick_test.rs @@ -13,12 +13,19 @@ mod language { fn quick_test() { let src = r#" { - "a": 5, - "b": [1, 2, 3, 4], - "c": null, - "d": true, - "e": false -} + "enabled": true, + "formatWithErrors": false, + "indentSize": 2, + "indentStyle": "space", + "lineWidth": 80, + "ignore": [ + "**/cache/**", + "**/dist/**", + "./packages/laravel/**/*", + "./packages/presets/templates/**/*", + "./sandboxes/**/*" + ] + } "#; let parse = parse_json(src); let options = JsonFormatOptions::default(); diff --git a/crates/rome_json_formatter/tests/specs/json/array/fill_layout.json b/crates/rome_json_formatter/tests/specs/json/array/fill_layout.json new file mode 100644 index 00000000000..407e5e3d432 --- /dev/null +++ b/crates/rome_json_formatter/tests/specs/json/array/fill_layout.json @@ -0,0 +1,3 @@ +[ + 123231321, 123213213, 12312321, 12321123211232112321, 12321321, 1232132112321321123213211232132112321321 +] diff --git a/crates/rome_json_formatter/tests/specs/json/array/fill_layout.json.snap b/crates/rome_json_formatter/tests/specs/json/array/fill_layout.json.snap new file mode 100644 index 00000000000..ed15539f5f7 --- /dev/null +++ b/crates/rome_json_formatter/tests/specs/json/array/fill_layout.json.snap @@ -0,0 +1,34 @@ +--- +source: crates/rome_formatter_test/src/snapshot_builder.rs +info: json/array/fill_layout.json +--- + +# Input + +```json +[ + 123231321, 123213213, 12312321, 12321123211232112321, 12321321, 1232132112321321123213211232132112321321 +] + +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Line width: 80 +----- + +```json +[ + 123231321, 123213213, 12312321, 12321123211232112321, 12321321, + 1232132112321321123213211232132112321321 +] +``` + + diff --git a/crates/rome_json_formatter/tests/specs/json/array/layout.json.snap b/crates/rome_json_formatter/tests/specs/json/array/layout.json.snap index 2e29ff35479..839ce8cc807 100644 --- a/crates/rome_json_formatter/tests/specs/json/array/layout.json.snap +++ b/crates/rome_json_formatter/tests/specs/json/array/layout.json.snap @@ -35,9 +35,15 @@ Line width: 80 ```json { "fillLayout": [ - "aaaa", "bbb", "cc", + "aaaa", + "bbb", + "cc", - true, false, 111, 44, null + true, + false, + 111, + 44, + null ], "oneLineLayout": [ "aaa", diff --git a/crates/rome_json_formatter/tests/specs/json/array/multi_line.json.snap b/crates/rome_json_formatter/tests/specs/json/array/multi_line.json.snap index 4aded88ca55..44df18d4ceb 100644 --- a/crates/rome_json_formatter/tests/specs/json/array/multi_line.json.snap +++ b/crates/rome_json_formatter/tests/specs/json/array/multi_line.json.snap @@ -32,7 +32,9 @@ Line width: 80 { "array": [ "Longggggggggggggggggggggggggggggggggggg", - 9999999999999999999999999999999999999, 1111112222222223333344444444, true + 9999999999999999999999999999999999999, + 1111112222222223333344444444, + true ], "indented": [ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", diff --git a/crates/rome_json_formatter/tests/specs/json/array/nested.json.snap b/crates/rome_json_formatter/tests/specs/json/array/nested.json.snap index 69b36781dc5..c9e8eb75151 100644 --- a/crates/rome_json_formatter/tests/specs/json/array/nested.json.snap +++ b/crates/rome_json_formatter/tests/specs/json/array/nested.json.snap @@ -36,8 +36,11 @@ Line width: 80 "long": [ 11111, [ - "nestedddddddddddd", 22222222222222222222222222, true, - 5555555555555555555555, "indenteddddddddddddddddddddddddddddd" + "nestedddddddddddd", + 22222222222222222222222222, + true, + 5555555555555555555555, + "indenteddddddddddddddddddddddddddddd" ], "Element" ] diff --git a/crates/rome_json_formatter/tests/specs/json/array/one_per_line_layout.json b/crates/rome_json_formatter/tests/specs/json/array/one_per_line_layout.json new file mode 100644 index 00000000000..376e6fe9cb9 --- /dev/null +++ b/crates/rome_json_formatter/tests/specs/json/array/one_per_line_layout.json @@ -0,0 +1,11 @@ +{ + "boolean": [ + true, true, true, true, true, true, true, true, false, false, false, true, false, false + ], + "strings": [ + "longlongstringlonglongstring", "12312321", "longlongstringlonglongstring" + ], + "null": [ + null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null + ] +} diff --git a/crates/rome_json_formatter/tests/specs/json/array/one_per_line_layout.json.snap b/crates/rome_json_formatter/tests/specs/json/array/one_per_line_layout.json.snap new file mode 100644 index 00000000000..72650f6bd03 --- /dev/null +++ b/crates/rome_json_formatter/tests/specs/json/array/one_per_line_layout.json.snap @@ -0,0 +1,81 @@ +--- +source: crates/rome_formatter_test/src/snapshot_builder.rs +info: json/array/one_per_line_layout.json +--- + +# Input + +```json +{ + "boolean": [ + true, true, true, true, true, true, true, true, false, false, false, true, false, false + ], + "strings": [ + "longlongstringlonglongstring", "12312321", "longlongstringlonglongstring" + ], + "null": [ + null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null + ] +} + +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Line width: 80 +----- + +```json +{ + "boolean": [ + true, + true, + true, + true, + true, + true, + true, + true, + false, + false, + false, + true, + false, + false + ], + "strings": [ + "longlongstringlonglongstring", + "12312321", + "longlongstringlonglongstring" + ], + "null": [ + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null + ] +} +``` + +