diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts index 41581cc46c..f90e18788c 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts @@ -251,7 +251,10 @@ function runWithEnvironment( validateNoSetStateInRender(hir); } - validateNoDerivedComputationsInEffects(hir); + if (env.config.validateNoDerivedComputationsInEffects) { + validateNoDerivedComputationsInEffects(hir); + } + if (env.config.validateNoSetStateInPassiveEffects) { validateNoSetStateInPassiveEffects(hir); } diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts index 113af2025d..d17333cc8a 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts @@ -324,6 +324,12 @@ const EnvironmentConfigSchema = z.object({ */ validateNoSetStateInPassiveEffects: z.boolean().default(false), + /** + * Validates that effects are not used to calculate derived data which could instead be computed + * during render. + */ + validateNoDerivedComputationsInEffects: z.boolean().default(false), + /** * Validates against creating JSX within a try block and recommends using an error boundary * instead. diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts index 123b536ce8..d026a94ed4 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts @@ -140,8 +140,27 @@ function validateEffect( return; } } + for (const phi of block.phis) { + const aggregateDeps: Set = new Set(); + for (const operand of phi.operands.values()) { + const deps = values.get(operand.identifier.id); + if (deps != null) { + for (const dep of deps) { + aggregateDeps.add(dep); + } + } + } + if (aggregateDeps.size !== 0) { + values.set(phi.place.identifier.id, Array.from(aggregateDeps)); + } + } for (const instr of block.instructions) { switch (instr.value.kind) { + case 'Primitive': + case 'JSXText': + case 'LoadGlobal': { + break; + } case 'LoadLocal': { const deps = values.get(instr.value.place.identifier.id); if (deps != null) { @@ -149,11 +168,12 @@ function validateEffect( } break; } - case 'Primitive': - case 'JSXText': - case 'LoadGlobal': + case 'ComputedLoad': + case 'PropertyLoad': case 'BinaryExpression': - case 'TemplateLiteral': { + case 'TemplateLiteral': + case 'CallExpression': + case 'MethodCall': { const aggregateDeps: Set = new Set(); for (const operand of eachInstructionValueOperand(instr.value)) { const deps = values.get(operand.identifier.id); @@ -166,10 +186,9 @@ function validateEffect( if (aggregateDeps.size !== 0) { values.set(instr.lvalue.identifier.id, Array.from(aggregateDeps)); } - break; - } - case 'CallExpression': { + if ( + instr.value.kind === 'CallExpression' && isSetStateType(instr.value.callee.identifier) && instr.value.args.length === 1 && instr.value.args[0].kind === 'Identifier' @@ -181,9 +200,6 @@ function validateEffect( // doesn't depend on any deps return; } - } else { - // some other call - return; } break; } @@ -194,6 +210,7 @@ function validateEffect( } for (const operand of eachTerminalOperand(block.terminal)) { if (values.has(operand.identifier.id)) { + // return; } } diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-validate-no-derived-computations-in-effects.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-validate-no-derived-computations-in-effects.expect.md index 7988adfb74..a3065de4eb 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-validate-no-derived-computations-in-effects.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-validate-no-derived-computations-in-effects.expect.md @@ -2,6 +2,7 @@ ## Input ```javascript +// @validateNoDerivedComputationsInEffects function Form() { const [firstName, setFirstName] = useState('Taylor'); const [lastName, setLastName] = useState('Swift'); @@ -9,7 +10,7 @@ function Form() { // 🔴 Avoid: redundant state and unnecessary Effect const [fullName, setFullName] = useState(''); useEffect(() => { - setFullName(firstName + ' ' + lastName); + setFullName(capitalize(firstName + ' ' + lastName)); }, [firstName, lastName]); return
{fullName}
; @@ -21,13 +22,13 @@ function Form() { ## Error ``` - 6 | const [fullName, setFullName] = useState(''); - 7 | useEffect(() => { -> 8 | setFullName(firstName + ' ' + lastName); - | ^^^^^^^^^^^ InvalidReact: Values derived from props and state should be calculated during render, not in an effect. (https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state) (8:8) - 9 | }, [firstName, lastName]); - 10 | - 11 | return
{fullName}
; + 7 | const [fullName, setFullName] = useState(''); + 8 | useEffect(() => { +> 9 | setFullName(capitalize(firstName + ' ' + lastName)); + | ^^^^^^^^^^^ InvalidReact: Values derived from props and state should be calculated during render, not in an effect. (https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state) (9:9) + 10 | }, [firstName, lastName]); + 11 | + 12 | return
{fullName}
; ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-validate-no-derived-computations-in-effects.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-validate-no-derived-computations-in-effects.js index 15a7de4057..f358e75aaa 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-validate-no-derived-computations-in-effects.js +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-validate-no-derived-computations-in-effects.js @@ -1,3 +1,4 @@ +// @validateNoDerivedComputationsInEffects function Form() { const [firstName, setFirstName] = useState('Taylor'); const [lastName, setLastName] = useState('Swift'); @@ -5,7 +6,7 @@ function Form() { // 🔴 Avoid: redundant state and unnecessary Effect const [fullName, setFullName] = useState(''); useEffect(() => { - setFullName(firstName + ' ' + lastName); + setFullName(capitalize(firstName + ' ' + lastName)); }, [firstName, lastName]); return
{fullName}
;