add internal AST.apply to reduce bundle (#588)

This commit is contained in:
Giulio Canti
2025-09-26 18:10:03 +02:00
committed by GitHub
parent dcd95da3b8
commit eadfaec7ec
2 changed files with 30 additions and 53 deletions
+24 -21
View File
@@ -1897,11 +1897,30 @@ export function appendChecks<A extends AST>(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<A extends AST>(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)`)
+6 -32
View File
@@ -25,16 +25,7 @@ export function json<T, E, RD, RE>(
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<T, E, RD, RE>(
}
/** @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<T, RD, RE>(
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" */