@samitouri / QOS-React-2 / commits / 38d3423970

Treat function expression deps as conditional

Sathya Gunasekaran committed Jan 15, 2024 at 12:44 UTC 38d34239705ca20b693bd549e16e0673a9f9be97
3 files changed +36 -8
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateScopeDependencies.ts
+27 -1
@@ -63,7 +63,11 @@ export function propagateScopeDependencies(fn: ReactiveFunction): void {
63 });
64 }
65 }
66 - visitReactiveFunction(fn, new PropagationVisitor(), context);
66 + visitReactiveFunction(
67 + fn,
68 + new PropagationVisitor(fn.env.config.enableTreatFunctionDepsAsConditional),
69 + context
70 + );
71 }
72
73 type TemporariesUsedOutsideDefiningScope = {
@@ -466,6 +470,14 @@ class Context {
470 }
471
472 class PropagationVisitor extends ReactiveFunctionVisitor<Context> {
473 + enableTreatFunctionDepsAsConditional = false;
474 +
475 + constructor(enableTreatFunctionDepsAsConditional: boolean) {
476 + super();
477 + this.enableTreatFunctionDepsAsConditional =
478 + enableTreatFunctionDepsAsConditional;
479 + }
480 +
481 override visitScope(scope: ReactiveScopeBlock, context: Context): void {
482 const scopeDependencies = context.enter(scope.scope, () => {
483 this.visitBlock(scope.instructions, context);
@@ -547,6 +559,20 @@ class PropagationVisitor extends ReactiveFunctionVisitor<Context> {
559 this.visitInstructionValue(context, id, value.value, null);
560 break;
561 }
562 + case "FunctionExpression": {
563 + if (this.enableTreatFunctionDepsAsConditional) {
564 + context.enterConditional(() => {
565 + for (const operand of eachInstructionValueOperand(value)) {
566 + context.visitOperand(operand);
567 + }
568 + });
569 + } else {
570 + for (const operand of eachInstructionValueOperand(value)) {
571 + context.visitOperand(operand);
572 + }
573 + }
574 + break;
575 + }
576 default: {
577 for (const operand of eachInstructionValueOperand(value)) {
578 context.visitOperand(operand);
"b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr\342\200\223conditional-access.expect.md" renamed
+7 -6
@@ -2,6 +2,7 @@
2 ## Input
3
4 ```javascript
5 +// @enableTreatFunctionDepsAsConditional
6 function Component(props) {
7 function getLength() {
8 return props.bar.length;
@@ -12,7 +13,7 @@ function Component(props) {
13
14 export const FIXTURE_ENTRYPOINT = {
15 fn: Component,
15 - params: [{ bar: [] }],
16 + params: [{ bar: null }],
17 };
18
19 ```
@@ -20,15 +21,15 @@ export const FIXTURE_ENTRYPOINT = {
21 ## Code
22
23 ```javascript
23 -import { unstable_useMemoCache as useMemoCache } from "react";
24 +import { unstable_useMemoCache as useMemoCache } from "react"; // @enableTreatFunctionDepsAsConditional
25 function Component(props) {
26 const $ = useMemoCache(5);
27 let t0;
27 - if ($[0] !== props.bar.length) {
28 + if ($[0] !== props) {
29 t0 = function getLength() {
30 return props.bar.length;
31 };
31 - $[0] = props.bar.length;
32 + $[0] = props;
33 $[1] = t0;
34 } else {
35 t0 = $[1];
@@ -48,10 +49,10 @@ function Component(props) {
49
50 export const FIXTURE_ENTRYPOINT = {
51 fn: Component,
51 - params: [{ bar: [] }],
52 + params: [{ bar: null }],
53 };
54
55 ```
56
57 ### Eval output
57 -(kind: ok) 0
\ No newline at end of file
58 +(kind: ok) null
\ No newline at end of file
"b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr\342\200\223conditional-access.js" renamed
+2 -1
@@ -1,3 +1,4 @@
1 +// @enableTreatFunctionDepsAsConditional
2 function Component(props) {
3 function getLength() {
4 return props.bar.length;
@@ -8,5 +9,5 @@ function Component(props) {
9
10 export const FIXTURE_ENTRYPOINT = {
11 fn: Component,
11 - params: [{ bar: [] }],
12 + params: [{ bar: null }],
13 };