feat(compiler): clarify preserve-manual-memoization error for forward…#188
feat(compiler): clarify preserve-manual-memoization error for forward…#188everettbu wants to merge 2 commits into
Conversation
…d CHANGELOG entry under Unreleased for enhanced forward reference guidance in babel-plugin-react-compiler.
Greptile OverviewGreptile SummaryImproves the diagnostic message in Key changes:
Issue found:
Confidence Score: 3/5
Important Files ChangedFile Analysis
|
| /** | ||
| * Issue: When a useCallback references a value from a useMemo that is | ||
| * declared later in the component, the compiler triggers a false positive | ||
| * preserve-manual-memoization error. | ||
| * | ||
| * The error occurs because the validation checks that dependencies have | ||
| * completed their scope before the manual memo block starts. However, | ||
| * when the callback is declared before the useMemo, the useMemo's scope | ||
| * hasn't completed yet. | ||
| * | ||
| * This is a valid pattern in React - declaration order doesn't matter | ||
| * for the runtime behavior since both are memoized. | ||
| */ |
There was a problem hiding this comment.
logic: Comment describes "false positive preserve-manual-memoization error" but the actual error (see .expect.md) is "Cannot access variable before it is declared" from the Immutability category (ErrorCategory.Immutability in InferMutationAliasingEffects.ts:1278), not from PreserveManualMemo validation.
The comment also claims "This is a valid pattern in React - declaration order doesn't matter" but forward references in JavaScript do cause ReferenceErrors at runtime. The compiler error is correct.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-validation/error.useCallback-references-later-useMemo.ts
Line: 4:16
Comment:
**logic:** Comment describes "false positive preserve-manual-memoization error" but the actual error (see `.expect.md`) is "Cannot access variable before it is declared" from the Immutability category (ErrorCategory.Immutability in `InferMutationAliasingEffects.ts:1278`), not from PreserveManualMemo validation.
The comment also claims "This is a valid pattern in React - declaration order doesn't matter" but forward references in JavaScript **do** cause ReferenceErrors at runtime. The compiler error is correct.
How can I resolve this? If you propose a fix, please make it concise.|
Upstream PR was closed or merged. Code is synced via branch mirror. |
Mirror of facebook/react#35086
Original author: Rithvik914
… references
Improve diagnostic when a callback (useCallback/useMemo) depends on a memoized value declared later in the component. Adds explicit guidance to declare memoized values before callbacks that capture them and includes the dependency identifier when available.
Changes:
Testing:
Rationale:
Clearer messaging reduces confusion around declaration order vs dependency inference, speeding developer remediation.
Summary
How did you test this change?