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

📎 Break useExhaustiveDependencies into two rules #630

Closed
crutchcorn opened this issue Oct 30, 2023 · 12 comments · Fixed by #4135
Closed

📎 Break useExhaustiveDependencies into two rules #630

crutchcorn opened this issue Oct 30, 2023 · 12 comments · Fixed by #4135
Assignees
Labels
A-Linter Area: linter S-Enhancement Status: Improve an existing feature S-Help-wanted Status: you're familiar with the code base and want to help the project

Comments

@crutchcorn
Copy link
Contributor

Description

I was migrating to the newest version of Biome and noticed that it seems to group in two seemingly unrelated rules into one:

  • × This hook do not specify all of its dependencies.
  • × This hook specifies more dependencies than necessary.

Inside of useExhaustiveDependencies

However, there's a problem with doing so this way.

While does not specify all of its deps is a rule that's ported over from React's common ESLint rules, specifies more deps than necessary isn't a rule, as it doesn't break the React Rules of Hooks. Moreover, specifying additional deps is how you're able to get some functionality to work as-expected at times.

Because of this, we should:

  • specifies more deps than necessary should be broken into a new rule
  • This new rule should be marked as optional, not required
@ematipico ematipico added S-Help-wanted Status: you're familiar with the code base and want to help the project A-Linter Area: linter S-Enhancement Status: Improve an existing feature labels Oct 30, 2023
@ematipico
Copy link
Member

How would you call the new rule?

@crutchcorn
Copy link
Contributor Author

What about useOnlyRequiredDependencies?

@arendjr
Copy link
Contributor

arendjr commented Dec 31, 2023

noExcessiveDependencies? :)

@arendjr
Copy link
Contributor

arendjr commented Apr 18, 2024

If we implement the suggestion from #2509 , do you think we can close this issue? If I understand correctly, the main reason for splitting is to be able to disable one type of check, while leaving others enabled. But if we can make exceptions per dependency, I think that use case is also covered.

@arendjr
Copy link
Contributor

arendjr commented Apr 21, 2024

Closing in favor of #2509.

@arendjr arendjr closed this as completed Apr 21, 2024
@flatplate
Copy link

I think this suggestion still makes sense even with the special ignore.

A way to disable "more dependencies than necessary" rule at least for useEffect and useLayoutEffect should be provided imo since that is what useEffect is used for.

I am migrating a large codebase right now and having to ignore thousands lines should be unnecessary.

@arendjr
Copy link
Contributor

arendjr commented Jul 11, 2024

It’s a fair point, although if it is indeed specific to these hooks (and not others), maybe a more specific configuration option might make sense instead of splitting 🤔

@arendjr arendjr reopened this Jul 11, 2024
@flatplate
Copy link

I agree that an option for this rule, or maybe in the hook options, would be enough.

@emigdio821
Copy link

I agree with @flatplate .
I recently migrated a project to Biome. Currently I have a simple React custom hook and this error is showing up. The only way I found to fix it is to use the // biome-ignore comment.
5512437009856178111_121

@wlechowicz
Copy link

I'd like to highlight that the messaging for the rule doesn't help either. If a useEffect has a dependency on a component prop (an "extra" dependency, that's not directly referenced in the effect code), then the message from Biome incorrectly states that:

× This hook specifies more dependencies than necessary: elementId
i Outer scope values aren't valid dependencies because mutating them doesn't re-render the component.

which is a misleading, false statement - a component prop is not an "outer scope value" and it sure as hell does "re-render the component". It's not until I started tinkering with the code that I understood that Biome is just unhappy that there is a dependency that's not referenced in the code, but the code is perfectly valid as per official React guidelines. While the "This hook specifies more dependencies than necessary" part makes sense (and could be configurable separately, per this discussion), the second piece of info looks like Biome failed to recognized that the referenced dependency is a reactive value.

@simon-paris
Copy link
Contributor

I'd like to implement this (as an option).

And I'd also like to make an option to re-enable this behavior: #608

But how should the config object look? Something like this?

{
  hooks: [...]
  reportMissingDependenciesArray: false,
  reportUnnecessaryDependencies: true,
}

But I noticed that the existing config schema is designed to be shared between multiple rules (but it's not actually shared anymore) and adding new keys would change that design.

@arendjr
Copy link
Contributor

arendjr commented Sep 27, 2024

Thanks! I’ll assign the issue to you then, @simon-paris.

I don’t see an issue with the options as you proposed them. I would suggest it’s better to implement them in separate PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter S-Enhancement Status: Improve an existing feature S-Help-wanted Status: you're familiar with the code base and want to help the project
Projects
None yet
7 participants