( unit: RootUnit, unitsByKey: Record<string, string | undefined>, keyOf: (root: BaseRoot) => string )
| 1259 | } |
| 1260 | |
| 1261 | const writePrecompiledUnit = ( |
| 1262 | unit: RootUnit, |
| 1263 | unitsByKey: Record<string, string | undefined>, |
| 1264 | keyOf: (root: BaseRoot) => string |
| 1265 | ): string | undefined => { |
| 1266 | const root = unit.roots[0] |
| 1267 | const dependencies = Object.values(unit.dependencies).map( |
| 1268 | ([node, kind]) => |
| 1269 | [keyOf(node as BaseRoot), traversalKinds.indexOf(kind)] as const |
| 1270 | ) |
| 1271 | // a unit calling an unwritable unit can't be written |
| 1272 | if (dependencies.some(([key]) => !unitsByKey[key])) return |
| 1273 | const nodes: BaseNode[] = [root] |
| 1274 | const variablesByNode = new Map<BaseNode, string>([[root, "n0"]]) |
| 1275 | const morphPipedVars = new Map<BaseNode, string>() |
| 1276 | const variablesById: Record<string, string> = { [root.id]: "n0" } |
| 1277 | // each node after the root, by the index of the node it's read from |
| 1278 | const statements: [from: number, statement: string][] = [] |
| 1279 | for (let i = 0; i < nodes.length; i++) { |
| 1280 | if ( |
| 1281 | i !== 0 && |
| 1282 | (variablesByNode.get(nodes[i]) !== `n${i}` || |
| 1283 | (nodes[i].isRoot() && !nodes[i].jsCond && !isRefinedBasis(nodes[i]))) |
| 1284 | ) |
| 1285 | continue |
| 1286 | for (const key of ["children", "otherRefs"] as const) { |
| 1287 | const referenced = nodes[i][key] |
| 1288 | for (let j = 0; j < referenced.length; j++) { |
| 1289 | // a morph's piped root can happen to be identical to its head in one |
| 1290 | // scope while another scope holds distinct nodes with the same key, |
| 1291 | // so keep otherRefs bound through otherRefs[j] for refs without |
| 1292 | // disturbing first-wins variables or re-expanding seen nodes |
| 1293 | const isMorphOtherRef = key === "otherRefs" && nodes[i].is("morph") |
| 1294 | const seen = variablesByNode.has(referenced[j]) |
| 1295 | if (seen && !isMorphOtherRef) continue |
| 1296 | const variable = `n${nodes.push(referenced[j]) - 1}` |
| 1297 | if (!seen) variablesByNode.set(referenced[j], variable) |
| 1298 | if (isMorphOtherRef) morphPipedVars.set(referenced[j], variable) |
| 1299 | variablesById[referenced[j].id] ??= variable |
| 1300 | statements.push([ |
| 1301 | i, |
| 1302 | `const ${variable} = ${variablesByNode.get(nodes[i])}.${key}[${j}]` |
| 1303 | ]) |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | const dependencyNodes = Object.values(unit.dependencies).map(([node]) => node) |
| 1308 | // a node outside the closure, e.g. a Type piped to, isn't keyed by its root |
| 1309 | if ( |
| 1310 | [...unit.declared, ...dependencyNodes].some(node => !variablesById[node.id]) |
| 1311 | ) |
| 1312 | return |
| 1313 | const accessorsByValue = accessorsOf(nodes, variablesByNode) |
| 1314 | for (const [node, variable] of morphPipedVars) |
| 1315 | accessorsByValue.set(node, variable) |
| 1316 | const refs = unit.refs.map(value => accessorsByValue.get(value)) |
| 1317 | if (refs.includes(undefined)) return |
| 1318 | const source = writeUnit( |
no test coverage detected