Skip to content

[lint] Allow eslint react hooks rule to apply to any wrapped components#34608

Open
javiergonzalez-synth wants to merge 4 commits intofacebook:mainfrom
javiergonzalez-synth:apply-eslint-react-hooks-rule-to-any-wrapped-components
Open

[lint] Allow eslint react hooks rule to apply to any wrapped components#34608
javiergonzalez-synth wants to merge 4 commits intofacebook:mainfrom
javiergonzalez-synth:apply-eslint-react-hooks-rule-to-any-wrapped-components

Conversation

@javiergonzalez-synth
Copy link
Copy Markdown

@javiergonzalez-synth javiergonzalez-synth commented Sep 26, 2025

Summary

The following construct is usual in mobx (and some other libraries that use high order functions over components)

const Component = observer(() => {
  ...
})

However, that construct is not recognized as a component by the eslint plugin react rules of hooks, so any failure to follow the rules there are silently ignored. Exhaustive deps rules work though, giving the user a false sense of security.

More over, the following construct does lint the rules properly.

const Component = observer(function Component() {
  ...
})

The current PR addresses this by allowing the detection of hooks rules in the first case (no matter the number of middle wrappers), but ONLY in the case of variables that follow the component naming rules. This is, the "inside a hook" detection mechanism is unchanged, only the "inside a component" detection mechanism is changed.

Since, after this change any wrapper becomes valid, we can then remove the custom detection logic for "memo" and "forwardRef", since they are also considered wrappers like any other.

How did you test this change?

The following unit tests where added as invalid cases:

{
      code: normalizeIndent`
        // Invalid because rules-of-hooks must also apply to components wrapped in wrapper functions.
        // This is a case where it wraps a properly named function.
        // This *must* be invalid.
        const ComponentWithConditionalHook = anyWrapper(function ComponentWithConditionalHook() {
          if (cond) {
            useConditionalHook();
          }
        })
      `,
      errors: [conditionalError('useConditionalHook')],
    },
    {
      code: normalizeIndent`
        // Invalid because rules-of-hooks must also apply to components wrapped in wrapper functions.
        // This is a case where it wraps an anonymous function.
        // This *must* be invalid.
        const ComponentWithConditionalHook = anyWrapper(function() {
          if (cond) {
            useConditionalHook();
          }
        })
      `,
      errors: [conditionalError('useConditionalHook')],
    },
    {
      code: normalizeIndent`
        // Invalid because rules-of-hooks must also apply to components wrapped in wrapper functions.
        // This is a case where it wraps an arrow function.
        // This *must* be invalid.
        const ComponentWithConditionalHook = anyWrapper(() => {
          if (cond) {
            useConditionalHook();
          }
        })
      `,
      errors: [conditionalError('useConditionalHook')],
    },
    {
      code: normalizeIndent`
        // Invalid because rules-of-hooks must also apply to components wrapped in many wrapper functions.
        // This is a case where it double wraps an arrow function.
        // This *must* be invalid.
        const ComponentWithConditionalHook = anyWrapper1(anyWrapper2(() => {
          if (cond) {
            useConditionalHook();
          }
        }))
      `,
      errors: [conditionalError('useConditionalHook')],
    },

@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Sep 26, 2025

Hi @javiergonzalez-synth!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Sep 26, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Sep 26, 2025
@javiergonzalez-synth javiergonzalez-synth changed the title fix: allow eslint react hooks rule to apply to any wrapped components Allow eslint react hooks rule to apply to any wrapped components Sep 26, 2025
@javiergonzalez-synth javiergonzalez-synth changed the title Allow eslint react hooks rule to apply to any wrapped components [lint] Allow eslint react hooks rule to apply to any wrapped components Sep 26, 2025
@javiergonzalez-synth
Copy link
Copy Markdown
Author

Please let me know if there's anything I can do to improve this PR in any way 🙏 @jackpope @jbrown215

@github-actions
Copy link
Copy Markdown

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions Bot added the Resolution: Stale Automatically closed due to inactivity label Dec 31, 2025
@javiergonzalez-synth
Copy link
Copy Markdown
Author

bump

@github-actions github-actions Bot removed the Resolution: Stale Automatically closed due to inactivity label Dec 31, 2025
@github-actions
Copy link
Copy Markdown

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions Bot added the Resolution: Stale Automatically closed due to inactivity label Mar 31, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 7, 2026

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@github-actions github-actions Bot closed this Apr 7, 2026
@javiergonzalez-synth
Copy link
Copy Markdown
Author

bump

const functionNameSkippingCallExpressions =
getFunctionNameSkippingCallExpressions(node);
if (functionNameSkippingCallExpressions) {
if (isComponentName(functionNameSkippingCallExpressions)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Today const memoized = React.memo(() => { useHook(); }) is valid but this requires pascal case for component names. It could also make this invalid case valid const Component = debounce(() => { useHook(); });

Can you add test cases for these and make sure to include the valid examples as well?

Copy link
Copy Markdown
Author

@javiergonzalez-synth javiergonzalez-synth Apr 9, 2026

Choose a reason for hiding this comment

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

Both const memoized = React.memo(() => { and const memoized = memo(function (props) { work properly as is.

As for const Component = debounce(() => { it is detected as a valid case with the current implementation since there's no way to distinguish "randomWrapper"/"observer" etc from "debounce".

I see two ways to address this:

  1. Accept that pattern (PascalCase = wrapper(() => ) is detected as a component (current code)
  • Pros: Simple, catches everything
  • Cons: False positives: debounce, throttle, etc. silently accepted for PascalCase assignations
  1. Hybrid: hard-code memo/forwardRef + configurable componentWrappers setting
  • Pros: No false positives out of the box, extensible
  • Cons: Requires users to configure third-party wrappers

Which one sounds better to you? I'm happy to explore either path.

@jackpope jackpope reopened this Apr 8, 2026
@github-actions github-actions Bot removed the Resolution: Stale Automatically closed due to inactivity label Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants