@samitouri / QOS-React / commits / 59504e1cb4

[hir] Traverse function to capture deps, not just body node

Technically there is a body node created for implicit return expression in a arrow function, so the existing logic should've worked fine. But there seems to be a Babel bug, so let's work around it by traversing the function. Added a test case that captures a dep as a param -- this is currently unsupported and also something that would've been ignored before this PR. Added a failing test to make sure we think about this case when we add support for default params.

Sathya Gunasekaran committed Sep 6, 2023 at 14:58 UTC 59504e1cb49733d00e4d1613cfd712c5d29c7bfb
5 files changed +59 -5
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+1 -1
@@ -3058,7 +3058,7 @@ function gatherCapturedDeps(
3058 }
3059 }
3060
3061 - fn.get("body").traverse({
3061 + fn.traverse({
3062 Expression(path) {
3063 visit(path);
3064 },
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.nested-function-with-param-as-captured-dep.expect.md new
+29
@@ -0,0 +1,29 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Foo() {
6 + (function t() {
7 + let x = {};
8 + return function a(x = () => {}) {
9 + return x;
10 + };
11 + })();
12 +}
13 +
14 +export const FIXTURE_ENTRYPOINT = {
15 + fn: Foo,
16 + params: [],
17 + isComponent: false,
18 +};
19 +
20 +```
21 +
22 +
23 +## Error
24 +
25 +```
26 +[ReactForget] Todo: (BuildHIR::node.lowerReorderableExpression) Expression type 'ArrowFunctionExpression' cannot be safely reordered (4:4)
27 +```
28 +
29 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.nested-function-with-param-as-captured-dep.ts new
+14
@@ -0,0 +1,14 @@
1 +function Foo() {
2 + (function t() {
3 + let x = {};
4 + return function a(x = () => {}) {
5 + return x;
6 + };
7 + })();
8 +}
9 +
10 +export const FIXTURE_ENTRYPOINT = {
11 + fn: Foo,
12 + params: [],
13 + isComponent: false,
14 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/lambda-return-expression.expect.md renamed
+15 -4
@@ -18,11 +18,22 @@ export const FIXTURE_ENTRYPOINT = {
18
19 ```
20
21 +## Code
22
22 -## Error
23 +```javascript
24 +import { invoke } from "shared-runtime";
25 +
26 +function useFoo() {
27 + const x = {};
28 + const result = invoke(() => x);
29 + console.log(result);
30 +}
31 +
32 +export const FIXTURE_ENTRYPOINT = {
33 + fn: useFoo,
34 + params: [],
35 + isComponent: false,
36 +};
37
38 ```
25 -[ReactForget] Invariant: Expected value for identifier `16` to be initialized. (5:5)
26 -```
27 -
39
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/lambda-return-expression.ts renamed