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

💅 useExhaustiveDependencies does not trigger if dependency is specified after it is used #4355

Open
1 task done
Sjlver opened this issue Oct 21, 2024 · 2 comments
Open
1 task done
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug

Comments

@Sjlver
Copy link

Sjlver commented Oct 21, 2024

Environment information

CLI:
  Version:                      1.7.0
  Color support:                true

Platform:
  CPU Architecture:             x86_64
  OS:                           linux

Environment:
  BIOME_LOG_DIR:                unset
  NO_COLOR:                     unset
  TERM:                         "xterm-256color"
  JS_RUNTIME_VERSION:           "v20.5.1"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         "yarn/3.6.3"

Biome Configuration:
  Status:                       Loaded successfully
  Formatter disabled:           false
  Linter disabled:              false
  Organize imports disabled:    false
  VCS disabled:                 true

Linter:
  Recommended:                  true
  All:                          false
  Rules:                        a11y/useKeyWithClickEvents = "off"
                                correctness/noUnusedImports = "error"
                                correctness/useHookAtTopLevel = "error"
                                suspicious/noExplicitAny = "off"

Workspace:
  Open Documents:               0

Rule name

lint/correctness/useExhaustiveDependencies

Playground link

https://biomejs.dev/playground/?code=IgB1AHMAZQAgAGMAbABpAGUAbgB0ACIAOwAKAAoAaQBtAHAAbwByAHQAIABSAGUAYQBjAHQALAAgAHsAIAB1AHMAZQBDAGEAbABsAGIAYQBjAGsALAAgAHUAcwBlAFMAdABhAHQAZQAgAH0AIABmAHIAbwBtACAAIgByAGUAYQBjAHQAIgA7AAoACgBlAHgAcABvAHIAdAAgAGMAbwBuAHMAdAAgAEYAbwBvAEMAbwBuAHQAZQB4AHQAIAA9ACAAUgBlAGEAYwB0AC4AYwByAGUAYQB0AGUAQwBvAG4AdABlAHgAdAAoAHsACgAgACAAZgBvAG8AOg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G8AdgBpAGQAZQByACAAdgBhAGwAdQBlAD0AewB7ACAAZgBvAG8ALAAgAGkAcwBCAGEAcgAsACAAYgBhAHIAaQBmAHkAIAB9AH0APgAKAAkACQAJAHsAYwBoAGkAbABkAHIAZQBuAH0ACgAJAAkAPAAvAEYAbwBvAEMAbwBuAHQAZQB4AHQALgBQAHIAbwB2AGkAZABlAHIAPgAKAAkAKQA7AAoAfQA7AAoA

Expected result

Biome should recognize dependencies no matter whether they are specified before or after the hook that depends on them.

Biome handles this correctly:

export const WithFoo = ({ children }: { children: React.ReactNode }) => {
  const [foo, setFoo] = useState("");

  const isBar = useCallback(() => foo.startsWith("bar"), [foo]);

  const barify = useCallback(() => {
    if (!isBar()) setFoo("bar");
  }, []);

  return (
    <FooContext.Provider value={{ foo, isBar, barify }}>
      {children}
    </FooContext.Provider>
  );
};

In the above, Biome correctly warns that barify does not specify it's dependency on isBar. However, if I reverse the order of the two functions, Biome produces no warning.

Code of Conduct

  • I agree to follow Biome's Code of Conduct
@Sjlver Sjlver added the S-Needs triage Status: this issue needs to be triaged label Oct 21, 2024
@arendjr
Copy link
Contributor

arendjr commented Oct 21, 2024

I think the report is valid, but we need to be careful with how we resolve this one. Simply adding isBar to the list of dependencies in the playground would result in invalid code, since const/let variables cannot be referenced before their declaration. Ideally we still detect this case, but offer a separate suggestion to the user to point out they might want to change the order of declarations.

@arendjr arendjr added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug and removed S-Needs triage Status: this issue needs to be triaged labels Oct 21, 2024
@Sjlver
Copy link
Author

Sjlver commented Oct 21, 2024

I agree with @arendjr that the fix is not trivial. It's so confusing that JavaScript/TypeScript allows accesses to isBar inside the barify function, but not in the dependency list :/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#temporal_dead_zone_tdz has an explanation of this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug
Projects
None yet
Development

No branches or pull requests

2 participants