* 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)
| 2628 | * `undefined` when this initializer is not a recognized component wrapper. |
| 2629 | */ |
| 2630 | private reactComponentHoc(valueNode: SyntaxNode): { inner: SyntaxNode | null } | undefined { |
| 2631 | if (this.isTypedStyledTemplate(valueNode)) return { inner: null }; |
| 2632 | if (valueNode.type !== 'call_expression') return undefined; |
| 2633 | const callee = getChildByField(valueNode, 'function'); |
| 2634 | if (!callee) return undefined; |
| 2635 | const calleeText = getNodeText(callee, this.source); |
| 2636 | // styled-components / emotion: `styled.button\`…\`` / `styled(Base)\`…\``. |
| 2637 | // tree-sitter models these tagged templates as a call_expression whose callee |
| 2638 | // is the `styled.x` / `styled(Base)` tag (\b avoids matching `styledFoo`). |
| 2639 | // No inline render fn — the argument is the CSS template. |
| 2640 | if (/^styled\b/.test(calleeText)) return { inner: null }; |
| 2641 | // React HOCs: `forwardRef`/`memo`/`React.forwardRef`/`React.memo`. |
| 2642 | if (!REACT_COMPONENT_HOCS.has(calleeText)) return undefined; |
| 2643 | // The first arrow / function-expression argument is the render fn (if inline; |
| 2644 | // `memo(Imported)` passes a bare identifier and has none). |
| 2645 | const args = getChildByField(valueNode, 'arguments'); |
| 2646 | let inner: SyntaxNode | null = null; |
| 2647 | if (args) { |
| 2648 | for (let i = 0; i < args.namedChildCount; i++) { |
| 2649 | const a = args.namedChild(i); |
| 2650 | if (a && (a.type === 'arrow_function' || a.type === 'function_expression')) { |
| 2651 | inner = a; |
| 2652 | break; |
| 2653 | } |
| 2654 | } |
| 2655 | } |
| 2656 | return { inner }; |
| 2657 | } |
| 2658 | |
| 2659 | /** |
| 2660 | * A styled tag with type arguments: `styled.div<Props>\`…\``, |
no test coverage detected