diff --git a/.changeset/schema-code-document-reserved-names.md b/.changeset/schema-code-document-reserved-names.md new file mode 100644 index 0000000000..8067d8e4cf --- /dev/null +++ b/.changeset/schema-code-document-reserved-names.md @@ -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`. diff --git a/packages/effect/src/Schema.ts b/packages/effect/src/Schema.ts index d9cfc4cad9..71357f8edc 100644 --- a/packages/effect/src/Schema.ts +++ b/packages/effect/src/Schema.ts @@ -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). diff --git a/packages/effect/src/internal/schema/toCodeDocument.ts b/packages/effect/src/internal/schema/toCodeDocument.ts index ab1662e8ef..a5c454342c 100644 --- a/packages/effect/src/internal/schema/toCodeDocument.ts +++ b/packages/effect/src/internal/schema/toCodeDocument.ts @@ -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 = [] const sorted = topologicalSort(document.references) const sanitizedReferences = new Map() - const uniqueIdentifiers = new Set() + const uniqueIdentifiers = new Set(["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}` ) }) diff --git a/packages/effect/test/schema/representation/fromJsonSchemaDocument.test.ts b/packages/effect/test/schema/representation/fromJsonSchemaDocument.test.ts index c051d07a72..7fbe92776f 100644 --- a/packages/effect/test/schema/representation/fromJsonSchemaDocument.test.ts +++ b/packages/effect/test/schema/representation/fromJsonSchemaDocument.test.ts @@ -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` ) }) @@ -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` ) }) diff --git a/packages/effect/test/schema/representation/fromJsonSchemaMultiDocument.test.ts b/packages/effect/test/schema/representation/fromJsonSchemaMultiDocument.test.ts index 74cdff7850..ed58022338 100644 --- a/packages/effect/test/schema/representation/fromJsonSchemaMultiDocument.test.ts +++ b/packages/effect/test/schema/representation/fromJsonSchemaMultiDocument.test.ts @@ -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` ) }] diff --git a/packages/effect/test/schema/representation/toCodeDocument.annotations.test.ts b/packages/effect/test/schema/representation/toCodeDocument.annotations.test.ts index eafb5acc7e..1ba6e683a8 100644 --- a/packages/effect/test/schema/representation/toCodeDocument.annotations.test.ts +++ b/packages/effect/test/schema/representation/toCodeDocument.annotations.test.ts @@ -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`) diff --git a/packages/effect/test/schema/representation/toCodeDocument.test.ts b/packages/effect/test/schema/representation/toCodeDocument.test.ts index c4236b6d17..dd7eb3e6ba 100644 --- a/packages/effect/test/schema/representation/toCodeDocument.test.ts +++ b/packages/effect/test/schema/representation/toCodeDocument.test.ts @@ -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" ) } diff --git a/packages/tools/openapi-generator/test/JsonSchemaGeneratorRepresentation.test.ts b/packages/tools/openapi-generator/test/JsonSchemaGeneratorRepresentation.test.ts index 80091d52cc..2011ac828b 100644 --- a/packages/tools/openapi-generator/test/JsonSchemaGeneratorRepresentation.test.ts +++ b/packages/tools/openapi-generator/test/JsonSchemaGeneratorRepresentation.test.ts @@ -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", () => {