Skip to content

Commit 5f475b5

Browse files
authored
Merge pull request #6 from sqs/allow-trailing-commas-in-array
Allow trailing commas in array
2 parents a6994d6 + 087e60a commit 5f475b5

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,9 @@ export function visit(text: string, visitor: JSONVisitor, options?: ParseOptions
11461146
}
11471147
onSeparator(',');
11481148
scanNext(); // consume comma
1149+
if (_scanner.getToken() === SyntaxKind.CloseBracketToken && allowTrailingComma) {
1150+
break;
1151+
}
11491152
} else if (needsComma) {
11501153
handleError(ParseErrorCode.CommaExpected, [], []);
11511154
}

src/test/json.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ function assertValidParse(input: string, expected: any, options?: ParseOptions):
3535
var errors: ParseError[] = [];
3636
var actual = parse(input, errors, options);
3737

38-
if (errors.length !== 0) {
39-
assert.notEqual("undefined", typeof errors[0].error);
40-
}
38+
assert.deepEqual([], errors)
4139
assert.deepEqual(actual, expected);
4240
}
4341

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

245243
test('parse: array with errors', () => {
246244
assertInvalidParse('[,]', []);
247-
assertInvalidParse('[ 1, 2, ]', [1, 2]);
248245
assertInvalidParse('[ 1 2, 3 ]', [1, 2, 3]);
249246
assertInvalidParse('[ ,1, 2, 3 ]', [1, 2, 3]);
250247
assertInvalidParse('[ ,1, 2, 3, ]', [1, 2, 3]);
@@ -265,9 +262,12 @@ suite('JSON', () => {
265262
assertValidParse('{ "hello": [] }', { hello: [] }, options);
266263
assertValidParse('{ "hello": [], "world": {}, }', { hello: [], world: {} }, options);
267264
assertValidParse('{ "hello": [], "world": {} }', { hello: [], world: {} }, options);
265+
assertValidParse('[ 1, 2, ]', [1, 2], options);
266+
assertValidParse('[ 1, 2 ]', [1, 2], options);
268267

269268
assertInvalidParse('{ "hello": [], }', { hello: [] });
270269
assertInvalidParse('{ "hello": [], "world": {}, }', { hello: [], world: {} });
270+
assertInvalidParse('[ 1, 2, ]', [1, 2]);
271271
});
272272
test('location: properties', () => {
273273
assertLocation('|{ "foo": "bar" }', [], void 0, false);

0 commit comments

Comments
 (0)