Skip to content

fix(component-hook-factories): exclude HOC patterns and test mocks#1652

Merged
Rel1cx merged 4 commits intoRel1cx:mainfrom
zerone0x:fix/component-hook-factories-false-positives
Mar 30, 2026
Merged

fix(component-hook-factories): exclude HOC patterns and test mocks#1652
Rel1cx merged 4 commits intoRel1cx:mainfrom
zerone0x:fix/component-hook-factories-false-positives

Conversation

@zerone0x
Copy link
Copy Markdown
Contributor

@zerone0x zerone0x commented Mar 28, 2026

Summary

  • Fixes false positive for Higher Order Components (HOCs) that take a component as a parameter. The rule now detects HOC patterns by checking for PascalCase parameter names (e.g., WrappedComponent) or ComponentType/FC type annotations, and skips reporting components defined inside them.
  • Fixes false positive for component definitions inside vi.mock() and jest.mock() callbacks, which are test-only code and should not be flagged.
  • Adds isJestMockCallback and isTestMockCallback utilities alongside the existing isViMockCallback.
  • All existing tests continue to pass; 5 new valid test cases added covering HOC patterns and mock callbacks.

Fixes #1666, Fixes #1667

Test plan

  • All 35 existing + new tests pass (vitest run on the spec file)
  • Existing invalid cases (factory functions, components-in-components, hook factories) remain correctly flagged
  • HOC with PascalCase param (withBorder(WrappedComponent)) is now valid
  • HOC with component param (withLoading(BaseComponent)) is now valid
  • vi.mock() component mocks are now valid
  • jest.mock() component mocks are now valid

🤖 Generated with Claude Code

Rel1cx and others added 2 commits March 28, 2026 13:17
- Bump eslint-plugin-unicorn to ^64.0.0
- Add @eslint-react/kit as a workspace dependency in apps/website
- Normalize Markdown tables in core docs for consistent formatting
- Update pnpm-lock.yaml to link the workspace kit
…lbacks from false positives

The rule now correctly handles two common false-positive scenarios:

1. Higher Order Components (HOCs) that take a component as parameter
   (detected by PascalCase parameter name or ComponentType annotation)
   are no longer flagged, since the HOC definition itself is a valid
   React pattern.

2. Component definitions inside vi.mock() and jest.mock() callbacks
   are no longer flagged, since test mocks are not runtime code.

Fixes Rel1cx#1632

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 28, 2026

@zerone0x is attempting to deploy a commit to the Rel1cx's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Rel1cx Rel1cx force-pushed the main branch 5 times, most recently from da55aa7 to 8a4396f Compare March 28, 2026 10:09
@Rel1cx
Copy link
Copy Markdown
Owner

Rel1cx commented Mar 30, 2026

Modified the description to fix sub-issues #1666 and #1667; the static-components rule needs to be added to close the parent issue.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 30, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
eslint-react Ready Ready Preview, Comment Mar 30, 2026 11:47am

Request Review

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the component-hook-factories ESLint rule to reduce false positives by excluding common Higher-Order Component (HOC) patterns and ignoring React component definitions created inside test mock factories.

Changes:

  • Add AST utilities to detect jest.mock and unify vi.mock/jest.mock callback detection via isTestMockCallback.
  • Update component-hook-factories to skip reporting components defined inside HOCs and inside vi.mock/jest.mock callbacks.
  • Extend the rule spec with new valid cases covering HOC patterns and mock callbacks.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
packages/utilities/ast/src/pattern-vitest-mock.ts Adds jest.mock + shared test-mock callback detection helpers.
packages/plugins/eslint-plugin-react-x/src/rules/component-hook-factories/component-hook-factories.ts Skips reports for nested components inside detected HOCs and test mock callbacks.
packages/plugins/eslint-plugin-react-x/src/rules/component-hook-factories/component-hook-factories.spec.ts Adds valid test cases for HOCs and vi.mock/jest.mock callback scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…k-factories/component-hook-factories.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: REL1CX <solarflarex@qq.com>
@Rel1cx Rel1cx merged commit b9a4c51 into Rel1cx:main Mar 30, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] component-hook-factories false positives on test mocks [bug] component-hook-factories false positives on HOC definitions

3 participants