[lint] Allow eslint react hooks rule to apply to any wrapped components#34608
Conversation
|
Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
…hooks-rule-to-any-wrapped-components
|
Please let me know if there's anything I can do to improve this PR in any way 🙏 @jackpope @jbrown215 |
|
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. |
|
bump |
|
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. |
|
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! |
|
bump |
| const functionNameSkippingCallExpressions = | ||
| getFunctionNameSkippingCallExpressions(node); | ||
| if (functionNameSkippingCallExpressions) { | ||
| if (isComponentName(functionNameSkippingCallExpressions)) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
- 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
- Hybrid: hard-code memo/forwardRef + configurable
componentWrapperssetting
- 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.
Summary
The following construct is usual in mobx (and some other libraries that use high order functions over components)
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.
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: