Skip to content

Commit 1a7ddbe

Browse files
authored
fix(shared): preserve JSONC properties after CR comments (#27069)
Co-authored-by: Simone <[email protected]>
1 parent 2274ff9 commit 1a7ddbe

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,35 @@ Done.`),
2828
).toBe('{"message":"literal } brace","nested":{"ok":true}}');
2929
});
3030

31-
it("decodes JSON with comments and trailing commas", () => {
32-
expect(
33-
decodeLenientJson(`{
31+
it.each(["\n", "\r\n", "\r"])(
32+
"decodes comments and trailing commas with %j line endings",
33+
(eol) => {
34+
expect(
35+
decodeLenientJson(
36+
`{
3437
// Comments are valid in settings files.
3538
"enabled": /* See "https://example.com/config" // still in the block */ true,
3639
"values": [1, 2,],
37-
}`),
38-
).toEqual({
39-
enabled: true,
40-
values: [1, 2],
41-
});
42-
});
40+
}`.replaceAll("\n", eol),
41+
),
42+
).toEqual({
43+
enabled: true,
44+
values: [1, 2],
45+
});
46+
},
47+
);
4348

4449
it("preserves comment markers and commas before brackets inside string values", () => {
4550
expect(decodeLenientJson('"1/* https://example.com */2"')).toBe("1/* https://example.com */2");
4651
expect(decodeLenientJson('{"note":"a,]"}')).toEqual({ note: "a,]" });
4752
expect(decodeLenientJson('{"list":["x,}"]}')).toEqual({ list: ["x,}"] });
4853
expect(decodeLenientJson('{"values":[1, 2,],}')).toEqual({ values: [1, 2] });
4954
});
55+
56+
it("keeps properties after CR-terminated comments in mixed line endings", () => {
57+
expect(decodeLenientJson('{\n"enabled":true,// comment\r"values":[1,2],\n}')).toEqual({
58+
enabled: true,
59+
values: [1, 2],
60+
});
61+
});
5062
});

‎packages/shared/src/schemaJson.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const decodeJsonString = Schema.decodeEffect(Schema.fromJsonString(Schema.Unknow
2323
const parseLenientJsonGetter = SchemaGetter.transformEffect((input: string) => {
2424
// Match comments together so URLs stay inside blocks; whitespace keeps adjacent tokens apart.
2525
let stripped = input.replace(
26-
/("(?:[^"\\]|\\.)*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g,
26+
/("(?:[^"\\]|\\.)*")|\/\/[^\r\n]*|\/\*[\s\S]*?\*\//g,
2727
(match, stringLiteral: string | undefined) => (stringLiteral ? match : " "),
2828
);
2929

0 commit comments

Comments
 (0)