MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / reactComponentHoc

Method reactComponentHoc

src/extraction/tree-sitter.ts:1625–1651  ·  view source on GitHub ↗

* Detect a React component declared via an HOC wrapper whose result is itself a * component: `forwardRef(...)`, `memo(...)`, `React.forwardRef/memo(...)`, and * styled-components / emotion `styled.tag\`…\`` / `styled(Base)\`…\``. These * initializers are a call / tagged-template (not a bare

(valueNode: SyntaxNode)

Source from the content-addressed store, hash-verified

1623 * `undefined` when this initializer is not a recognized component wrapper.
1624 */
1625 private reactComponentHoc(valueNode: SyntaxNode): { inner: SyntaxNode | null } | undefined {
1626 if (valueNode.type !== 'call_expression') return undefined;
1627 const callee = getChildByField(valueNode, 'function');
1628 if (!callee) return undefined;
1629 const calleeText = getNodeText(callee, this.source);
1630 // styled-components / emotion: `styled.button\`…\`` / `styled(Base)\`…\``.
1631 // tree-sitter models these tagged templates as a call_expression whose callee
1632 // is the `styled.x` / `styled(Base)` tag (\b avoids matching `styledFoo`).
1633 // No inline render fn — the argument is the CSS template.
1634 if (/^styled\b/.test(calleeText)) return { inner: null };
1635 // React HOCs: `forwardRef`/`memo`/`React.forwardRef`/`React.memo`.
1636 if (!REACT_COMPONENT_HOCS.has(calleeText)) return undefined;
1637 // The first arrow / function-expression argument is the render fn (if inline;
1638 // `memo(Imported)` passes a bare identifier and has none).
1639 const args = getChildByField(valueNode, 'arguments');
1640 let inner: SyntaxNode | null = null;
1641 if (args) {
1642 for (let i = 0; i < args.namedChildCount; i++) {
1643 const a = args.namedChild(i);
1644 if (a && (a.type === 'arrow_function' || a.type === 'function_expression')) {
1645 inner = a;
1646 break;
1647 }
1648 }
1649 }
1650 return { inner };
1651 }
1652
1653 /**
1654 * Emit a `component` node for an HOC-wrapped React component declaration (see

Callers 1

extractVariableMethod · 0.95

Calls 3

getChildByFieldFunction · 0.90
getNodeTextFunction · 0.90
hasMethod · 0.80

Tested by

no test coverage detected