From 6ba3f80ff402291d87e358624044d401a52e3547 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Mon, 22 Oct 2018 16:08:47 +0200 Subject: [PATCH] Trailing comma in settings.json breaks settings editor. fixes #61341 --- src/vs/base/common/json.ts | 12 +++++++++--- src/vs/base/test/common/json.test.ts | 6 +++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/vs/base/common/json.ts b/src/vs/base/common/json.ts index 6efede87596..bed9da5ed52 100644 --- a/src/vs/base/common/json.ts +++ b/src/vs/base/common/json.ts @@ -144,6 +144,12 @@ export interface ParseOptions { allowTrailingComma?: boolean; } +export namespace ParseOptions { + export const DEFAULT = { + allowTrailingComma: true + }; +} + export interface JSONVisitor { /** * Invoked when an open brace is encountered and an object is started. The offset and length represent the location of the open brace. @@ -839,7 +845,7 @@ export function getLocation(text: string, position: number): Location { * Parses the given text and returns the object the JSON content represents. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result. * Therefore always check the errors list to find out if the input was valid. */ -export function parse(text: string, errors: ParseError[] = [], options?: ParseOptions): any { +export function parse(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT): any { let currentProperty: string | null = null; let currentParent: any = []; let previousParents: any[] = []; @@ -889,7 +895,7 @@ export function parse(text: string, errors: ParseError[] = [], options?: ParseOp /** * Parses the given text and returns a tree representation the JSON content. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result. */ -export function parseTree(text: string, errors: ParseError[] = [], options?: ParseOptions): Node { +export function parseTree(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT): Node { let currentParent: NodeImpl = { type: 'array', offset: -1, length: -1, children: [], parent: void 0 }; // artificial root function ensurePropertyComplete(endOffset: number) { @@ -1061,7 +1067,7 @@ export function findNodeAtOffset(node: Node, offset: number, includeRightBound = /** * Parses the given text and invokes the visitor functions for each object, array and literal reached. */ -export function visit(text: string, visitor: JSONVisitor, options?: ParseOptions): any { +export function visit(text: string, visitor: JSONVisitor, options: ParseOptions = ParseOptions.DEFAULT): any { let _scanner = createScanner(text, false); diff --git a/src/vs/base/test/common/json.test.ts b/src/vs/base/test/common/json.test.ts index 07f67d08448..86988f99f92 100644 --- a/src/vs/base/test/common/json.test.ts +++ b/src/vs/base/test/common/json.test.ts @@ -23,7 +23,7 @@ function assertScanError(text: string, expectedKind: SyntaxKind, scanError: Scan assert.equal(scanner.getTokenError(), scanError); } -function assertValidParse(input: string, expected: any, options: ParseOptions = { allowTrailingComma: true }): void { +function assertValidParse(input: string, expected: any, options?: ParseOptions): void { var errors: ParseError[] = []; var actual = parse(input, errors, options); @@ -33,7 +33,7 @@ function assertValidParse(input: string, expected: any, options: ParseOptions = assert.deepEqual(actual, expected); } -function assertInvalidParse(input: string, expected: any, options: ParseOptions = { allowTrailingComma: true }): void { +function assertInvalidParse(input: string, expected: any, options?: ParseOptions): void { var errors: ParseError[] = []; var actual = parse(input, errors, options); @@ -41,7 +41,7 @@ function assertInvalidParse(input: string, expected: any, options: ParseOptions assert.deepEqual(actual, expected); } -function assertTree(input: string, expected: any, expectedErrors: number[] = [], options: ParseOptions = { allowTrailingComma: true }): void { +function assertTree(input: string, expected: any, expectedErrors: number[] = [], options?: ParseOptions): void { var errors: ParseError[] = []; var actual = parseTree(input, errors, options); -- GitLab