Skip to content

Commit dab51b5

Browse files
committed
Parse: allow trailing commas in array (not just object)
1 parent df0433a commit dab51b5

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

parser_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func TestParser(t *testing.T) {
4848
"[]": {want: "[]"},
4949
"[ [], [ [] ]]": {want: "[[],[[]]]"},
5050
"[ 1, 2, 3 ]": {want: "[1,2,3]"},
51+
"[ 1, 5, ]": {want: "[1,5]"},
5152
`[ { "a": null } ]`: {want: `[{"a":null}]`},
5253

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

6162
// array with errors
6263
"[,]": {want: "[]", errors: true},
63-
"[ 1, 5, ]": {want: "[1,5]"},
6464
"[ 1, 2, ]": {options: &ParseOptions{TrailingCommas: false}, want: "[1,2]", errors: true},
6565
"[ 1 2, 3]": {want: "[1,2,3]", errors: true},
6666
"[ ,1, 2, 3 ]": {want: "[1,2,3]", errors: true},
@@ -93,6 +93,9 @@ func TestParser(t *testing.T) {
9393
if test.errors && errors == nil {
9494
t.Errorf("%s: got no parse errors, want parse errors", label)
9595
}
96+
if !test.errors && errors != nil {
97+
t.Errorf("%s: got parse errors %v, want no parse errors", label, errors)
98+
}
9699
if string(output) != test.want {
97100
t.Errorf("%s: got output %s, want %s", label, output, test.want)
98101
}

visitor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ func (w *walker) parseArray() bool {
260260
}
261261
w.onSeparator(',')
262262
w.scanNext() // consume comma
263+
if w.scanner.Token() == CloseBracketToken && w.options.TrailingCommas {
264+
break
265+
}
263266
} else if needsComma {
264267
w.handleError(CommaExpected, nil, nil)
265268
}

0 commit comments

Comments
 (0)