Skip to content

Commit 48f7cd2

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

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

parser_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ func TestParser(t *testing.T) {
6060

6161
// array with errors
6262
"[,]": {want: "[]", errors: true},
63-
"[ 1, 5, ]": {want: "[1,5]"},
6463
"[ 1, 2, ]": {options: &ParseOptions{TrailingCommas: false}, want: "[1,2]", errors: true},
6564
"[ 1 2, 3]": {want: "[1,2,3]", errors: true},
6665
"[ ,1, 2, 3 ]": {want: "[1,2,3]", errors: true},
@@ -76,8 +75,10 @@ func TestParser(t *testing.T) {
7675
`{ "hello": [] }`: {want: `{"hello":[]}`},
7776
`{ "hello": [], "world": {}, }`: {want: `{"hello":[],"world":{}}`},
7877
`{ "hello2": [], "world": {} }`: {want: `{"hello2":[],"world":{}}`},
78+
"[ 1, 5, ]": {want: "[1,5]"},
7979
`{ "hello2": [], }`: {options: &ParseOptions{TrailingCommas: false}, want: `{"hello2":[]}`, errors: true},
8080
`{ "hello2": [], "world": {}, }`: {options: &ParseOptions{TrailingCommas: false}, want: `{"hello2":[],"world":{}}`, errors: true},
81+
"[ 1, 6, ]": {options: &ParseOptions{TrailingCommas: false}, want: "[1,6]", errors: true},
8182
}
8283
for input, test := range tests {
8384
label := fmt.Sprintf("%q", input)
@@ -93,6 +94,9 @@ func TestParser(t *testing.T) {
9394
if test.errors && errors == nil {
9495
t.Errorf("%s: got no parse errors, want parse errors", label)
9596
}
97+
if !test.errors && errors != nil {
98+
t.Errorf("%s: got parse errors %v, want no parse errors", label, errors)
99+
}
96100
if string(output) != test.want {
97101
t.Errorf("%s: got output %s, want %s", label, output, test.want)
98102
}

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)