Support useMemo w named functions
Supports useMemo invocations where the function is not an inline function, such as `useMemo(someFunction, [])`.
Joe Savona committed
Apr 26, 2023 at 18:16 UTC
afe2f5c810b10065ba8600ae367884e59c135465
3 files changed
+38
-6
compiler/forget/src/Inference/InlineUseMemo.ts
+4
-6
@@ -17,12 +17,12 @@ import {
17
GotoVariant,
18
HIR,
19
HIRFunction,
20
- IdentifierId,
20
Identifier,
21
+ IdentifierId,
22
InstructionKind,
23
+ Place,
24
makeInstructionId,
25
makeType,
25
- Place,
26
reversePostorderBlocks,
27
shrink,
28
} from "../HIR";
@@ -97,10 +97,8 @@ export function inlineUseMemo(fn: HIRFunction): void {
97
}
98
const body = functions.get(lambda.identifier.id);
99
if (body === undefined) {
100
- CompilerError.invariant(
101
- "Expected first argument to useMemo() to be a function expression",
102
- lambda.loc
103
- );
100
+ // Allow passing a named function to useMemo, eg `useMemo(someImportedFunction, [])`
101
+ continue;
102
}
103
104
if (body.loweredFunc.params.length > 0) {
compiler/forget/src/__tests__/fixtures/compiler/useMemo-named-function.expect.md
new
+30
@@ -0,0 +1,30 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ const x = useMemo(someHelper, []);
7
+ return x;
8
+}
9
+
10
+```
11
+
12
+## Code
13
+
14
+```javascript
15
+import { unstable_useMemoCache as useMemoCache } from "react";
16
+function Component(props) {
17
+ const $ = useMemoCache(1);
18
+ let t0;
19
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
20
+ t0 = someHelper();
21
+ $[0] = t0;
22
+ } else {
23
+ t0 = $[0];
24
+ }
25
+ const x = t0;
26
+ return x;
27
+}
28
+
29
+```
30
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/useMemo-named-function.js
new
+4
@@ -0,0 +1,4 @@
1
+function Component(props) {
2
+ const x = useMemo(someHelper, []);
3
+ return x;
4
+}