dillon1000/react
Commit
Browse files [RFC] useEffect dependency memoization check
This is one approach to testing whether useEffect dependencies are memoized. The idea is based off the observation that the only reason dependencies wouldn't be memoized (other than compiler bugs) is that they are mutated later. If they're mutated later, then the dep array will have a mutable range which encompasses the InstructionId of the useEffect call. So we look for that pattern and throw a validation error. The downside of this approach is that we might reject code that happens to be valid: specifically, that the lack of memoization isn't a problem in practice because the effect won't trigger a loop. But (per test plan) this doesn't seem to introduce that many new bailouts on www. Rather than implement a complex validation that checks whether we un-memoized something that was memoized in the input, it seems more practical to: 1. Enable this more comprehensive validation against any form of un-memo'd effect dependency 2. Flip the default for hooks (to assume they follow the rules), which will fix the primary cause of Forget pessimistically not memoizing dependencies. ## Test Plan Synced to www and checked output via the upgrade script: a few components stop getting memoized bc they have un-memoized effect dependencies. Let's chat!
Changed paths13 files
First-parent comparisoncompiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/HIR/Globals.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/HIR/ObjectShape.ts ModifiedA compiler/packages/babel-plugin-react-forget/src/Validation/ValidateMemoizedEffectDependencies.ts AddedM compiler/packages/babel-plugin-react-forget/src/Validation/index.ts ModifiedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized.js AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.invalid-useInsertionEffect-dep-not-memoized.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.invalid-useInsertionEffect-dep-not-memoized.js AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.invalid-useLayoutEffect-dep-not-memoized.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.invalid-useLayoutEffect-dep-not-memoized.js AddedPatch
Files changed
Rendering syntax-highlighted changes…