mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-21 09:08:53 +01:00
[json] allow trailing comma by default
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
Reference in New Issue
Block a user