Skip to content

Commit 3460497

Browse files
fix(shared): read JSONC block comments containing URLs (pingdotgg#19166)
Co-authored-by: Simone <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 1a552d1 commit 3460497

3 files changed

Lines changed: 14 additions & 13 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Done.`),
4444
expect(
4545
decodeLenientJson(`{
4646
// Comments are valid in settings files.
47-
"enabled": true,
47+
"enabled": /* See "https://example.com/config" // still in the block */ true,
4848
"values": [1, 2,],
4949
}`),
5050
).toEqual({
@@ -55,9 +55,11 @@ Done.`),
5555

5656
it("rejects malformed JSON after lenient preprocessing", () => {
5757
expect(() => decodeLenientJson('{ "enabled": true,, }')).toThrow();
58+
expect(() => decodeLenientJson("[1/* https://example.com */2]")).toThrow();
5859
});
5960

60-
it("preserves commas before brackets inside string values", () => {
61+
it("preserves comment markers and commas before brackets inside string values", () => {
62+
expect(decodeLenientJson('"1/* https://example.com */2"')).toBe("1/* https://example.com */2");
6163
expect(decodeLenientJson('{"note":"a,]"}')).toEqual({ note: "a,]" });
6264
expect(decodeLenientJson('{"list":["x,}"]}')).toEqual({ list: ["x,}"] });
6365
expect(decodeLenientJson('{"values":[1, 2,],}')).toEqual({ values: [1, 2] });

‎packages/shared/src/schemaJson.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,10 @@ export const formatSchemaError = (cause: Cause.Cause<Schema.SchemaError>) => {
158158
const decodeJsonString = Schema.decodeEffect(Schema.fromJsonString(Schema.Unknown));
159159

160160
const parseLenientJsonGetter = SchemaGetter.transformEffect((input: string) => {
161+
// Match comments together so URLs stay inside blocks; whitespace keeps adjacent tokens apart.
161162
let stripped = input.replace(
162-
/("(?:[^"\\]|\\.)*")|\/\/[^\n]*/g,
163-
(match, stringLiteral: string | undefined) => (stringLiteral ? match : ""),
164-
);
165-
166-
stripped = stripped.replace(
167-
/("(?:[^"\\]|\\.)*")|\/\*[\s\S]*?\*\//g,
168-
(match, stringLiteral: string | undefined) => (stringLiteral ? match : ""),
163+
/("(?:[^"\\]|\\.)*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g,
164+
(match, stringLiteral: string | undefined) => (stringLiteral ? match : " "),
169165
);
170166

171167
stripped = stripped.replace(

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ describe("T3ProjectFileFromJson", () => {
8282
});
8383

8484
describe("parseT3ProjectFile", () => {
85-
it("returns the decoded file for valid contents", () => {
86-
expect(parseT3ProjectFile('{ "defaultThreadEnvMode": "worktree" }')).toEqual({
87-
defaultThreadEnvMode: "worktree",
88-
});
85+
it("reads project defaults from JSONC with a URL in a block comment", () => {
86+
expect(
87+
parseT3ProjectFile(`{
88+
/* See https://t3.codes/schema/t3.json */
89+
"defaultThreadEnvMode": "worktree"
90+
}`),
91+
).toEqual({ defaultThreadEnvMode: "worktree" });
8992
});
9093

9194
it("returns null for malformed or invalid contents", () => {

0 commit comments

Comments
 (0)