* The value a function body hands back from its first top-level `return`, * its declarations bound on the way there. Nested blocks and functions are * stepped over: what is declared or returned in there is theirs.
(body: string, scope: Map<string, Loaded | null>)
| 1731 | * stepped over: what is declared or returned in there is theirs. |
| 1732 | */ |
| 1733 | function blockValue(body: string, scope: Map<string, Loaded | null>): Loaded | null { |
| 1734 | const word = /[A-Za-z_$][\w$]*/y; |
| 1735 | for (let i = 0; i < body.length;) { |
| 1736 | const ch = body[i]!; |
| 1737 | if (ch === '"' || ch === "'" || ch === '`' || ch === '{' || ch === '(' || ch === '[') { |
| 1738 | const end = ch === '{' || ch === '(' || ch === '[' ? matchBracket(body, i) : skipString(body, i); |
| 1739 | if (end < 0) return null; |
| 1740 | i = end + 1; |
| 1741 | continue; |
| 1742 | } |
| 1743 | word.lastIndex = i; |
| 1744 | const w = word.exec(body)?.[0]; |
| 1745 | if (!w) { |
| 1746 | i++; |
| 1747 | continue; |
| 1748 | } |
| 1749 | const after = i + w.length; |
| 1750 | const statement = i === 0 || !/[\w$.]/.test(body[i - 1]!); |
| 1751 | i = after; |
| 1752 | if (!statement) continue; |
| 1753 | if (w === 'return') return valueOf(body.slice(after, statementEnd(body, after)), scope); |
| 1754 | if (w === 'const' || w === 'let' || w === 'var') i = declare(body, after, scope); |
| 1755 | else if (w === 'function' || w === 'class') { |
| 1756 | const name = /^\s*\*?\s*([A-Za-z_$][\w$]*)/.exec(body.slice(after, after + 128)); |
| 1757 | if (name) scope.set(name[1]!, null); |
| 1758 | } |
| 1759 | } |
| 1760 | return null; |
| 1761 | } |
| 1762 | |
| 1763 | /** Bind each declarator of a `const`, `let` or `var` whose first pattern starts at `at`; where the declaration ends. */ |
| 1764 | function declare(body: string, at: number, scope: Map<string, Loaded | null>): number { |
no test coverage detected