(node: BaseRoot)
| 55 | |
| 56 | // the decoding graph with each morph inverted |
| 57 | const encoderOf = (node: BaseRoot): BaseRoot => { |
| 58 | // whether each branch of a union or head of a morph transforms, read from |
| 59 | // the original before its children transform, as a branch reaching an alias |
| 60 | // that's still being encoded can't resolve yet |
| 61 | const branchTfs: boolean[][] = [] |
| 62 | const headTfs: boolean[] = [] |
| 63 | const mapper: DeepNodeTransformation = (kind, inner, ctx) => |
| 64 | kind === "morph" ? |
| 65 | invert(inner as never, headTfs.pop()!, node.$, mapper, ctx) |
| 66 | : kind === "optional" && "default" in inner ? |
| 67 | node.$.node("required", { |
| 68 | ...omit(inner as Optional.Inner, { default: 1 }), |
| 69 | value: decodedValueOf((inner as Optional.Inner).value, node.$) |
| 70 | }) |
| 71 | : kind === "structure" ? requireDefaulted(inner as never) |
| 72 | : kind === "sequence" ? requireDefaultables(inner as never, node.$) |
| 73 | : kind === "union" ? |
| 74 | encodeUnion(inner as Union.Inner, branchTfs.pop()!, node.$) |
| 75 | : (inner as never) |
| 76 | return node.transform(mapper, { |
| 77 | shouldTf: n => { |
| 78 | const tfs = n.tfs || (n.is("optional") && n.hasDef()) |
| 79 | if (tfs && n.is("union")) |
| 80 | branchTfs.push(n.branches.map(transformsOnEncode)) |
| 81 | if (tfs && n.is("morph")) |
| 82 | headTfs.push(n.head ? transformsOnEncode(n.head) : false) |
| 83 | return tfs |
| 84 | } |
| 85 | }) as never |
| 86 | } |
| 87 | |
| 88 | // whether a branch reaches a transform that survives encoding (morphs and |
| 89 | // undeclared: "delete"), excluding defaults stripped to required/prefix |
no test coverage detected