Skip to content

Commit a4211b8

Browse files
fix(shared): preserve JSONC comment and token boundaries
1 parent 0b90eae commit a4211b8

3 files changed

Lines changed: 10 additions & 35 deletions

File tree

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

Lines changed: 3 additions & 21 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,29 +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.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", () => {
61+
it("preserves comment markers and commas before brackets inside string values", () => {
6962
expect(decodeLenientJson('"1/* https://example.com */2"')).toBe("1/* https://example.com */2");
70-
});
71-
72-
it.each([
73-
'{ /* See https://example.com/config */ "enabled": true }',
74-
'{ "enabled": /* // this is still a block comment */ true }',
75-
'{ "enabled": true /* a "quoted" note with // markers */ }',
76-
])("preserves block comment boundaries in %s", (input) => {
77-
expect(decodeLenientJson(input)).toEqual({ enabled: true });
78-
});
79-
80-
it("preserves commas before brackets inside string values", () => {
8163
expect(decodeLenientJson('{"note":"a,]"}')).toEqual({ note: "a,]" });
8264
expect(decodeLenientJson('{"list":["x,}"]}')).toEqual({ list: ["x,}"] });
8365
expect(decodeLenientJson('{"values":[1, 2,],}')).toEqual({ values: [1, 2] });

‎packages/shared/src/schemaJson.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ 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-
// Strip comments together so line-comment markers inside a block comment stay in that block.
162-
// The string alternation preserves quoted values.
161+
// Match comments together so URLs stay inside blocks; whitespace keeps adjacent tokens apart.
163162
let stripped = input.replace(
164163
/("(?:[^"\\]|\\.)*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g,
165164
(match, stringLiteral: string | undefined) => (stringLiteral ? match : " "),

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,17 @@ 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-
});
89-
});
90-
91-
it("returns null for malformed or invalid contents", () => {
92-
expect(parseT3ProjectFile("{ not json")).toBeNull();
93-
expect(parseT3ProjectFile('{ "defaultThreadEnvMode": "spaceship" }')).toBeNull();
94-
});
95-
96-
it("reads project defaults after a block comment containing a URL", () => {
85+
it("reads project defaults from JSONC with a URL in a block comment", () => {
9786
expect(
9887
parseT3ProjectFile(`{
9988
/* See https://t3.codes/schema/t3.json */
10089
"defaultThreadEnvMode": "worktree"
10190
}`),
10291
).toEqual({ defaultThreadEnvMode: "worktree" });
10392
});
93+
94+
it("returns null for malformed or invalid contents", () => {
95+
expect(parseT3ProjectFile("{ not json")).toBeNull();
96+
expect(parseT3ProjectFile('{ "defaultThreadEnvMode": "spaceship" }')).toBeNull();
97+
});
10498
});

0 commit comments

Comments
 (0)