-
-
Notifications
You must be signed in to change notification settings - Fork 97
Add react exhaustive dependencies linter rule #657
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
Open
BitwiseAndrea
wants to merge
11
commits into
Kampfkarren:main
Choose a base branch
from
BitwiseAndrea:feature/react-missing-dependencies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
af06a89
Add react missing dependencies linter rule
BitwiseAndrea 8a7c824
format
BitwiseAndrea 4cf7b56
format
BitwiseAndrea c55edb7
Update selene-lib/src/lints/roblox_react_exhaustive_deps.rs
BitwiseAndrea 400ea08
uhhh ok
BitwiseAndrea 4d0b718
Merge branch 'feature/react-missing-dependencies' of https://github.c…
BitwiseAndrea 60703f9
address PR feedback
BitwiseAndrea de5e133
update the stderr stuff
BitwiseAndrea 8dba752
Discard changes to selene-lib/src/lib.rs
BitwiseAndrea 27f2245
add tests from chris's pr
BitwiseAndrea 00176ec
and add changelog of course!
BitwiseAndrea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # roblox_react_exhaustive_deps | ||
|
|
||
| ## What it does | ||
| Checks that all dependencies used inside React/Roact hooks like `useEffect`, `useCallback`, and `useMemo` are correctly specified in the dependency array. | ||
|
|
||
| ## Why this is bad | ||
| Missing dependencies can cause your effects or memoized values to use stale data, leading to bugs. Unnecessary dependencies can cause unnecessary re-renders or re-computations, hurting performance. | ||
|
|
||
| When you reference a variable inside a React hook callback, React expects you to declare it as a dependency so it knows when to re-run the effect or recompute the value. Failing to do so can lead to: | ||
|
|
||
| 1. **Stale closures**: The callback captures the variable's value from when it was created, not its current value | ||
| 2. **Missing updates**: Changes to dependencies won't trigger the effect to re-run | ||
| 3. **Hard-to-debug issues**: The behavior might work initially but break when component re-renders occur in different orders | ||
|
|
||
| ## Example | ||
|
|
||
| ### Missing dependencies | ||
| ```lua | ||
| local React = require(Packages.React) | ||
|
|
||
| local function Component(props) | ||
| -- Bad: props.userId is used but not in dependencies | ||
| React.useEffect(function() | ||
| fetchUser(props.userId) | ||
| end, {}) | ||
|
|
||
| -- Good: all dependencies are listed | ||
| React.useEffect(function() | ||
| fetchUser(props.userId) | ||
| end, { props.userId }) | ||
| end | ||
| ``` | ||
|
|
||
| ### Unnecessary dependencies | ||
| ```lua | ||
| -- Bad: count is not used in the effect | ||
| React.useEffect(function() | ||
| print("Hello") | ||
| end, { count }) | ||
|
|
||
| -- Good: empty dependencies since nothing is used | ||
| React.useEffect(function() | ||
| print("Hello") | ||
| end, {}) | ||
| ``` | ||
|
|
||
| ### Property access dependencies | ||
| ```lua | ||
| -- Bad: props.user.name is used but not in dependencies | ||
| React.useEffect(function() | ||
| setTitle(props.user.name) | ||
| end, {}) | ||
|
|
||
| -- Good: property access is correctly tracked | ||
| React.useEffect(function() | ||
| setTitle(props.user.name) | ||
| end, { props.user.name }) | ||
| ``` | ||
|
|
||
| ## Supported hooks | ||
| This lint checks the following React/Roact hooks: | ||
| - `useEffect` - dependencies are the second parameter | ||
| - `useLayoutEffect` - dependencies are the second parameter | ||
| - `useCallback` - dependencies are the second parameter | ||
| - `useMemo` - dependencies are the second parameter | ||
| - `useImperativeHandle` - dependencies are the third parameter | ||
|
|
||
| ## Remarks | ||
| This lint works with both the legacy Roact API (using `Roact.useEffect`) and the new React-like API (using `React.useEffect`). | ||
|
|
||
| The lint analyzes variable references within the hook callback and compares them against the declared dependency array. It will: | ||
|
|
||
| 1. Report missing dependencies that are used in the callback but not listed | ||
| 2. Report unnecessary dependencies that are listed but not used | ||
| 3. Track property access (e.g., `props.value`) as separate dependencies | ||
| 4. Ignore built-in Lua globals and Roblox APIs (like `print`, `game`, `workspace`, etc.) | ||
|
|
||
| ### Limitations | ||
| - The lint assumes variables are stable and doesn't track complex control flow | ||
| - It doesn't detect dependencies in nested function definitions | ||
| - Complex dependency expressions (computed property access with brackets) may not be analyzed correctly | ||
| - setState functions and refs are currently not detected as stable (unlike in React) | ||
|
|
||
| This lint is only active when using the Roblox standard library. | ||
|
|
||
| ## Configuration | ||
| This lint does not have any configuration options. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.