From eadfaec7ec0054694cb85abe1f611afc811ca783 Mon Sep 17 00:00:00 2001 From: Giulio Canti Date: Fri, 26 Sep 2025 18:10:03 +0200 Subject: [PATCH] add internal AST.apply to reduce bundle (#588) --- packages/effect/src/schema/AST.ts | 45 +++++++++++++----------- packages/effect/src/schema/Serializer.ts | 38 ++++---------------- 2 files changed, 30 insertions(+), 53 deletions(-) diff --git a/packages/effect/src/schema/AST.ts b/packages/effect/src/schema/AST.ts index 589438a34f..4f84de2eef 100644 --- a/packages/effect/src/schema/AST.ts +++ b/packages/effect/src/schema/AST.ts @@ -1897,11 +1897,30 @@ export function appendChecks(ast: A, checks: Checks): A { return replaceChecks(ast, ast.checks ? [...ast.checks, ...checks] : checks) } +/** @internal */ +export function apply(f: (ast: AST) => AST): (ast: AST) => AST { + function out(ast: AST): AST { + if (ast.encoding) { + const links = ast.encoding + const last = links[links.length - 1] + const to = out(last.to) + return to === last.to ? + ast : + replaceEncoding(ast, replaceLastLink(links, new Link(to, last.transformation))) + } + return f(ast) + } + return out +} + function applyEncoded(ast: A, f: (ast: AST) => AST): A { if (ast.encoding) { const links = ast.encoding const last = links[links.length - 1] - return replaceEncoding(ast, replaceLastLink(links, new Link(f(last.to), last.transformation))) + const to = f(last.to) + return to === last.to ? + ast : + replaceEncoding(ast, replaceLastLink(links, new Link(to, last.transformation))) } return ast } @@ -2395,15 +2414,7 @@ function coerceBigInt(ast: BigIntKeyword): BigIntKeyword { return replaceEncoding(ast, [bigIntLink]) } -const goIndexSignature = memoize((ast: AST): AST => { - if (ast.encoding) { - const links = ast.encoding - const last = links[links.length - 1] - const to = goIndexSignature(last.to) - return to === last.to ? - ast : - replaceEncoding(ast, replaceLastLink(links, new Link(to, last.transformation))) - } +const goIndexSignature = memoize(apply((ast: AST): AST => { switch (ast._tag) { case "NumberKeyword": return ast.goStringPojo() @@ -2412,17 +2423,9 @@ const goIndexSignature = memoize((ast: AST): AST => { default: return ast } -}) +})) -const goTemplateLiteral = memoize((ast: AST): AST => { - if (ast.encoding) { - const links = ast.encoding - const last = links[links.length - 1] - const to = goTemplateLiteral(last.to) - return to === last.to ? - ast : - replaceEncoding(ast, replaceLastLink(links, new Link(to, last.transformation))) - } +const goTemplateLiteral = memoize(apply((ast: AST): AST => { switch (ast._tag) { case "StringKeyword": case "TemplateLiteral": @@ -2437,7 +2440,7 @@ const goTemplateLiteral = memoize((ast: AST): AST => { return ast.go(goTemplateLiteral) } throw new Error(`Unsupported template literal part tag: ${ast._tag}`) -}) +})) const numberKeysRegExp = new RegExp(`(?:${NUMBER_KEYWORD_PATTERN}|Infinity|-Infinity|NaN)`) diff --git a/packages/effect/src/schema/Serializer.ts b/packages/effect/src/schema/Serializer.ts index 4dff439128..c6d37a646b 100644 --- a/packages/effect/src/schema/Serializer.ts +++ b/packages/effect/src/schema/Serializer.ts @@ -25,16 +25,7 @@ export function json( return Schema.make(goJson(codec.ast)) } -const goJson = memoize((ast: AST.AST): AST.AST => { - if (ast.encoding) { - const links = ast.encoding - const last = links[links.length - 1] - const to = goJson(last.to) - return to === last.to - ? ast - : AST.replaceEncoding(ast, AST.replaceLastLink(links, new AST.Link(to, last.transformation))) - } - +const goJson = memoize(AST.apply((ast: AST.AST): AST.AST => { function go(ast: AST.AST): AST.AST { switch (ast._tag) { case "UnknownKeyword": @@ -73,7 +64,7 @@ const goJson = memoize((ast: AST.AST): AST.AST => { const out = go(ast) return AST.isOptional(ast) ? AST.optionalKey(out) : out -}) +})) function requiredGoJsonAnnotation(ast: AST.AST): AST.AST { return forbidden( @@ -139,15 +130,7 @@ export function stringPojo( } /** @internal */ -export const goStringPojo = memoize((ast: AST.AST): AST.AST => { - if (ast.encoding) { - const links = ast.encoding - const last = links[links.length - 1] - const to = goStringPojo(last.to) - return to === last.to ? - ast : - AST.replaceEncoding(ast, AST.replaceLastLink(links, new AST.Link(to, last.transformation))) - } +export const goStringPojo = memoize(AST.apply((ast: AST.AST): AST.AST => { function go(ast: AST.AST): AST.AST { switch (ast._tag) { case "UnknownKeyword": @@ -190,7 +173,7 @@ export const goStringPojo = memoize((ast: AST.AST): AST.AST => { } const out = go(ast) return AST.isOptional(ast) ? AST.optionalKey(out) : out -}) +})) const nullLink = new AST.Link( AST.undefinedKeyword, @@ -227,16 +210,7 @@ export function ensureArray( const ENSURE_ARRAY_ANNOTATION_KEY = "~effect/schema/Serializer/ensureArray" /** @internal */ -export const goEnsureArray = memoize((ast: AST.AST): AST.AST => { - if (ast.encoding) { - const links = ast.encoding - const last = links[links.length - 1] - const to = goEnsureArray(last.to) - if (to === last.to) { - return ast - } - return AST.replaceEncoding(ast, AST.replaceLastLink(links, new AST.Link(to, last.transformation))) - } +export const goEnsureArray = memoize(AST.apply((ast: AST.AST): AST.AST => { if (AST.isUnionType(ast) && ast.annotations?.[ENSURE_ARRAY_ANNOTATION_KEY]) { return ast } @@ -260,7 +234,7 @@ export const goEnsureArray = memoize((ast: AST.AST): AST.AST => { return out.context?.isOptional ? AST.optionalKey(ensure) : ensure } return out -}) +})) type XmlEncoderOptions = { /** Root element name for the returned XML string. Default: "root" */