Skip to content

Fix superfluous comma in flow style mappings - #323

Draft
sidsri14 wants to merge 1 commit into
google:mainfrom
sidsri14:fix/flow-mapping-trailing-comma-110
Draft

Fix superfluous comma in flow style mappings#323
sidsri14 wants to merge 1 commit into
google:mainfrom
sidsri14:fix/flow-mapping-trailing-comma-110

Conversation

@sidsri14

Copy link
Copy Markdown

Summary

Fixes #110 — a flow-style mapping that carries a comment (e.g. a comment line below a flow mapping sequence item, which retain_line_breaks injects) was emitted with a superfluous trailing comma: - {foo: bar,}.

The flow-mapping MAPPING_END branch in yaml_emitter_emit_flow_mapping_key wrote a separator comma whenever any comment buffer was non-empty (!first && !trail). Comments on/below a flow mapping attach to the mapping node and arrive with the FLOW_MAPPING_END event, so that condition produced a comma before the closing brace even though no separator was needed.

Interior flow-map comments always flow through the TRAIL path (yaml_emitter_emit_flow_mapping_value), so a comma is never legitimately required at MAPPING_END. This change aligns the flow-mapping branch with the flow-sequence END branch, which already writes the comma only in canonical mode.

- if (emitter.canonical || len(emitter.head_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0) && !first && !trail {
+ if emitter.canonical && !first && !trail {

Test plan

Added a regression case to TestFormatter using the exact reporter scenario (include_document_start + retain_line_breaks, blank line below - {foo: bar}):

  • Before fix: output is ---\n- {foo: bar,}\n → test fails
  • After fix: output is ---\n- {foo: bar}\n → test passes

Verified the full go test ./... failure set is identical with/without the change (no regressions), and gofmt/go vet are clean.

Notes

  • This bug is inherited from upstream yaml.v3's emitter logic (identical condition in go.yaml.in/yaml/v3), not introduced by the yamlfmt fork.
  • AI assistance: this patch was developed with the aid of an AI coding assistant (Claude Code); root cause, fix, and test were reviewed and verified manually.

@google-cla

google-cla Bot commented Aug 27, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@sidsri14
sidsri14 force-pushed the fix/flow-mapping-trailing-comma-110 branch from 0d63beb to f955917 Compare August 27, 2026 09:50
The flow-mapping MAPPING_END branch of the emitter wrote a separator
comma whenever a comment buffer was non-empty, matching yaml.v3's
original logic. Comments on or below a flow mapping attach to the
mapping and arrive with the FLOW_MAPPING_END event, producing output
like "- {foo: bar,}".

Interior flow-map comments always flow through the TRAIL path, so a
comma is never legitimately needed at MAPPING_END. Align with the
flow-sequence END branch, which writes the comma only in canonical
mode.

Fixes google#110
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Superfluous comma in flow style

1 participant