MCPcopy Create free account
hub / github.com/cortexkit/magic-context / stripJsonComments

Function stripJsonComments

packages/plugin/src/shared/jsonc-parser.ts:3–64  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

1import { existsSync, readFileSync } from "node:fs";
2
3export function stripJsonComments(content: string): string {
4 let result = "";
5 let inString = false;
6 let escaped = false;
7 let inLineComment = false;
8 let inBlockComment = false;
9
10 for (let index = 0; index < content.length; index += 1) {
11 const char = content[index];
12 const next = content[index + 1];
13
14 if (inLineComment) {
15 if (char === "\n") {
16 inLineComment = false;
17 result += char;
18 }
19 continue;
20 }
21
22 if (inBlockComment) {
23 if (char === "*" && next === "/") {
24 inBlockComment = false;
25 index += 1;
26 }
27 continue;
28 }
29
30 if (inString) {
31 result += char;
32 if (escaped) {
33 escaped = false;
34 } else if (char === "\\") {
35 escaped = true;
36 } else if (char === '"') {
37 inString = false;
38 }
39 continue;
40 }
41
42 if (char === '"') {
43 inString = true;
44 result += char;
45 continue;
46 }
47
48 if (char === "/" && next === "/") {
49 inLineComment = true;
50 index += 1;
51 continue;
52 }
53
54 if (char === "/" && next === "*") {
55 inBlockComment = true;
56 index += 1;
57 continue;
58 }
59
60 result += char;

Callers 2

parseJsoncFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected