Skip to content

Fix misported emitCommentsBeforeToken #1562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5051,13 +5051,13 @@ func (p *Printer) emitCommentsBeforeToken(token ast.Kind, pos int, contextNode *
return nil, pos
}

node := p.emitContext.ParseNode(contextNode)
isSimilarNode := node != nil && node.Kind == contextNode.Kind
startPos := pos
if p.currentSourceFile != nil {
if isSimilarNode && p.currentSourceFile != nil {
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

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

The condition combines isSimilarNode with the null check for p.currentSourceFile, but this changes the logic from the original code. The original code would skip trivia whenever p.currentSourceFile != nil, regardless of node similarity. This change might cause trivia to not be skipped in cases where it should be, potentially affecting comment positioning.

Suggested change
if isSimilarNode && p.currentSourceFile != nil {
if p.currentSourceFile != nil {

Copilot uses AI. Check for mistakes.

pos = scanner.SkipTrivia(p.currentSourceFile.Text(), startPos)
}

node := p.emitContext.ParseNode(contextNode)
isSimilarNode := node != nil && node.Kind == contextNode.Kind
if !isSimilarNode {
return nil, pos
}
Expand Down
Loading