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

[ESLint] Preserve optional chains in declared dependencies #25932

Closed

Conversation

motiz88
Copy link
Contributor

@motiz88 motiz88 commented Dec 23, 2022

Summary

The react-hooks/exhaustive-deps lint rule currently strips ?. from declared dependencies (when it has a reason to rewrite them), which can turn a valid dependency into one that throws when evaluated. The issue is broader than what's fixed here (see e.g. #23248) and a comprehensive fix would require something closer to type analysis (with some notion of type guards / refinement).

This PR is narrow in scope and fixes the case where:

  1. The user has declared a dependency using optional chaining.
  2. We have no evidence to suggest (per the logic of [ESLint] Handle optional member chains #19275) that we should use non-optional access for the same dependency.
  3. We are suggesting a fix for some other issue in the same dependency array.

This is done by recording the optional chaining information while visiting the declared dependencies, instead of discarding it.

I'm not deeply familiar with the code for this lint rule, but reading #19275 and in particular this comment leads me to believe this fix is safe and correct:

// Most of our algorithm deals with dependency paths with optional chaining stripped.
// This function is the last step before printing a dependency, so now is a good time to
// check whether any members in our path are always used as optional-only. In that case,
// we will use ?. instead of . to concatenate those parts of the path.

How did you test this change?

Added a regression test and updated existing tests (some error messages were superficially changed).

Copy link
Collaborator

@gaearon gaearon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls fix prettier? yarn prettier

Copy link

github-actions bot commented Apr 9, 2024

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.

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Apr 9, 2024
Copy link

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!

@github-actions github-actions bot closed this Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Resolution: Stale Automatically closed due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants