[hir] Add invariant to check if useMemo callback is async or a generator

This commit is contained in:
Sathya Gunasekaran
2023-04-11 15:06:53 +01:00
parent fd252e42ab
commit 7678a52c9c
5 changed files with 78 additions and 0 deletions
@@ -108,6 +108,13 @@ export function inlineUseMemo(fn: HIRFunction): void {
);
}
if (body.loweredFunc.async || body.loweredFunc.generator) {
CompilerError.invariant(
"Did not expect useMemo callback to be async or a generator",
body.loc
);
}
// We know this function is used for useMemo and can prune it later
useMemoFunctions.add(lambda.identifier.id);
@@ -0,0 +1,22 @@
## Input
```javascript
// @inlineUseMemo
function component(a, b) {
let x = useMemo(async () => {
await a;
}, []);
return x;
}
```
## Error
```
[ReactForget] Invariant: Did not expect useMemo callback to be async or a generator (3:5)
```
@@ -0,0 +1,7 @@
// @inlineUseMemo
function component(a, b) {
let x = useMemo(async () => {
await a;
}, []);
return x;
}
@@ -0,0 +1,32 @@
## Input
```javascript
// @inlineUseMemo
function component(a, b) {
// we don't handle generators at all so this test isn't
// useful for now, but adding this test in case we do
// add support for generators in the future.
let x = useMemo(function* () {
yield a;
}, []);
return x;
}
```
## Error
```
[ReactForget] TodoError: (BuildHIR::lowerExpression) Handle YieldExpression expressions
5 | // add support for generators in the future.
6 | let x = useMemo(function* () {
> 7 | yield a;
| ^^^^^^^
8 | }, []);
9 | return x;
10 | }
```
@@ -0,0 +1,10 @@
// @inlineUseMemo
function component(a, b) {
// we don't handle generators at all so this test isn't
// useful for now, but adding this test in case we do
// add support for generators in the future.
let x = useMemo(function* () {
yield a;
}, []);
return x;
}