@samitouri / QOS-React / commits / 31d8aad40e

[hir] Check if defined before looking up ValueKind

This handles globals now without throwing

Sathya Gunasekaran committed Feb 13, 2023 at 16:32 UTC 31d8aad40ee10badd4829dd53fb06b625ac1ddc1
3 files changed +40 -1
compiler/forget/src/Inference/InferReferenceEffects.ts
+3 -1
@@ -851,7 +851,9 @@ export function parseHookCall(place: Place): Hook | null {
851
852 function hasContextRefOperand(env: Environment, instrValue: InstructionValue) {
853 for (const place of eachInstructionValueOperand(instrValue)) {
854 - if (env.kind(place) === ValueKind.Context) return true;
854 + if (env.isDefined(place) && env.kind(place) === ValueKind.Context) {
855 + return true;
856 + }
857 }
858 return false;
859 }
compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.expect.md new
+32
@@ -0,0 +1,32 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +let someGlobal = {};
6 +function component(a) {
7 + let x = { a, someGlobal };
8 + return x;
9 +}
10 +
11 +```
12 +
13 +## Code
14 +
15 +```javascript
16 +let someGlobal = {};
17 +function component(a) {
18 + const $ = React.unstable_useMemoCache();
19 + const c_0 = $[0] !== a;
20 + let x;
21 + if (c_0) {
22 + x = { a: a, someGlobal: someGlobal };
23 + $[0] = a;
24 + $[1] = x;
25 + } else {
26 + x = $[1];
27 + }
28 + return x;
29 +}
30 +
31 +```
32 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.js new
+5
@@ -0,0 +1,5 @@
1 +let someGlobal = {};
2 +function component(a) {
3 + let x = { a, someGlobal };
4 + return x;
5 +}