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 specify stable results of custom hooks #1484

Closed
1 task done
atomiks opened this issue Jan 9, 2024 · 1 comment
Closed
1 task done

Comments

@atomiks
Copy link

atomiks commented Jan 9, 2024

Environment information

CLI:
  Version:                      1.5.0
  Color support:                true

Platform:
  CPU Architecture:             aarch64
  OS:                           macos

Environment:
  BIOME_LOG_DIR:                unset
  NO_COLOR:                     unset
  TERM:                         "xterm-256color"
  JS_RUNTIME_VERSION:           "v20.10.0"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         "npm/10.2.3"

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

Workspace:
  Open Documents:               0

Rule name

useExhaustiveDependencies

Playground link

https://biomejs.dev/playground/?code=aQBtAHAAbwByAHQAIAAqACAAYQBzACAAUgBlAGEAYwB0ACAAZgByAG8AbQAgACcAcgBlAGEAYwB0ACcAOwAKAAoAdAB5AHAAZQAgAEEAbgB5AEYAdQBuAGMAdABpAG8AbgAgAD0AIAAoAC4ALgAuAGEAcgBnAHMAOgAgAHUAbgBrAG4AbwB3AG4AWwBdACkAIAA9AD4AIAB1AG4AawBuAG8AdwBuADsACgAKAGYAdQBuAGMAdABpAG8AbgAgAHUAcwBlAEUAZgBmAGUAYwB0AEUAdgBlAG4AdAA8AFQAIABlAHgAdABlAG4AZABzACAAQQBuAHkARgB1AG4AYwB0AGkAbwBuAD4AKABjAGEAbABsAGIAYQBjAGsAPwA6ACAAVAApACAAewAKACAAIABjAG8AbgBzAHQAIAByAGUAZgAgAD0AIABSAGUAYQBjAHQALgB1AHMAZQBSAGUAZgA8AEEAbgB5AEYAdQBuAGMAdABpAG8AbgAgAHwAIAB1AG4AZABlAGYAaQBuAGUAZAA%2BACgAKAApACAAPQA%2BACAAewAKACAAIAAgACAAdABoAHIAbwB3ACAAbgBlAHcAIABFAHIAcgBvAHIAKAAnAEMAYQBuAG4AbwB0ACAAYwBhAGwAbAAgAGEAbgAgAGUAdgBlAG4AdAAgAGgAYQBuAGQAbABlAHIAIAB3AGgAaQBsAGUAIAByAGUAbgBkAGUAcgBpAG4AZwAuACcAKQA7AAoAIAAgAH0AKQA7AAoACgAgACAAUgBlAGEAYwB0AC4AdQBzAGUASQBuAHMAZQByAHQAaQBvAG4ARQBmAGYAZQBjAHQAKAAoACkAIAA9AD4AIAB7AAoAIAAgACAAIAByAGUAZgAuAGMAdQByAHIAZQBuAHQAIAA9ACAAYwBhAGwAbABiAGEAYwBrADsACgAgACAAfQApADsACgAKACAAIAByAGUAdAB1AHIAbgAgAFIAZQBhAGMAdAAuAHUAcwBlAEMAYQBsAGwAYgBhAGMAawA8AEEAbgB5AEYAdQBuAGMAdABpAG8AbgA%2BACgACgAgACAAIAAgACgALgAuAC4AYQByAGcAcwApACAAPQA%2BACAAcgBlAGYALgBjAHUAcgByAGUAbgB0AD8ALgAoAC4ALgAuAGEAcgBnAHMAKQAsAAoAIAAgACAAIABbAF0ALAAKACAAIAApACAAYQBzACAAVAA7AAoAfQAKAGYAdQBuAGMAdABpAG8AbgAgAEEAcABwACgAKQAgAHsACgAgACAAYwBvAG4AcwB0ACAAcwB0AGEAYgBsAGUAIAA9ACAAdQBzAGUARQBmAGYAZQBjAHQARQB2AGUAbgB0ACgAKAApACAAPQA%2BACAAewB9ACkAOwAKACAAIABSAGUAYQBjAHQALgB1AHMAZQBFAGYAZgBlAGMAdAAoACgAKQAgAD0APgAgAHsACgAgACAAIAAgAHMAdABhAGIAbABlACgAKQA7AAoAIAAgAH0ALAAgAFsAXQApADsACgB9AA%3D%3D

Expected result

I'm not sure if I understand the purpose of closureIndex and dependenciesIndex for this rule - shouldn't you specify the stability of the result(s), not the arguments passed?

For example, I want to declare my custom useEffectEvent ponyfill returns a stable result:

{
  "$schema": "https://biomejs.dev/schemas/1.5.0/schema.json",
  "linter": {
    "rules": {
      "correctness": {
        "useExhaustiveDependencies": {
          "level": "error",
          "options": {
            "hooks": [
              {
                "name": "useEffectEvent",
                "closureIndex": 0
              }
            ]
          }
        }
      }
    }
  }
}
import * as React from 'react';

type AnyFunction = (...args: unknown[]) => unknown;

function useEffectEvent<T extends AnyFunction>(callback?: T) {
  const ref = React.useRef<AnyFunction | undefined>(() => {
    throw new Error('Cannot call an event handler while rendering.');
  });

  React.useInsertionEffect(() => {
    ref.current = callback;
  });

  return React.useCallback<AnyFunction>(
    (...args) => ref.current?.(...args),
    [],
  ) as T;
}
function App() {
  const stable = useEffectEvent(() => {});
  React.useEffect(() => {
    stable();
  }, []);
}
index.ts:29:9 lint/correctness/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  ✖ This hook does not specify all of its dependencies.
  
    27 │ function App() {
    28 │   const stable = useEffectEvent(() => {});
  > 29 │   React.useEffect(() => {
       │         ^^^^^^^^^
    30 │     stable();
    31 │   }, []);

Code of Conduct

  • I agree to follow Biome's Code of Conduct
@arendjr
Copy link
Contributor

arendjr commented Jan 14, 2024

The hooks configuration allows you to configure hooks whose closures should be checked against a list of dependencies, similar to how the dependencies array of useEffect() is checked, for instance. For useEffect(), closureIndex is 0 (since that argument contains the closure to check) and dependenciesIndex is 1 (since that argument is the dependencies array).

I think what you are looking for is mentioned in this issue: #1128

So I’ll close this as a duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants