Skip to content
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

Merged
merged 26 commits into from
May 21, 2020

Conversation

swissspidy
Copy link
Collaborator

@swissspidy swissspidy commented May 6, 2020

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

  • Fix outstanding lint errors

Fixes #1562

@swissspidy swissspidy added the Type: Infrastructure Changes impacting testing infrastructure or build tooling label May 6, 2020
@swissspidy swissspidy changed the title Fix/eslint plugin react hooks Bring back eslint-plugin-react-hooks and fix issues May 6, 2020
@codecov

This comment has been minimized.

@github-actions

This comment has been minimized.

@swissspidy
Copy link
Collaborator Author

I thought the additionalHooks option in the ESLint config would eliminate most of the reported errors. Am I missing something? 🤔

@carloskelly13
Copy link
Contributor

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.

@swissspidy
Copy link
Collaborator Author

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 Effect the same as useEffect (facebook/react#18580), but doesn't seem to be in 4.0.0.

Also, refs shouldn't need to be in the dependencies list.

@dvoytenko
Copy link
Contributor

@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?

@swissspidy
Copy link
Collaborator Author

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.

@dvoytenko
Copy link
Contributor

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.

@swissspidy swissspidy removed Status: Blocked On hold for the time being AMP Format Issues that need to be addressed in the AMP story format labels May 20, 2020
@@ -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,
Copy link
Collaborator Author

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.

Copy link
Contributor

@barklund barklund left a 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]
Copy link
Contributor

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?

Copy link
Collaborator Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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/library/libraryTabs.js Outdated Show resolved Hide resolved
assets/src/edit-story/utils/useLiveRegion.js Outdated Show resolved Hide resolved
Copy link
Contributor

@barklund barklund left a 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!

.eslintrc Outdated Show resolved Hide resolved
@spacedmonkey
Copy link
Contributor

Small nitpicks, but we are looking good.

@swissspidy swissspidy merged commit 3073f27 into master May 21, 2020
@swissspidy swissspidy deleted the fix/eslint-plugin-react-hooks branch May 21, 2020 07:42
@dvoytenko
Copy link
Contributor

@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
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

obetomuniz added a commit that referenced this pull request May 25, 2020
* 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
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Infrastructure Changes impacting testing infrastructure or build tooling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add eslint-plugin-react-hooks to the JavaScript project
6 participants