( s: ConstraintIntersectionState<kind> )
| 49 | new Disjoint(...disjoint.map(entry => ({ ...entry, optional: true }))) |
| 50 | |
| 51 | export const intersectConstraints = <kind extends ConstraintGroupKind>( |
| 52 | s: ConstraintIntersectionState<kind> |
| 53 | ): nodeOfKind<RootKind | Extract<kind, "structure">> | Disjoint => { |
| 54 | const head = s.r.shift() |
| 55 | if (!head) { |
| 56 | // a structure with no constraints still checks its undeclared keys |
| 57 | let result: BaseNode | Disjoint = |
| 58 | ( |
| 59 | s.l.length === 0 && |
| 60 | s.kind === "structure" && |
| 61 | (!s.baseInner.undeclared || s.baseInner.undeclared === "ignore") |
| 62 | ) ? |
| 63 | $ark.kw.unknown.internal |
| 64 | // an intersection of a basis alone is the basis |
| 65 | : s.l.length === 0 && s.kind === "intersection" && basisOf(s.baseInner) ? |
| 66 | basisOf(s.baseInner)! |
| 67 | : s.ctx.$.node( |
| 68 | s.kind, |
| 69 | Object.assign(s.baseInner, unflattenConstraints(s.l)) as never, |
| 70 | { reduced: true } |
| 71 | ) |
| 72 | // a root, e.g. an empty array, can only meet the object a structure constrains |
| 73 | if (s.roots.length && result.is("structure")) |
| 74 | result = s.ctx.$.node("intersection", { structure: result }) |
| 75 | |
| 76 | for (const root of s.roots) { |
| 77 | result = intersectOrPipeNodes(root, result, s.ctx)! |
| 78 | if (result instanceof Disjoint) return asOptionalDisjoint(result) |
| 79 | } |
| 80 | |
| 81 | return result as never |
| 82 | } |
| 83 | let matched = false |
| 84 | const headKey = (head as BaseProp).key |
| 85 | for (let i = 0; i < s.l.length; i++) { |
| 86 | // props meet only at the same key |
| 87 | const key = (s.l[i] as BaseProp).key |
| 88 | if (key !== headKey && key !== undefined && headKey !== undefined) continue |
| 89 | const result: BaseNode | Disjoint | null = intersectOrPipeNodes( |
| 90 | s.l[i], |
| 91 | head, |
| 92 | s.ctx |
| 93 | ) |
| 94 | if (result === null) continue |
| 95 | if (result instanceof Disjoint) { |
| 96 | return s.derivedR?.has(head) || s.derivedL?.has(s.l[i]) ? |
| 97 | asOptionalDisjoint(result) |
| 98 | : result |
| 99 | } |
| 100 | |
| 101 | if (result.isRoot()) { |
| 102 | s.roots.push(result) |
| 103 | // sequences that met as tuples of several lengths are the root's now, |
| 104 | // but what's left of a structure rejecting undeclared keys still |
| 105 | // declares the indices they did |
| 106 | if ( |
| 107 | s.kind === "structure" && |
| 108 | s.baseInner.undeclared === "reject" && |
no test coverage detected