@samitouri / QOS-React-2 / commits / fd252e42ab

[hir] Add invariant to check if useMemo callback accepts args

Sathya Gunasekaran committed Apr 11, 2023 at 15:00 UTC fd252e42ab5bf8ebd23baca5e08a1b8399b3f740
3 files changed +33
compiler/forget/src/Inference/InlineUseMemo.ts
+8
@@ -100,6 +100,14 @@ export function inlineUseMemo(fn: HIRFunction): void {
100 lambda.loc
101 );
102 }
103 +
104 + if (body.loweredFunc.params.length > 0) {
105 + CompilerError.invariant(
106 + "Did not expect any arguments to useMemo callback",
107 + body.loc
108 + );
109 + }
110 +
111 // We know this function is used for useMemo and can prune it later
112 useMemoFunctions.add(lambda.identifier.id);
113
compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-callback-args.expect.md new
+20
@@ -0,0 +1,20 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +// @inlineUseMemo
6 +function component(a, b) {
7 + let x = useMemo((c) => a, []);
8 + return x;
9 +}
10 +
11 +```
12 +
13 +
14 +## Error
15 +
16 +```
17 +[ReactForget] Invariant: Did not expect any arguments to useMemo callback (3:3)
18 +```
19 +
20 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/error.useMemo-callback-args.js new
+5
@@ -0,0 +1,5 @@
1 +// @inlineUseMemo
2 +function component(a, b) {
3 + let x = useMemo((c) => a, []);
4 + return x;
5 +}