This commit is contained in:
Sandeep Somavarapu
2017-02-02 16:54:46 +01:00
parent 5d7b9e89fe
commit c7c00d5afe
3 changed files with 17 additions and 1 deletions
+5
View File
@@ -817,6 +817,7 @@ export function getLocation(text: string, position: number): Location {
export interface ParseOptions {
disallowComments?: boolean;
allowTrailingComma?: boolean;
}
/**
@@ -1005,6 +1006,7 @@ export function visit(text: string, visitor: JSONVisitor, options?: ParseOptions
onError = toOneArgVisit(visitor.onError);
let disallowComments = options && options.disallowComments;
let allowTrailingComma = options && options.allowTrailingComma;
function scanNext(): SyntaxKind {
while (true) {
let token = _scanner.scan();
@@ -1116,6 +1118,9 @@ export function visit(text: string, visitor: JSONVisitor, options?: ParseOptions
}
onSeparator(',');
scanNext(); // consume comma
if (_scanner.getToken() === SyntaxKind.CloseBraceToken && allowTrailingComma) {
break;
}
} else if (needsComma) {
handleError(ParseErrorCode.CommaExpected, [], []);
}
+11
View File
@@ -234,6 +234,17 @@ suite('JSON', () => {
assertInvalidParse('{ "foo": /*comment*/ true }', { foo: true }, options);
});
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);
assertInvalidParse('{ "hello": [], }', { hello: [] });
assertInvalidParse('{ "hello": [], "world": {}, }', { hello: [], world: {} });
});
test('location: properties', () => {
assertLocation('|{ "foo": "bar" }', [], void 0, false);
assertLocation('{| "foo": "bar" }', [], void 0, true);
@@ -233,7 +233,7 @@ export class ConfigurationEditingService implements IConfigurationEditingService
let error = void 0;
const parseErrors: json.ParseError[] = [];
json.parse(content, parseErrors);
json.parse(content, parseErrors, { allowTrailingComma: true });
if (!options.writeToBuffer && parseErrors.length > 0) {
error = ConfigurationEditingErrorCode.ERROR_INVALID_CONFIGURATION;
}