Skip to content

fix: handle synthetic comments in commentBefore function#2745

Merged
xushiwei merged 3 commits into
goplus:mainfrom
go-wyvern:event-comment
Jun 9, 2026
Merged

fix: handle synthetic comments in commentBefore function#2745
xushiwei merged 3 commits into
goplus:mainfrom
go-wyvern:event-comment

Conversation

@go-wyvern

@go-wyvern go-wyvern commented Jun 9, 2026

Copy link
Copy Markdown
Member

fix: #2747

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies the commentBefore function in printer/printer.go to correctly handle synthetic comments attached via CommentedStmts when their offset matches the statement's first token offset. It also adds a corresponding unit test TestCommentedNodesInEventHandler in printer/printer_test.go to verify this behavior. No review comments were provided, so there is no additional feedback.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@codecov

codecov Bot commented Jun 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.20%. Comparing base (a7e45c7) to head (cee9464).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2745      +/-   ##
==========================================
+ Coverage   94.09%   94.20%   +0.10%     
==========================================
  Files          32       32              
  Lines       10074    10074              
==========================================
+ Hits         9479     9490      +11     
+ Misses        425      419       -6     
+ Partials      170      165       -5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overview

This PR fixes a bug where synthetic comments attached to statements via CommentedStmts were not being printed before their associated statement. The root cause: the original commentBefore check used strict < comparison, so a synthetic comment with Slash set to exactly the statement's first-token offset (offset equal, not less-than) was silently dropped.

Code Change Analysis

printer/printer.go:167–171commentBefore

The two changes to the return expression are both necessary:

  1. <<= — correct fix for the core bug: a synthetic comment whose Pos() equals the statement's offset is now treated as a leading comment.
  2. Added p.commentOffset != infinity guard — critical correctness requirement. Without it, the new <= would fire when p.flush is called at EOF with Offset: infinity and p.commentOffset is also infinity (no more comments). The old < happened to be safe there; the new <= is not.

The combination is correct. The comment in the code explains the <= change but omits mention of why the != infinity guard is needed — a reader might think it is defensive boilerplate when it is load-bearing.

Suggestion: Expand the inline comment to document the infinity guard:

// For synthetic comments attached via CommentedStmts, the comment offset may
// be exactly the same as the statement's first token offset. Treat it as a
// leading comment to preserve expected placement.
// The != infinity guard is required because flush() calls this with
// Offset: infinity; without it, <= would falsely match when no comments remain.
return p.commentOffset != infinity && p.commentOffset <= next.Offset && (!p.impliedSemi || !p.commentNewline)

printer/nodes.go:1417–1424stmt (unchanged, context only)

The injection point where setComment is called before the switch. setComment only fires when p.useNodeComments is true — the PR relies on this path being active, which it is when CommentedNodes is used. This looks correct.

Test Coverage

TestCommentedNodesInEventHandler correctly reproduces the bug scenario:

  • Parses a .gox file with an onStart => event handler (the exact construct that triggered the issue).
  • Sets Slash: eventStmt.Pos() to co-locate the synthetic comment with the statement (the key precondition).
  • Asserts the comment text immediately precedes onStart => in the output.

Minor observations:

  • The test uses strings.Contains rather than an exact match, which is appropriate since the full file has surrounding content.
  • ShadowEntry access is guarded with nil checks before use — good defensive test setup.
  • The test does not cover the infinity edge case directly. This is acceptable: the guard is already exercised implicitly by every test that calls Fprint to completion (all of which invoke the final flush at EOF).
  • Existing TestCommentedNodes subtests continue to cover the strict < cases, providing regression coverage.

Summary

The fix is correct, minimal, and well-targeted. The new test covers the bug scenario precisely. The only actionable suggestion is to document why the != infinity guard is load-bearing in the inline comment, so a future reader does not mistake it for optional boilerplate and remove it.

Comment thread printer/printer.go
@xushiwei

xushiwei commented Jun 9, 2026

Copy link
Copy Markdown
Member

please create an issue before creating a PR.

@go-wyvern

Copy link
Copy Markdown
Member Author

please create an issue before creating a PR.

created: #2747

@xushiwei xushiwei merged commit 06849ba into goplus:main Jun 9, 2026
11 checks passed
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.

Printer drops or repositions CommentedStmts comments attached

2 participants