Skip to content

Commit

Permalink
Merge pull request #6 from sqs/allow-trailing-commas-in-array
Browse files Browse the repository at this point in the history
Allow trailing commas in array
  • Loading branch information
aeschli authored Mar 5, 2018
2 parents a6994d6 + 087e60a commit 5f475b5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,9 @@ export function visit(text: string, visitor: JSONVisitor, options?: ParseOptions
}
onSeparator(',');
scanNext(); // consume comma
if (_scanner.getToken() === SyntaxKind.CloseBracketToken && allowTrailingComma) {
break;
}
} else if (needsComma) {
handleError(ParseErrorCode.CommaExpected, [], []);
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ function assertValidParse(input: string, expected: any, options?: ParseOptions):
var errors: ParseError[] = [];
var actual = parse(input, errors, options);

if (errors.length !== 0) {
assert.notEqual("undefined", typeof errors[0].error);
}
assert.deepEqual([], errors)
assert.deepEqual(actual, expected);
}

Expand Down Expand Up @@ -244,7 +242,6 @@ suite('JSON', () => {

test('parse: array with errors', () => {
assertInvalidParse('[,]', []);
assertInvalidParse('[ 1, 2, ]', [1, 2]);
assertInvalidParse('[ 1 2, 3 ]', [1, 2, 3]);
assertInvalidParse('[ ,1, 2, 3 ]', [1, 2, 3]);
assertInvalidParse('[ ,1, 2, 3, ]', [1, 2, 3]);
Expand All @@ -265,9 +262,12 @@ suite('JSON', () => {
assertValidParse('{ "hello": [] }', { hello: [] }, options);
assertValidParse('{ "hello": [], "world": {}, }', { hello: [], world: {} }, options);
assertValidParse('{ "hello": [], "world": {} }', { hello: [], world: {} }, options);
assertValidParse('[ 1, 2, ]', [1, 2], options);
assertValidParse('[ 1, 2 ]', [1, 2], options);

assertInvalidParse('{ "hello": [], }', { hello: [] });
assertInvalidParse('{ "hello": [], "world": {}, }', { hello: [], world: {} });
assertInvalidParse('[ 1, 2, ]', [1, 2]);
});
test('location: properties', () => {
assertLocation('|{ "foo": "bar" }', [], void 0, false);
Expand Down

0 comments on commit 5f475b5

Please sign in to comment.