({ code, program, scopes }, s, body)
| 1718 | // and in the same order with each impure call; a function read once moves with |
| 1719 | // the value it's passed in, as a traversal into its validator's creation |
| 1720 | const writtenWhereRead: UnitsPass = ({ code, program, scopes }, s, body) => { |
| 1721 | const functions: [VariableDeclarator, Reference][] = [] |
| 1722 | const values: [VariableDeclarator, Reference][] = [] |
| 1723 | // where each impure statement or initializer runs |
| 1724 | const impure = new Map<Node, number>() |
| 1725 | for (const statement of program.body as Node[]) { |
| 1726 | if (statement.type !== "VariableDeclaration") { |
| 1727 | if ( |
| 1728 | statement.type !== "EmptyStatement" && |
| 1729 | (statement.type !== "ExpressionStatement" || |
| 1730 | !isPureExpression(statement.expression, code)) |
| 1731 | ) |
| 1732 | impure.set(statement, statement.start) |
| 1733 | continue |
| 1734 | } |
| 1735 | for (const declarator of statement.declarations) { |
| 1736 | const { id, init } = declarator |
| 1737 | if (init && !isPureExpression(init, code)) |
| 1738 | impure.set(declarator, declarator.start) |
| 1739 | const binding = scopes.bindingsById.get(id) |
| 1740 | const [read] = binding?.references ?? [] |
| 1741 | if ( |
| 1742 | statement.kind !== "const" || |
| 1743 | !init || |
| 1744 | binding?.references.length !== 1 || |
| 1745 | read.isWrite || |
| 1746 | read.id.start < declarator.end |
| 1747 | ) |
| 1748 | continue |
| 1749 | if (isFunctionExpression(unwrapExpression(init))) { |
| 1750 | if (isRunOnce(read.id)) functions.push([declarator, read]) |
| 1751 | } else if (isRunSurely(read.id)) values.push([declarator, read]) |
| 1752 | } |
| 1753 | } |
| 1754 | const moving = new Map<Reference, VariableDeclarator>() |
| 1755 | // the last first, so one moving past another sees where it went |
| 1756 | for (const [declarator, read] of values.reverse()) { |
| 1757 | const passes = |
| 1758 | [...impure].some( |
| 1759 | ([other, at]) => |
| 1760 | other !== declarator && declarator.end < at && at < read.id.start |
| 1761 | ) || callsBefore(topStatementOf(read.id), read.id, code) |
| 1762 | if (passes) continue |
| 1763 | moving.set(read, declarator) |
| 1764 | if (impure.has(declarator)) impure.set(declarator, read.id.start) |
| 1765 | } |
| 1766 | if (!moving.size) return true |
| 1767 | const isInMoved = spansContaining([...moving.values()].map(d => d.init!)) |
| 1768 | for (const [declarator, read] of functions) |
| 1769 | if (isInMoved(read.id)) moving.set(read, declarator) |
| 1770 | const reads = [...moving.keys()].sort((l, r) => l.id.start - r.id.start) |
| 1771 | // a span's text with each read within it written as what it reads |
| 1772 | const textOf = (start: number, end: number): string => { |
| 1773 | let text = "" |
| 1774 | for (const read of reads) { |
| 1775 | if (read.id.start < start || read.id.end > end) continue |
| 1776 | text += code.slice(start, read.id.start) + writtenAt(read) |
| 1777 | start = read.id.end |
nothing calls this directly
no test coverage detected