Skip to content

Commit 6758030

Browse files
authored
Replace hardcoded value in precompileMorphs for tuple defaults (#1574)
* expose hardcoded default value in precompileMorphs (introduced in #1381) * fix hardcoded default value in `precompuledMorphs`
1 parent 6d0639b commit 6758030

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

ark/schema/structure/structure.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,8 @@ const precompileMorphs = (js: NodeCompiler, node: Structure.Node) => {
10261026
if (node.sequence?.defaultables) {
10271027
js.for(
10281028
`i < ${node.sequence.defaultables.length}`,
1029-
js => js.set(`data[i]`, 5),
1029+
js =>
1030+
js.line(`${node.sequence!.defaultValueMorphsReference}[i]${args}`),
10301031
`data.length - ${node.sequence.prefixLength}`
10311032
)
10321033
}

ark/type/__tests__/arrays/defaults.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { attest, contextualize } from "@ark/attest"
2+
import { Traversal } from "@ark/schema"
23
import { type } from "arktype"
34
import { defaultablePostOptionalMessage } from "arktype/internal/parser/tupleLiteral.ts"
45

@@ -108,4 +109,18 @@ contextualize(() => {
108109

109110
attest(T.out.expression).snap("[number]")
110111
})
112+
113+
it("compiled defaults use correct values", () => {
114+
const T = type(["string = 'foo'"])
115+
const internal = T.internal
116+
117+
// Call traverseApply directly with a Traversal context
118+
// This exercises the JIT-compiled code path
119+
const data: string[] = []
120+
const ctx = new Traversal(data, internal.$.resolvedConfig)
121+
internal.traverseApply(data, ctx)
122+
const result = ctx.finalize(null)
123+
124+
attest(result).equals(["foo"])
125+
})
111126
})

0 commit comments

Comments
 (0)