Skip to content

Commit 550c442

Browse files
Go: Fix comments within field access (#7561)
1 parent 1791ef5 commit 550c442

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

rewrite-go/pkg/parser/go_parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,7 @@ func (ctx *parseContext) mapCallExpr(expr *ast.CallExpr) tree.Expression {
17581758

17591759
switch f := fun.(type) {
17601760
case *tree.FieldAccess:
1761-
selRP := tree.RightPadded[tree.Expression]{Element: f.Target}
1761+
selRP := tree.RightPadded[tree.Expression]{Element: f.Target, After: f.Name.Before}
17621762
sel = &selRP
17631763
name = f.Name.Element
17641764
case *tree.Identifier:

rewrite-go/pkg/printer/go_printer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ func (p *GoPrinter) VisitMethodInvocation(mi *tree.MethodInvocation, param any)
279279
if mi.Select != nil {
280280
p.Visit(mi.Select.Element, out)
281281
if mi.Name.Name != "" {
282+
p.visitSpace(mi.Select.After, out)
282283
out.Append(".")
283284
}
284285
}

rewrite-go/test/comment_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ func TestParseCommentInsideEmptyDelimiters(t *testing.T) {
113113
`))
114114
}
115115

116+
func TestParseBlockCommentBeforeSelector(t *testing.T) {
117+
NewRecipeSpec().RewriteRun(t,
118+
Golang(`
119+
package main
120+
121+
import "unsafe"
122+
123+
func f(x int) {
124+
unsafe /* a comment */ .Alignof(x)
125+
}
126+
`))
127+
}
128+
116129
func TestParseMultilineBlockComment(t *testing.T) {
117130
NewRecipeSpec().RewriteRun(t,
118131
Golang(`

0 commit comments

Comments
 (0)