Skip to content

Commit d934b7b

Browse files
Fix parsing of a trailing comma in argument list (#7273)
1 parent f6682ed commit d934b7b

3 files changed

Lines changed: 48 additions & 20 deletions

File tree

rewrite-go/rewrite/pkg/parser/go_parser.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -736,19 +736,39 @@ func (ctx *parseContext) mapFieldListAsParams(fl *ast.FieldList) tree.Container[
736736
}
737737
}
738738

739-
closeParen := ctx.prefix(fl.Closing)
740-
ctx.skip(1) // ")"
741-
739+
var markers tree.Markers
742740
if len(elements) > 0 {
743-
elements[len(elements)-1].After = closeParen
744-
} else if len(closeParen.Comments) > 0 {
745-
elements = append(elements, tree.RightPadded[tree.Statement]{
746-
Element: &tree.Empty{ID: uuid.New()},
747-
After: closeParen,
748-
})
741+
trailingCommaOff := ctx.findNextBefore(',', int(fl.Closing)-ctx.file.Base())
742+
if trailingCommaOff >= 0 {
743+
commaBefore := ctx.prefix(ctx.file.Pos(trailingCommaOff))
744+
ctx.skip(1) // ","
745+
commaAfter := ctx.prefix(fl.Closing)
746+
ctx.skip(1) // ")"
747+
markers = tree.Markers{
748+
ID: uuid.New(),
749+
Entries: []tree.Marker{tree.TrailingComma{
750+
Ident: uuid.New(),
751+
Before: commaBefore,
752+
After: commaAfter,
753+
}},
754+
}
755+
} else {
756+
closePrefix := ctx.prefix(fl.Closing)
757+
ctx.skip(1) // ")"
758+
elements[len(elements)-1].After = closePrefix
759+
}
760+
} else {
761+
closeParen := ctx.prefix(fl.Closing)
762+
ctx.skip(1) // ")"
763+
if len(closeParen.Comments) > 0 {
764+
elements = append(elements, tree.RightPadded[tree.Statement]{
765+
Element: &tree.Empty{ID: uuid.New()},
766+
After: closeParen,
767+
})
768+
}
749769
}
750770

751-
return tree.Container[tree.Statement]{Before: before, Elements: elements}
771+
return tree.Container[tree.Statement]{Before: before, Elements: elements, Markers: markers}
752772
}
753773

754774
// mapBlockStmt maps a block statement.

rewrite-go/rewrite/pkg/printer/go_printer.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,16 @@ func (p *GoPrinter) VisitMethodDeclaration(md *tree.MethodDeclaration, param any
230230
func (p *GoPrinter) printParamList(params tree.Container[tree.Statement], out *PrintOutputCapture) {
231231
p.visitSpace(params.Before, out)
232232
out.Append("(")
233+
tc := tree.FindMarker[tree.TrailingComma](params.Markers)
233234
for i, rp := range params.Elements {
234235
p.Visit(rp.Element, out)
235236
if i < len(params.Elements)-1 {
236237
p.visitSpace(rp.After, out)
237238
out.Append(",")
239+
} else if tc != nil {
240+
p.visitSpace(tc.Before, out)
241+
out.Append(",")
242+
p.visitSpace(tc.After, out)
238243
} else {
239244
p.visitSpace(rp.After, out)
240245
}
@@ -792,16 +797,7 @@ func (p *GoPrinter) VisitFuncType(ft *tree.FuncType, param any) tree.J {
792797
out := param.(*PrintOutputCapture)
793798
p.beforeSyntax(ft.Prefix, ft.Markers, out)
794799
out.Append("func")
795-
p.visitSpace(ft.Parameters.Before, out)
796-
out.Append("(")
797-
for i, rp := range ft.Parameters.Elements {
798-
p.Visit(rp.Element, out)
799-
if i < len(ft.Parameters.Elements)-1 {
800-
p.visitSpace(rp.After, out)
801-
out.Append(",")
802-
}
803-
}
804-
out.Append(")")
800+
p.printParamList(ft.Parameters, out)
805801
if ft.ReturnType != nil {
806802
p.Visit(ft.ReturnType, out)
807803
}

rewrite-go/rewrite/test/trailing_comma_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ func TestParseTrailingCommaAnonymousStruct(t *testing.T) {
149149
`))
150150
}
151151

152+
func TestParseTrailingCommaFuncTypeParams(t *testing.T) {
153+
NewRecipeSpec().RewriteRun(t,
154+
Golang(`
155+
package main
156+
157+
var f = map[string]func(
158+
a int,
159+
b int,
160+
) error{}
161+
`))
162+
}
163+
152164
func TestParseTrailingCommaMapOfSlices(t *testing.T) {
153165
NewRecipeSpec().RewriteRun(t,
154166
Golang(`

0 commit comments

Comments
 (0)