writes its Transform
(js: NodeCompiler)
| 1148 | |
| 1149 | /** writes its Transform */ |
| 1150 | private jsTf(js: NodeCompiler): void { |
| 1151 | const transformedProps = this.inOrder.filter( |
| 1152 | prop => this.tfsOf(prop).length |
| 1153 | ) |
| 1154 | const transformedIndex = this.index?.filter(index => index.value.tfs) ?? [] |
| 1155 | const deletes = this.undeclared === "delete" |
| 1156 | const transformedChildren = transformedProps.flatMap(prop => |
| 1157 | this.tfsOf(prop).map(step => step.node) |
| 1158 | ) |
| 1159 | for (const index of transformedIndex) transformedChildren.push(index.value) |
| 1160 | if (this.sequence?.tfs) transformedChildren.push(this.sequence) |
| 1161 | transformedChildren.push(...this.defaultable) |
| 1162 | js.initTf(transformedChildren) |
| 1163 | // a transform of its sequence alone, copying nothing else, returns what that returns |
| 1164 | if ( |
| 1165 | transformedChildren.length === 1 && |
| 1166 | transformedChildren[0] === this.sequence && |
| 1167 | !this.slicesSeq && |
| 1168 | !this.sequence.defaultables && |
| 1169 | !this.inOrder.length && |
| 1170 | !deletes && |
| 1171 | !js.checksErrs |
| 1172 | ) { |
| 1173 | js.return(js.inv(this.sequence, { arg: "data" })) |
| 1174 | return |
| 1175 | } |
| 1176 | if ( |
| 1177 | transformedChildren.length >= 2 && |
| 1178 | transformedChildren.length === this.defaultable.length |
| 1179 | ) |
| 1180 | this.jsCopyOnce(js) |
| 1181 | js.let("out", "data") |
| 1182 | // a lone site nothing after can fail completes its copy itself |
| 1183 | const once = |
| 1184 | transformedChildren.length === 1 && |
| 1185 | transformedProps.length === 1 && |
| 1186 | !js.needsCtx |
| 1187 | for (let i = 0; i < transformedProps.length; i++) { |
| 1188 | const prop = transformedProps[i] |
| 1189 | const { key, optional } = prop |
| 1190 | const keyExpr = js.serialize(key) |
| 1191 | const onChange = () => { |
| 1192 | this.jsCopy(js, { first: i === 0 }).line( |
| 1193 | jsSetOwnProp(js, "out", key, `transformed${i}`) |
| 1194 | ) |
| 1195 | if (once) this.jsDeclaredCopy(js, [key]) |
| 1196 | } |
| 1197 | js.const(`value${i}`, `data${js.prop(key)}`).tfKey( |
| 1198 | `transformed${i}`, |
| 1199 | `value${i}`, |
| 1200 | this.tfsOf(transformedProps[i]), |
| 1201 | { |
| 1202 | keyExpr, |
| 1203 | ownKey: `Object.prototype.${typeof key === "symbol" ? "hasOwnProperty" : "propertyIsEnumerable"}.call(data, ${keyExpr})`, |
| 1204 | ...(optional ? { condition: prop.jsHoldsValue(js, "data") } : {}), |
| 1205 | ...(deletes ? {} : { onChange }) |
| 1206 | } |
| 1207 | ) |
no test coverage detected