[json] allow trailing comma by default

This commit is contained in:
Martin Aeschlimann
2018-06-26 12:35:35 +02:00
parent 45eb2dcc4a
commit 91b3e3e8d5
5 changed files with 14 additions and 10 deletions

View File

@@ -629,7 +629,7 @@ export type JSONPath = Segment[];
export interface ParseOptions {
disallowComments?: boolean;
allowTrailingComma?: boolean;
disallowTrailingComma?: boolean;
}
/**
@@ -818,7 +818,7 @@ export function visit(text: string, visitor: JSONVisitor, options?: ParseOptions
onError = toOneArgVisit(visitor.onError);
let disallowComments = options && options.disallowComments;
let allowTrailingComma = options && options.allowTrailingComma;
let disallowTrailingComma = options && options.disallowTrailingComma;
function scanNext(): SyntaxKind {
while (true) {
let token = _scanner.scan();
@@ -930,7 +930,7 @@ export function visit(text: string, visitor: JSONVisitor, options?: ParseOptions
}
onSeparator(',');
scanNext(); // consume comma
if (_scanner.getToken() === SyntaxKind.CloseBraceToken && allowTrailingComma) {
if (_scanner.getToken() === SyntaxKind.CloseBraceToken && !disallowTrailingComma) {
break;
}
} else if (needsComma) {
@@ -962,7 +962,7 @@ export function visit(text: string, visitor: JSONVisitor, options?: ParseOptions
}
onSeparator(',');
scanNext(); // consume comma
if (_scanner.getToken() === SyntaxKind.CloseBracketToken && allowTrailingComma) {
if (_scanner.getToken() === SyntaxKind.CloseBracketToken && !disallowTrailingComma) {
break;
}
} else if (needsComma) {

View File

@@ -229,15 +229,19 @@ suite('JSON', () => {
});
test('parse: trailing comma', () => {
let options = { allowTrailingComma: true };
// default is allow
assertValidParse('{ "hello": [], }', { hello: [] });
let options = { disallowTrailingComma: false };
assertValidParse('{ "hello": [], }', { hello: [] }, options);
assertValidParse('{ "hello": [] }', { hello: [] }, options);
assertValidParse('{ "hello": [], "world": {}, }', { hello: [], world: {} }, options);
assertValidParse('{ "hello": [], "world": {} }', { hello: [], world: {} }, options);
assertValidParse('{ "hello": [1,] }', { hello: [1] }, options);
assertInvalidParse('{ "hello": [], }', { hello: [] });
assertInvalidParse('{ "hello": [], "world": {}, }', { hello: [], world: {} });
options = { disallowTrailingComma: true };
assertInvalidParse('{ "hello": [], }', { hello: [] }, options);
assertInvalidParse('{ "hello": [], "world": {}, }', { hello: [], world: {} }, options);
});
test('tree: literals', () => {