Skip to content

Commit 3c20136

Browse files
committed
fix(shared): preserve token boundaries when stripping JSONC comments
1 parent f3e6acf commit 3c20136

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎packages/shared/src/schemaJson.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ Done.`),
5757
expect(() => decodeLenientJson('{ "enabled": true,, }')).toThrow();
5858
});
5959

60+
it.each([
61+
"[1/* https://example.com */2]",
62+
"[1/* https://example.com */.5]",
63+
"[tr/* https://example.com */ue]",
64+
])("rejects tokens separated by a block comment in %s", (input) => {
65+
expect(() => decodeLenientJson(input)).toThrow();
66+
});
67+
68+
it("preserves comment markers inside string values", () => {
69+
expect(decodeLenientJson('"1/* https://example.com */2"')).toBe("1/* https://example.com */2");
70+
});
71+
6072
it.each([
6173
'{ /* See https://example.com/config */ "enabled": true }',
6274
'{ "enabled": /* // this is still a block comment */ true }',

‎packages/shared/src/schemaJson.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ const parseLenientJsonGetter = SchemaGetter.transformEffect((input: string) => {
168168
// The string alternation preserves quoted values.
169169
let stripped = input.replace(
170170
/("(?:[^"\\]|\\.)*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g,
171-
(match, stringLiteral: string | undefined) => (stringLiteral ? match : ""),
171+
(match, stringLiteral: string | undefined) => (stringLiteral ? match : " "),
172172
);
173173

174174
// Strip trailing commas before `}` or `]`. The alternation preserves quoted

0 commit comments

Comments
 (0)