Fix toCodeDocument output breaking on Schema, RegExp, Date or Symbol references (#9771)

Co-authored-by: Effect Bot <tech-ops@effectful.co>
This commit is contained in:
Ibrahim Elkamali
2026-10-23 13:19:26 +02:00
committed by GitHub
co-authored by Effect Bot
parent e62a149ee1
commit c97b331ab7
8 changed files with 52 additions and 29 deletions
@@ -0,0 +1,5 @@
---
"effect": patch
---
Fix `SchemaRepresentation.toCodeDocument` generating code that fails to load when a schema identifier is `Schema`, `RegExp`, `Date` or `Symbol`.
+3 -3
View File
@@ -6904,8 +6904,8 @@ export function isPattern(
const flags = regExp.flags
const canExport = /^[dg]*uy?$/.test(flags)
const runtimeRegExp = flags === ""
? `new RegExp(${format(source)})`
: `new RegExp(${format(source)}, ${format(flags)})`
? `new globalThis.RegExp(${format(source)})`
: `new globalThis.RegExp(${format(source)}, ${format(flags)})`
return SchemaAST.isPattern(regExp, {
toJsonSchema: () => canExport ? { pattern: flags.endsWith("y") ? `^(?:${source})` : source } : [{}, true],
toCode: () => ({ runtime: `Schema.isPattern(${runtimeRegExp})` }),
@@ -8068,7 +8068,7 @@ function encodeDatePayload(date: globalThis.Date): string {
}
function formatDateRuntime(date: globalThis.Date): string {
return `new Date(${format(date.getTime())})`
return `new globalThis.Date(${format(date.getTime())})`
}
/**
* Validates that a Date is greater than the specified value (exclusive).
@@ -26,7 +26,7 @@ function renderEmittableAnnotation(input: unknown): string | undefined {
) return format(input)
if (typeof input === "symbol") {
const key = globalThis.Symbol.keyFor(input)
return key === undefined ? undefined : `Symbol.for(${format(key)})`
return key === undefined ? undefined : `globalThis.Symbol.for(${format(key)})`
}
if (typeof input !== "object") return undefined
if (Array.isArray(input)) {
@@ -230,7 +230,7 @@ export function toCodeDocument(
const artifacts: Array<SchemaRepresentation.Artifact> = []
const sorted = topologicalSort(document.references)
const sanitizedReferences = new Map<string, string>()
const uniqueIdentifiers = new Set<string>()
const uniqueIdentifiers = new Set<string>(["Schema"])
let compilingRecursiveDefinition = false
let explicitSuspendDepth = 0
@@ -289,8 +289,8 @@ export function toCodeDocument(
identifier,
code: makeCode(
key === undefined
? `Symbol(${description === undefined ? "" : format(description)})`
: `Symbol.for(${format(key)})`,
? `globalThis.Symbol(${description === undefined ? "" : format(description)})`
: `globalThis.Symbol.for(${format(key)})`,
`typeof ${identifier}`
)
})
@@ -382,7 +382,7 @@ describe("fromJsonSchemaDocument", () => {
{ schema: { type: "string", pattern: "a*" } },
{
codes: makeCode(
`Schema.String.check(Schema.isPattern(new RegExp("a*", "u")).annotate({ "expected": "a string matching the RegExp a*" }))`,
`Schema.String.check(Schema.isPattern(new globalThis.RegExp("a*", "u")).annotate({ "expected": "a string matching the RegExp a*" }))`,
`string`
)
}
@@ -392,7 +392,7 @@ describe("fromJsonSchemaDocument", () => {
it("pattern only constrains strings", () => {
assertFromJsonSchema({ schema: { pattern: "^a+$" } }, {
codes: makeCode(
`Schema.Union([Schema.Null, Schema.String.check(Schema.isPattern(new RegExp("^a+$", "u")).annotate({ "expected": "a string matching the RegExp ^a+$" })), Schema.Finite, Schema.Boolean, Schema.Record(Schema.String, Schema.Json.annotate({ "expected": "JSON value" })), Schema.Array(Schema.Json.annotate({ "expected": "JSON value" }))])`,
`Schema.Union([Schema.Null, Schema.String.check(Schema.isPattern(new globalThis.RegExp("^a+$", "u")).annotate({ "expected": "a string matching the RegExp ^a+$" })), Schema.Finite, Schema.Boolean, Schema.Record(Schema.String, Schema.Json.annotate({ "expected": "JSON value" })), Schema.Array(Schema.Json.annotate({ "expected": "JSON value" }))])`,
`null | string | number | boolean | { readonly [x: string]: Schema.Json } | ReadonlyArray<Schema.Json>`
)
})
@@ -962,7 +962,7 @@ describe("fromJsonSchemaDocument", () => {
}, {
$ref: `Values`,
code: makeCode(
`Schema.Record(Schema.String.check(Schema.isPattern(new RegExp("a", "u")).annotate({ "expected": "a string matching the RegExp a" })), Value).annotate({ "identifier": "Values" })`,
`Schema.Record(Schema.String.check(Schema.isPattern(new globalThis.RegExp("a", "u")).annotate({ "expected": "a string matching the RegExp a" })), Value).annotate({ "identifier": "Values" })`,
`{ readonly [x: string]: Value }`
)
}]
@@ -979,7 +979,7 @@ describe("fromJsonSchemaDocument", () => {
}
}, {
codes: makeCode(
`Schema.Record(Schema.String.check(Schema.isPattern(new RegExp("^a", "u")).annotate({ "expected": "a string matching the RegExp ^a" })), Schema.Never)`,
`Schema.Record(Schema.String.check(Schema.isPattern(new globalThis.RegExp("^a", "u")).annotate({ "expected": "a string matching the RegExp ^a" })), Schema.Never)`,
`{ readonly [x: string]: never }`
)
})
@@ -1059,7 +1059,7 @@ describe("fromJsonSchemaDocument", () => {
},
{
codes: makeCode(
`Schema.Record(Schema.String, Schema.Json.annotate({ "expected": "JSON value" })).check(Schema.isPropertyNames(Schema.String.check(Schema.isPattern(new RegExp("^[A-Z]", "u")).annotate({ "expected": "a string matching the RegExp ^[A-Z]" }))).annotate({ "expected": "an object with property names matching the schema" }))`,
`Schema.Record(Schema.String, Schema.Json.annotate({ "expected": "JSON value" })).check(Schema.isPropertyNames(Schema.String.check(Schema.isPattern(new globalThis.RegExp("^[A-Z]", "u")).annotate({ "expected": "a string matching the RegExp ^[A-Z]" }))).annotate({ "expected": "an object with property names matching the schema" }))`,
`{ readonly [x: string]: Schema.Json }`
)
}
@@ -1095,7 +1095,7 @@ describe("fromJsonSchemaDocument", () => {
},
{
codes: makeCode(
`Schema.Record(Schema.String, Schema.Json.annotate({ "expected": "JSON value" })).check(Schema.isPropertyNames(Schema.String.check(Schema.isPattern(new RegExp("^[A-Z]", "u")).annotate({ "expected": "a string matching the RegExp ^[A-Z]" }))).annotate({ "expected": "an object with property names matching the schema" })).check(Schema.isPropertyNames(Schema.String.check(Schema.isMinCodePoints(2).annotate({ "expected": "a string with at least 2 code points" }))).annotate({ "expected": "an object with property names matching the schema" }))`,
`Schema.Record(Schema.String, Schema.Json.annotate({ "expected": "JSON value" })).check(Schema.isPropertyNames(Schema.String.check(Schema.isPattern(new globalThis.RegExp("^[A-Z]", "u")).annotate({ "expected": "a string matching the RegExp ^[A-Z]" }))).annotate({ "expected": "an object with property names matching the schema" })).check(Schema.isPropertyNames(Schema.String.check(Schema.isMinCodePoints(2).annotate({ "expected": "a string with at least 2 code points" }))).annotate({ "expected": "an object with property names matching the schema" }))`,
`{ readonly [x: string]: Schema.Json }`
)
}
@@ -3157,7 +3157,7 @@ describe("fromJsonSchemaDocument", () => {
it("applies patterns explicitly", () => {
assertFromJsonSchema({ schema: { type: "string", pattern: "^a+$" }, options: { patterns: "apply" } }, {
codes: makeCode(
`Schema.String.check(Schema.isPattern(new RegExp("^a+$", "u")).annotate({ "expected": "a string matching the RegExp ^a+$" }))`,
`Schema.String.check(Schema.isPattern(new globalThis.RegExp("^a+$", "u")).annotate({ "expected": "a string matching the RegExp ^a+$" }))`,
`string`
)
})
@@ -158,7 +158,7 @@ describe("SchemaRepresentation.fromJsonSchemaMultiDocument", () => {
nonRecursives: [{
$ref: "A",
code: makeCode(
`Schema.String.check(Schema.isPattern(new RegExp("^a+$", "u")).annotate({ "expected": "a string matching the RegExp ^a+$", "identifier": "A" }))`,
`Schema.String.check(Schema.isPattern(new globalThis.RegExp("^a+$", "u")).annotate({ "expected": "a string matching the RegExp ^a+$", "identifier": "A" }))`,
`string`
)
}]
@@ -51,7 +51,7 @@ describe("SchemaRepresentation.toCodeDocument annotations", () => {
assert.deepStrictEqual(output.codes, [
{
runtime:
`Schema.String.check(Schema.isPattern(new RegExp("^a+$")).annotate({ "expected": "a string matching the RegExp ^a+$" }))`,
`Schema.String.check(Schema.isPattern(new globalThis.RegExp("^a+$")).annotate({ "expected": "a string matching the RegExp ^a+$" }))`,
Type: "string"
},
{
@@ -113,7 +113,7 @@ describe("SchemaRepresentation.toCodeDocument annotations", () => {
const runtime = SchemaRepresentation.toCodeDocument(document).codes[0].runtime
assertInclude(runtime, `"bigint": 1n`)
assertInclude(runtime, `"symbol": Symbol.for("shared")`)
assertInclude(runtime, `"symbol": globalThis.Symbol.for("shared")`)
assertInclude(runtime, `"nan": NaN`)
assertInclude(runtime, `"positive": Infinity`)
assertInclude(runtime, `"negative": -Infinity`)
@@ -172,7 +172,7 @@ describe("toCodeDocument", () => {
assertSchema(
{ schema: Schema.Date.check(Schema.isGreaterThanDate(new Date(0))) },
{
codes: makeCode(`Schema.Date.check(Schema.isGreaterThanDate(new Date(0)))`, "globalThis.Date")
codes: makeCode(`Schema.Date.check(Schema.isGreaterThanDate(new globalThis.Date(0)))`, "globalThis.Date")
}
)
})
@@ -346,6 +346,18 @@ describe("toCodeDocument", () => {
})
})
it("renames an identifier that collides with the Schema import", () => {
assertSchema({ schema: Schema.String.annotate({ identifier: "Schema" }) }, {
codes: makeCode("Schema1", "Schema1"),
references: {
nonRecursives: [{
$ref: "Schema1",
code: makeCode(`Schema.String.annotate({ "identifier": "Schema" })`, "string")
}]
}
})
})
it("String & annotations", () => {
assertSchema(
{ schema: Schema.String.annotate({ "description": "a" }) },
@@ -627,7 +639,7 @@ describe("toCodeDocument", () => {
artifacts: [{
_tag: "Symbol",
identifier: "_symbol",
code: makeCode(`Symbol("a")`, `typeof _symbol`)
code: makeCode(`globalThis.Symbol("a")`, `typeof _symbol`)
}]
}
)
@@ -638,7 +650,7 @@ describe("toCodeDocument", () => {
artifacts: [{
_tag: "Symbol",
identifier: "_symbol",
code: makeCode(`Symbol()`, `typeof _symbol`)
code: makeCode(`globalThis.Symbol()`, `typeof _symbol`)
}]
}
)
@@ -652,7 +664,7 @@ describe("toCodeDocument", () => {
artifacts: [{
_tag: "Symbol",
identifier: "_symbol",
code: makeCode(`Symbol.for("a")`, `typeof _symbol`)
code: makeCode(`globalThis.Symbol.for("a")`, `typeof _symbol`)
}]
}
)
@@ -666,7 +678,7 @@ describe("toCodeDocument", () => {
artifacts: [{
_tag: "Symbol",
identifier: "_symbol",
code: makeCode(`Symbol.for("a")`, `typeof _symbol`)
code: makeCode(`globalThis.Symbol.for("a")`, `typeof _symbol`)
}]
}
)
@@ -1422,7 +1434,7 @@ describe("toCodeDocument", () => {
artifacts: [{
_tag: "Symbol",
identifier: "_symbol",
code: makeCode(`Symbol.for("a")`, `typeof _symbol`)
code: makeCode(`globalThis.Symbol.for("a")`, `typeof _symbol`)
}]
}
)
@@ -1784,7 +1796,7 @@ describe("toCodeDocument", () => {
assertSchema(
{ schema: Schema.Date.check(Schema.isGreaterThanDate(new Date(0))) },
{
codes: makeCode(`Schema.Date.check(Schema.isGreaterThanDate(new Date(0)))`, "globalThis.Date")
codes: makeCode(`Schema.Date.check(Schema.isGreaterThanDate(new globalThis.Date(0)))`, "globalThis.Date")
}
)
})
@@ -1793,7 +1805,10 @@ describe("toCodeDocument", () => {
assertSchema(
{ schema: Schema.Date.check(Schema.isGreaterThanOrEqualToDate(new Date(0))) },
{
codes: makeCode(`Schema.Date.check(Schema.isGreaterThanOrEqualToDate(new Date(0)))`, "globalThis.Date")
codes: makeCode(
`Schema.Date.check(Schema.isGreaterThanOrEqualToDate(new globalThis.Date(0)))`,
"globalThis.Date"
)
}
)
})
@@ -1802,7 +1817,7 @@ describe("toCodeDocument", () => {
assertSchema(
{ schema: Schema.Date.check(Schema.isLessThanDate(new Date(0))) },
{
codes: makeCode(`Schema.Date.check(Schema.isLessThanDate(new Date(0)))`, "globalThis.Date")
codes: makeCode(`Schema.Date.check(Schema.isLessThanDate(new globalThis.Date(0)))`, "globalThis.Date")
}
)
})
@@ -1811,7 +1826,10 @@ describe("toCodeDocument", () => {
assertSchema(
{ schema: Schema.Date.check(Schema.isLessThanOrEqualToDate(new Date(0))) },
{
codes: makeCode(`Schema.Date.check(Schema.isLessThanOrEqualToDate(new Date(0)))`, "globalThis.Date")
codes: makeCode(
`Schema.Date.check(Schema.isLessThanOrEqualToDate(new globalThis.Date(0)))`,
"globalThis.Date"
)
}
)
})
@@ -1821,7 +1839,7 @@ describe("toCodeDocument", () => {
{ schema: Schema.Date.check(Schema.isBetweenDate({ minimum: new Date(0), maximum: new Date(1) })) },
{
codes: makeCode(
`Schema.Date.check(Schema.isBetweenDate({ minimum: new Date(0), maximum: new Date(1), exclusiveMinimum: undefined, exclusiveMaximum: undefined }))`,
`Schema.Date.check(Schema.isBetweenDate({ minimum: new globalThis.Date(0), maximum: new globalThis.Date(1), exclusiveMinimum: undefined, exclusiveMaximum: undefined }))`,
"globalThis.Date"
)
}
@@ -8,7 +8,7 @@ describe("JsonSchemaGenerator representation", () => {
const output = generator.generate("openapi-3.1", {}, false)
assert.include(output, `Schema.isPattern(new RegExp("^a+$", "u"))`)
assert.include(output, `Schema.isPattern(new globalThis.RegExp("^a+$", "u"))`)
})
it("emits only reachable definitions", () => {