Skip to content
TypeScript

fix(shared): preserve JSONC properties after CR comments (#27069)

Simone1a7ddbef644b
diff --git a/packages/shared/src/schemaJson.test.ts b/packages/shared/src/schemaJson.test.ts
index 8c5036f7855..a710ce52f28 100644
--- a/packages/shared/src/schemaJson.test.ts
+++ b/packages/shared/src/schemaJson.test.ts
@@ -28,18 +28,23 @@ Done.`),
).toBe('{"message":"literal } brace","nested":{"ok":true}}');
});
- it("decodes JSON with comments and trailing commas", () => {
- expect(
- decodeLenientJson(`{
+ it.each(["\n", "\r\n", "\r"])(
+ "decodes comments and trailing commas with %j line endings",
+ (eol) => {
+ expect(
+ decodeLenientJson(
+ `{
// Comments are valid in settings files.
"enabled": /* See "https://example.com/config" // still in the block */ true,
"values": [1, 2,],
- }`),
- ).toEqual({
- enabled: true,
- values: [1, 2],
- });
- });
+ }`.replaceAll("\n", eol),
+ ),
+ ).toEqual({
+ enabled: true,
+ values: [1, 2],
+ });
+ },
+ );
it("preserves comment markers and commas before brackets inside string values", () => {
expect(decodeLenientJson('"1/* https://example.com */2"')).toBe("1/* https://example.com */2");
@@ -47,4 +52,11 @@ Done.`),
expect(decodeLenientJson('{"list":["x,}"]}')).toEqual({ list: ["x,}"] });
expect(decodeLenientJson('{"values":[1, 2,],}')).toEqual({ values: [1, 2] });
});
+
+ it("keeps properties after CR-terminated comments in mixed line endings", () => {
+ expect(decodeLenientJson('{\n"enabled":true,// comment\r"values":[1,2],\n}')).toEqual({
+ enabled: true,
+ values: [1, 2],
+ });
+ });
});
diff --git a/packages/shared/src/schemaJson.ts b/packages/shared/src/schemaJson.ts
index 8a6051422cd..df1a4d551bc 100644
--- a/packages/shared/src/schemaJson.ts
+++ b/packages/shared/src/schemaJson.ts
@@ -23,7 +23,7 @@ const decodeJsonString = Schema.decodeEffect(Schema.fromJsonString(Schema.Unknow
const parseLenientJsonGetter = SchemaGetter.transformEffect((input: string) => {
// Match comments together so URLs stay inside blocks; whitespace keeps adjacent tokens apart.
let stripped = input.replace(
- /("(?:[^"\\]|\\.)*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g,
+ /("(?:[^"\\]|\\.)*")|\/\/[^\r\n]*|\/\*[\s\S]*?\*\//g,
(match, stringLiteral: string | undefined) => (stringLiteral ? match : " "),
);