Skip to content

Commit 09056ab

Browse files
authored
[Compiler] Improve error for calculate in render useEffect validation (#34580)
Summary: Change error and update snapshots The error now mentions what values are causing the issue which should provide better context on how to fix the issue --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34580). * __->__ #34580 * #34579 * #34578 * #34577 * #34575 * #34574
1 parent c91783c commit 09056ab

File tree

35 files changed

+1718
-9
lines changed

35 files changed

+1718
-9
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
@@ -324,6 +324,12 @@ export const EnvironmentConfigSchema = z.object({
324324
*/
325325
validateNoDerivedComputationsInEffects: z.boolean().default(false),
326326

327+
/**
328+
* Experimental: Validates that effects are not used to calculate derived data which could instead be computed
329+
* during render. Generates a custom error message for each type of violation.
330+
*/
331+
validateNoDerivedComputationsInEffects_exp: z.boolean().default(false),
332+
327333
/**
328334
* Validates against creating JSX within a try block and recommends using an error boundary
329335
* instead.

0 commit comments

Comments
 (0)