Skip to content
Merged
Show file tree
Hide file tree
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
40 changes: 30 additions & 10 deletions rewrite-go/rewrite/pkg/parser/go_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,19 +736,39 @@ func (ctx *parseContext) mapFieldListAsParams(fl *ast.FieldList) tree.Container[
}
}

closeParen := ctx.prefix(fl.Closing)
ctx.skip(1) // ")"

var markers tree.Markers
if len(elements) > 0 {
elements[len(elements)-1].After = closeParen
} else if len(closeParen.Comments) > 0 {
elements = append(elements, tree.RightPadded[tree.Statement]{
Element: &tree.Empty{ID: uuid.New()},
After: closeParen,
})
trailingCommaOff := ctx.findNextBefore(',', int(fl.Closing)-ctx.file.Base())
if trailingCommaOff >= 0 {
commaBefore := ctx.prefix(ctx.file.Pos(trailingCommaOff))
ctx.skip(1) // ","
commaAfter := ctx.prefix(fl.Closing)
ctx.skip(1) // ")"
markers = tree.Markers{
ID: uuid.New(),
Entries: []tree.Marker{tree.TrailingComma{
Ident: uuid.New(),
Before: commaBefore,
After: commaAfter,
}},
}
} else {
closePrefix := ctx.prefix(fl.Closing)
ctx.skip(1) // ")"
elements[len(elements)-1].After = closePrefix
}
} else {
closeParen := ctx.prefix(fl.Closing)
ctx.skip(1) // ")"
if len(closeParen.Comments) > 0 {
elements = append(elements, tree.RightPadded[tree.Statement]{
Element: &tree.Empty{ID: uuid.New()},
After: closeParen,
})
}
}

return tree.Container[tree.Statement]{Before: before, Elements: elements}
return tree.Container[tree.Statement]{Before: before, Elements: elements, Markers: markers}
}

// mapBlockStmt maps a block statement.
Expand Down
16 changes: 6 additions & 10 deletions rewrite-go/rewrite/pkg/printer/go_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,16 @@ func (p *GoPrinter) VisitMethodDeclaration(md *tree.MethodDeclaration, param any
func (p *GoPrinter) printParamList(params tree.Container[tree.Statement], out *PrintOutputCapture) {
p.visitSpace(params.Before, out)
out.Append("(")
tc := tree.FindMarker[tree.TrailingComma](params.Markers)
for i, rp := range params.Elements {
p.Visit(rp.Element, out)
if i < len(params.Elements)-1 {
p.visitSpace(rp.After, out)
out.Append(",")
} else if tc != nil {
p.visitSpace(tc.Before, out)
out.Append(",")
p.visitSpace(tc.After, out)
} else {
p.visitSpace(rp.After, out)
}
Expand Down Expand Up @@ -792,16 +797,7 @@ func (p *GoPrinter) VisitFuncType(ft *tree.FuncType, param any) tree.J {
out := param.(*PrintOutputCapture)
p.beforeSyntax(ft.Prefix, ft.Markers, out)
out.Append("func")
p.visitSpace(ft.Parameters.Before, out)
out.Append("(")
for i, rp := range ft.Parameters.Elements {
p.Visit(rp.Element, out)
if i < len(ft.Parameters.Elements)-1 {
p.visitSpace(rp.After, out)
out.Append(",")
}
}
out.Append(")")
p.printParamList(ft.Parameters, out)
if ft.ReturnType != nil {
p.Visit(ft.ReturnType, out)
}
Expand Down
12 changes: 12 additions & 0 deletions rewrite-go/rewrite/test/trailing_comma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ func TestParseTrailingCommaAnonymousStruct(t *testing.T) {
`))
}

func TestParseTrailingCommaFuncTypeParams(t *testing.T) {
NewRecipeSpec().RewriteRun(t,
Golang(`
package main

var f = map[string]func(
a int,
b int,
) error{}
`))
}

func TestParseTrailingCommaMapOfSlices(t *testing.T) {
NewRecipeSpec().RewriteRun(t,
Golang(`
Expand Down