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