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 48f7cd2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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 All @@ -76,8 +75,10 @@ func TestParser(t *testing.T) {
`{ "hello": [] }`: {want: `{"hello":[]}`},
`{ "hello": [], "world": {}, }`: {want: `{"hello":[],"world":{}}`},
`{ "hello2": [], "world": {} }`: {want: `{"hello2":[],"world":{}}`},
"[ 1, 5, ]": {want: "[1,5]"},
`{ "hello2": [], }`: {options: &ParseOptions{TrailingCommas: false}, want: `{"hello2":[]}`, errors: true},
`{ "hello2": [], "world": {}, }`: {options: &ParseOptions{TrailingCommas: false}, want: `{"hello2":[],"world":{}}`, errors: true},
"[ 1, 6, ]": {options: &ParseOptions{TrailingCommas: false}, want: "[1,6]", errors: true},
}
for input, test := range tests {
label := fmt.Sprintf("%q", input)
Expand All @@ -93,6 +94,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 48f7cd2

Please sign in to comment.