diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateScopeDependencies.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateScopeDependencies.ts index ae4b5715ef..188f78dc92 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateScopeDependencies.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateScopeDependencies.ts @@ -63,7 +63,11 @@ export function propagateScopeDependencies(fn: ReactiveFunction): void { }); } } - visitReactiveFunction(fn, new PropagationVisitor(), context); + visitReactiveFunction( + fn, + new PropagationVisitor(fn.env.config.enableTreatFunctionDepsAsConditional), + context + ); } type TemporariesUsedOutsideDefiningScope = { @@ -466,6 +470,14 @@ class Context { } class PropagationVisitor extends ReactiveFunctionVisitor { + enableTreatFunctionDepsAsConditional = false; + + constructor(enableTreatFunctionDepsAsConditional: boolean) { + super(); + this.enableTreatFunctionDepsAsConditional = + enableTreatFunctionDepsAsConditional; + } + override visitScope(scope: ReactiveScopeBlock, context: Context): void { const scopeDependencies = context.enter(scope.scope, () => { this.visitBlock(scope.instructions, context); @@ -547,6 +559,20 @@ class PropagationVisitor extends ReactiveFunctionVisitor { this.visitInstructionValue(context, id, value.value, null); break; } + case "FunctionExpression": { + if (this.enableTreatFunctionDepsAsConditional) { + context.enterConditional(() => { + for (const operand of eachInstructionValueOperand(value)) { + context.visitOperand(operand); + } + }); + } else { + for (const operand of eachInstructionValueOperand(value)) { + context.visitOperand(operand); + } + } + break; + } default: { for (const operand of eachInstructionValueOperand(value)) { context.visitOperand(operand); diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr–conditional-access.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr–conditional-access.expect.md index fdfc995fd2..9daf0259d1 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr–conditional-access.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr–conditional-access.expect.md @@ -2,6 +2,7 @@ ## Input ```javascript +// @enableTreatFunctionDepsAsConditional function Component(props) { function getLength() { return props.bar.length; @@ -12,7 +13,7 @@ function Component(props) { export const FIXTURE_ENTRYPOINT = { fn: Component, - params: [{ bar: [] }], + params: [{ bar: null }], }; ``` @@ -20,15 +21,15 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript -import { unstable_useMemoCache as useMemoCache } from "react"; +import { unstable_useMemoCache as useMemoCache } from "react"; // @enableTreatFunctionDepsAsConditional function Component(props) { const $ = useMemoCache(5); let t0; - if ($[0] !== props.bar.length) { + if ($[0] !== props) { t0 = function getLength() { return props.bar.length; }; - $[0] = props.bar.length; + $[0] = props; $[1] = t0; } else { t0 = $[1]; @@ -48,10 +49,10 @@ function Component(props) { export const FIXTURE_ENTRYPOINT = { fn: Component, - params: [{ bar: [] }], + params: [{ bar: null }], }; ``` ### Eval output -(kind: ok) 0 \ No newline at end of file +(kind: ok) null \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr–conditional-access.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr–conditional-access.js index a960ff498b..055fd0778f 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr–conditional-access.js +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr–conditional-access.js @@ -1,3 +1,4 @@ +// @enableTreatFunctionDepsAsConditional function Component(props) { function getLength() { return props.bar.length; @@ -8,5 +9,5 @@ function Component(props) { export const FIXTURE_ENTRYPOINT = { fn: Component, - params: [{ bar: [] }], + params: [{ bar: null }], };