[MIL-1550] fix useNavigation destructure false positive#481
Open
rayhanadev wants to merge 2 commits into
Open
Conversation
|
✅ No new issues Reviewed by reactreview for commit 1717270. Configure here. |
There was a problem hiding this comment.
Pull request overview
This PR updates the react-compiler-destructure-method architecture rule to avoid recommending method destructuring for React Navigation’s useNavigation() return value, since React Navigation’s documented API expects method calls off the navigation object (e.g. navigation.navigate(...)).
Changes:
- Added an import-source keyed carve-out (
HOOK_IMPORT_SOURCES_WITH_UNSAFE_METHOD_DESTRUCTURING) so specific hook/module combinations can opt out of method-destructuring advice. - Suppressed
react-compiler-destructure-methodhits foruseNavigationwhen imported from@react-navigation/nativeor@react-navigation/core. - Added regression tests to ensure React Navigation is not flagged while similarly named non-RN hooks still are.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/react-doctor/tests/regressions/architecture-rules.test.ts | Adds regression coverage for React Navigation and custom useNavigation cases. |
| packages/oxlint-plugin-react-doctor/src/plugin/rules/architecture/react-compiler-destructure-method.ts | Implements import-source-based suppression for unsafe hook-method destructuring (React Navigation). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
132
to
136
| const allowedMethods = HOOK_OBJECTS_WITH_METHODS.get(hookSource); | ||
| if (!allowedMethods || !allowedMethods.has(methodName)) return; | ||
| if (isUnsafeMethodDestructureHookImport(node, hookSource)) return; | ||
|
|
||
| if (!isNodeOfType(node.parent, "CallExpression") || node.parent.callee !== node) return; |
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Why?
react-compiler-destructure-methodrecommends destructuring methods from hook return values so React Compiler can reason about stable method references and dependencies more clearly. That guidance is still useful for ordinary hook objects, but it is not a safe recommendation for React Navigation'suseNavigation()API.React Navigation documents
useNavigationas a hook imported from@react-navigation/nativethat returns the screen'snavigationobject, and its examples call methods asnavigation.navigate(...)rather than destructuring the method first: https://reactnavigation.org/docs/use-navigation. The navigation object reference also describesnavigate,goBack,dispatch,setOptions, and related APIs as methods on thenavigationobject, and notes that navigation functions such asnavigateusedispatchbehind the scenes: https://reactnavigation.org/docs/navigation-object.The implementation matches that public contract:
useNavigationreturns the navigation object from React Navigation context (or the root navigation ref fallback), not an arbitrary plain hook-return object owned by userland code: https://github.com/react-navigation/react-navigation/blob/main/packages/core/src/useNavigation.tsx. Because the rule is a compiler-oriented hint, it should avoid suggesting a rewrite that diverges from React Navigation's documented object-method usage.What changed?
HOOK_IMPORT_SOURCES_WITH_UNSAFE_METHOD_DESTRUCTURINGmap so import-source carve-outs can be extended for future unsafe method-bearing hook APIs without adding bespoke rule branches.react-compiler-destructure-methodonly when the tracked hook is imported from a known unsafe source. Today that covers React Navigation'suseNavigationfrom@react-navigation/nativeand@react-navigation/core.useNavigation()hooks, so similarly named userland hooks still report.@react-navigation/native,@react-navigation/core, and the custom-hook case that should still report.Before:
The rule reported
navigation.navigate(...)and suggested destructuringnavigate.After:
The React Navigation call is no longer reported, while custom/non-React-Navigation
useNavigation()hooks still receive the destructuring recommendation.Test plan
Users can verify correctness by running: