diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 9280fc0..9f7e570 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -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 { diff --git a/tests/specs/comments/Comments_IgnoreNode_TrailingCommas_Always.txt b/tests/specs/comments/Comments_IgnoreNode_TrailingCommas_Always.txt new file mode 100644 index 0000000..1cfedc2 --- /dev/null +++ b/tests/specs/comments/Comments_IgnoreNode_TrailingCommas_Always.txt @@ -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 ] + ], +} diff --git a/tests/specs/comments/Comments_IgnoreNode_TrailingCommas_Jsonc_Json.txt b/tests/specs/comments/Comments_IgnoreNode_TrailingCommas_Jsonc_Json.txt new file mode 100644 index 0000000..a78d202 --- /dev/null +++ b/tests/specs/comments/Comments_IgnoreNode_TrailingCommas_Jsonc_Json.txt @@ -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 ], + ] +} diff --git a/tests/specs/comments/Comments_IgnoreNode_TrailingCommas_Never.txt b/tests/specs/comments/Comments_IgnoreNode_TrailingCommas_Never.txt new file mode 100644 index 0000000..0d97b73 --- /dev/null +++ b/tests/specs/comments/Comments_IgnoreNode_TrailingCommas_Never.txt @@ -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 ], + ] +}