Repository navigation
docs: clarify that immutability lint rule does not flag method mutations without inferred types - #8769
Open
pawannyadav05 wants to merge 1 commit into
Conversation
…ons without inferred types The immutability lint documentation previously listed array push() and sort() under 'Invalid' examples as if the rule flagged them. However, per react/react#35157, eslint-plugin-react-hooks only produces diagnostics on property assignments unless the compiler can infer the type. This updates the examples to present property assignment as the verified invalid example and adds an explanatory Note explaining why method mutations do not emit a diagnostic while remaining bad practice. Fixes reactjs#8758
Size changesDetails📦 Next.js Bundle Analysis for react-devThis analysis was generated by the Next.js Bundle Analysis action. 🤖 This PR introduced no changes to the JavaScript bundle! 🙌 |
This branch was successfully deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #8758
Summary
Updates the
immutabilityESLint rule reference page to accurately reflect rule diagnostics: retains verified object property assignments under### Invalidand moves mutating methods (push(),sort()) into an instructional Note explaining compiler inference boundaries.Problem
The
immutabilityrule documentation previously presented three examples under### Invalid:items.push(4); setItems(items);user.name = 'Bob'; setUser(user);setItems(items.sort());However, as reported in #8758 and tracked in
react/react#35157,eslint-plugin-react-hooksonly emits diagnostics on direct property assignments (likeuser.name = 'Bob'). Calling mutating methods on state variables produces no diagnostic unless the compiler can fully infer the state type (which it does not infer for arbitrary state arrays). Presentingpush()andsort()as cases flagged by the rule misleads developers when their linter produces no error.Solution
### Invalidas the verified case flagged by the linter.items.push(4)oritems.sort()will not trigger a linter diagnostic without inferred types, mutating state in-place is still prohibited because identical references prevent re-renders.Changes
src/content/reference/eslint-plugin-react-hooks/lints/immutability.md: Refined the Invalid section and added the note on method mutations.