( modules: Iterable<SourceModule>, options?: ThisReadsOptions )
| 1687 | } |
| 1688 | |
| 1689 | export const thisReadsOf = ( |
| 1690 | modules: Iterable<SourceModule>, |
| 1691 | options?: ThisReadsOptions |
| 1692 | ): ThisReads => { |
| 1693 | const moduleList = [...modules] |
| 1694 | const ctx = buildThisReadsContext(moduleList, options) |
| 1695 | const reads: ThisReads = { |
| 1696 | escaped: { keys: new Set(), any: false }, |
| 1697 | byLiteral: new Map(), |
| 1698 | patchesBuiltins: ctx.patchesBuiltins |
| 1699 | } |
| 1700 | const addLiteralTarget = (literal: Node, targets: KeyReads[]) => { |
| 1701 | let own = reads.byLiteral.get(literal) |
| 1702 | if (!own) { |
| 1703 | own = { keys: new Set(), any: false } |
| 1704 | ;(reads.byLiteral as Map<Node, KeyReads>).set(literal, own) |
| 1705 | } |
| 1706 | targets.push(own) |
| 1707 | } |
| 1708 | for (const module of moduleList) { |
| 1709 | walk(module.program, node => { |
| 1710 | if (node.type !== "ThisExpression") return true |
| 1711 | const fn = enclosingThisFunctionOf(node) |
| 1712 | if (!fn || isSkippedFunction(fn, module, ctx)) return true |
| 1713 | const read = namesReadOf(node, module, ctx) |
| 1714 | if (read !== "any" && !read.size) return true |
| 1715 | const bound = boundLiteralsOf(fn, module) |
| 1716 | const ownLiteral = |
| 1717 | ( |
| 1718 | fn.parent?.type === "Property" && |
| 1719 | fn.parent.value === fn && |
| 1720 | fn.parent.parent?.type === "ObjectExpression" |
| 1721 | ) ? |
| 1722 | fn.parent.parent |
| 1723 | : undefined |
| 1724 | const targets: KeyReads[] = [] |
| 1725 | if (bound === "escapes") { |
| 1726 | targets.push(reads.escaped) |
| 1727 | // keep own literal's reads indexed even when its method escapes, so |
| 1728 | // isolated-receiver checks on that literal still see its own methods |
| 1729 | if (ownLiteral) addLiteralTarget(ownLiteral, targets) |
| 1730 | } else for (const literal of bound) addLiteralTarget(literal, targets) |
| 1731 | |
| 1732 | for (const target of targets) { |
| 1733 | if (read === "any") target.any = true |
| 1734 | else for (const key of read) target.keys.add(key) |
| 1735 | } |
| 1736 | return true |
| 1737 | }) |
| 1738 | } |
| 1739 | return reads |
| 1740 | } |
| 1741 | |
| 1742 | const classMemberParentOf = (fn: Node): ClassMemberNode | undefined => { |
| 1743 | const [operand, parent] = operatorOf(fn) |
no test coverage detected