zod-compiler 1.23.0 → 1.23.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -6
- package/dist/cli/commands/check.d.ts +10 -7
- package/dist/cli/commands/check.d.ts.map +1 -1
- package/dist/cli/commands/check.js +125 -148
- package/dist/cli/commands/check.js.map +1 -1
- package/dist/cli/commands/generate.d.ts +22 -19
- package/dist/cli/commands/generate.d.ts.map +1 -1
- package/dist/cli/commands/generate.js +102 -133
- package/dist/cli/commands/generate.js.map +1 -1
- package/dist/cli/commands/watch.d.ts +17 -15
- package/dist/cli/commands/watch.d.ts.map +1 -1
- package/dist/cli/commands/watch.js +126 -154
- package/dist/cli/commands/watch.js.map +1 -1
- package/dist/cli/emitter.d.ts +19 -16
- package/dist/cli/emitter.d.ts.map +1 -1
- package/dist/cli/emitter.js +55 -71
- package/dist/cli/emitter.js.map +1 -1
- package/dist/cli/errors.d.ts +4 -1
- package/dist/cli/errors.d.ts.map +1 -1
- package/dist/cli/errors.js +6 -2
- package/dist/cli/errors.js.map +1 -1
- package/dist/cli/index.d.ts +1 -3
- package/dist/cli/index.js +147 -160
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/logger.d.ts +18 -15
- package/dist/cli/logger.d.ts.map +1 -1
- package/dist/cli/logger.js +38 -37
- package/dist/cli/logger.js.map +1 -1
- package/dist/core/codegen/build-path.d.ts +7 -41
- package/dist/core/codegen/build-path.d.ts.map +1 -1
- package/dist/core/codegen/build-path.js +481 -570
- package/dist/core/codegen/build-path.js.map +1 -1
- package/dist/core/codegen/context.d.ts +256 -253
- package/dist/core/codegen/context.d.ts.map +1 -1
- package/dist/core/codegen/context.js +363 -437
- package/dist/core/codegen/context.js.map +1 -1
- package/dist/core/codegen/dedupe.d.ts +22 -56
- package/dist/core/codegen/dedupe.d.ts.map +1 -1
- package/dist/core/codegen/dedupe.js +150 -242
- package/dist/core/codegen/dedupe.js.map +1 -1
- package/dist/core/codegen/emit-issue.d.ts +44 -68
- package/dist/core/codegen/emit-issue.d.ts.map +1 -1
- package/dist/core/codegen/emit-issue.js +76 -108
- package/dist/core/codegen/emit-issue.js.map +1 -1
- package/dist/core/codegen/emit.d.ts +4 -1
- package/dist/core/codegen/emit.d.ts.map +1 -1
- package/dist/core/codegen/emit.js +16 -13
- package/dist/core/codegen/emit.js.map +1 -1
- package/dist/core/codegen/fast-path.d.ts +7 -12
- package/dist/core/codegen/fast-path.d.ts.map +1 -1
- package/dist/core/codegen/fast-path.js +116 -145
- package/dist/core/codegen/fast-path.js.map +1 -1
- package/dist/core/codegen/fast-size.d.ts +12 -26
- package/dist/core/codegen/fast-size.d.ts.map +1 -1
- package/dist/core/codegen/fast-size.js +169 -244
- package/dist/core/codegen/fast-size.js.map +1 -1
- package/dist/core/codegen/index.d.ts +25 -23
- package/dist/core/codegen/index.d.ts.map +1 -1
- package/dist/core/codegen/index.js +239 -405
- package/dist/core/codegen/index.js.map +1 -1
- package/dist/core/codegen/issue-decls.d.ts +15 -12
- package/dist/core/codegen/issue-decls.d.ts.map +1 -1
- package/dist/core/codegen/issue-decls.js +131 -162
- package/dist/core/codegen/issue-decls.js.map +1 -1
- package/dist/core/codegen/regex-unroll.d.ts +4 -1
- package/dist/core/codegen/regex-unroll.d.ts.map +1 -1
- package/dist/core/codegen/regex-unroll.js +173 -197
- package/dist/core/codegen/regex-unroll.js.map +1 -1
- package/dist/core/codegen/schemas/any.d.ts +7 -4
- package/dist/core/codegen/schemas/any.d.ts.map +1 -1
- package/dist/core/codegen/schemas/any.js +8 -4
- package/dist/core/codegen/schemas/any.js.map +1 -1
- package/dist/core/codegen/schemas/array.d.ts +8 -5
- package/dist/core/codegen/schemas/array.d.ts.map +1 -1
- package/dist/core/codegen/schemas/array.js +77 -95
- package/dist/core/codegen/schemas/array.js.map +1 -1
- package/dist/core/codegen/schemas/bigint.d.ts +7 -4
- package/dist/core/codegen/schemas/bigint.d.ts.map +1 -1
- package/dist/core/codegen/schemas/bigint.js +44 -56
- package/dist/core/codegen/schemas/bigint.js.map +1 -1
- package/dist/core/codegen/schemas/boolean.d.ts +7 -4
- package/dist/core/codegen/schemas/boolean.d.ts.map +1 -1
- package/dist/core/codegen/schemas/boolean.js +12 -11
- package/dist/core/codegen/schemas/boolean.js.map +1 -1
- package/dist/core/codegen/schemas/catch.d.ts +8 -5
- package/dist/core/codegen/schemas/catch.d.ts.map +1 -1
- package/dist/core/codegen/schemas/catch.js +23 -23
- package/dist/core/codegen/schemas/catch.js.map +1 -1
- package/dist/core/codegen/schemas/date.d.ts +7 -4
- package/dist/core/codegen/schemas/date.d.ts.map +1 -1
- package/dist/core/codegen/schemas/date.js +49 -68
- package/dist/core/codegen/schemas/date.js.map +1 -1
- package/dist/core/codegen/schemas/default.d.ts +9 -6
- package/dist/core/codegen/schemas/default.d.ts.map +1 -1
- package/dist/core/codegen/schemas/default.js +29 -27
- package/dist/core/codegen/schemas/default.js.map +1 -1
- package/dist/core/codegen/schemas/discriminated-union.d.ts +12 -10
- package/dist/core/codegen/schemas/discriminated-union.d.ts.map +1 -1
- package/dist/core/codegen/schemas/discriminated-union.js +153 -178
- package/dist/core/codegen/schemas/discriminated-union.js.map +1 -1
- package/dist/core/codegen/schemas/effect.d.ts +9 -6
- package/dist/core/codegen/schemas/effect.d.ts.map +1 -1
- package/dist/core/codegen/schemas/effect.js +54 -67
- package/dist/core/codegen/schemas/effect.js.map +1 -1
- package/dist/core/codegen/schemas/enum.d.ts +7 -4
- package/dist/core/codegen/schemas/enum.d.ts.map +1 -1
- package/dist/core/codegen/schemas/enum.js +15 -21
- package/dist/core/codegen/schemas/enum.js.map +1 -1
- package/dist/core/codegen/schemas/fallback.d.ts +6 -3
- package/dist/core/codegen/schemas/fallback.d.ts.map +1 -1
- package/dist/core/codegen/schemas/fallback.js +14 -13
- package/dist/core/codegen/schemas/fallback.js.map +1 -1
- package/dist/core/codegen/schemas/file.d.ts +7 -4
- package/dist/core/codegen/schemas/file.d.ts.map +1 -1
- package/dist/core/codegen/schemas/file.js +51 -57
- package/dist/core/codegen/schemas/file.js.map +1 -1
- package/dist/core/codegen/schemas/intersection.d.ts +8 -5
- package/dist/core/codegen/schemas/intersection.d.ts.map +1 -1
- package/dist/core/codegen/schemas/intersection.js +16 -13
- package/dist/core/codegen/schemas/intersection.js.map +1 -1
- package/dist/core/codegen/schemas/literal.d.ts +7 -4
- package/dist/core/codegen/schemas/literal.d.ts.map +1 -1
- package/dist/core/codegen/schemas/literal.js +20 -23
- package/dist/core/codegen/schemas/literal.js.map +1 -1
- package/dist/core/codegen/schemas/map.d.ts +8 -5
- package/dist/core/codegen/schemas/map.d.ts.map +1 -1
- package/dist/core/codegen/schemas/map.js +61 -67
- package/dist/core/codegen/schemas/map.js.map +1 -1
- package/dist/core/codegen/schemas/nan.d.ts +7 -4
- package/dist/core/codegen/schemas/nan.d.ts.map +1 -1
- package/dist/core/codegen/schemas/nan.js +8 -4
- package/dist/core/codegen/schemas/nan.js.map +1 -1
- package/dist/core/codegen/schemas/never.d.ts +7 -4
- package/dist/core/codegen/schemas/never.d.ts.map +1 -1
- package/dist/core/codegen/schemas/never.js +8 -4
- package/dist/core/codegen/schemas/never.js.map +1 -1
- package/dist/core/codegen/schemas/null.d.ts +7 -4
- package/dist/core/codegen/schemas/null.d.ts.map +1 -1
- package/dist/core/codegen/schemas/null.js +8 -4
- package/dist/core/codegen/schemas/null.js.map +1 -1
- package/dist/core/codegen/schemas/nullable.d.ts +8 -5
- package/dist/core/codegen/schemas/nullable.d.ts.map +1 -1
- package/dist/core/codegen/schemas/nullable.js +8 -5
- package/dist/core/codegen/schemas/nullable.js.map +1 -1
- package/dist/core/codegen/schemas/number.d.ts +7 -4
- package/dist/core/codegen/schemas/number.d.ts.map +1 -1
- package/dist/core/codegen/schemas/number.js +120 -176
- package/dist/core/codegen/schemas/number.js.map +1 -1
- package/dist/core/codegen/schemas/object.d.ts +8 -5
- package/dist/core/codegen/schemas/object.d.ts.map +1 -1
- package/dist/core/codegen/schemas/object.js +144 -231
- package/dist/core/codegen/schemas/object.js.map +1 -1
- package/dist/core/codegen/schemas/optional.d.ts +9 -6
- package/dist/core/codegen/schemas/optional.d.ts.map +1 -1
- package/dist/core/codegen/schemas/optional.js +23 -31
- package/dist/core/codegen/schemas/optional.js.map +1 -1
- package/dist/core/codegen/schemas/pipe.d.ts +8 -5
- package/dist/core/codegen/schemas/pipe.d.ts.map +1 -1
- package/dist/core/codegen/schemas/pipe.js +20 -27
- package/dist/core/codegen/schemas/pipe.js.map +1 -1
- package/dist/core/codegen/schemas/readonly.d.ts +8 -5
- package/dist/core/codegen/schemas/readonly.d.ts.map +1 -1
- package/dist/core/codegen/schemas/readonly.js +8 -5
- package/dist/core/codegen/schemas/readonly.js.map +1 -1
- package/dist/core/codegen/schemas/record.d.ts +8 -5
- package/dist/core/codegen/schemas/record.d.ts.map +1 -1
- package/dist/core/codegen/schemas/record.js +52 -65
- package/dist/core/codegen/schemas/record.js.map +1 -1
- package/dist/core/codegen/schemas/recursive-ref.d.ts +9 -6
- package/dist/core/codegen/schemas/recursive-ref.d.ts.map +1 -1
- package/dist/core/codegen/schemas/recursive-ref.js +44 -44
- package/dist/core/codegen/schemas/recursive-ref.js.map +1 -1
- package/dist/core/codegen/schemas/set.d.ts +8 -5
- package/dist/core/codegen/schemas/set.d.ts.map +1 -1
- package/dist/core/codegen/schemas/set.js +61 -73
- package/dist/core/codegen/schemas/set.js.map +1 -1
- package/dist/core/codegen/schemas/string-bool.d.ts +6 -3
- package/dist/core/codegen/schemas/string-bool.d.ts.map +1 -1
- package/dist/core/codegen/schemas/string-bool.js +22 -23
- package/dist/core/codegen/schemas/string-bool.js.map +1 -1
- package/dist/core/codegen/schemas/string.d.ts +8 -5
- package/dist/core/codegen/schemas/string.d.ts.map +1 -1
- package/dist/core/codegen/schemas/string.js +168 -207
- package/dist/core/codegen/schemas/string.js.map +1 -1
- package/dist/core/codegen/schemas/symbol.d.ts +7 -4
- package/dist/core/codegen/schemas/symbol.d.ts.map +1 -1
- package/dist/core/codegen/schemas/symbol.js +8 -4
- package/dist/core/codegen/schemas/symbol.js.map +1 -1
- package/dist/core/codegen/schemas/template-literal.d.ts +7 -4
- package/dist/core/codegen/schemas/template-literal.d.ts.map +1 -1
- package/dist/core/codegen/schemas/template-literal.js +12 -13
- package/dist/core/codegen/schemas/template-literal.js.map +1 -1
- package/dist/core/codegen/schemas/tuple.d.ts +8 -5
- package/dist/core/codegen/schemas/tuple.d.ts.map +1 -1
- package/dist/core/codegen/schemas/tuple.js +84 -94
- package/dist/core/codegen/schemas/tuple.js.map +1 -1
- package/dist/core/codegen/schemas/undefined.d.ts +7 -4
- package/dist/core/codegen/schemas/undefined.d.ts.map +1 -1
- package/dist/core/codegen/schemas/undefined.js +8 -4
- package/dist/core/codegen/schemas/undefined.js.map +1 -1
- package/dist/core/codegen/schemas/union.d.ts +8 -5
- package/dist/core/codegen/schemas/union.d.ts.map +1 -1
- package/dist/core/codegen/schemas/union.js +49 -69
- package/dist/core/codegen/schemas/union.js.map +1 -1
- package/dist/core/codegen/schemas/unknown.d.ts +7 -4
- package/dist/core/codegen/schemas/unknown.d.ts.map +1 -1
- package/dist/core/codegen/schemas/unknown.js +8 -4
- package/dist/core/codegen/schemas/unknown.js.map +1 -1
- package/dist/core/codegen/schemas/void.d.ts +7 -4
- package/dist/core/codegen/schemas/void.d.ts.map +1 -1
- package/dist/core/codegen/schemas/void.js +8 -4
- package/dist/core/codegen/schemas/void.js.map +1 -1
- package/dist/core/codegen/slow-path.d.ts +7 -4
- package/dist/core/codegen/slow-path.d.ts.map +1 -1
- package/dist/core/codegen/slow-path.js +71 -96
- package/dist/core/codegen/slow-path.js.map +1 -1
- package/dist/core/codegen/well-known-regex.d.ts +22 -19
- package/dist/core/codegen/well-known-regex.d.ts.map +1 -1
- package/dist/core/codegen/well-known-regex.js +127 -96
- package/dist/core/codegen/well-known-regex.js.map +1 -1
- package/dist/core/compile.d.ts +7 -4
- package/dist/core/compile.d.ts.map +1 -1
- package/dist/core/compile.js +48 -48
- package/dist/core/compile.js.map +1 -1
- package/dist/core/diagnostic.d.ts +38 -39
- package/dist/core/diagnostic.d.ts.map +1 -1
- package/dist/core/diagnostic.js +140 -164
- package/dist/core/diagnostic.js.map +1 -1
- package/dist/core/extract/checks.d.ts +18 -15
- package/dist/core/extract/checks.d.ts.map +1 -1
- package/dist/core/extract/checks.js +318 -311
- package/dist/core/extract/checks.js.map +1 -1
- package/dist/core/extract/effects.d.ts +13 -10
- package/dist/core/extract/effects.d.ts.map +1 -1
- package/dist/core/extract/effects.js +153 -222
- package/dist/core/extract/effects.js.map +1 -1
- package/dist/core/extract/extractors/any.d.ts +5 -2
- package/dist/core/extract/extractors/any.d.ts.map +1 -1
- package/dist/core/extract/extractors/any.js +5 -1
- package/dist/core/extract/extractors/any.js.map +1 -1
- package/dist/core/extract/extractors/array.d.ts +6 -3
- package/dist/core/extract/extractors/array.d.ts.map +1 -1
- package/dist/core/extract/extractors/array.js +25 -20
- package/dist/core/extract/extractors/array.js.map +1 -1
- package/dist/core/extract/extractors/bigint.d.ts +6 -3
- package/dist/core/extract/extractors/bigint.d.ts.map +1 -1
- package/dist/core/extract/extractors/bigint.js +66 -64
- package/dist/core/extract/extractors/bigint.js.map +1 -1
- package/dist/core/extract/extractors/boolean.d.ts +5 -2
- package/dist/core/extract/extractors/boolean.d.ts.map +1 -1
- package/dist/core/extract/extractors/boolean.js +7 -3
- package/dist/core/extract/extractors/boolean.js.map +1 -1
- package/dist/core/extract/extractors/catch.d.ts +6 -3
- package/dist/core/extract/extractors/catch.d.ts.map +1 -1
- package/dist/core/extract/extractors/catch.js +26 -17
- package/dist/core/extract/extractors/catch.js.map +1 -1
- package/dist/core/extract/extractors/date.d.ts +6 -3
- package/dist/core/extract/extractors/date.d.ts.map +1 -1
- package/dist/core/extract/extractors/date.js +42 -46
- package/dist/core/extract/extractors/date.js.map +1 -1
- package/dist/core/extract/extractors/default.d.ts +6 -3
- package/dist/core/extract/extractors/default.d.ts.map +1 -1
- package/dist/core/extract/extractors/default.js +20 -14
- package/dist/core/extract/extractors/default.js.map +1 -1
- package/dist/core/extract/extractors/enum.d.ts +5 -2
- package/dist/core/extract/extractors/enum.d.ts.map +1 -1
- package/dist/core/extract/extractors/enum.js +12 -13
- package/dist/core/extract/extractors/enum.js.map +1 -1
- package/dist/core/extract/extractors/file.d.ts +6 -3
- package/dist/core/extract/extractors/file.d.ts.map +1 -1
- package/dist/core/extract/extractors/file.js +34 -28
- package/dist/core/extract/extractors/file.js.map +1 -1
- package/dist/core/extract/extractors/intersection.d.ts +5 -2
- package/dist/core/extract/extractors/intersection.d.ts.map +1 -1
- package/dist/core/extract/extractors/intersection.js +13 -11
- package/dist/core/extract/extractors/intersection.js.map +1 -1
- package/dist/core/extract/extractors/lazy.d.ts +6 -3
- package/dist/core/extract/extractors/lazy.d.ts.map +1 -1
- package/dist/core/extract/extractors/lazy.js +21 -30
- package/dist/core/extract/extractors/lazy.js.map +1 -1
- package/dist/core/extract/extractors/literal.d.ts +5 -2
- package/dist/core/extract/extractors/literal.d.ts.map +1 -1
- package/dist/core/extract/extractors/literal.js +8 -1
- package/dist/core/extract/extractors/literal.js.map +1 -1
- package/dist/core/extract/extractors/map.d.ts +6 -3
- package/dist/core/extract/extractors/map.d.ts.map +1 -1
- package/dist/core/extract/extractors/map.js +10 -4
- package/dist/core/extract/extractors/map.js.map +1 -1
- package/dist/core/extract/extractors/nan.d.ts +5 -2
- package/dist/core/extract/extractors/nan.d.ts.map +1 -1
- package/dist/core/extract/extractors/nan.js +5 -1
- package/dist/core/extract/extractors/nan.js.map +1 -1
- package/dist/core/extract/extractors/never.d.ts +5 -2
- package/dist/core/extract/extractors/never.d.ts.map +1 -1
- package/dist/core/extract/extractors/never.js +5 -1
- package/dist/core/extract/extractors/never.js.map +1 -1
- package/dist/core/extract/extractors/null.d.ts +5 -2
- package/dist/core/extract/extractors/null.d.ts.map +1 -1
- package/dist/core/extract/extractors/null.js +5 -1
- package/dist/core/extract/extractors/null.js.map +1 -1
- package/dist/core/extract/extractors/nullable.d.ts +5 -2
- package/dist/core/extract/extractors/nullable.d.ts.map +1 -1
- package/dist/core/extract/extractors/nullable.js +7 -3
- package/dist/core/extract/extractors/nullable.js.map +1 -1
- package/dist/core/extract/extractors/number.d.ts +6 -3
- package/dist/core/extract/extractors/number.d.ts.map +1 -1
- package/dist/core/extract/extractors/number.js +35 -35
- package/dist/core/extract/extractors/number.js.map +1 -1
- package/dist/core/extract/extractors/object.d.ts +6 -3
- package/dist/core/extract/extractors/object.d.ts.map +1 -1
- package/dist/core/extract/extractors/object.js +49 -76
- package/dist/core/extract/extractors/object.js.map +1 -1
- package/dist/core/extract/extractors/optional.d.ts +5 -2
- package/dist/core/extract/extractors/optional.d.ts.map +1 -1
- package/dist/core/extract/extractors/optional.js +10 -12
- package/dist/core/extract/extractors/optional.js.map +1 -1
- package/dist/core/extract/extractors/pipe.d.ts +6 -3
- package/dist/core/extract/extractors/pipe.d.ts.map +1 -1
- package/dist/core/extract/extractors/pipe.js +40 -41
- package/dist/core/extract/extractors/pipe.js.map +1 -1
- package/dist/core/extract/extractors/readonly.d.ts +5 -2
- package/dist/core/extract/extractors/readonly.d.ts.map +1 -1
- package/dist/core/extract/extractors/readonly.js +10 -6
- package/dist/core/extract/extractors/readonly.js.map +1 -1
- package/dist/core/extract/extractors/record.d.ts +6 -3
- package/dist/core/extract/extractors/record.d.ts.map +1 -1
- package/dist/core/extract/extractors/record.js +24 -35
- package/dist/core/extract/extractors/record.js.map +1 -1
- package/dist/core/extract/extractors/set.d.ts +6 -3
- package/dist/core/extract/extractors/set.d.ts.map +1 -1
- package/dist/core/extract/extractors/set.js +36 -30
- package/dist/core/extract/extractors/set.js.map +1 -1
- package/dist/core/extract/extractors/string-bool.d.ts +7 -4
- package/dist/core/extract/extractors/string-bool.d.ts.map +1 -1
- package/dist/core/extract/extractors/string-bool.js +66 -84
- package/dist/core/extract/extractors/string-bool.js.map +1 -1
- package/dist/core/extract/extractors/string.d.ts +6 -3
- package/dist/core/extract/extractors/string.d.ts.map +1 -1
- package/dist/core/extract/extractors/string.js +34 -37
- package/dist/core/extract/extractors/string.js.map +1 -1
- package/dist/core/extract/extractors/symbol.d.ts +5 -2
- package/dist/core/extract/extractors/symbol.d.ts.map +1 -1
- package/dist/core/extract/extractors/symbol.js +5 -1
- package/dist/core/extract/extractors/symbol.js.map +1 -1
- package/dist/core/extract/extractors/template-literal.d.ts +6 -3
- package/dist/core/extract/extractors/template-literal.d.ts.map +1 -1
- package/dist/core/extract/extractors/template-literal.js +11 -6
- package/dist/core/extract/extractors/template-literal.js.map +1 -1
- package/dist/core/extract/extractors/tuple.d.ts +6 -3
- package/dist/core/extract/extractors/tuple.d.ts.map +1 -1
- package/dist/core/extract/extractors/tuple.js +10 -4
- package/dist/core/extract/extractors/tuple.js.map +1 -1
- package/dist/core/extract/extractors/undefined.d.ts +5 -2
- package/dist/core/extract/extractors/undefined.d.ts.map +1 -1
- package/dist/core/extract/extractors/undefined.js +5 -1
- package/dist/core/extract/extractors/undefined.js.map +1 -1
- package/dist/core/extract/extractors/union.d.ts +6 -3
- package/dist/core/extract/extractors/union.d.ts.map +1 -1
- package/dist/core/extract/extractors/union.js +38 -64
- package/dist/core/extract/extractors/union.js.map +1 -1
- package/dist/core/extract/extractors/unknown.d.ts +5 -2
- package/dist/core/extract/extractors/unknown.d.ts.map +1 -1
- package/dist/core/extract/extractors/unknown.js +5 -1
- package/dist/core/extract/extractors/unknown.js.map +1 -1
- package/dist/core/extract/extractors/void.d.ts +5 -2
- package/dist/core/extract/extractors/void.d.ts.map +1 -1
- package/dist/core/extract/extractors/void.js +5 -1
- package/dist/core/extract/extractors/void.js.map +1 -1
- package/dist/core/extract/fallback.d.ts +6 -3
- package/dist/core/extract/fallback.d.ts.map +1 -1
- package/dist/core/extract/fallback.js +21 -7
- package/dist/core/extract/fallback.js.map +1 -1
- package/dist/core/extract/index.d.ts +6 -4
- package/dist/core/extract/index.d.ts.map +1 -1
- package/dist/core/extract/index.js +15 -15
- package/dist/core/extract/index.js.map +1 -1
- package/dist/core/extract/registry.d.ts +39 -36
- package/dist/core/extract/registry.d.ts.map +1 -1
- package/dist/core/extract/registry.js +96 -123
- package/dist/core/extract/registry.js.map +1 -1
- package/dist/core/extract/types.d.ts +124 -120
- package/dist/core/extract/types.d.ts.map +1 -1
- package/dist/core/extract/types.js +0 -1
- package/dist/core/iife.d.ts +14 -15
- package/dist/core/iife.d.ts.map +1 -1
- package/dist/core/iife.js +182 -206
- package/dist/core/iife.js.map +1 -1
- package/dist/core/pipeline.d.ts +33 -30
- package/dist/core/pipeline.d.ts.map +1 -1
- package/dist/core/pipeline.js +67 -74
- package/dist/core/pipeline.js.map +1 -1
- package/dist/core/types.d.ts +430 -427
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js +0 -5
- package/dist/discovery.d.ts +8 -5
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +31 -32
- package/dist/discovery.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +2 -2
- package/dist/jit.d.ts +15 -42
- package/dist/jit.d.ts.map +1 -1
- package/dist/jit.js +187 -216
- package/dist/jit.js.map +1 -1
- package/dist/loader.d.ts +14 -11
- package/dist/loader.d.ts.map +1 -1
- package/dist/loader.js +226 -258
- package/dist/loader.js.map +1 -1
- package/dist/static-filter.d.ts +4 -1
- package/dist/static-filter.d.ts.map +1 -1
- package/dist/static-filter.js +240 -367
- package/dist/static-filter.js.map +1 -1
- package/dist/swc.d.ts +38 -35
- package/dist/swc.d.ts.map +1 -1
- package/dist/swc.js +83 -76
- package/dist/swc.js.map +1 -1
- package/dist/unplugin/bun.d.ts +5 -2
- package/dist/unplugin/bun.d.ts.map +1 -1
- package/dist/unplugin/bun.js +5 -1
- package/dist/unplugin/bun.js.map +1 -1
- package/dist/unplugin/dep-graph.d.ts +10 -7
- package/dist/unplugin/dep-graph.d.ts.map +1 -1
- package/dist/unplugin/dep-graph.js +216 -252
- package/dist/unplugin/dep-graph.js.map +1 -1
- package/dist/unplugin/disk-cache.d.ts +121 -92
- package/dist/unplugin/disk-cache.d.ts.map +1 -1
- package/dist/unplugin/disk-cache.js +425 -444
- package/dist/unplugin/disk-cache.js.map +1 -1
- package/dist/unplugin/edits.d.ts +15 -12
- package/dist/unplugin/edits.d.ts.map +1 -1
- package/dist/unplugin/edits.js +24 -37
- package/dist/unplugin/edits.js.map +1 -1
- package/dist/unplugin/esbuild.d.ts +5 -2
- package/dist/unplugin/esbuild.d.ts.map +1 -1
- package/dist/unplugin/esbuild.js +5 -1
- package/dist/unplugin/esbuild.js.map +1 -1
- package/dist/unplugin/farm.d.ts +5 -2
- package/dist/unplugin/farm.d.ts.map +1 -1
- package/dist/unplugin/farm.js +5 -1
- package/dist/unplugin/farm.js.map +1 -1
- package/dist/unplugin/hoist-compile.d.ts +14 -42
- package/dist/unplugin/hoist-compile.d.ts.map +1 -1
- package/dist/unplugin/hoist-compile.js +67 -112
- package/dist/unplugin/hoist-compile.js.map +1 -1
- package/dist/unplugin/hoist.d.ts +56 -54
- package/dist/unplugin/hoist.d.ts.map +1 -1
- package/dist/unplugin/hoist.js +945 -1170
- package/dist/unplugin/hoist.js.map +1 -1
- package/dist/unplugin/index.d.ts +5 -3
- package/dist/unplugin/index.d.ts.map +1 -1
- package/dist/unplugin/index.js +175 -250
- package/dist/unplugin/index.js.map +1 -1
- package/dist/unplugin/rolldown.d.ts +5 -2
- package/dist/unplugin/rolldown.d.ts.map +1 -1
- package/dist/unplugin/rolldown.js +5 -1
- package/dist/unplugin/rolldown.js.map +1 -1
- package/dist/unplugin/rollup.d.ts +5 -2
- package/dist/unplugin/rollup.d.ts.map +1 -1
- package/dist/unplugin/rollup.js +5 -1
- package/dist/unplugin/rollup.js.map +1 -1
- package/dist/unplugin/rsbuild.d.ts +6 -3
- package/dist/unplugin/rsbuild.d.ts.map +1 -1
- package/dist/unplugin/rsbuild.js +15 -11
- package/dist/unplugin/rsbuild.js.map +1 -1
- package/dist/unplugin/rspack.d.ts +5 -2
- package/dist/unplugin/rspack.d.ts.map +1 -1
- package/dist/unplugin/rspack.js +5 -1
- package/dist/unplugin/rspack.js.map +1 -1
- package/dist/unplugin/transform.d.ts +30 -27
- package/dist/unplugin/transform.d.ts.map +1 -1
- package/dist/unplugin/transform.js +433 -620
- package/dist/unplugin/transform.js.map +1 -1
- package/dist/unplugin/types.d.ts +189 -184
- package/dist/unplugin/types.d.ts.map +1 -1
- package/dist/unplugin/types.js +22 -18
- package/dist/unplugin/types.js.map +1 -1
- package/dist/unplugin/virtual.d.ts +11 -8
- package/dist/unplugin/virtual.d.ts.map +1 -1
- package/dist/unplugin/virtual.js +65 -83
- package/dist/unplugin/virtual.js.map +1 -1
- package/dist/unplugin/vite.d.ts +5 -2
- package/dist/unplugin/vite.d.ts.map +1 -1
- package/dist/unplugin/vite.js +5 -1
- package/dist/unplugin/vite.js.map +1 -1
- package/dist/unplugin/webpack.d.ts +5 -2
- package/dist/unplugin/webpack.d.ts.map +1 -1
- package/dist/unplugin/webpack.js +5 -1
- package/dist/unplugin/webpack.js.map +1 -1
- package/package.json +23 -14
- package/dist/cli/index.d.ts.map +0 -1
- package/dist/core/extract/types.js.map +0 -1
- package/dist/core/types.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
|
@@ -1,205 +1,180 @@
|
|
|
1
|
-
import { declareFastTemps, emitConstant, escapeString, extendPath, hasMutation, literalToJs
|
|
1
|
+
import { declareFastTemps, emitConstant, escapeString, extendPath, hasMutation, literalToJs } from "../context.js";
|
|
2
2
|
import { emit } from "../emit.js";
|
|
3
3
|
import { invalidType } from "../emit-issue.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
//#region src/core/codegen/schemas/discriminated-union.ts
|
|
5
|
+
function slowDiscriminatedUnion(ir, g) {
|
|
6
|
+
const discKey = escapeString(ir.discriminator);
|
|
7
|
+
let code = emit`
|
|
7
8
|
if(typeof ${g.input}!=="object"||${g.input}===null||Array.isArray(${g.input})){
|
|
8
9
|
${invalidType(g, "object")}
|
|
9
10
|
}else{`;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
const objVar = g.temp("du");
|
|
12
|
+
code += `var ${objVar}=${g.input};switch(${objVar}[${discKey}]){`;
|
|
13
|
+
for (const { value, option: index } of ir.cases) {
|
|
14
|
+
const option = ir.options[index];
|
|
15
|
+
code += emit`
|
|
15
16
|
case ${literalToJs(value)}:
|
|
16
|
-
${g.visit(option, {
|
|
17
|
+
${g.visit(option, {
|
|
18
|
+
input: objVar,
|
|
19
|
+
output: objVar
|
|
20
|
+
})}
|
|
17
21
|
break;`;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
}
|
|
23
|
+
const validValues = ir.cases.map((c) => literalToJs(c.value)).join(",");
|
|
24
|
+
const msgProp = g.typeMsg === void 0 ? "" : `,message:${JSON.stringify(g.typeMsg)}`;
|
|
25
|
+
code += emit`
|
|
22
26
|
default:
|
|
23
27
|
${g.issues}.push({code:"invalid_union",errors:[],note:"No matching discriminator",discriminator:${discKey},options:[${validValues}]${msgProp},input:${g.input},path:${extendPath(g.path, discKey)}});
|
|
24
28
|
}`;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// reassigned into objVar and stranded there; without this write-back the caller
|
|
29
|
-
// returns the ORIGINAL input by reference and the mutation is silently lost.
|
|
30
|
-
// Gated on mutation so a pure-validation union stays a zero-write pass-through
|
|
31
|
-
// (objVar still aliases the input). On the no-match/failure paths objVar equals
|
|
32
|
-
// the input, so the write is a harmless self-assignment.
|
|
33
|
-
if (ir.options.some(hasMutation)) {
|
|
34
|
-
code += `${g.output}=${objVar};`;
|
|
35
|
-
}
|
|
36
|
-
code += `}`;
|
|
37
|
-
return `${code}\n`;
|
|
29
|
+
if (ir.options.some(hasMutation)) code += `${g.output}=${objVar};`;
|
|
30
|
+
code += `}`;
|
|
31
|
+
return `${code}\n`;
|
|
38
32
|
}
|
|
39
33
|
/**
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
//
|
|
84
|
-
// `typeof t==="string"` is load-bearing: property access would coerce a
|
|
85
|
-
// non-string discriminator (an object's toString, a number) into a key that
|
|
86
|
-
// could hit a case whose own discriminator check the switch has stripped.
|
|
87
|
-
// Strict equality never matched those, so the guard keeps the verdict.
|
|
88
|
-
const caseStrs = [];
|
|
89
|
-
for (const [optionIndex, ordinal] of table.ordinals) {
|
|
90
|
-
const check = body.visit(options[optionIndex], { discSkipKey: discriminator });
|
|
91
|
-
if (check === null)
|
|
92
|
-
return null;
|
|
93
|
-
caseStrs.push(`case ${ordinal}:return ${check};`);
|
|
94
|
-
}
|
|
95
|
-
const tableVar = emitConstant(g.ctx, "dt", table.initializer);
|
|
96
|
-
const t = g.temp("dv");
|
|
97
|
-
g.ctx.preamble.push(`function ${helperName}(${helperParam}){${declareFastTemps(body.scope)}var ${t}=${helperParam}[${discKey}];` +
|
|
98
|
-
`switch(typeof ${t}==="string"?${tableVar}[${t}]:0){${caseStrs.join("")}default:return false;}}`);
|
|
99
|
-
}
|
|
100
|
-
return `typeof ${x}==="object"&&${x}!==null&&!Array.isArray(${x})&&${helperName}(${x})`;
|
|
34
|
+
* Emit an O(1) switch-dispatch fast-check for a discriminated union — real
|
|
35
|
+
* (`z.discriminatedUnion`) or one detected inside a plain `z.union`
|
|
36
|
+
* (see {@link detectUnionDiscriminator}). Both share this so the detected case
|
|
37
|
+
* inherits the size-gating and the per-case guard strip.
|
|
38
|
+
*
|
|
39
|
+
* `discSkipKey` tells each object option to drop its own type-guard and
|
|
40
|
+
* discriminator re-check: the caller's guard (`typeof x==="object"&&…` below)
|
|
41
|
+
* already proved object-ness, and the matched switch case has fixed the
|
|
42
|
+
* discriminator value, so re-emitting either is pure redundancy the optimizer
|
|
43
|
+
* only removes when it inlines this helper — which a union large enough to
|
|
44
|
+
* matter won't. Routed through the normal `visit` so size-gated extraction
|
|
45
|
+
* still bounds the switch (the strip survives into any hoisted helper);
|
|
46
|
+
* non-object options ignore the hint and keep their own guard.
|
|
47
|
+
*
|
|
48
|
+
* Returns null if any option is fast-path-ineligible.
|
|
49
|
+
*/
|
|
50
|
+
function emitFastDiscriminatedSwitch(g, discriminator, cases, options) {
|
|
51
|
+
const x = g.input;
|
|
52
|
+
const discKey = escapeString(discriminator);
|
|
53
|
+
const helperName = g.temp("du");
|
|
54
|
+
const helperParam = g.temp("dx");
|
|
55
|
+
const body = g.scoped(helperParam);
|
|
56
|
+
const table = stringDispatchTable(cases);
|
|
57
|
+
if (table === null) {
|
|
58
|
+
const caseStrs = [];
|
|
59
|
+
for (const { value, option: index } of cases) {
|
|
60
|
+
const check = body.visit(options[index], { discSkipKey: discriminator });
|
|
61
|
+
if (check === null) return null;
|
|
62
|
+
caseStrs.push(`case ${literalToJs(value)}:return ${check};`);
|
|
63
|
+
}
|
|
64
|
+
g.ctx.preamble.push(`function ${helperName}(${helperParam}){${declareFastTemps(body.scope)}switch(${helperParam}[${discKey}]){${caseStrs.join("")}default:return false;}}`);
|
|
65
|
+
} else {
|
|
66
|
+
const caseStrs = [];
|
|
67
|
+
for (const [optionIndex, ordinal] of table.ordinals) {
|
|
68
|
+
const check = body.visit(options[optionIndex], { discSkipKey: discriminator });
|
|
69
|
+
if (check === null) return null;
|
|
70
|
+
caseStrs.push(`case ${ordinal}:return ${check};`);
|
|
71
|
+
}
|
|
72
|
+
const tableVar = emitConstant(g.ctx, "dt", table.initializer);
|
|
73
|
+
const t = g.temp("dv");
|
|
74
|
+
g.ctx.preamble.push(`function ${helperName}(${helperParam}){${declareFastTemps(body.scope)}var ${t}=${helperParam}[${discKey}];switch(typeof ${t}==="string"?${tableVar}[${t}]:0){${caseStrs.join("")}default:return false;}}`);
|
|
75
|
+
}
|
|
76
|
+
return `typeof ${x}==="object"&&${x}!==null&&!Array.isArray(${x})&&${helperName}(${x})`;
|
|
101
77
|
}
|
|
102
78
|
/**
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
79
|
+
* Minimum case count for ordinal dispatch. Measured crossover is 3: at 2 cases
|
|
80
|
+
* the string switch is one comparison and beats the extra table lookup
|
|
81
|
+
* (4.1 ns vs 4.5), at 3 the table already wins (6.1 vs 4.6).
|
|
82
|
+
*/
|
|
107
83
|
const MIN_TABLE_DISPATCH = 3;
|
|
108
84
|
/**
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
85
|
+
* Build the `{value: ordinal}` dispatch table for a set of cases, or null when
|
|
86
|
+
* the plain string switch should be kept. Requires every discriminator value to
|
|
87
|
+
* be a string — mixed types would collide once coerced to property keys (`5`
|
|
88
|
+
* and `"5"`) — and excludes `__proto__`, which an object literal cannot hold as
|
|
89
|
+
* an own key. Values that select the SAME option share one ordinal, so a
|
|
90
|
+
* multi-value literal emits its check once instead of per value.
|
|
91
|
+
*/
|
|
116
92
|
function stringDispatchTable(cases) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
93
|
+
if (cases.length < MIN_TABLE_DISPATCH) return null;
|
|
94
|
+
if (!cases.every((c) => typeof c.value === "string" && c.value !== "__proto__")) return null;
|
|
95
|
+
const ordinals = /* @__PURE__ */ new Map();
|
|
96
|
+
const entries = [];
|
|
97
|
+
for (const { value, option } of cases) {
|
|
98
|
+
let ordinal = ordinals.get(option);
|
|
99
|
+
if (ordinal === void 0) {
|
|
100
|
+
ordinal = ordinals.size + 1;
|
|
101
|
+
ordinals.set(option, ordinal);
|
|
102
|
+
}
|
|
103
|
+
entries.push(`${escapeString(value)}:${ordinal}`);
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
initializer: `{${entries.join(",")}}`,
|
|
107
|
+
ordinals
|
|
108
|
+
};
|
|
132
109
|
}
|
|
133
|
-
|
|
134
|
-
|
|
110
|
+
function fastDiscriminatedUnion(ir, g) {
|
|
111
|
+
return emitFastDiscriminatedSwitch(g, ir.discriminator, ir.cases, ir.options);
|
|
135
112
|
}
|
|
136
113
|
/**
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
114
|
+
* Minimum option count for rewriting a plain `z.union` to switch dispatch. Below
|
|
115
|
+
* this the switch helper's fixed call overhead loses to a fully-inlined
|
|
116
|
+
* `||`-chain that V8 keeps flat: measured crossover is ~4 options (n=3 can hit
|
|
117
|
+
* 0.6x — a regression — while n=5 is 1.27x and n=32 is 2.34x), so 5 captures the
|
|
118
|
+
* stable wins with margin and never regresses a small union. Real
|
|
119
|
+
* `z.discriminatedUnion` is unaffected — it dispatches via switch by construction
|
|
120
|
+
* regardless of size.
|
|
121
|
+
*/
|
|
145
122
|
const MIN_AUTO_DISCRIMINATE_OPTIONS = 5;
|
|
146
123
|
/** Values that switch correctly under `===` (excludes `undefined` and `NaN`). */
|
|
147
124
|
function isSwitchableDiscriminant(v) {
|
|
148
|
-
|
|
149
|
-
typeof v === "string" ||
|
|
150
|
-
typeof v === "boolean" ||
|
|
151
|
-
typeof v === "bigint" ||
|
|
152
|
-
(typeof v === "number" && !Number.isNaN(v)));
|
|
125
|
+
return v === null || typeof v === "string" || typeof v === "boolean" || typeof v === "bigint" || typeof v === "number" && !Number.isNaN(v);
|
|
153
126
|
}
|
|
154
127
|
/**
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return null;
|
|
128
|
+
* Detect whether a plain (untagged) `z.union` is *structurally* a discriminated
|
|
129
|
+
* union, so its fast path can use O(1) switch dispatch instead of probing every
|
|
130
|
+
* arm. Returns the discriminator + dispatch table, or null to keep the
|
|
131
|
+
* `||`-chain.
|
|
132
|
+
*
|
|
133
|
+
* Requires (proving the switch accepts exactly what the `||`-chain would): every
|
|
134
|
+
* option is a plain object that pins one shared key to a REQUIRED literal
|
|
135
|
+
* (`prop.type === "literal"` — an optional/non-literal key is rejected), and the
|
|
136
|
+
* literal values are pairwise DISJOINT across options. Disjointness is the crux:
|
|
137
|
+
* it guarantees at most one option can accept any given input, so dispatching to
|
|
138
|
+
* that single option is equivalent to trying them all. Any value shared by two
|
|
139
|
+
* options (ambiguous), a non-switchable value (`undefined`/`NaN`), or a
|
|
140
|
+
* non-object option makes detection bail to the safe `||`-chain.
|
|
141
|
+
*
|
|
142
|
+
* Fast-path only: the slow path keeps `z.union`'s sequential trial and its
|
|
143
|
+
* `invalid_union` error shape, so failure output stays byte-identical to Zod.
|
|
144
|
+
*/
|
|
145
|
+
function detectUnionDiscriminator(options) {
|
|
146
|
+
if (options.length < MIN_AUTO_DISCRIMINATE_OPTIONS) return null;
|
|
147
|
+
const objects = [];
|
|
148
|
+
for (const option of options) {
|
|
149
|
+
if (option.type !== "object") return null;
|
|
150
|
+
objects.push(option);
|
|
151
|
+
}
|
|
152
|
+
const first = objects[0];
|
|
153
|
+
if (first === void 0) return null;
|
|
154
|
+
candidate: for (const key of Object.keys(first.properties)) {
|
|
155
|
+
const seen = /* @__PURE__ */ new Set();
|
|
156
|
+
const cases = [];
|
|
157
|
+
for (const [i, object] of objects.entries()) {
|
|
158
|
+
const prop = object.properties[key];
|
|
159
|
+
if (prop === void 0 || prop.type !== "literal") continue candidate;
|
|
160
|
+
for (const value of prop.values) {
|
|
161
|
+
if (!isSwitchableDiscriminant(value)) continue candidate;
|
|
162
|
+
if (seen.has(value)) continue candidate;
|
|
163
|
+
seen.add(value);
|
|
164
|
+
cases.push({
|
|
165
|
+
value,
|
|
166
|
+
option: i
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
discriminator: key,
|
|
172
|
+
cases
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
204
176
|
}
|
|
177
|
+
//#endregion
|
|
178
|
+
export { detectUnionDiscriminator, emitFastDiscriminatedSwitch, fastDiscriminatedUnion, slowDiscriminatedUnion };
|
|
179
|
+
|
|
205
180
|
//# sourceMappingURL=discriminated-union.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discriminated-union.js","sourceRoot":"","sources":["../../../../src/core/codegen/schemas/discriminated-union.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,gBAAgB,EAChB,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,WAAW,EACX,WAAW,GACZ,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAK/C,MAAM,UAAU,sBAAsB,CACpC,EAA6C,EAC7C,CAAU;IAEV,MAAM,OAAO,GAAG,YAAY,CAAC,EAAE,CAAC,aAAa,CAAC,CAAC;IAE/C,IAAI,IAAI,GAAG,IAAI,CAAA;gBACD,CAAC,CAAC,KAAK,gBAAgB,CAAC,CAAC,KAAK,0BAA0B,CAAC,CAAC,KAAK;QACvE,WAAW,CAAC,CAAC,EAAE,QAAQ,CAAC;WACrB,CAAC;IAEV,MAAM,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC5B,IAAI,IAAI,OAAO,MAAM,IAAI,CAAC,CAAC,KAAK,WAAW,MAAM,IAAI,OAAO,KAAK,CAAC;IAElE,KAAK,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC;QAChD,MAAM,MAAM,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAa,CAAC;QAC7C,IAAI,IAAI,IAAI,CAAA;aACH,WAAW,CAAC,KAAK,CAAC;UACrB,CAAC,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;eAC7C,CAAC;IACd,CAAC;IAED,MAAM,WAAW,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,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AAC,CAAC;IACvB,CAAC;IACD,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IACzB,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC,CAAC,qCAAqC;IAE3E,wEAAwE;IACxE,SAAS,EAAE,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE,CAAC;QAC3D,MAAM,IAAI,GAAG,IAAI,GAAG,EAA6C,CAAC;QAClE,MAAM,KAAK,GAAwB,EAAE,CAAC;QACtC,KAAK,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,IAAI,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;YAC5C,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;YACpC,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS;gBAAE,SAAS,SAAS,CAAC;YACtE,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;gBAChC,IAAI,CAAC,wBAAwB,CAAC,KAAK,CAAC;oBAAE,SAAS,SAAS,CAAC;gBACzD,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC;oBAAE,SAAS,SAAS,CAAC,CAAC,oCAAoC;gBAC7E,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;gBAChB,KAAK,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;YACnC,CAAC;QACH,CAAC;QACD,OAAO,EAAE,aAAa,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;IACvC,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
1
|
+
{"version":3,"file":"discriminated-union.js","names":[],"sources":["../../../../src/core/codegen/schemas/discriminated-union.ts"],"sourcesContent":["import type { DiscriminatedUnionIR, ObjectIR, SchemaIR } from \"../../types.js\";\nimport type { FastGen, SlowGen } from \"../context.js\";\nimport {\n declareFastTemps,\n emitConstant,\n escapeString,\n extendPath,\n hasMutation,\n literalToJs,\n} from \"../context.js\";\nimport { emit } from \"../emit.js\";\nimport { invalidType } from \"../emit-issue.js\";\n\n/** One `discriminator value → option index` dispatch entry. */\ntype DiscriminatorCase = DiscriminatedUnionIR[\"cases\"][number];\n\nexport function slowDiscriminatedUnion(\n ir: SchemaIR & { type: \"discriminatedUnion\" },\n g: SlowGen,\n): string {\n const discKey = escapeString(ir.discriminator);\n\n let code = emit`\n if(typeof ${g.input}!==\"object\"||${g.input}===null||Array.isArray(${g.input})){\n ${invalidType(g, \"object\")}\n }else{`;\n\n const objVar = g.temp(\"du\");\n code += `var ${objVar}=${g.input};switch(${objVar}[${discKey}]){`;\n\n for (const { value, option: index } of ir.cases) {\n const option = ir.options[index] as SchemaIR;\n code += emit`\n case ${literalToJs(value)}:\n ${g.visit(option, { input: objVar, output: objVar })}\n break;`;\n }\n\n const validValues = ir.cases.map((c) => literalToJs(c.value)).join(\",\");\n const msgProp = g.typeMsg === undefined ? \"\" : `,message:${JSON.stringify(g.typeMsg)}`;\n code += emit`\n default:\n ${g.issues}.push({code:\"invalid_union\",errors:[],note:\"No matching discriminator\",discriminator:${discKey},options:[${validValues}]${msgProp},input:${g.input},path:${extendPath(g.path, discKey)}});\n }`;\n // Propagate option-applied mutations (defaults, coercions, transforms,\n // overwrite checks, stringbool) back to the output location. Each option is\n // visited with output:objVar — a fresh local — so a mutating option's clone is\n // reassigned into objVar and stranded there; without this write-back the caller\n // returns the ORIGINAL input by reference and the mutation is silently lost.\n // Gated on mutation so a pure-validation union stays a zero-write pass-through\n // (objVar still aliases the input). On the no-match/failure paths objVar equals\n // the input, so the write is a harmless self-assignment.\n if (ir.options.some(hasMutation)) {\n code += `${g.output}=${objVar};`;\n }\n code += `}`;\n return `${code}\\n`;\n}\n\n/**\n * Emit an O(1) switch-dispatch fast-check for a discriminated union — real\n * (`z.discriminatedUnion`) or one detected inside a plain `z.union`\n * (see {@link detectUnionDiscriminator}). Both share this so the detected case\n * inherits the size-gating and the per-case guard strip.\n *\n * `discSkipKey` tells each object option to drop its own type-guard and\n * discriminator re-check: the caller's guard (`typeof x===\"object\"&&…` below)\n * already proved object-ness, and the matched switch case has fixed the\n * discriminator value, so re-emitting either is pure redundancy the optimizer\n * only removes when it inlines this helper — which a union large enough to\n * matter won't. Routed through the normal `visit` so size-gated extraction\n * still bounds the switch (the strip survives into any hoisted helper);\n * non-object options ignore the hint and keep their own guard.\n *\n * Returns null if any option is fast-path-ineligible.\n */\nexport function emitFastDiscriminatedSwitch(\n g: FastGen,\n discriminator: string,\n cases: readonly DiscriminatorCase[],\n options: readonly SchemaIR[],\n): string | null {\n const x = g.input;\n const discKey = escapeString(discriminator);\n const helperName = g.temp(\"du\");\n const helperParam = g.temp(\"dx\");\n\n // The switch body is its own function: size-gate the options against the cap\n // in a fresh scope, otherwise many small options accumulate into the caller's\n // scope while this helper itself grows unbounded past the TurboFan budget.\n const body = g.scoped(helperParam);\n const table = stringDispatchTable(cases);\n\n if (table === null) {\n const caseStrs: string[] = [];\n for (const { value, option: index } of cases) {\n const check = body.visit(options[index] as SchemaIR, { discSkipKey: discriminator });\n if (check === null) return null;\n caseStrs.push(`case ${literalToJs(value)}:return ${check};`);\n }\n g.ctx.preamble.push(\n `function ${helperName}(${helperParam}){${declareFastTemps(body.scope)}switch(${helperParam}[${discKey}]){${caseStrs.join(\"\")}default:return false;}}`,\n );\n } else {\n // Dispatch through a string→ordinal table, then switch on the ordinal. V8\n // compiles a switch over STRING labels as sequential `===` comparisons, so\n // the plain form costs ~0.5 ns per preceding case: measured 4.1 ns (2\n // variants), 11.4 (8), 52.1 (80). A dense integer switch becomes a jump\n // table, leaving the lookup flat — 5.9 ns (8) and 15.1 (80), i.e. 1.9x to\n // 3.5x, for a table that adds ~1.3% to a union's generated bytes.\n //\n // `typeof t===\"string\"` is load-bearing: property access would coerce a\n // non-string discriminator (an object's toString, a number) into a key that\n // could hit a case whose own discriminator check the switch has stripped.\n // Strict equality never matched those, so the guard keeps the verdict.\n const caseStrs: string[] = [];\n for (const [optionIndex, ordinal] of table.ordinals) {\n const check = body.visit(options[optionIndex] as SchemaIR, { discSkipKey: discriminator });\n if (check === null) return null;\n caseStrs.push(`case ${ordinal}:return ${check};`);\n }\n const tableVar = emitConstant(g.ctx, \"dt\", table.initializer);\n const t = g.temp(\"dv\");\n g.ctx.preamble.push(\n `function ${helperName}(${helperParam}){${declareFastTemps(body.scope)}var ${t}=${helperParam}[${discKey}];` +\n `switch(typeof ${t}===\"string\"?${tableVar}[${t}]:0){${caseStrs.join(\"\")}default:return false;}}`,\n );\n }\n\n return `typeof ${x}===\"object\"&&${x}!==null&&!Array.isArray(${x})&&${helperName}(${x})`;\n}\n\n/**\n * Minimum case count for ordinal dispatch. Measured crossover is 3: at 2 cases\n * the string switch is one comparison and beats the extra table lookup\n * (4.1 ns vs 4.5), at 3 the table already wins (6.1 vs 4.6).\n */\nconst MIN_TABLE_DISPATCH = 3;\n\n/**\n * Build the `{value: ordinal}` dispatch table for a set of cases, or null when\n * the plain string switch should be kept. Requires every discriminator value to\n * be a string — mixed types would collide once coerced to property keys (`5`\n * and `\"5\"`) — and excludes `__proto__`, which an object literal cannot hold as\n * an own key. Values that select the SAME option share one ordinal, so a\n * multi-value literal emits its check once instead of per value.\n */\nfunction stringDispatchTable(\n cases: readonly DiscriminatorCase[],\n): { initializer: string; ordinals: Map<number, number> } | null {\n if (cases.length < MIN_TABLE_DISPATCH) return null;\n if (!cases.every((c) => typeof c.value === \"string\" && c.value !== \"__proto__\")) return null;\n\n const ordinals = new Map<number, number>();\n const entries: string[] = [];\n for (const { value, option } of cases) {\n let ordinal = ordinals.get(option);\n if (ordinal === undefined) {\n ordinal = ordinals.size + 1;\n ordinals.set(option, ordinal);\n }\n entries.push(`${escapeString(value as string)}:${ordinal}`);\n }\n return { initializer: `{${entries.join(\",\")}}`, ordinals };\n}\n\nexport function fastDiscriminatedUnion(ir: DiscriminatedUnionIR, g: FastGen): string | null {\n return emitFastDiscriminatedSwitch(g, ir.discriminator, ir.cases, ir.options);\n}\n\n/**\n * Minimum option count for rewriting a plain `z.union` to switch dispatch. Below\n * this the switch helper's fixed call overhead loses to a fully-inlined\n * `||`-chain that V8 keeps flat: measured crossover is ~4 options (n=3 can hit\n * 0.6x — a regression — while n=5 is 1.27x and n=32 is 2.34x), so 5 captures the\n * stable wins with margin and never regresses a small union. Real\n * `z.discriminatedUnion` is unaffected — it dispatches via switch by construction\n * regardless of size.\n */\nconst MIN_AUTO_DISCRIMINATE_OPTIONS = 5;\n\n/** Values that switch correctly under `===` (excludes `undefined` and `NaN`). */\nfunction isSwitchableDiscriminant(\n v: string | number | boolean | null | bigint | undefined,\n): v is string | number | boolean | bigint | null {\n return (\n v === null ||\n typeof v === \"string\" ||\n typeof v === \"boolean\" ||\n typeof v === \"bigint\" ||\n (typeof v === \"number\" && !Number.isNaN(v))\n );\n}\n\n/**\n * Detect whether a plain (untagged) `z.union` is *structurally* a discriminated\n * union, so its fast path can use O(1) switch dispatch instead of probing every\n * arm. Returns the discriminator + dispatch table, or null to keep the\n * `||`-chain.\n *\n * Requires (proving the switch accepts exactly what the `||`-chain would): every\n * option is a plain object that pins one shared key to a REQUIRED literal\n * (`prop.type === \"literal\"` — an optional/non-literal key is rejected), and the\n * literal values are pairwise DISJOINT across options. Disjointness is the crux:\n * it guarantees at most one option can accept any given input, so dispatching to\n * that single option is equivalent to trying them all. Any value shared by two\n * options (ambiguous), a non-switchable value (`undefined`/`NaN`), or a\n * non-object option makes detection bail to the safe `||`-chain.\n *\n * Fast-path only: the slow path keeps `z.union`'s sequential trial and its\n * `invalid_union` error shape, so failure output stays byte-identical to Zod.\n */\nexport function detectUnionDiscriminator(\n options: readonly SchemaIR[],\n): { discriminator: string; cases: DiscriminatorCase[] } | null {\n if (options.length < MIN_AUTO_DISCRIMINATE_OPTIONS) return null;\n const objects: ObjectIR[] = [];\n for (const option of options) {\n if (option.type !== \"object\") return null;\n objects.push(option);\n }\n const first = objects[0];\n if (first === undefined) return null; // unreachable (length checked above)\n\n // Only keys present in the first option can be shared by all; try each.\n candidate: for (const key of Object.keys(first.properties)) {\n const seen = new Set<string | number | boolean | bigint | null>();\n const cases: DiscriminatorCase[] = [];\n for (const [i, object] of objects.entries()) {\n const prop = object.properties[key];\n if (prop === undefined || prop.type !== \"literal\") continue candidate;\n for (const value of prop.values) {\n if (!isSwitchableDiscriminant(value)) continue candidate;\n if (seen.has(value)) continue candidate; // shared value → ambiguous dispatch\n seen.add(value);\n cases.push({ value, option: i });\n }\n }\n return { discriminator: key, cases };\n }\n return null;\n}\n"],"mappings":";;;;AAgBA,SAAgB,uBACd,IACA,GACQ;CACR,MAAM,UAAU,aAAa,GAAG,aAAa;CAE7C,IAAI,OAAO,IAAI;gBACD,EAAE,MAAM,eAAe,EAAE,MAAM,yBAAyB,EAAE,MAAM;QACxE,YAAY,GAAG,QAAQ,EAAE;;CAG/B,MAAM,SAAS,EAAE,KAAK,IAAI;CAC1B,QAAQ,OAAO,OAAO,GAAG,EAAE,MAAM,UAAU,OAAO,GAAG,QAAQ;CAE7D,KAAK,MAAM,EAAE,OAAO,QAAQ,WAAW,GAAG,OAAO;EAC/C,MAAM,SAAS,GAAG,QAAQ;EAC1B,QAAQ,IAAI;aACH,YAAY,KAAK,EAAE;UACtB,EAAE,MAAM,QAAQ;GAAE,OAAO;GAAQ,QAAQ;EAAO,CAAC,EAAE;;CAE3D;CAEA,MAAM,cAAc,GAAG,MAAM,KAAK,MAAM,YAAY,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG;CACtE,MAAM,UAAU,EAAE,YAAY,KAAA,IAAY,KAAK,YAAY,KAAK,UAAU,EAAE,OAAO;CACnF,QAAQ,IAAI;;QAEN,EAAE,OAAO,uFAAuF,QAAQ,YAAY,YAAY,GAAG,QAAQ,SAAS,EAAE,MAAM,QAAQ,WAAW,EAAE,MAAM,OAAO,EAAE;;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,KAAK,MAAM;CACrB;CACA,MAAM,QAAQ,QAAQ;CACtB,IAAI,UAAU,KAAA,GAAW,OAAO;CAGhC,WAAW,KAAK,MAAM,OAAO,OAAO,KAAK,MAAM,UAAU,GAAG;EAC1D,MAAM,uBAAO,IAAI,IAA+C;EAChE,MAAM,QAA6B,CAAC;EACpC,KAAK,MAAM,CAAC,GAAG,WAAW,QAAQ,QAAQ,GAAG;GAC3C,MAAM,OAAO,OAAO,WAAW;GAC/B,IAAI,SAAS,KAAA,KAAa,KAAK,SAAS,WAAW,SAAS;GAC5D,KAAK,MAAM,SAAS,KAAK,QAAQ;IAC/B,IAAI,CAAC,yBAAyB,KAAK,GAAG,SAAS;IAC/C,IAAI,KAAK,IAAI,KAAK,GAAG,SAAS;IAC9B,KAAK,IAAI,KAAK;IACd,MAAM,KAAK;KAAE;KAAO,QAAQ;IAAE,CAAC;GACjC;EACF;EACA,OAAO;GAAE,eAAe;GAAK;EAAM;CACrC;CACA,OAAO;AACT"}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { RefineEffectCheckIR, SuperRefineEffectCheckIR, TransformEffectIR } from "../../types.js";
|
|
2
|
+
import { FastGen, SlowGen } from "../context.js";
|
|
3
|
+
//#region src/core/codegen/schemas/effect.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* Generate code for a TransformEffectIR node.
|
|
5
6
|
* Validates the inner schema, then applies the transform function and writes back the result.
|
|
6
7
|
*/
|
|
7
|
-
|
|
8
|
+
declare function slowEffect(ir: TransformEffectIR, g: SlowGen): string;
|
|
8
9
|
/**
|
|
9
10
|
* Generate code for a RefineEffectCheckIR (inline refine function call).
|
|
10
11
|
* Called from string/number/object check loops when a refine_effect is encountered.
|
|
@@ -13,12 +14,12 @@ export declare function slowEffect(ir: TransformEffectIR, g: SlowGen): string;
|
|
|
13
14
|
* @param expr - The expression to validate (may differ from g.input, e.g. objVar in object generators)
|
|
14
15
|
* @param g - SlowGen context (provides path, issues)
|
|
15
16
|
*/
|
|
16
|
-
|
|
17
|
+
declare function refineCheck(check: RefineEffectCheckIR, expr: string, g: SlowGen): string;
|
|
17
18
|
/**
|
|
18
19
|
* Fast-path test for a SuperRefineEffectCheckIR: a boolean term reporting that
|
|
19
20
|
* the callback added no issue and left the value alone (see ZC_SR_OK_DECL).
|
|
20
21
|
*/
|
|
21
|
-
|
|
22
|
+
declare function superRefineFastTest(check: SuperRefineEffectCheckIR, expr: string, g: FastGen): string;
|
|
22
23
|
/**
|
|
23
24
|
* Generate code for a SuperRefineEffectCheckIR: call the payload-taking
|
|
24
25
|
* callback with a synthesized `{ value, issues }` and merge what it collected.
|
|
@@ -31,5 +32,7 @@ export declare function superRefineFastTest(check: SuperRefineEffectCheckIR, exp
|
|
|
31
32
|
* (the node reports as mutating, see hasMutation), so the write-back lands on
|
|
32
33
|
* every parse that reaches it — not only on failures.
|
|
33
34
|
*/
|
|
34
|
-
|
|
35
|
+
declare function superRefineCheck(check: SuperRefineEffectCheckIR, expr: string, g: SlowGen): string;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { refineCheck, slowEffect, superRefineCheck, superRefineFastTest };
|
|
35
38
|
//# sourceMappingURL=effect.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effect.d.ts","
|
|
1
|
+
{"version":3,"file":"effect.d.ts","names":[],"sources":["../../../../src/core/codegen/schemas/effect.ts"],"mappings":";;;;;;;iBAmBgB,WAAW,IAAI,mBAAmB,GAAG;;;;;;;;;iBA0BrC,YAAY,OAAO,qBAAqB,cAAc,GAAG;;;;;iBAoCzD,oBACd,OAAO,0BACP,cACA,GAAG;;;;;;;;;;;;;iBAmBW,iBACd,OAAO,0BACP,cACA,GAAG"}
|
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
import { emitEffectCallable, emitRuntimeHelper, extendStaticPath, extendStaticPathIndex
|
|
1
|
+
import { emitEffectCallable, emitRuntimeHelper, extendStaticPath, extendStaticPathIndex } from "../context.js";
|
|
2
2
|
import { emit } from "../emit.js";
|
|
3
3
|
import { ZC_SR_DECL, ZC_SR_OK_DECL, ZC_SR_RUN_DECL } from "../issue-decls.js";
|
|
4
|
+
//#region src/core/codegen/schemas/effect.ts
|
|
4
5
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// option must therefore count as aborted even if `inner`'s only issue is a
|
|
14
|
-
// non-aborting `custom`/check-level code (mirrors slowPipe's abort branch).
|
|
15
|
-
const abortBranch = g.aborted ? `else{${g.aborted}=true;}` : "";
|
|
16
|
-
return `${emit `
|
|
6
|
+
* Generate code for a TransformEffectIR node.
|
|
7
|
+
* Validates the inner schema, then applies the transform function and writes back the result.
|
|
8
|
+
*/
|
|
9
|
+
function slowEffect(ir, g) {
|
|
10
|
+
const beforeVar = g.temp("ib");
|
|
11
|
+
const innerCode = g.visit(ir.inner);
|
|
12
|
+
const abortBranch = g.aborted ? `else{${g.aborted}=true;}` : "";
|
|
13
|
+
return `${emit`
|
|
17
14
|
var ${beforeVar}=${g.issues}.length;
|
|
18
15
|
${innerCode}
|
|
19
16
|
if(${g.issues}.length===${beforeVar}){
|
|
@@ -22,69 +19,59 @@ export function slowEffect(ir, g) {
|
|
|
22
19
|
`}\n`;
|
|
23
20
|
}
|
|
24
21
|
/**
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const messageProp = check.message === undefined ? "" : `,message:${JSON.stringify(check.message)}`;
|
|
37
|
-
// `.refine(fn, { path })` reports against a member of the refined value, so
|
|
38
|
-
// the configured segments extend this node's path.
|
|
39
|
-
const path = (check.path ?? []).reduce((acc, segment) => typeof segment === "number"
|
|
40
|
-
? extendStaticPathIndex(acc, segment)
|
|
41
|
-
: extendStaticPath(acc, segment), g.path);
|
|
42
|
-
return emit `
|
|
22
|
+
* Generate code for a RefineEffectCheckIR (inline refine function call).
|
|
23
|
+
* Called from string/number/object check loops when a refine_effect is encountered.
|
|
24
|
+
*
|
|
25
|
+
* @param check - The refine effect check IR
|
|
26
|
+
* @param expr - The expression to validate (may differ from g.input, e.g. objVar in object generators)
|
|
27
|
+
* @param g - SlowGen context (provides path, issues)
|
|
28
|
+
*/
|
|
29
|
+
function refineCheck(check, expr, g) {
|
|
30
|
+
const messageProp = check.message === void 0 ? "" : `,message:${JSON.stringify(check.message)}`;
|
|
31
|
+
const path = (check.path ?? []).reduce((acc, segment) => typeof segment === "number" ? extendStaticPathIndex(acc, segment) : extendStaticPath(acc, segment), g.path);
|
|
32
|
+
return emit`
|
|
43
33
|
if(!${emitEffectCallable(g.ctx, check)}(${expr})){
|
|
44
34
|
${g.issues}.push({code:"custom",path:${path}${messageProp},input:${expr}});
|
|
45
35
|
}`;
|
|
46
36
|
}
|
|
47
37
|
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
38
|
+
* Declare the shared invoker both superRefine helpers call. It is module-local
|
|
39
|
+
* rather than an imported helper (lean mode declares it in the runtime module
|
|
40
|
+
* beside them), so inline mode must place it in the preamble itself.
|
|
41
|
+
*/
|
|
52
42
|
function emitSuperRefineRunner(ctx) {
|
|
53
|
-
|
|
54
|
-
ctx.preamble.push(ZC_SR_RUN_DECL);
|
|
55
|
-
}
|
|
43
|
+
if (ctx.mode !== "lean" && !ctx.preamble.includes("function __zcSrRun(f,p){var r=f(p);if(r&&typeof r.then===\"function\"){throw new __zcCore.$ZodAsyncError();}}")) ctx.preamble.push(ZC_SR_RUN_DECL);
|
|
56
44
|
}
|
|
57
45
|
/**
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return `${ok}(${emitEffectCallable(g.ctx, check)},${expr})`;
|
|
46
|
+
* Fast-path test for a SuperRefineEffectCheckIR: a boolean term reporting that
|
|
47
|
+
* the callback added no issue and left the value alone (see ZC_SR_OK_DECL).
|
|
48
|
+
*/
|
|
49
|
+
function superRefineFastTest(check, expr, g) {
|
|
50
|
+
emitSuperRefineRunner(g.ctx);
|
|
51
|
+
return `${emitRuntimeHelper(g.ctx, "__zcSrOk", ZC_SR_OK_DECL)}(${emitEffectCallable(g.ctx, check)},${expr})`;
|
|
65
52
|
}
|
|
66
53
|
/**
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (g.aborted)
|
|
87
|
-
code += `if(${p}.aborted){${g.aborted}=true;}`;
|
|
88
|
-
return code;
|
|
54
|
+
* Generate code for a SuperRefineEffectCheckIR: call the payload-taking
|
|
55
|
+
* callback with a synthesized `{ value, issues }` and merge what it collected.
|
|
56
|
+
* zod's own wrapper installs `addIssue` and normalizes the result, so the
|
|
57
|
+
* issues are zod's — the helper only reprojects them onto this node's path and
|
|
58
|
+
* strips the internal bookkeeping zod deletes before they become visible.
|
|
59
|
+
*
|
|
60
|
+
* The payload's `value` is writable public API, so it is written back to the
|
|
61
|
+
* output slot the way an overwrite effect is. This walk is always the eager one
|
|
62
|
+
* (the node reports as mutating, see hasMutation), so the write-back lands on
|
|
63
|
+
* every parse that reaches it — not only on failures.
|
|
64
|
+
*/
|
|
65
|
+
function superRefineCheck(check, expr, g) {
|
|
66
|
+
emitSuperRefineRunner(g.ctx);
|
|
67
|
+
const helper = emitRuntimeHelper(g.ctx, "__zcSr", ZC_SR_DECL);
|
|
68
|
+
const fn = emitEffectCallable(g.ctx, check);
|
|
69
|
+
const p = g.temp("sp");
|
|
70
|
+
let code = `var ${p}=${helper}(${fn},${expr},${g.path},${g.issues});${g.output}=${p}.value;`;
|
|
71
|
+
if (g.aborted) code += `if(${p}.aborted){${g.aborted}=true;}`;
|
|
72
|
+
return code;
|
|
89
73
|
}
|
|
74
|
+
//#endregion
|
|
75
|
+
export { refineCheck, slowEffect, superRefineCheck, superRefineFastTest };
|
|
76
|
+
|
|
90
77
|
//# sourceMappingURL=effect.js.map
|