Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse: allow trailing commas in array (not just object) #1

Merged
merged 1 commit into from
Mar 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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