( container: SyntaxNode, rule: CaptureRule, spec: FnRefSpec, source: string )
| 435 | * (name, position) pairs of every function-value-shaped expression found. |
| 436 | */ |
| 437 | export function captureFnRefCandidates( |
| 438 | container: SyntaxNode, |
| 439 | rule: CaptureRule, |
| 440 | spec: FnRefSpec, |
| 441 | source: string |
| 442 | ): FnRefCandidate[] { |
| 443 | const valueNodes: SyntaxNode[] = []; |
| 444 | |
| 445 | switch (rule.mode) { |
| 446 | case 'args': |
| 447 | case 'list': { |
| 448 | for (let i = 0; i < container.namedChildCount; i++) { |
| 449 | const child = container.namedChild(i); |
| 450 | if (child) valueNodes.push(child); |
| 451 | } |
| 452 | break; |
| 453 | } |
| 454 | case 'rhs': { |
| 455 | const rhs = rule.field |
| 456 | ? getChildByField(container, rule.field) |
| 457 | : container.namedChild(container.namedChildCount - 1); |
| 458 | if (rhs) { |
| 459 | // Param-storage skip: `this.status = status` / `o->cb = cb` — when |
| 460 | // the assigned member's name EQUALS the RHS identifier, the RHS is a |
| 461 | // local/parameter being stored, and the function it holds (if any) |
| 462 | // is unknowable statically. A same-named function elsewhere would |
| 463 | // resolve to the WRONG target (excalidraw A/B finding), so skip. |
| 464 | const lhs = |
| 465 | getChildByField(container, 'left') ?? |
| 466 | getChildByField(container, 'lhs') ?? |
| 467 | getChildByField(container, 'target') ?? |
| 468 | (container.namedChildCount >= 2 ? container.namedChild(0) : null); |
| 469 | const lhsText = lhs ? getNodeText(lhs, source) : ''; |
| 470 | const lhsLastName = lhsText.match(/([A-Za-z_$][A-Za-z0-9_$]*)\s*$/)?.[1]; |
| 471 | const rhsText = getNodeText(rhs, source).trim(); |
| 472 | if (lhsLastName && lhsLastName === rhsText) break; |
| 473 | valueNodes.push(rhs); |
| 474 | } |
| 475 | break; |
| 476 | } |
| 477 | case 'value': { |
| 478 | let value = rule.field ? getChildByField(container, rule.field) : null; |
| 479 | // Keyed containers without a value field (Go keyed_element): the value |
| 480 | // is the LAST named child (the first is the key). |
| 481 | if (!value && container.namedChildCount > 0) { |
| 482 | value = container.namedChild(container.namedChildCount - 1); |
| 483 | } |
| 484 | if (value) valueNodes.push(value); |
| 485 | break; |
| 486 | } |
| 487 | case 'varinit': { |
| 488 | // Destructuring (`const { center } = ellipse`) extracts DATA from the |
| 489 | // RHS — never a function alias. Without this skip, a parameter that |
| 490 | // shadows a same-named imported function produced a wrong edge. |
| 491 | const nameNode = |
| 492 | getChildByField(container, 'name') ?? getChildByField(container, 'pattern'); |
| 493 | if (nameNode && (nameNode.type === 'object_pattern' || nameNode.type === 'array_pattern' || |
| 494 | nameNode.type === 'tuple_pattern' || nameNode.type === 'struct_pattern')) { |
no test coverage detected