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
14 changes: 11 additions & 3 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,21 @@ fn gen_comma_separated_values<'a>(
};
let items = ir_helpers::new_line_group({
let is_final_node = i == nodes_count - 1;
let use_comma_for_last = !is_final_node
|| match context.config.trailing_commas {
let use_comma_for_last = !is_final_node || {
let leading_comments = context.comments.get(&value.start()).map(|c| c.as_slice());
let trailing_commas = if has_ignore_comment(leading_comments, context) {
// an ignore comment also applies to the node's trailing comma
TrailingCommaKind::Maintain
} else {
context.config.trailing_commas
};
match trailing_commas {
TrailingCommaKind::Always => true,
TrailingCommaKind::Maintain => context.token_finder.get_next_token_if_comma(&value).is_some(),
TrailingCommaKind::Jsonc => context.is_jsonc,
TrailingCommaKind::Never => false,
};
}
};
let maybe_comma = if !is_final_node {
sc_items(COMMA_SC)
} else if use_comma_for_last {
Expand Down
32 changes: 32 additions & 0 deletions tests/specs/comments/Comments_IgnoreNode_TrailingCommas_Always.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
~~ trailingCommas: always ~~
== should not add a trailing comma to an ignored node ==
{
"tasks": {
// dprint-ignore
"foo": "echo bar"
},
"other": {
"foo": "echo bar"
},
"array": [
1,
// dprint-ignore
[ 2, 3 ]
]
}

[expect]
{
"tasks": {
// dprint-ignore
"foo": "echo bar"
},
"other": {
"foo": "echo bar",
},
"array": [
1,
// dprint-ignore
[ 2, 3 ]
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
~~ trailingCommas: jsonc ~~
== should maintain the trailing comma of an ignored node in a json file ==
{
"tasks": {
// dprint-ignore
"foo": "echo bar",
},
"other": {
"foo": "echo bar",
},
"array": [
1,
// dprint-ignore
[ 2, 3 ],
]
}

[expect]
{
"tasks": {
// dprint-ignore
"foo": "echo bar",
},
"other": {
"foo": "echo bar"
},
"array": [
1,
// dprint-ignore
[ 2, 3 ],
]
}
32 changes: 32 additions & 0 deletions tests/specs/comments/Comments_IgnoreNode_TrailingCommas_Never.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
~~ trailingCommas: never ~~
== should maintain the trailing comma of an ignored node ==
{
"tasks": {
// dprint-ignore
"foo": "echo bar",
},
"other": {
"foo": "echo bar",
},
"array": [
1,
// dprint-ignore
[ 2, 3 ],
]
}

[expect]
{
"tasks": {
// dprint-ignore
"foo": "echo bar",
},
"other": {
"foo": "echo bar"
},
"array": [
1,
// dprint-ignore
[ 2, 3 ],
]
}