Skip to content
TypeScript

fix(shared): read JSONC block comments containing URLs (#19166)

Simone346049725edd
diff --git a/packages/shared/src/schemaJson.test.ts b/packages/shared/src/schemaJson.test.ts
index a653ad712f2..ac30a5a273a 100644
--- a/packages/shared/src/schemaJson.test.ts
+++ b/packages/shared/src/schemaJson.test.ts
@@ -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({
@@ -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] });
diff --git a/packages/shared/src/schemaJson.ts b/packages/shared/src/schemaJson.ts
index a65ad62573e..5a8ef72cba6 100644
--- a/packages/shared/src/schemaJson.ts
+++ b/packages/shared/src/schemaJson.ts
@@ -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,
+ (match, stringLiteral: string | undefined) => (stringLiteral ? match : " "),
);
stripped = stripped.replace(
diff --git a/packages/shared/src/t3ProjectFile.test.ts b/packages/shared/src/t3ProjectFile.test.ts
index 782262fc6e2..5f2ecf24f1b 100644
--- a/packages/shared/src/t3ProjectFile.test.ts
+++ b/packages/shared/src/t3ProjectFile.test.ts
@@ -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", () => {