mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[hir] Add invariant to check if useMemo callback is async or a generator
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
+22
@@ -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;
|
||||
}
|
||||
+32
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user