Restore React.useMemo validation
This got lost by moving the validation earlier.
Joe Savona committed
Nov 6, 2023 at 08:33 UTC
e502e69b2dedb5bd6bd08ef7d70a2b853294d953
3 files changed
+38
compiler/packages/babel-plugin-react-forget/src/Validation/ValidateUseMemo.ts
+11
@@ -10,6 +10,7 @@ import { FunctionExpression, HIRFunction, IdentifierId } from "../HIR";
10
11
export function validateUseMemo(fn: HIRFunction): void {
12
const useMemos = new Set<IdentifierId>();
13
+ const react = new Set<IdentifierId>();
14
const functions = new Map<IdentifierId, FunctionExpression>();
15
for (const [, block] of fn.body.blocks) {
16
for (const { lvalue, value } of block.instructions) {
@@ -17,6 +18,16 @@ export function validateUseMemo(fn: HIRFunction): void {
18
case "LoadGlobal": {
19
if (value.name === "useMemo") {
20
useMemos.add(lvalue.identifier.id);
21
+ } else if (value.name === "React") {
22
+ react.add(lvalue.identifier.id);
23
+ }
24
+ break;
25
+ }
26
+ case "PropertyLoad": {
27
+ if (react.has(value.object.identifier.id)) {
28
+ if (value.property === "useMemo") {
29
+ useMemos.add(lvalue.identifier.id);
30
+ }
31
}
32
break;
33
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.invalid-ReactUseMemo-async-callback.expect.md
new
+21
@@ -0,0 +1,21 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function component(a, b) {
6
+ let x = React.useMemo(async () => {
7
+ await a;
8
+ }, []);
9
+ return x;
10
+}
11
+
12
+```
13
+
14
+
15
+## Error
16
+
17
+```
18
+[ReactForget] InvalidReact: useMemo callbacks may not be async or generator functions (2:4)
19
+```
20
+
21
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.invalid-ReactUseMemo-async-callback.js
new
+6
@@ -0,0 +1,6 @@
1
+function component(a, b) {
2
+ let x = React.useMemo(async () => {
3
+ await a;
4
+ }, []);
5
+ return x;
6
+}