-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bring back eslint-plugin-react-hooks and fix issues #1589
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I thought the |
It seems this plugin "assumes that any function starting with ”use” and a capital letter right after it is a Hook." so I think it should now catch everything. |
Code in question: https://github.com/facebook/react/blob/7992ca10df497002e0e91bb5bfbc9661c9c85b88/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js#L1492-L1533 I see that version 4.0.0 is brand new and had some breaking changes: https://github.com/facebook/react/blob/master/packages/eslint-plugin-react-hooks/CHANGELOG.md Hmm, turns out they now treat any hook ending in Also, refs shouldn't need to be in the dependencies list. |
@swissspidy So it looks like the linter wants us to rearrange arguments in the keyboard hooks? Is this at all configurable? Stylistically, for these components, it's better to have two last arguments to be function + deps. But it looks like the linter always wants the first two? |
Right 🤦 somehow I didn't see the forest for the trees here. Yes, the linter expects the first two to be callback + deps. See https://github.com/facebook/react/blob/edf6eac8a181860fd8a2d076a43806f1237495a1/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js#L1492-L1533 This is currently not configurable, however they might be willing to allow this. They seem to be very open about contributions to the lint rule. |
Ok. Let's try it. I sent them a pull request: facebook/react#18861. But we may have to be prepared to change signatures if it doesn't move fast enough. This is an important linter to avoid bugs. |
@@ -60,6 +60,7 @@ function EditablePreview({ label, value, width, format, onChange }) { | |||
); | |||
|
|||
// Handle ESC keypress to toggle input field. | |||
//eslint-disable-next-line react-hooks/exhaustive-deps | |||
useKeyDownEffect(wrapperRef, { key: 'esc', editable: true }, disableEditing, [ | |||
isEditing, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding disableEditing
as the only dependency (as enforced by the rule) breaks the test in assets/src/edit-story/components/colorPicker/test/colorPicker/footerInteraction.js
. The callback is simply not fired.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just a couple of nits and comments.
@@ -27,7 +27,7 @@ function useKeyDeleteStop(ref, onDelete) { | |||
ref.current.focus(); | |||
onDelete(); | |||
}, | |||
[] | |||
[onDelete, ref] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't ever need to have refs as dependencies - as they're always stable. Can we somehow annotate that anything named *ref
is in fact a ref?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, I'd have to check how the rule works under the hood.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can skip this for now, but we do have a lot of those around - would be awesome if we could however set the linter up to assume, that anything named /[Rr]ef$/
and only accessed via .current
is a stable ref and never needs to be in dependencies. It might be wishful thinking though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to not have refs
as deps. but in this case it arrives from outside so there's a limited info that this is indeed a ref.
assets/src/edit-story/components/inspector/document/publish/index.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
Small nitpicks, but we are looking good. |
@swissspidy sorry for delay. I just rebased the original proposal pull request. patch-package for this is a brilliant! |
@@ -0,0 +1,52 @@ | |||
diff --git a/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js b/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@swissspidy is there no way at all here to actually put the pull request reference? Or even maybe automatically pull the diff from the pull request? I'm asking since I just rebased the pull request and I'll have to do it probably again and again for some time until it's accepted, hopefully.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there's any automated way for this, but I'll have a look.
* master: (101 commits) Update list of Google Fonts Fix google fonts build script Use data arg for force=true instead of as a literal string Added call to delete API for media in apiProvider Fix using incorrect input for adding color preset. (#1933) only use shapes and background colors for dummy story data to avoid local host image issues Bump percy from 0.26.6 to 0.26.7 (#1937) Renamed animation components for better clarity Add custom calendar implementation (#1743) Fixed lint errors Added tests to AMPKeyframes Bump uuid from 8.0.0 to 8.1.0 (#1909) Bump @wordpress/icons from 1.4.0 to 2.0.0 (#1928) Bring back eslint-plugin-react-hooks and fix issues (#1589) Use use-context-selector versions of createContext, useContext Add useContextSelectorShallow hook Moved SimpleAnimation to root of parts Dashboard: Converted BlinkOn animation to new standard splitting out storiesView and emptyView from content. Adding tests in content/test and storybook examples in content/ . Force stylelint to 13.3.3, because 13.5.0 was failing ...
Summary
Adds back missing
eslint-plugin-react-hooks
dependency and fixes all reported issues.Relevant Technical Choices
Re-enables the
react-hooks/rules-of-hooks
rule.To-do
Fixes #1562