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

Method reactComponentHoc

src/extraction/tree-sitter.ts:2513–2540  ·  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)\`…\``, typed * (`styled.tag \`…\``) or not. These * initial

(valueNode: SyntaxNode)

Source from the content-addressed store, hash-verified

2511 * `undefined` when this initializer is not a recognized component wrapper.
2512 */
2513 private reactComponentHoc(valueNode: SyntaxNode): { inner: SyntaxNode | null } | undefined {
2514 if (this.isTypedStyledTemplate(valueNode)) return { inner: null };
2515 if (valueNode.type !== 'call_expression') return undefined;
2516 const callee = getChildByField(valueNode, 'function');
2517 if (!callee) return undefined;
2518 const calleeText = getNodeText(callee, this.source);
2519 // styled-components / emotion: `styled.button\`…\`` / `styled(Base)\`…\``.
2520 // tree-sitter models these tagged templates as a call_expression whose callee
2521 // is the `styled.x` / `styled(Base)` tag (\b avoids matching `styledFoo`).
2522 // No inline render fn — the argument is the CSS template.
2523 if (/^styled\b/.test(calleeText)) return { inner: null };
2524 // React HOCs: `forwardRef`/`memo`/`React.forwardRef`/`React.memo`.
2525 if (!REACT_COMPONENT_HOCS.has(calleeText)) return undefined;
2526 // The first arrow / function-expression argument is the render fn (if inline;
2527 // `memo(Imported)` passes a bare identifier and has none).
2528 const args = getChildByField(valueNode, 'arguments');
2529 let inner: SyntaxNode | null = null;
2530 if (args) {
2531 for (let i = 0; i < args.namedChildCount; i++) {
2532 const a = args.namedChild(i);
2533 if (a && (a.type === 'arrow_function' || a.type === 'function_expression')) {
2534 inner = a;
2535 break;
2536 }
2537 }
2538 }
2539 return { inner };
2540 }
2541
2542 /**
2543 * A styled tag with type arguments: `styled.div<Props>\`…\``,

Callers 1

extractVariableMethod · 0.95

Calls 4

isTypedStyledTemplateMethod · 0.95
getChildByFieldFunction · 0.90
getNodeTextFunction · 0.90
hasMethod · 0.80

Tested by

no test coverage detected