Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,15 @@ fn gen_surrounded_by_tokens<'a, 'b>(
context,
)));
if let Some(leading_comments) = context.comments.get(&close_token_start) {
// the separating newline is emitted elsewhere, so only add a newline for a blank line in the source
if let Some(start) = leading_comments.first().map(|c| c.start()) {
let comment_start_line = context.text_info.line_index(start);
if let Some(prev_token) = context.token_finder.get_previous_token(&Range::from_byte_index(start))
&& comment_start_line > context.text_info.line_index(prev_token.end()) + 1
{
items.push_signal(Signal::NewLine);
}
}
items.extend(ir_helpers::with_indent(gen_comments_as_statements(
leading_comments.iter(),
None,
Expand Down
64 changes: 64 additions & 0 deletions tests/specs/comments/Comments_BlankLineBeforeTrailing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-- /file.jsonc --
== should preserve a blank line before trailing comments at the end of an object (#34) ==
{
// This is a setting.
"foo": "",

// These settings are temporarily commented out.
// "baz": "",
// "qux": ""
}

[expect]
{
// This is a setting.
"foo": "",

// These settings are temporarily commented out.
// "baz": "",
// "qux": ""
}

== should preserve a blank line before trailing comments at the end of an array ==
[
"foo",

// trailing comment
// another
]

[expect]
[
"foo",

// trailing comment
// another
]

== should not add a blank line before trailing comments when the source had none ==
{
"foo": ""
// trailing comment
}

[expect]
{
"foo": "",
// trailing comment
}

== should collapse multiple blank lines before trailing comments to one ==
{
"foo": ""



// trailing comment
}

[expect]
{
"foo": "",

// trailing comment
}