Skip to content

Commit 2f90d59

Browse files
author
Joe Savona
committed
[compiler] Implement exhaustive dependency checking for manual memoization
The compiler currently drops manual memoization and rewrites it using its own inference. If the existing manual memo dependencies has missing or extra dependencies, compilation can change behavior by running the computation more often (if deps were missing) or less often (if there were extra deps). We currently address this by relying on the developer to use the ESLint plugin and have `eslint-disable-next-line react-hooks/exhaustive-deps` suppressions in their code. If a suppression exists, we skip compilation. But not everyone is using the linter! Relying on the linter is also imprecise since it forces us to bail out on exhaustive-deps checks that only effect (ahem) effects — and while it isn't good to have incorrect deps on effects, it isn't a problem for compilation. So this PR is a rough sketch of validating manual memoization dependencies in the compiler. Long-term we could use this to also check effect deps and replace the ExhaustiveDeps lint rule, but for now I'm focused specifically on manual memoization use-cases. If this works, we can stop bailing out on ESLint suppressions, since the compiler will implement all the appropriate checks (we already check rules of hooks).
1 parent 96d9825 commit 2f90d59

File tree

5 files changed

+789
-0
lines changed

5 files changed

+789
-0
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ import {validateNoFreezingKnownMutableFunctions} from '../Validation/ValidateNoF
103103
import {inferMutationAliasingEffects} from '../Inference/InferMutationAliasingEffects';
104104
import {inferMutationAliasingRanges} from '../Inference/InferMutationAliasingRanges';
105105
import {validateNoDerivedComputationsInEffects} from '../Validation/ValidateNoDerivedComputationsInEffects';
106+
import {validateExhaustiveDependencies} from '../Validation/ValidateExhaustiveDependencies';
106107

107108
export type CompilerPipelineValue =
108109
| {kind: 'ast'; name: string; value: CodegenFunction}
@@ -294,6 +295,8 @@ function runWithEnvironment(
294295
inferReactivePlaces(hir);
295296
log({kind: 'hir', name: 'InferReactivePlaces', value: hir});
296297

298+
validateExhaustiveDependencies(hir).unwrap();
299+
297300
rewriteInstructionKindsBasedOnReassignment(hir);
298301
log({
299302
kind: 'hir',

compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,19 @@ export function areEqualPaths(a: DependencyPath, b: DependencyPath): boolean {
16361636
)
16371637
);
16381638
}
1639+
export function isSubPath(
1640+
subpath: DependencyPath,
1641+
path: DependencyPath,
1642+
): boolean {
1643+
return (
1644+
subpath.length <= path.length &&
1645+
subpath.every(
1646+
(item, ix) =>
1647+
item.property === path[ix].property &&
1648+
item.optional === path[ix].optional,
1649+
)
1650+
);
1651+
}
16391652

16401653
export function getPlaceScope(
16411654
id: InstructionId,

0 commit comments

Comments
 (0)