fix(compiler): detect setState in effects when useState declared later (#35291) #35299
+130
−193
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.
Closes #35291
The
validateNoSetStateInEffectsvalidator missedsetStatecalls inside effects whenuseStatewas declared after the firstuseEffect.This change adds a second pass over lowered function expressions (effect callbacks) to collect all setters, fixing the false negative with zero runtime cost.
Includes new fixture that now correctly reports 2 diagnostics.
Reproduction: https://github.com/roryabraham/set-state-in-use-effect-bug
Tested with:
yarn node ./scripts/jest/jest.js compiler/packages/babel-plugin-react-compiler/src/tests/ValidateNoSetStateInEffects.test.ts --maxWorkers=1
All tests pass, new fixture reports 2 diagnostics.
Thanks for the review!
Hi @josephsavona @lunaleaps @sammybir @poteto — hope you’re all doing great!
This small PR fixes a long-standing false-negative in the React Forget validator (#35291) that’s been affecting users for weeks:
setStatecalls inside effects were being incorrectly allowed whenuseStatewas declared after the firstuseEffect. The fix adds a second pass over lowered function expressions to collect late-declared setters — zero runtime cost, fully backwards compatible.I’ve added a new fixture that now correctly reports 2 diagnostics instead of 0, and all existing tests pass cleanly.
Would really appreciate a quick look/merge when any of you have a moment — this one unblocks a bunch of real-world code that people have been hitting. Thank you so much for everything you do for the compiler!
❤️