( data: unknown, ctx: InternalTraversal | undefined, transforms: readonly TraverseTransform[], nodes?: readonly BaseNode[] )
| 123 | * node where it's passed one |
| 124 | */ |
| 125 | export const transformKept = ( |
| 126 | data: unknown, |
| 127 | ctx: InternalTraversal | undefined, |
| 128 | transforms: readonly TraverseTransform[], |
| 129 | nodes?: readonly BaseNode[] |
| 130 | ): unknown => { |
| 131 | // one branch alone writes an object it can't copy in place, as it would |
| 132 | // outside a union |
| 133 | if (transforms.length === 1) |
| 134 | return transforms[0].call(nodes?.[0], data, ctx as never) |
| 135 | const outer = copiedInPlace |
| 136 | const copied = (copiedInPlace ??= new Map()) |
| 137 | try { |
| 138 | let out = data |
| 139 | for (let i = 0; i < transforms.length; i++) { |
| 140 | const kept = transforms[i].call(nodes?.[i], data, ctx as never) |
| 141 | // one that deleted nothing kept all the others do |
| 142 | if (kept === data) return data |
| 143 | out = i === 0 ? kept : joinKept(data, out, kept) |
| 144 | } |
| 145 | // transformed in a branch of another such union, it leaves the copies |
| 146 | // for that one to write, once it has joined its own branches |
| 147 | return outer || !copied.size ? out : writeCopied(data, out, copied) |
| 148 | } finally { |
| 149 | copiedInPlace = outer |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // writes each copy standing in for an object written in place into it: the |
| 154 | // keys every branch reaching it deleted are deleted from it, and it takes the |
no test coverage detected