Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/shared/src/schemaJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Done.`),
expect(
decodeLenientJson(`{
// Comments are valid in settings files.
"enabled": true,
"enabled": /* See "https://example.com/config" // still in the block */ true,
"values": [1, 2,],
}`),
).toEqual({
Expand All @@ -55,9 +55,11 @@ Done.`),

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

it("preserves commas before brackets inside string values", () => {
it("preserves comment markers and commas before brackets inside string values", () => {
expect(decodeLenientJson('"1/* https://example.com */2"')).toBe("1/* https://example.com */2");
expect(decodeLenientJson('{"note":"a,]"}')).toEqual({ note: "a,]" });
expect(decodeLenientJson('{"list":["x,}"]}')).toEqual({ list: ["x,}"] });
expect(decodeLenientJson('{"values":[1, 2,],}')).toEqual({ values: [1, 2] });
Expand Down
10 changes: 3 additions & 7 deletions packages/shared/src/schemaJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,10 @@ export const formatSchemaError = (cause: Cause.Cause<Schema.SchemaError>) => {
const decodeJsonString = Schema.decodeEffect(Schema.fromJsonString(Schema.Unknown));

const parseLenientJsonGetter = SchemaGetter.transformEffect((input: string) => {
// Match comments together so URLs stay inside blocks; whitespace keeps adjacent tokens apart.
let stripped = input.replace(
/("(?:[^"\\]|\\.)*")|\/\/[^\n]*/g,
(match, stringLiteral: string | undefined) => (stringLiteral ? match : ""),
);

stripped = stripped.replace(
/("(?:[^"\\]|\\.)*")|\/\*[\s\S]*?\*\//g,
(match, stringLiteral: string | undefined) => (stringLiteral ? match : ""),
/("(?:[^"\\]|\\.)*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g,
Comment thread
Lucenx9 marked this conversation as resolved.
(match, stringLiteral: string | undefined) => (stringLiteral ? match : " "),
);

stripped = stripped.replace(
Expand Down
11 changes: 7 additions & 4 deletions packages/shared/src/t3ProjectFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ describe("T3ProjectFileFromJson", () => {
});

describe("parseT3ProjectFile", () => {
it("returns the decoded file for valid contents", () => {
expect(parseT3ProjectFile('{ "defaultThreadEnvMode": "worktree" }')).toEqual({
defaultThreadEnvMode: "worktree",
});
it("reads project defaults from JSONC with a URL in a block comment", () => {
expect(
parseT3ProjectFile(`{
/* See https://t3.codes/schema/t3.json */
"defaultThreadEnvMode": "worktree"
}`),
).toEqual({ defaultThreadEnvMode: "worktree" });
});

it("returns null for malformed or invalid contents", () => {
Expand Down
Loading