Skip to content

Commit

Permalink
fix(json_formatter): handle comments in empty arrays/objects (#1323)
Browse files Browse the repository at this point in the history
  • Loading branch information
faultyserver authored Dec 24, 2023
1 parent d12e99c commit 5f9610b
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 10 deletions.
36 changes: 34 additions & 2 deletions crates/biome_json_formatter/src/comments.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::prelude::*;
use biome_diagnostics::category;
use biome_formatter::comments::{
is_alignable_comment, CommentKind, CommentStyle, Comments, SourceComment,
is_alignable_comment, CommentKind, CommentPlacement, CommentStyle, Comments, DecoratedComment,
SourceComment,
};
use biome_formatter::formatter::Formatter;
use biome_formatter::{write, FormatResult, FormatRule};
use biome_json_syntax::{JsonLanguage, TextLen};
use biome_json_syntax::{JsonArrayValue, JsonLanguage, JsonObjectValue, JsonSyntaxKind, TextLen};
use biome_rowan::SyntaxTriviaPieceComments;
use biome_suppression::parse_suppression_comment;

Expand Down Expand Up @@ -82,4 +83,35 @@ impl CommentStyle for JsonCommentStyle {
CommentKind::Line
}
}

fn place_comment(
&self,
comment: biome_formatter::comments::DecoratedComment<Self::Language>,
) -> biome_formatter::comments::CommentPlacement<Self::Language> {
handle_empty_list_comment(comment)
}
}

fn handle_empty_list_comment(
comment: DecoratedComment<JsonLanguage>,
) -> CommentPlacement<JsonLanguage> {
if !matches!(
comment.enclosing_node().kind(),
JsonSyntaxKind::JSON_ARRAY_VALUE | JsonSyntaxKind::JSON_OBJECT_VALUE,
) {
return CommentPlacement::Default(comment);
}

if let Some(array) = JsonArrayValue::cast_ref(comment.enclosing_node()) {
if array.elements().is_empty() {
return CommentPlacement::dangling(comment.enclosing_node().clone(), comment);
}
}
if let Some(object) = JsonObjectValue::cast_ref(comment.enclosing_node()) {
if object.json_member_list().is_empty() {
return CommentPlacement::dangling(comment.enclosing_node().clone(), comment);
}
}

CommentPlacement::Default(comment)
}
19 changes: 15 additions & 4 deletions crates/biome_json_formatter/src/json/value/array_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::prelude::*;
use biome_formatter::write;
use biome_json_syntax::JsonArrayValue;
use biome_json_syntax::JsonArrayValueFields;
use biome_formatter::{format_args, write};
use biome_json_syntax::{JsonArrayValue, JsonArrayValueFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsonArrayValue;
Expand All @@ -13,13 +12,25 @@ impl FormatNodeRule<JsonArrayValue> for FormatJsonArrayValue {
r_brack_token,
} = node.as_fields();

let should_expand = f.comments().has_dangling_comments(node.syntax());

write!(
f,
[
l_brack_token.format(),
group(&soft_block_indent(&elements.format())),
group(&soft_block_indent(&format_args![
elements.format(),
format_dangling_comments(node.syntax())
]))
.should_expand(should_expand),
line_suffix_boundary(),
r_brack_token.format()
]
)
}

fn fmt_dangling_comments(&self, _: &JsonArrayValue, _: &mut JsonFormatter) -> FormatResult<()> {
// Handled as part of `fmt_fields`
Ok(())
}
}
20 changes: 16 additions & 4 deletions crates/biome_json_formatter/src/json/value/object_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ pub(crate) struct FormatJsonObjectValue;

impl FormatNodeRule<JsonObjectValue> for FormatJsonObjectValue {
fn fmt_fields(&self, node: &JsonObjectValue, f: &mut JsonFormatter) -> FormatResult<()> {
let should_expand = node.json_member_list().syntax().has_leading_newline();
let should_expand = node.json_member_list().syntax().has_leading_newline()
|| f.comments().has_dangling_comments(node.syntax());

let list = format_with(|f| {
write!(
f,
[group(&soft_space_or_block_indent(
&node.json_member_list().format()
))
[group(&soft_space_or_block_indent(&format_args![
&node.json_member_list().format(),
format_dangling_comments(node.syntax()),
]))
.should_expand(should_expand)]
)
});

if f.comments().has_leading_comments(node.syntax()) {
write!(
f,
Expand Down Expand Up @@ -47,4 +50,13 @@ impl FormatNodeRule<JsonObjectValue> for FormatJsonObjectValue {
// Formatted as part of `fmt_fields`
Ok(())
}

fn fmt_dangling_comments(
&self,
_: &JsonObjectValue,
_: &mut JsonFormatter,
) -> FormatResult<()> {
// Formatted as part of `fmt_fields`
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object-block": { /* here's a block comment */},
"object-line": { // here's a line comment
},
"object-ownline": {
/* here's a block comment */
// and a line comment
},
"array-block": [ /* here's a block comment */],
"array-line": [ // here's a line comment
],
"array-ownline": [
/* here's a block comment */
// and a line comment
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: json/comments/empty_with_comments.json
---

# Input

```json
{
"object-block": { /* here's a block comment */},
"object-line": { // here's a line comment
},
"object-ownline": {
/* here's a block comment */
// and a line comment
},
"array-block": [ /* here's a block comment */],
"array-line": [ // here's a line comment
],
"array-ownline": [
/* here's a block comment */
// and a line comment
]
}
```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
-----

```json
{
"object-block": {
/* here's a block comment */
},
"object-line": {
// here's a line comment
},
"object-ownline": {
/* here's a block comment */
// and a line comment
},
"array-block": [
/* here's a block comment */
],
"array-line": [
// here's a line comment
],
"array-ownline": [
/* here's a block comment */
// and a line comment
]
}
```


0 comments on commit 5f9610b

Please sign in to comment.