[hir] Check if defined before looking up ValueKind

This handles globals now without throwing
This commit is contained in:
Sathya Gunasekaran
2023-02-13 16:32:50 +00:00
parent 7277897405
commit 31d8aad40e
3 changed files with 40 additions and 1 deletions
@@ -851,7 +851,9 @@ export function parseHookCall(place: Place): Hook | null {
function hasContextRefOperand(env: Environment, instrValue: InstructionValue) {
for (const place of eachInstructionValueOperand(instrValue)) {
if (env.kind(place) === ValueKind.Context) return true;
if (env.isDefined(place) && env.kind(place) === ValueKind.Context) {
return true;
}
}
return false;
}
@@ -0,0 +1,32 @@
## Input
```javascript
let someGlobal = {};
function component(a) {
let x = { a, someGlobal };
return x;
}
```
## Code
```javascript
let someGlobal = {};
function component(a) {
const $ = React.unstable_useMemoCache();
const c_0 = $[0] !== a;
let x;
if (c_0) {
x = { a: a, someGlobal: someGlobal };
$[0] = a;
$[1] = x;
} else {
x = $[1];
}
return x;
}
```
@@ -0,0 +1,5 @@
let someGlobal = {};
function component(a) {
let x = { a, someGlobal };
return x;
}