diff --git a/src/main.ts b/src/main.ts index de00362..d266a74 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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, [], []); } diff --git a/src/test/json.test.ts b/src/test/json.test.ts index 15cd6e1..cdd5df0 100644 --- a/src/test/json.test.ts +++ b/src/test/json.test.ts @@ -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); } @@ -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]); @@ -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);