(
node: Structure.Node,
other: Structure.Node,
otherInner: {
optional?: readonly OptionalNode[]
index?: readonly nodeOfKind<"index">[]
sequence?: nodeOfKind<"sequence">
},
derived: OptionalNode[],
roots: BaseRoot[],
$: BaseScope
)
| 147 | // structure with undeclared keys allows, with node on the left of a disjoint |
| 148 | // for each required key or element it doesn't |
| 149 | const narrowToKeysOf = ( |
| 150 | node: Structure.Node, |
| 151 | other: Structure.Node, |
| 152 | otherInner: { |
| 153 | optional?: readonly OptionalNode[] |
| 154 | index?: readonly nodeOfKind<"index">[] |
| 155 | sequence?: nodeOfKind<"sequence"> |
| 156 | }, |
| 157 | derived: OptionalNode[], |
| 158 | roots: BaseRoot[], |
| 159 | $: BaseScope |
| 160 | ): Disjoint => { |
| 161 | const disjoint = new Disjoint() |
| 162 | const keys = allowedKeysOf(node) |
| 163 | const count = |
| 164 | node.undeclared === "reject" && other.sequence ? |
| 165 | allowedIndexCountOf(keys, other.sequence) |
| 166 | : null |
| 167 | if (count !== null) { |
| 168 | const sequence = other.sequence! |
| 169 | if (sequence.minLength > count) { |
| 170 | const value = requiredElementAt(sequence, count) |
| 171 | disjoint.push( |
| 172 | ...Disjoint.init( |
| 173 | "presence", |
| 174 | $ark.kw.never.internal, |
| 175 | value |
| 176 | ).withPrefixKey(count, "required") |
| 177 | ) |
| 178 | } |
| 179 | // as a length bound, 0 would be an exact length, which a union could |
| 180 | // mistake for a discriminant of node, which needn't be an array |
| 181 | else if (count === 0) { |
| 182 | otherInner.sequence = $.node("sequence", { |
| 183 | variadic: $ark.kw.never.internal |
| 184 | }) |
| 185 | } else { |
| 186 | roots.push( |
| 187 | $.node("intersection", { |
| 188 | proto: $.node("proto", { proto: Array }), |
| 189 | // at least 1, so it doesn't reduce to an exact length |
| 190 | maxLength: $.node("maxLength", { rule: count }) as never |
| 191 | }) |
| 192 | ) |
| 193 | } |
| 194 | } |
| 195 | for (const k of other.requiredKeys) { |
| 196 | if (keys.allowsUnused(k)) continue |
| 197 | disjoint.add("presence", $ark.kw.never.internal, other.byKey[k]!.value, { |
| 198 | path: [k], |
| 199 | undeclared: node.handlesUndeclared |
| 200 | }) |
| 201 | } |
| 202 | if (otherInner.optional) { |
| 203 | otherInner.optional = otherInner.optional.filter(n => |
| 204 | keys.allowsUnused(n.key) |
| 205 | ) |
| 206 | } |
no test coverage detected