Restore React.useMemo validation

This got lost by moving the validation earlier.
This commit is contained in:
Joe Savona
2023-11-06 08:33:30 -08:00
parent 0d3f3884b8
commit e502e69b2d
3 changed files with 38 additions and 0 deletions
@@ -10,6 +10,7 @@ import { FunctionExpression, HIRFunction, IdentifierId } from "../HIR";
export function validateUseMemo(fn: HIRFunction): void {
const useMemos = new Set<IdentifierId>();
const react = new Set<IdentifierId>();
const functions = new Map<IdentifierId, FunctionExpression>();
for (const [, block] of fn.body.blocks) {
for (const { lvalue, value } of block.instructions) {
@@ -17,6 +18,16 @@ export function validateUseMemo(fn: HIRFunction): void {
case "LoadGlobal": {
if (value.name === "useMemo") {
useMemos.add(lvalue.identifier.id);
} else if (value.name === "React") {
react.add(lvalue.identifier.id);
}
break;
}
case "PropertyLoad": {
if (react.has(value.object.identifier.id)) {
if (value.property === "useMemo") {
useMemos.add(lvalue.identifier.id);
}
}
break;
}
@@ -0,0 +1,21 @@
## Input
```javascript
function component(a, b) {
let x = React.useMemo(async () => {
await a;
}, []);
return x;
}
```
## Error
```
[ReactForget] InvalidReact: useMemo callbacks may not be async or generator functions (2:4)
```
@@ -0,0 +1,6 @@
function component(a, b) {
let x = React.useMemo(async () => {
await a;
}, []);
return x;
}