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.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filejsonc.ts ×1 · 3 introduced LOCjsonc.ts ×1jsonc.ts ×1 · 2 introduced LOCjsonc.ts ×1jsonParse.test|title=JSON Parse Block comment - multi line|occurrence=1, jsonParse.test|title=JSON Parse Block comment - single line|occurrence=1 · 0 introduced LOCjsonParse.test|title=JSO…jsonc.ts ×1 · 3 introduced LOCjsonc.ts ×1jsonc.ts ×1 · 2 introduced LOCjsonc.ts ×1jsonParse.test|title=JSON Parse No strings - double quote|occurrence=1 · 0 introduced LOCjsonParse.test|title=JSO…jsonc.ts ×1 · 4 introduced LOCjsonc.ts ×1jsonc.ts ×2 · 6 introduced LOCjsonc.ts ×2jsonc.ts ×1 · 4 introduced LOCjsonc.ts ×1jsonc.ts ×1 · 2 introduced LOCjsonc.ts ×1jsonc.ts ×1 · 1 introduced LOCjsonc.ts ×1jsonc.ts ×3 · 37 introduced LOCjsonc.ts ×3jsonParse.test|title=JSON Parse Block comment - multi line|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse Block comment - multi line|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse Block comment - shortest match|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse Block comment - shortest match|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse Block comment - single line|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse Block comment - single line|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse Line comment - EOF - \r\n|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse Line comment - EOF - \r\n|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse Line comment - EOF|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse Line comment - EOF|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse Line comment - \r\n|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse Line comment - \r\n|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse Line comment|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse Line comment|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse No strings - double quote|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse No strings - double quote|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse No strings - single quote|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse No strings - single quote|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse Trailing comma - EOF|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse Trailing comma - EOF|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse Trailing comma in array|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse Trailing comma in array|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse Trailing comma in object|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse Trailing comma in object|occurrence=1jsonParse.test|title=JSO…jsonParse.test|title=JSON Parse Trailing comma|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/jsonParse.test|title=JSON Parse Trailing comma|occurrence=1jsonParse.test|title=JSO…Focused file · src/vs/base/common/jsonc.ts · 64 LOCcommon/jsonc.ts

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.

1 > /*--------------------------------------------------------------------------------------------- jsonc.ts ×3
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > // First group matches a double quoted string
7 > // Second group matches a single quoted string
8 > // Third group matches a multi line comment
9 > // Forth group matches a single line comment
10 > // Fifth group matches a trailing comma
11 > const regexp = /("[^"\\]*(?:\\.[^"\\]*)*")|('[^'\\]*(?:\\.[^'\\]*)*')|(\/\*[^\/\*]*(?:(?:\*|\/)[^\/\*]*)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))|(,\s*[}\]])/g;
12 >
13 > /**
14 > * Strips single and multi line JavaScript comments from JSON
15 > * content. Ignores characters in strings BUT doesn't support
16 > * string continuation across multiple lines since it is not
17 > * supported in JSON.
18 > *
19 > * @param content the content to strip comments from
20 > * @returns the content without comments
21 > */
22 > export function stripComments(content: string): string {
23 > return content.replace(regexp, function (match, _m1, _m2, m3, m4, m5) {
24 > // Only one of m1, m2, m3, m4, m5 matches
25 > if (m3) {
26 > // A block comment. Replace with nothing jsonc.ts ×1
27 > return '';
28 > } else if (m4) { jsonc.ts ×3
29 > // Since m4 is a single line comment is is at least of length 2 (e.g. //) jsonc.ts ×1
30 > // If it ends in \r?\n then keep it.
31 > const length = m4.length;
32 > if (m4[length - 1] === '\n') {
33 > return m4[length - 2] === '\r' ? '\r\n' : '\n'; jsonc.ts ×1
34 > }
35 > else { jsonc.ts ×1
36 > return '';
37 > }
38 > } else if (m5) { jsonc.ts ×1
39 > // Remove the trailing comma jsonc.ts ×1
40 > return match.substring(1);
41 > } else { jsonc.ts ×1
42 > // We match a string
43 > return match;
44 > }
45 > }); jsonc.ts ×3
46 > }
47 >
48 > /**
49 > * A drop-in replacement for JSON.parse that can parse
50 > * JSON with comments and trailing commas.
51 > *
52 > * @param content the content to strip comments from
53 > * @returns the parsed content as JSON
54 > */
55 > export function parse<T>(content: string): T {
56 > const commentsStripped = stripComments(content); jsonc.ts ×2
57 >
58 > try {
59 > return JSON.parse(commentsStripped);
60 > } catch (error) {
61 > const trailingCommasStriped = commentsStripped.replace(/,\s*([}\]])/g, '$1'); jsonc.ts ×1
62 > return JSON.parse(trailingCommasStriped);
63 > }
64 > } jsonc.ts ×2