Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/impl/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {

namespace ParseOptionsConfigs {
export const DEFAULT = {
allowTrailingComma: false
allowTrailingComma: true
};
}

Expand Down
36 changes: 19 additions & 17 deletions src/test/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ function assertInvalidParse(input: string, expected: any, options?: ParseOptions
assert.deepStrictEqual(actual, expected);
}

function assertTree(input: string, expected: any, expectedErrors: ParseError[] = []): void {
function assertTree(input: string, expected: any, expectedErrors: ParseError[] = [], options?: ParseOptions): void {
var errors: ParseError[] = [];
var actual = parseTree(input, errors);
var actual = parseTree(input, errors, options);

assert.deepStrictEqual(errors, expectedErrors);
let checkParent = (node: Node | undefined) => {
Expand Down Expand Up @@ -275,9 +275,16 @@ suite('JSON', () => {
assertValidParse('[ { "a": null } ]', [{ a: null }]);
});

test('parse: objects with errors', () => {
test('parse: objects with errors with no trailing comma', () => {
let options = { allowTrailingComma: false };
assertInvalidParse('{ "foo": true, }', { foo: true }, options);
assertInvalidParse('{ "hello": [], }', { hello: [] }, options);
assertInvalidParse('{ "hello": [], "world": {}, }', { hello: [], world: {} }, options);
assertInvalidParse('[ 1, 2, ]', [1, 2], options);
});

test('parse objects with errors', () => {
assertInvalidParse('{,}', {});
assertInvalidParse('{ "foo": true, }', { foo: true });
assertInvalidParse('{ "bar": 8 "xoo": "foo" }', { bar: 8, xoo: 'foo' });
assertInvalidParse('{ ,"bar": 8 }', { bar: 8 });
assertInvalidParse('{ ,"bar": 8, "foo" }', { bar: 8 });
Expand Down Expand Up @@ -307,17 +314,12 @@ suite('JSON', () => {
});

test('parse: trailing comma', () => {
let options = { allowTrailingComma: true };
assertValidParse('{ "hello": [], }', { hello: [] }, options);
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]);
assertValidParse('{ "hello": [], }', { hello: [] });
assertValidParse('{ "hello": [] }', { hello: [] });
assertValidParse('{ "hello": [], "world": {}, }', { hello: [], world: {} });
assertValidParse('{ "hello": [], "world": {} }', { hello: [], world: {} });
assertValidParse('[ 1, 2, ]', [1, 2]);
assertValidParse('[ 1, 2 ]', [1, 2]);
});
test('location: properties', () => {
assertLocation('|{ "foo": "bar" }', [], void 0, false);
Expand Down Expand Up @@ -432,8 +434,8 @@ suite('JSON', () => {
]
}, [
{ error: ParseErrorCode.PropertyNameExpected, offset: 26, length: 1, startLine: 0, startCharacter: 26 },
{ error: ParseErrorCode.ValueExpected, offset: 26, length: 1, startLine: 0, startCharacter: 26 }
]);
{ error: ParseErrorCode.ValueExpected, offset: 26, length: 1, startLine: 0, startCharacter: 26 },
], { allowTrailingComma: false });
});

test('visit: object', () => {
Expand Down