(expr, names, constants)
| 1350 | return from instanceof code_1._CodeOrName ? addNames(names, from.names) : names; |
| 1351 | } |
| 1352 | function optimizeExpr(expr, names, constants) { |
| 1353 | if (expr instanceof code_1.Name) |
| 1354 | return replaceName(expr); |
| 1355 | if (!canOptimize(expr)) |
| 1356 | return expr; |
| 1357 | return new code_1._Code(expr._items.reduce((items, c) => { |
| 1358 | if (c instanceof code_1.Name) |
| 1359 | c = replaceName(c); |
| 1360 | if (c instanceof code_1._Code) |
| 1361 | items.push(...c._items); |
| 1362 | else |
| 1363 | items.push(c); |
| 1364 | return items; |
| 1365 | }, [])); |
| 1366 | function replaceName(n) { |
| 1367 | const c = constants[n.str]; |
| 1368 | if (c === undefined || names[n.str] !== 1) |
| 1369 | return n; |
| 1370 | delete names[n.str]; |
| 1371 | return c; |
| 1372 | } |
| 1373 | function canOptimize(e) { |
| 1374 | return (e instanceof code_1._Code && |
| 1375 | e._items.some((c) => c instanceof code_1.Name && names[c.str] === 1 && constants[c.str] !== undefined)); |
| 1376 | } |
| 1377 | } |
| 1378 | function subtractNames(names, from) { |
| 1379 | for (const n in from) |
| 1380 | names[n] = (names[n] || 0) - (from[n] || 0); |
no test coverage detected