[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.
This commit is contained in:
Sathya Gunasekaran
2023-09-06 14:58:04 +01:00
parent a93aa8f322
commit 59504e1cb4
5 changed files with 59 additions and 5 deletions
@@ -3058,7 +3058,7 @@ function gatherCapturedDeps(
}
}
fn.get("body").traverse({
fn.traverse({
Expression(path) {
visit(path);
},
@@ -0,0 +1,29 @@
## Input
```javascript
function Foo() {
(function t() {
let x = {};
return function a(x = () => {}) {
return x;
};
})();
}
export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [],
isComponent: false,
};
```
## Error
```
[ReactForget] Todo: (BuildHIR::node.lowerReorderableExpression) Expression type 'ArrowFunctionExpression' cannot be safely reordered (4:4)
```
@@ -0,0 +1,14 @@
function Foo() {
(function t() {
let x = {};
return function a(x = () => {}) {
return x;
};
})();
}
export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [],
isComponent: false,
};
@@ -18,11 +18,22 @@ export const FIXTURE_ENTRYPOINT = {
```
## Code
## Error
```javascript
import { invoke } from "shared-runtime";
function useFoo() {
const x = {};
const result = invoke(() => x);
console.log(result);
}
export const FIXTURE_ENTRYPOINT = {
fn: useFoo,
params: [],
isComponent: false,
};
```
[ReactForget] Invariant: Expected value for identifier `16` to be initialized. (5:5)
```