Skip to content

Conversation

javiergonzalez-synth
Copy link

@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')],
    },

Copy link

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 [email protected]. Thanks!

Copy link

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
Author

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

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.

1 participant