Clarify purity for null-guarded ref initialization#674
Conversation
Greptile SummaryThis PR adds regression tests and documentation clarifying that the null-guard ref initialization exception (
Confidence Score: 5/5
Important Files Changed
Last reviewed commit: a494195 |
| if (ref.current === null) { | ||
| ref.current = Date.now(); | ||
| } | ||
| return <div>{ref.current}</div>; |
There was a problem hiding this comment.
Ref read in return also violates the Refs rule
ref.current is read inside the JSX return expression, which would itself be a Refs violation in addition to the purity violation being tested. Because this test suite runs only the Purity rule, the refs error is never surfaced, but the test component as written is invalid under both rules.
To keep the test focused solely on the purity regression, consider dropping the ref read from the return value:
| return <div>{ref.current}</div>; | |
| return <div />; |
This matches the style used in the new valid test in NoRefAccessInRender-tests.ts (line 28) and avoids unintentionally implying that reading ref.current in JSX is acceptable in this context.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/eslint-plugin-react-compiler/__tests__/ImpureFunctionCallsRule-test.ts
Line: 45
Comment:
**Ref read in return also violates the Refs rule**
`ref.current` is read inside the JSX return expression, which would itself be a `Refs` violation in addition to the purity violation being tested. Because this test suite runs only the `Purity` rule, the refs error is never surfaced, but the test component as written is invalid under _both_ rules.
To keep the test focused solely on the purity regression, consider dropping the ref read from the return value:
```suggestion
return <div />;
```
This matches the style used in the new valid test in `NoRefAccessInRender-tests.ts` (line 28) and avoids unintentionally implying that reading `ref.current` in JSX is acceptable in this context.
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#35987
Original author: angular-bootstrap
Summary
Clarifies the interaction between the
refsandpuritylints for null-guarded ref initialization.The null-guard pattern:
is allowed by the refs rule, but purity still applies to the initializer on the right-hand side. This change adds regression coverage for that behavior and updates the ref-validation docs to make the distinction explicit.
Changes
Add a purity regression test showing Date.now() still fails inside a null-guarded ref initialization.
Add a refs regression test showing the null-guarded stable assignment pattern remains allowed.
Clarify docs for validateNoRefAccessInRender to note that the exception only applies to ref access, not to impure initializer expressions.
Test Plan
Issue
Closes #35973