[hir] Add invariant to check if useMemo callback is async or a generator
Sathya Gunasekaran committed
Apr 11, 2023 at 15:06 UTC
7678a52c9cc74d896c867919ec705daf26fcd7d8
5 files changed
+78
compiler/forget/src/Inference/InlineUseMemo.ts
+7
@@ -108,6 +108,13 @@ export function inlineUseMemo(fn: HIRFunction): void {
108
);
109
}
110
111
+ if (body.loweredFunc.async || body.loweredFunc.generator) {
112
+ CompilerError.invariant(
113
+ "Did not expect useMemo callback to be async or a generator",
114
+ body.loc
115
+ );
116
+ }
117
+
118
// We know this function is used for useMemo and can prune it later
119
useMemoFunctions.add(lambda.identifier.id);
120
compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-async-callback.expect.md
new
+22
@@ -0,0 +1,22 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+// @inlineUseMemo
6
+function component(a, b) {
7
+ let x = useMemo(async () => {
8
+ await a;
9
+ }, []);
10
+ return x;
11
+}
12
+
13
+```
14
+
15
+
16
+## Error
17
+
18
+```
19
+[ReactForget] Invariant: Did not expect useMemo callback to be async or a generator (3:5)
20
+```
21
+
22
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-async-callback.js
new
+7
@@ -0,0 +1,7 @@
1
+// @inlineUseMemo
2
+function component(a, b) {
3
+ let x = useMemo(async () => {
4
+ await a;
5
+ }, []);
6
+ return x;
7
+}
compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.expect.md
new
+32
@@ -0,0 +1,32 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+// @inlineUseMemo
6
+function component(a, b) {
7
+ // we don't handle generators at all so this test isn't
8
+ // useful for now, but adding this test in case we do
9
+ // add support for generators in the future.
10
+ let x = useMemo(function* () {
11
+ yield a;
12
+ }, []);
13
+ return x;
14
+}
15
+
16
+```
17
+
18
+
19
+## Error
20
+
21
+```
22
+[ReactForget] TodoError: (BuildHIR::lowerExpression) Handle YieldExpression expressions
23
+ 5 | // add support for generators in the future.
24
+ 6 | let x = useMemo(function* () {
25
+> 7 | yield a;
26
+ | ^^^^^^^
27
+ 8 | }, []);
28
+ 9 | return x;
29
+ 10 | }
30
+```
31
+
32
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.js
new
+10
@@ -0,0 +1,10 @@
1
+// @inlineUseMemo
2
+function component(a, b) {
3
+ // we don't handle generators at all so this test isn't
4
+ // useful for now, but adding this test in case we do
5
+ // add support for generators in the future.
6
+ let x = useMemo(function* () {
7
+ yield a;
8
+ }, []);
9
+ return x;
10
+}