src/vs/base/common/jsonc.ts
64 LOC · 64 covered · 0 uncovered · 13 ranges · 12 concepts · 10 introducers · 13 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
jsonc.ts ×3
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// First group matches a double quoted string
// Second group matches a single quoted string
// Third group matches a multi line comment
// Forth group matches a single line comment
// Fifth group matches a trailing comma
const regexp = /("[^"\\]*(?:\\.[^"\\]*)*")|('[^'\\]*(?:\\.[^'\\]*)*')|(\/\*[^\/\*]*(?:(?:\*|\/)[^\/\*]*)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))|(,\s*[}\]])/g;
/**
* Strips single and multi line JavaScript comments from JSON
* content. Ignores characters in strings BUT doesn't support
* string continuation across multiple lines since it is not
* supported in JSON.
*
* @param content the content to strip comments from
* @returns the content without comments
*/
export function stripComments(content: string): string {
return content.replace(regexp, function (match, _m1, _m2, m3, m4, m5) {
// Only one of m1, m2, m3, m4, m5 matches
if (m3) {
return '';
// If it ends in \r?\n then keep it.
const length = m4.length;
if (m4[length - 1] === '\n') {
}
return '';
}
return match.substring(1);
// We match a string
return match;
}
}
/**
* A drop-in replacement for JSON.parse that can parse
* JSON with comments and trailing commas.
*
* @param content the content to strip comments from
* @returns the parsed content as JSON
*/
export function parse<T>(content: string): T {
try {
return JSON.parse(commentsStripped);
} catch (error) {
return JSON.parse(trailingCommasStriped);
}