Skip to content

Commit d8f72f9

Browse files
committed
[Compiler] Change ValidateNoDerivedComputationsInEffect logic to track prop and local state derived values variables and add extra tests
Summary: Biggest change of the stack, we track how values prop and local state values are derived throughout the entire component. We are iterating over instructions instead of effects since some mutations can not be caught otherwise. For every derivation we track the type of value its coming from (props or local state) and also the top most relevant sources (These would be the ones that are actually named instead of promoted like t0) We propagate these relevant sources to each derivation. This allows us to catch more complex useEffects though right now we are overcapturing some more complex cases which will be refined further up the stack. This PR also adds a couple tests we will work towards fixing Test Plan: Added: ref-conditional-in-effect-no-error effect-contains-prop-function-call-no-error derived-state-from-ref-and-state-no-error
1 parent f3172ce commit d8f72f9

File tree

36 files changed

+826
-719
lines changed

36 files changed

+826
-719
lines changed

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

Lines changed: 5 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 {validateNoDerivedComputationsInEffects_exp} from '../Validation/ValidateNoDerivedComputationsInEffects_exp';
106107
import {nameAnonymousFunctions} from '../Transform/NameAnonymousFunctions';
107108

108109
export type CompilerPipelineValue =
@@ -275,6 +276,10 @@ function runWithEnvironment(
275276
validateNoDerivedComputationsInEffects(hir);
276277
}
277278

279+
if (env.config.validateNoDerivedComputationsInEffects_exp) {
280+
validateNoDerivedComputationsInEffects_exp(hir);
281+
}
282+
278283
if (env.config.validateNoSetStateInEffects) {
279284
env.logErrors(validateNoSetStateInEffects(hir, env));
280285
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ export const EnvironmentConfigSchema = z.object({
334334
*/
335335
validateNoDerivedComputationsInEffects: z.boolean().default(false),
336336

337+
/**
338+
* Experimental: Validates that effects are not used to calculate derived data which could instead be computed
339+
* during render. Generates a custom error message for each type of violation.
340+
*/
341+
validateNoDerivedComputationsInEffects_exp: z.boolean().default(false),
342+
337343
/**
338344
* Validates against creating JSX within a try block and recommends using an error boundary
339345
* instead.

0 commit comments

Comments
 (0)