Skip to content

Commit

Permalink
fix(rome_json_formatter): JSON formatter does not respect lineWidth f…
Browse files Browse the repository at this point in the history
…or arrays rome#4351
  • Loading branch information
denbezrukov committed Apr 11, 2023
1 parent 5367d89 commit 958067e
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions crates/rome_json_formatter/src/json/lists/array_element_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_))))
}
19 changes: 13 additions & 6 deletions crates/rome_json_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
123231321, 123213213, 12312321, 12321123211232112321, 12321321, 1232132112321321123213211232132112321321
]
Original file line number Diff line number Diff line change
@@ -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
]
```


Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ Line width: 80
{
"array": [
"Longggggggggggggggggggggggggggggggggggg",
9999999999999999999999999999999999999, 1111112222222223333344444444, true
9999999999999999999999999999999999999,
1111112222222223333344444444,
true
],
"indented": [
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ Line width: 80
"long": [
11111,
[
"nestedddddddddddd", 22222222222222222222222222, true,
5555555555555555555555, "indenteddddddddddddddddddddddddddddd"
"nestedddddddddddd",
22222222222222222222222222,
true,
5555555555555555555555,
"indenteddddddddddddddddddddddddddddd"
],
"Element"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -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
]
}
Original file line number Diff line number Diff line change
@@ -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
]
}
```


0 comments on commit 958067e

Please sign in to comment.