Skip to content

Commit

Permalink
feat: support trailing comma after the last argument (robertkrimen#520)
Browse files Browse the repository at this point in the history
Add support for trailing comma after the last argument of a method call.
  • Loading branch information
linuxerwang authored May 3, 2024
1 parent 1ca7723 commit 2d23528
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
26 changes: 12 additions & 14 deletions parser/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,21 +384,19 @@ func (p *parser) parseArgumentList() (argumentList []ast.Expression, idx0, idx1
p.comments.Unset()
}
idx0 = p.expect(token.LEFT_PARENTHESIS)
if p.token != token.RIGHT_PARENTHESIS {
for {
exp := p.parseAssignmentExpression()
if p.mode&StoreComments != 0 {
p.comments.SetExpression(exp)
}
argumentList = append(argumentList, exp)
if p.token != token.COMMA {
break
}
if p.mode&StoreComments != 0 {
p.comments.Unset()
}
p.next()
for p.token != token.RIGHT_PARENTHESIS {
exp := p.parseAssignmentExpression()
if p.mode&StoreComments != 0 {
p.comments.SetExpression(exp)
}
argumentList = append(argumentList, exp)
if p.token != token.COMMA {
break
}
if p.mode&StoreComments != 0 {
p.comments.Unset()
}
p.next()
}
if p.mode&StoreComments != 0 {
p.comments.Unset()
Expand Down
9 changes: 9 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,15 @@ func TestParser(t *testing.T) {
2
debugger
`, nil)

test(`
function mergeObjects(x, y) { /* dummy body */}
mergeObjects(
{ "duck": "quack" },
{ "dog": "bark" }, // Allow trailing comma after the last argument.
);
`, nil)
})
}

Expand Down

0 comments on commit 2d23528

Please sign in to comment.