-
Notifications
You must be signed in to change notification settings - Fork 18
feat(enforce-exhaustive-useUnit-destructuring): add new rule for destructured units #175
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
kireevmp
merged 12 commits into
effector:master
from
Olovyannikov:new/use-unit-destructuring
Apr 13, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
545e9af
feat(rule): add new rule for destructured units
Olovyannikov 65e072f
docs(rule): add docs for new rule
Olovyannikov a8fc73c
feat(use-unit-destructuring): update to v17
Olovyannikov e8e65b7
fix(files): remove legacy empty files
Olovyannikov 40bd653
refactor(review): update tests,aliasing,refactor
Olovyannikov b0e9f02
refactor(esquery): support esquery dynamic selectors
Olovyannikov 25e16c9
refactor(test): add error column and line indentifier
Olovyannikov a3ec5e1
fix(test): bringing testcase names to a common format
Olovyannikov aa98a41
refactor: moved common functions to shared
Olovyannikov 4c53dde
refactor: moved feature-specific code from shared
Olovyannikov c508ed4
refactor: remove unnecessary lib dir from rule
Olovyannikov b8243b7
chore(changeset): changeset added
Olovyannikov 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "eslint-plugin-effector": minor | ||
| --- | ||
|
|
||
| Add new rule `enforce-exhaustive-useUnit-destructuring` |
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 |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| .idea/ | ||
|
|
||
| # Logs | ||
| logs | ||
| *.log | ||
|
|
||
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
157 changes: 157 additions & 0 deletions
157
...ce-exhaustive-useUnit-destructuring/enforce-exhaustive-useUnit-destructuring.md
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,157 @@ | ||
| --- | ||
| description: Ensure all units passed to useUnit are properly destructured to avoid unused subscriptions and implicit re-renders. | ||
| --- | ||
|
|
||
| # effector/enforce-exhaustive-useUnit-destructuring | ||
|
|
||
| [Related documentation](https://effector.dev/en/api/effector-react/useunit/) | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule enforces that: | ||
|
|
||
| - All properties passed in an object to useUnit must be destructured to prevent implicit subscriptions; | ||
| - All elements passed in an array to useUnit must be destructured to prevent implicit subscriptions also. | ||
|
|
||
| ### Object shape | ||
|
|
||
| When using useUnit with an object, you must destructure all keys that you pass. Otherwise, unused units will still | ||
| create subscriptions and cause unnecessary re-renders. | ||
| TypeScript | ||
|
|
||
| ```ts | ||
| // 👍 correct - all properties are destructured | ||
| const { value, setValue } = useUnit({ | ||
| value: $store, | ||
| setValue: event, | ||
| }); | ||
| ``` | ||
|
|
||
| ```ts | ||
| // 👎 incorrect - setValue is not destructured but still creates subscription | ||
| const { value } = useUnit({ | ||
| value: $store, | ||
| setValue: event, // unused but subscribed! | ||
| }); | ||
| ``` | ||
|
|
||
| ```ts | ||
| // 👎 incorrect - extra is destructured but not passed | ||
| const { | ||
| value, | ||
| setValue, | ||
| extra // extra is missing - will be undefined | ||
| } = useUnit({ | ||
| value: $store, | ||
| setValue: event, | ||
| }); | ||
| ``` | ||
|
|
||
| ### Array shape | ||
|
|
||
| When using useUnit with an array, you must destructure all elements. Elements that are not destructured will still | ||
| create subscriptions, leading to implicit re-renders. | ||
| TypeScript | ||
|
|
||
| ```ts | ||
| // 👍 correct - all elements are destructured | ||
| const [value, setValue] = useUnit([$store, event]); | ||
| ``` | ||
|
|
||
| ```ts | ||
| // 👎 incorrect - $store is not destructured but creates implicit subscription | ||
| const [setValue] = useUnit([event, $store]); | ||
| // Component will re-render when $store changes, even though you don't use it! | ||
| ``` | ||
|
|
||
| ```ts | ||
| // 👎 incorrect - event and $anotherStore cause implicit subscriptions | ||
| const [value] = useUnit([$store, event, $anotherStore]); | ||
| // Component re-renders on $store, event, and $anotherStore changes | ||
| ``` | ||
|
|
||
| ## Why is this important? | ||
|
|
||
| Implicit subscriptions can lead to: | ||
|
|
||
| - Performance issues: unnecessary re-renders when unused stores update | ||
| - Hard-to-debug behavior: component re-renders for unclear reasons | ||
| - Memory leaks: subscriptions that are never cleaned up properly | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Real-world example | ||
|
|
||
| ```tsx | ||
| import React, { Fragment } from "react"; | ||
| import { createEvent, createStore } from "effector"; | ||
| import { useUnit } from "effector-react"; | ||
|
|
||
| const $store = createStore("Hello World!"); | ||
| const event = createEvent(); | ||
|
|
||
| // 👎 incorrect | ||
| const BadComponent = () => { | ||
| const { value } = useUnit({ | ||
| value: $store, | ||
| setValue: event, // ❌ not used but subscribed! | ||
| }); | ||
|
|
||
| return <Fragment>{value}</Fragment>; | ||
| }; | ||
|
|
||
| // 👍 correct | ||
| const GoodComponent = () => { | ||
| const { value, setValue } = useUnit({ | ||
| value: $store, | ||
| setValue: event, | ||
| }); | ||
|
|
||
| return <button onClick={() => setValue("New value")}>{value}</button>; | ||
| }; | ||
| ``` | ||
|
|
||
| ```tsx | ||
| import React, { Fragment } from "react"; | ||
| import { createEvent, createStore } from "effector"; | ||
| import { useUnit } from "effector-react"; | ||
|
|
||
| const $store = createStore("Hello World!"); | ||
| const event = createEvent(); | ||
|
|
||
| // 👎 incorrect - implicit subscription to $store | ||
| const BadComponent = () => { | ||
| const [setValue] = useUnit([event, $store]); // ❌ $store not used but subscribed! | ||
|
|
||
| return <button onClick={() => setValue("New value")}>Click</button>; | ||
| }; | ||
|
|
||
| // 👍 correct - explicit destructuring | ||
| const GoodComponent = () => { | ||
| const [value, setValue] = useUnit([$store, event]); | ||
|
|
||
| return <button onClick={() => setValue("New value")}>{value}</button>; | ||
| }; | ||
|
|
||
| // 👍 also correct - only pass what you need | ||
| const AlsoGoodComponent = () => { | ||
| const [setValue] = useUnit([event]); // ✅ no implicit subscriptions | ||
|
|
||
| return <button onClick={() => setValue("New value")}>Click</button>; | ||
| }; | ||
| ``` | ||
|
|
||
| ### When Not To Use It | ||
|
|
||
| If you intentionally want to subscribe to a store without using its value (rare case), you can disable this rule for | ||
| that line: | ||
|
|
||
| ```tsx | ||
| // eslint-disable-next-line effector/enforce-exhaustive-useUnit-destructuring | ||
| const { value } = useUnit({ | ||
| value: $store, | ||
| trigger: $triggerStore, // intentionally subscribing without using | ||
| }); | ||
| ``` | ||
|
|
||
| However, in most cases, you should refactor your code to avoid implicit subscriptions. | ||
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.