+
Skip to content

feat(biome_js_analyze): allow specifying stable object keys in useExhaustiveDependencies configuration #6398

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

Merged

Conversation

josh-
Copy link

@josh- josh- commented Jun 19, 2025

Summary

Closes #6386

Currently Biome supports specifying that React hook results are stable when hook returns either a single value or an array. This PR adds support for specifying stable object keys inside a hook return value as well.

For example, this hook returns a stable setMyState function:

const useCustomHook = () => {
  const [myState, setMyState] = useState(false)
  return {myState, setMyState}
}

const Component = () => {
  const {myState, setMyState} = useCustomHook()
  const toggleMyState = useCallback(() => {
    setMyState(!myState);
  }, [myState]);

  return <button onClick={toggleMyState}>button</button>
}

but when linting with Biome the following error is generated:

file.tsx:26:25 lint/correctness/useExhaustiveDependencies  FIXABLE  
  ✖ This hook does not specify all of its dependencies: setMyState
  
    24 │ const Component = () => {
    25 │   const {myState, setMyState} = useCustomHook()
  > 26 │   const toggleMyState = useCallback(() => {
       │                         ^^^^^^^^^^^
    27 │     setMyState(!myState);
    28 │   }, [myState]);
  
  ℹ This dependency is not specified in the hook dependency list.
  
    25 │   const {myState, setMyState} = useCustomHook()
    26 │   const toggleMyState = useCallback(() => {
  > 27 │     setMyState(!myState);
       │     ^^^^^^^^^^
    28 │   }, [myState]);
    29 │ 
  
  ℹ Unsafe fix: Add the missing dependencies to the list.
  
    28 │ ··},·[myState,·setMyState]);
       │              ++++++++++++

with no ability to specify that this value is stable without suppressing the rule.

This PR adds a new option which allows specifying that such object properties are stable:

"useExhaustiveDependencies": {
    "level": "error",
    "options": {
        "hooks": [{
            "name": "useCustomHook",
            "stableResult": [
                "setMyState"
            ]
        }]
    }
}

Test Plan

Added two tests:

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Jun 19, 2025
@josh- josh- force-pushed the use-exhaustive-dependencies-stable-object-keys branch from a1252bd to 1d0e6aa Compare June 19, 2025 00:12
Copy link

codspeed-hq bot commented Jun 19, 2025

CodSpeed Performance Report

Merging #6398 will degrade performances by 61.17%

Comparing josh-:use-exhaustive-dependencies-stable-object-keys (7ad42c8) with next (275cc8b)

Summary

⚡ 2 improvements
❌ 2 regressions
✅ 110 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
js_analyzer[index_3894593175024091846.js] 49.4 ms 52.7 ms -6.18%
json_formatter[eucjp_1600564308684076393.json] 2.6 ms 2.5 ms +6.14%
deserialize_from_json_str[package.json] 1,038.3 µs 930.8 µs +11.55%
deserialize_from_json_str[tsconfig.json] 218 µs 561.5 µs -61.17%

Copy link
Member

@siketyan siketyan left a comment

Choose a reason for hiding this comment

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

Nice and very clean implementation! I can't believe this is your first time contributing to Biome 🤯 Could you update the outdated snapshots so the failing tests should pass?

@josh- josh- force-pushed the use-exhaustive-dependencies-stable-object-keys branch 2 times, most recently from ba66358 to 4cfbd58 Compare June 19, 2025 03:10
@josh-
Copy link
Author

josh- commented Jun 19, 2025

@siketyan thank you for the kind words! 😄 appreciate all the effort that's gone into the contributing guide!

@josh- josh- force-pushed the use-exhaustive-dependencies-stable-object-keys branch from 4cfbd58 to 37a9631 Compare June 22, 2025 23:19
Copy link

changeset-bot bot commented Jun 22, 2025

🦋 Changeset detected

Latest commit: 7ad42c8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages
Name Type
@biomejs/biome Minor
@biomejs/cli-win32-x64 Minor
@biomejs/cli-win32-arm64 Minor
@biomejs/cli-darwin-x64 Minor
@biomejs/cli-darwin-arm64 Minor
@biomejs/cli-linux-x64 Minor
@biomejs/cli-linux-arm64 Minor
@biomejs/cli-linux-x64-musl Minor
@biomejs/cli-linux-arm64-musl Minor
@biomejs/wasm-web Minor
@biomejs/wasm-bundler Minor
@biomejs/wasm-nodejs Minor
@biomejs/backend-jsonrpc Patch
@biomejs/js-api Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@josh-
Copy link
Author

josh- commented Jun 23, 2025

@siketyan just pushed a new commit which should hopefully improve perf - let me know what you think!

Copy link
Contributor

@arendjr arendjr left a comment

Choose a reason for hiding this comment

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

Very nice work indeed, thank you!

@arendjr arendjr added this to the Biome 2.1 milestone Jun 23, 2025
@ematipico ematipico modified the milestones: Biome 2.1, Biome v2.2 Jul 8, 2025
@arendjr
Copy link
Contributor

arendjr commented Jul 8, 2025

I apologise, we had hoped to include this one in the Biome 2.1 release, but overlooked it. Could you please rebase the PR on top of next so we can make sure to include it in the next?

@josh- josh- force-pushed the use-exhaustive-dependencies-stable-object-keys branch 2 times, most recently from 656d219 to 0125914 Compare July 9, 2025 11:25
@github-actions github-actions bot added A-CLI Area: CLI A-Parser Area: parser A-Diagnostic Area: diagnostocis labels Jul 9, 2025
@josh- josh- changed the base branch from main to next July 9, 2025 11:27
@josh- josh- force-pushed the use-exhaustive-dependencies-stable-object-keys branch from 0125914 to 7ad42c8 Compare July 9, 2025 11:29
@josh-
Copy link
Author

josh- commented Jul 9, 2025

No worries at all @arendjr - just rebased on next

@github-actions github-actions bot removed A-Parser Area: parser A-Diagnostic Area: diagnostocis labels Jul 9, 2025
@arendjr arendjr merged commit d1a315d into biomejs:next Jul 9, 2025
26 of 27 checks passed
@arendjr
Copy link
Contributor

arendjr commented Jul 9, 2025

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-CLI Area: CLI A-Linter Area: linter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📎 Allow keys returned from an object to be listed as stable result
4 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载