Skip to content

Commit

Permalink
Schema: remove non-tree-shakable compiler dependencies from the Schem…
Browse files Browse the repository at this point in the history
…a module (#2586)
  • Loading branch information
gcanti committed Apr 21, 2024
1 parent e4ba97d commit 9dfc156
Show file tree
Hide file tree
Showing 191 changed files with 319 additions and 244 deletions.
9 changes: 9 additions & 0 deletions .changeset/dry-rocks-sleep.md
@@ -0,0 +1,9 @@
---
"@effect/schema": patch
---

remove non-tree-shakable compiler dependencies from the Schema module:

- remove dependency from `Arbitrary` compiler
- remove dependency from `Equivalence` compiler
- remove dependency from `Pretty` compiler
7 changes: 2 additions & 5 deletions packages/schema/src/Pretty.ts
Expand Up @@ -156,7 +156,7 @@ export const match: AST.Match<Pretty<any>> = {
continue
}
output.push(
`${getPrettyPropertyKey(name)}: ${propertySignaturesTypes[i](input[name])}`
`${util_.formatPropertyKey(name)}: ${propertySignaturesTypes[i](input[name])}`
)
}
// ---------------------------------------------
Expand All @@ -170,7 +170,7 @@ export const match: AST.Match<Pretty<any>> = {
if (Object.prototype.hasOwnProperty.call(expectedKeys, key)) {
continue
}
output.push(`${getPrettyPropertyKey(key)}: ${type(input[key])}`)
output.push(`${util_.formatPropertyKey(key)}: ${type(input[key])}`)
}
}
}
Expand Down Expand Up @@ -213,6 +213,3 @@ export const match: AST.Match<Pretty<any>> = {
}

const compile = AST.getCompiler(match)

const getPrettyPropertyKey = (name: PropertyKey): string =>
typeof name === "string" ? JSON.stringify(name) : String(name)
126 changes: 74 additions & 52 deletions packages/schema/src/Schema.ts
Expand Up @@ -39,6 +39,7 @@ import * as arbitrary_ from "./Arbitrary.js"
import type { ParseOptions } from "./AST.js"
import * as AST from "./AST.js"
import * as equivalence_ from "./Equivalence.js"
import type * as fastCheck_ from "./FastCheck.js"
import * as errors_ from "./internal/errors.js"
import * as filters_ from "./internal/filters.js"
import * as serializable_ from "./internal/serializable.js"
Expand Down Expand Up @@ -5049,10 +5050,11 @@ const optionEncoded = <A, I, R>(value: Schema<A, I, R>) =>
const optionDecode = <A>(input: OptionEncoded<A>): option_.Option<A> =>
input._tag === "None" ? option_.none() : option_.some(input.value)

const optionArbitrary = <A>(value: LazyArbitrary<A>): LazyArbitrary<option_.Option<A>> => {
const arb = arbitrary_.makeLazy(optionEncoded(schemaFromArbitrary(value)))
return (fc) => arb(fc).map(optionDecode)
}
const optionArbitrary = <A>(value: LazyArbitrary<A>): LazyArbitrary<option_.Option<A>> => (fc) =>
fc.oneof(
fc.record({ _tag: fc.constant("None" as const) }),
fc.record({ _tag: fc.constant("Some" as const), value: value(fc) })
).map(optionDecode)

const optionPretty = <A>(value: pretty_.Pretty<A>): pretty_.Pretty<option_.Option<A>> =>
option_.match({
Expand Down Expand Up @@ -5277,10 +5279,12 @@ const eitherDecode = <R, L>(input: EitherEncoded<R, L>): either_.Either<R, L> =>
const eitherArbitrary = <R, L>(
right: LazyArbitrary<R>,
left: LazyArbitrary<L>
): LazyArbitrary<either_.Either<R, L>> => {
const arb = arbitrary_.makeLazy(eitherEncoded(schemaFromArbitrary(right), schemaFromArbitrary(left)))
return (fc) => arb(fc).map(eitherDecode)
}
): LazyArbitrary<either_.Either<R, L>> =>
(fc) =>
fc.oneof(
fc.record({ _tag: fc.constant("Left" as const), left: left(fc) }),
fc.record({ _tag: fc.constant("Right" as const), right: right(fc) })
).map(eitherDecode)

const eitherPretty = <R, L>(
right: pretty_.Pretty<R>,
Expand Down Expand Up @@ -6374,14 +6378,13 @@ export const TaggedError = <Self = never>(identifier?: string) =>
tag: { _tag: tag },
annotations,
toStringOverride(self) {
if (!(Predicate.isString(self.message) && self.message.length > 0)) {
return pretty_.make(self.constructor as any)(self)
}
let message = `${self._tag}: ${self.message}`
if (Predicate.isString(self.stack)) {
message = `${message}\n${self.stack.split("\n").slice(1).join("\n")}`
if ((Predicate.isString(self.message) && self.message.length > 0)) {
let message = `${self._tag}: ${self.message}`
if (Predicate.isString(self.stack)) {
message = `${message}\n${self.stack.split("\n").slice(1).join("\n")}`
}
return message
}
return message
}
})
}
Expand Down Expand Up @@ -6481,7 +6484,7 @@ const makeClass = ({ Base, annotations, fields, fromSchema, identifier, kind, ta
fromSchema?: Schema.Any | undefined
tag?: { _tag: AST.LiteralValue } | undefined
annotations?: Annotations.Schema<any> | undefined
toStringOverride?: ((self: any) => string) | undefined
toStringOverride?: (self: any) => string | undefined
}): any => {
const classSymbol = Symbol.for(`@effect/schema/${kind}/${identifier}`)
const schema = fromSchema ?? Struct(fields)
Expand Down Expand Up @@ -6524,7 +6527,16 @@ const makeClass = ({ Base, annotations, fields, fromSchema, identifier, kind, ta
}

toString() {
return toStringOverride !== undefined ? toStringOverride(this) : pretty_.make(this.constructor as any)(this)
if (toStringOverride !== undefined) {
const out = toStringOverride(this)
if (out !== undefined) {
return out
}
}
return `${identifier}({ ${
util_.ownKeys(fields).map((p: any) => `${util_.formatPropertyKey(p)}: ${util_.formatUnknown(this[p])}`)
.join(", ")
} })`
}

static fields = { ...fields }
Expand All @@ -6536,11 +6548,8 @@ const makeClass = ({ Base, annotations, fields, fromSchema, identifier, kind, ta
const guard = ParseResult.is(toSchema)
const fallbackInstanceOf = (u: unknown) => Predicate.hasProperty(u, classSymbol) && guard(u)
const encode = ParseResult.encodeUnknown(toSchema)
const pretty = pretty_.make(toSchema)
const arb = arbitrary_.makeLazy(toSchema)
const equivalence = equivalence_.make(toSchema)
const declaration: Schema.Any = declare(
[],
[toSchema],
{
decode: () => (input, _, ast) =>
input instanceof this || fallbackInstanceOf(input)
Expand All @@ -6558,9 +6567,9 @@ const makeClass = ({ Base, annotations, fields, fromSchema, identifier, kind, ta
identifier,
title: identifier,
description: `an instance of ${identifier}`,
pretty: () => (self: any) => `${identifier}(${pretty(self)})`,
arbitrary: () => (fc: any) => arb(fc).map((props: any) => new this(props)),
equivalence: () => equivalence as any,
pretty: (pretty) => (self: any) => `${identifier}(${pretty(self)})`,
arbitrary: (arb) => (fc: any) => arb(fc).map((props: any) => new this(props)),
equivalence: identity,
[AST.SurrogateAnnotationId]: toSchema.ast,
...annotations
}
Expand Down Expand Up @@ -6646,37 +6655,41 @@ export type FiberIdEncoded =
readonly startTimeMillis: number
}

const FiberIdCompositeEncoded = Struct({
_tag: Literal("Composite"),
left: suspend(() => FiberIdEncoded),
right: suspend(() => FiberIdEncoded)
}).annotations({ identifier: "FiberIdCompositeEncoded" })

const FiberIdNoneEncoded = Struct({
_tag: Literal("None")
}).annotations({ identifier: "FiberIdNoneEncoded" })

const FiberIdRuntimeEncoded = Struct({
_tag: Literal("Runtime"),
id: Int.pipe(nonNegative({
id: Int.annotations({
title: "id",
description: "id"
})),
startTimeMillis: Int.pipe(nonNegative({
}),
startTimeMillis: Int.annotations({
title: "startTimeMillis",
description: "startTimeMillis"
}))
})
}).annotations({ identifier: "FiberIdRuntimeEncoded" })

const FiberIdCompositeEncoded = Struct({
_tag: Literal("Composite"),
left: suspend(() => FiberIdEncoded),
right: suspend(() => FiberIdEncoded)
}).annotations({ identifier: "FiberIdCompositeEncoded" })

const FiberIdEncoded: Schema<FiberIdEncoded> = Union(
FiberIdNoneEncoded,
FiberIdRuntimeEncoded,
FiberIdCompositeEncoded
).annotations({ identifier: "FiberIdEncoded" })

const fiberIdFromArbitrary = arbitrary_.makeLazy(FiberIdEncoded)

const fiberIdArbitrary: LazyArbitrary<fiberId_.FiberId> = (fc) => fiberIdFromArbitrary(fc).map(fiberIdDecode)
const fiberIdArbitrary: LazyArbitrary<fiberId_.FiberId> = (fc) =>
fc.letrec((tie) => ({
None: fc.record({ _tag: fc.constant("None" as const) }),
Runtime: fc.record({ _tag: fc.constant("Runtime" as const), id: fc.integer(), startTimeMillis: fc.integer() }),
Composite: fc.record({ _tag: fc.constant("Composite" as const), left: tie("FiberId"), right: tie("FiberId") }),
FiberId: fc.oneof(tie("None"), tie("Runtime"), tie("Composite")) as any as fastCheck_.Arbitrary<fiberId_.FiberId>
})).FiberId.map(fiberIdDecode)

const fiberIdPretty: pretty_.Pretty<fiberId_.FiberId> = (fiberId) => {
switch (fiberId._tag) {
Expand Down Expand Up @@ -6835,10 +6848,24 @@ const causeEncoded = <E, EI, R1, R2>(
const causeArbitrary = <E>(
error: LazyArbitrary<E>,
defect: LazyArbitrary<unknown>
): LazyArbitrary<cause_.Cause<E>> => {
const arb = arbitrary_.makeLazy(causeEncoded(schemaFromArbitrary(error), schemaFromArbitrary(defect)))
return (fc) => arb(fc).map(causeDecode)
}
): LazyArbitrary<cause_.Cause<E>> =>
(fc) =>
fc.letrec((tie) => ({
Empty: fc.record({ _tag: fc.constant("Empty" as const) }),
Fail: fc.record({ _tag: fc.constant("Fail" as const), error: error(fc) }),
Die: fc.record({ _tag: fc.constant("Die" as const), defect: defect(fc) }),
Interrupt: fc.record({ _tag: fc.constant("Interrupt" as const), fiberId: fiberIdArbitrary(fc) }),
Sequential: fc.record({ _tag: fc.constant("Sequential" as const), left: tie("Cause"), right: tie("Cause") }),
Parallel: fc.record({ _tag: fc.constant("Parallel" as const), left: tie("Cause"), right: tie("Cause") }),
Cause: fc.oneof(
tie("Empty"),
tie("Fail"),
tie("Die"),
tie("Interrupt"),
tie("Sequential"),
tie("Parallel")
) as any as fastCheck_.Arbitrary<cause_.Cause<E>>
})).Cause.map(causeDecode)

const causePretty = <E>(error: pretty_.Pretty<E>): pretty_.Pretty<cause_.Cause<E>> => (cause) => {
const f = (cause: cause_.Cause<E>): string => {
Expand Down Expand Up @@ -7061,12 +7088,12 @@ const exitArbitrary = <A, E>(
value: LazyArbitrary<A>,
error: LazyArbitrary<E>,
defect: LazyArbitrary<unknown>
): LazyArbitrary<exit_.Exit<A, E>> => {
const arb = arbitrary_.makeLazy(
exitEncoded(schemaFromArbitrary(value), schemaFromArbitrary(error), schemaFromArbitrary(defect))
)
return (fc) => arb(fc).map(exitDecode)
}
): LazyArbitrary<exit_.Exit<A, E>> =>
(fc) =>
fc.oneof(
fc.record({ _tag: fc.constant("Failure" as const), cause: causeArbitrary(error, defect)(fc) }),
fc.record({ _tag: fc.constant("Success" as const), value: value(fc) })
).map(exitDecode)

const exitPretty =
<A, E>(value: pretty_.Pretty<A>, error: pretty_.Pretty<E>): pretty_.Pretty<exit_.Exit<A, E>> => (exit) =>
Expand Down Expand Up @@ -7525,11 +7552,6 @@ export const SortedSet = <Value extends Schema.Any>(
)
}

const schemaFromArbitrary = <A>(value: LazyArbitrary<A>): Schema<A> =>
suspend<A, A, never>(() => Any).annotations({
[arbitrary_.ArbitraryHookId]: () => value
})

/**
* @category api interface
* @since 1.0.0
Expand Down
4 changes: 4 additions & 0 deletions packages/schema/src/internal/util.ts
Expand Up @@ -72,3 +72,7 @@ export const formatUnknown = (u: unknown): string => {
return String(u)
}
}

/** @internal */
export const formatPropertyKey = (name: PropertyKey): string =>
typeof name === "string" ? JSON.stringify(name) : String(name)
2 changes: 1 addition & 1 deletion packages/schema/test/AST/suspend.test.ts
@@ -1,6 +1,6 @@
import type * as AST from "@effect/schema/AST"
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { describe, expect, it } from "vitest"

describe("AST.Suspend", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/Arbitrary/Arbitrary.test.ts
@@ -1,6 +1,6 @@
import * as Arbitrary from "@effect/schema/Arbitrary"
import * as S from "@effect/schema/Schema"
import { expectValidArbitrary } from "@effect/schema/test/util"
import { expectValidArbitrary } from "@effect/schema/test/TestUtils"
import { isUnknown } from "effect/Predicate"
import * as fc from "fast-check"
import { describe, expect, it } from "vitest"
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/Arbitrary/Class.test.ts
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import { expectValidArbitrary } from "@effect/schema/test/util"
import { expectValidArbitrary } from "@effect/schema/test/TestUtils"
import { describe, it } from "vitest"

describe("class", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/ArrayFormatter.test.ts
Expand Up @@ -2,7 +2,7 @@ import * as ArrayFormatter from "@effect/schema/ArrayFormatter"
import type { ParseOptions } from "@effect/schema/AST"
import * as ParseResult from "@effect/schema/ParseResult"
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import * as Either from "effect/Either"
import * as Option from "effect/Option"
import { describe, expect, it } from "vitest"
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/Schema/Any/Any.test.ts
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { describe, it } from "vitest"

describe("Any", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/Schema/Array/head.test.ts
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import * as Option from "effect/Option"
import { describe, it } from "vitest"

Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/Schema/Array/headOrElse.test.ts
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { describe, it } from "vitest"

describe("headOrElse", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/Schema/Array/itemsCount.test.ts
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { describe, it } from "vitest"

describe("itemsCount", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/Schema/Array/maxItems.test.ts
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { describe, it } from "vitest"

describe("maxItems", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/Schema/Array/minItems.test.ts
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { describe, it } from "vitest"

describe("minItems", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/Schema/BigDecimal/BigDecimal.test.ts
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { BigDecimal } from "effect"
import { describe, it } from "vitest"

Expand Down
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { BigDecimal } from "effect"
import { describe, it } from "vitest"

Expand Down
@@ -1,7 +1,7 @@
import * as Equivalence from "@effect/schema/Equivalence"
import * as Pretty from "@effect/schema/Pretty"
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { BigDecimal } from "effect"
import { describe, expect, it } from "vitest"

Expand Down
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { BigDecimal } from "effect"
import { describe, it } from "vitest"

Expand Down
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { BigDecimal } from "effect"
import { describe, it } from "vitest"

Expand Down
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { BigDecimal } from "effect"
import { describe, it } from "vitest"

Expand Down
@@ -1,5 +1,5 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
import * as Util from "@effect/schema/test/TestUtils"
import { BigDecimal } from "effect"
import { describe, it } from "vitest"

Expand Down

0 comments on commit 9dfc156

Please sign in to comment.