| 505 | |
| 506 | /** Whether `method`, a `def` directly in its class, can only ever run with that class's `self`. */ |
| 507 | const selfIsTheClass = (method: FlowMethod, scope: FlowMethod): boolean => { |
| 508 | let answer = selfMemo.get(method.id); |
| 509 | if (answer !== undefined) return answer; |
| 510 | const lines = linesOf(method.filePath); |
| 511 | answer = true; |
| 512 | for (let i = method.startLine - 1; answer && i < method.endLine && i < lines.length; i++) { |
| 513 | if (OTHER_SELF.test(lines.at(i)!)) answer = false; |
| 514 | } |
| 515 | // A `def` inside a block of the class body (`included do`, `Struct.new |
| 516 | // do`) belongs to whatever the block runs on. |
| 517 | let indent = (lines.at(method.startLine - 1) ?? '').search(/\S/); |
| 518 | for (let i = method.startLine - 2; answer && i >= scope.startLine; i--) { |
| 519 | const line = lines.at(i)!; |
| 520 | const at = line.search(/\S/); |
| 521 | if (at < 0 || at >= indent || line[at] === '#') continue; |
| 522 | indent = at; |
| 523 | if (/(?:\bdo(?:\s*\|[^|]*\|)?|\{(?:\s*\|[^|]*\|)?)\s*(?:#.*)?$/.test(line)) answer = false; |
| 524 | } |
| 525 | selfMemo.set(method.id, answer); |
| 526 | return answer; |
| 527 | }; |
| 528 | |
| 529 | /** The value of `self` at a site: only inside a method of a class says which. */ |
| 530 | const selfValue = (site: FlowSite): FlowValue | Abstention => { |