Skip to content

Commit

Permalink
Parse: allow trailing commas in array (not just object)
Browse files Browse the repository at this point in the history
  • Loading branch information
sqs committed Mar 3, 2018
1 parent df0433a commit dab51b5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestParser(t *testing.T) {
"[]": {want: "[]"},
"[ [], [ [] ]]": {want: "[[],[[]]]"},
"[ 1, 2, 3 ]": {want: "[1,2,3]"},
"[ 1, 5, ]": {want: "[1,5]"},
`[ { "a": null } ]`: {want: `[{"a":null}]`},

// objects with errors
Expand All @@ -60,7 +61,6 @@ func TestParser(t *testing.T) {

// array with errors
"[,]": {want: "[]", errors: true},
"[ 1, 5, ]": {want: "[1,5]"},
"[ 1, 2, ]": {options: &ParseOptions{TrailingCommas: false}, want: "[1,2]", errors: true},
"[ 1 2, 3]": {want: "[1,2,3]", errors: true},
"[ ,1, 2, 3 ]": {want: "[1,2,3]", errors: true},
Expand Down Expand Up @@ -93,6 +93,9 @@ func TestParser(t *testing.T) {
if test.errors && errors == nil {
t.Errorf("%s: got no parse errors, want parse errors", label)
}
if !test.errors && errors != nil {
t.Errorf("%s: got parse errors %v, want no parse errors", label, errors)
}
if string(output) != test.want {
t.Errorf("%s: got output %s, want %s", label, output, test.want)
}
Expand Down
3 changes: 3 additions & 0 deletions visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ func (w *walker) parseArray() bool {
}
w.onSeparator(',')
w.scanNext() // consume comma
if w.scanner.Token() == CloseBracketToken && w.options.TrailingCommas {
break
}
} else if needsComma {
w.handleError(CommaExpected, nil, nil)
}
Expand Down

0 comments on commit dab51b5

Please sign in to comment.