From a8eef40e1599e9352ce75678c9aefd6ecbafb7dc Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Fri, 2 Mar 2018 22:14:11 -0800 Subject: [PATCH 1/2] allow trailing commas in array when allowTrailingCommas option is true Previously, allowTrailingCommas only allowed trailing commas in objects. --- src/main.ts | 3 +++ src/test/json.test.ts | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) 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..70972ff 100644 --- a/src/test/json.test.ts +++ b/src/test/json.test.ts @@ -244,7 +244,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 +264,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); From 087e60af05a597532cdbb17eefc43bb79d3175d8 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Fri, 2 Mar 2018 22:14:38 -0800 Subject: [PATCH 2/2] fix assertValidParse: fail if there are errors Previously, it only failed the test if an error's code was "undefined". --- src/test/json.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/json.test.ts b/src/test/json.test.ts index 70972ff..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); }