Infer property types for destructuring

This commit is contained in:
Joe Savona
2023-05-24 09:54:07 -07:00
parent ecc730c587
commit c87a1916f4
3 changed files with 86 additions and 1 deletions
@@ -201,9 +201,39 @@ function* generateInstructionTypes(
break;
}
case "Destructure": {
const pattern = value.lvalue.pattern;
if (pattern.kind === "ArrayPattern") {
for (let i = 0; i < pattern.items.length; i++) {
const item = pattern.items[i];
if (item.kind === "Identifier") {
// To simulate tuples we use properties with `String(<index>)`, eg "0".
const propertyName = String(i);
yield equation(item.identifier.type, {
kind: "Property",
object: value.value.identifier.type,
propertyName,
});
} else {
break;
}
}
} else {
for (const property of pattern.properties) {
if (property.kind === "ObjectProperty") {
yield equation(property.place.identifier.type, {
kind: "Property",
object: value.value.identifier.type,
propertyName: property.name,
});
}
}
}
break;
}
case "DeclareLocal":
case "DeclareContext":
case "Destructure":
case "NewExpression":
case "TypeCastExpression":
case "JsxExpression":
@@ -0,0 +1,48 @@
## Input
```javascript
function Component(props) {
const x = [];
x.push(props.value);
const { length: y } = x;
foo(y);
return [x, y];
}
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
function Component(props) {
const $ = useMemoCache(5);
const c_0 = $[0] !== props.value;
let x;
if (c_0) {
x = [];
x.push(props.value);
$[0] = props.value;
$[1] = x;
} else {
x = $[1];
}
const { length: y } = x;
foo(y);
const c_2 = $[2] !== x;
const c_3 = $[3] !== y;
let t0;
if (c_2 || c_3) {
t0 = [x, y];
$[2] = x;
$[3] = y;
$[4] = t0;
} else {
t0 = $[4];
}
return t0;
}
```
@@ -0,0 +1,7 @@
function Component(props) {
const x = [];
x.push(props.value);
const { length: y } = x;
foo(y);
return [x, y];
}