Put type-annotation-based inference behind feature flag

This commit is contained in:
Joe Savona
2023-12-11 11:34:29 -08:00
parent 12fbfc4fee
commit a1c7a26fc6
16 changed files with 46 additions and 17 deletions
@@ -114,6 +114,14 @@ const EnvironmentConfigSchema = z.object({
*/
memoizeJsxElements: z.boolean().default(true),
/**
* Enable use of type annotations in the source to drive type inference. By default
* Forget attemps to infer types using only information that is guaranteed correct
* given the source, and does not trust user-supplied type annotations. This mode
* enables trusting user type annotations.
*/
enableUseTypeAnnotations: z.boolean().default(false),
/*
* Enable validation of hooks to partially check that the component honors the rules of hooks.
* When disabled, the component is assumed to follow the rules (though the Babel plugin looks
@@ -137,12 +137,20 @@ function* generateInstructionTypes(
}
case "StoreLocal": {
yield equation(
value.lvalue.place.identifier.type,
value.value.identifier.type
);
yield equation(value.type, value.lvalue.place.identifier.type);
yield equation(left, value.type);
if (env.config.enableUseTypeAnnotations) {
yield equation(
value.lvalue.place.identifier.type,
value.value.identifier.type
);
yield equation(value.type, value.lvalue.place.identifier.type);
yield equation(left, value.type);
} else {
yield equation(left, value.value.identifier.type);
yield equation(
value.lvalue.place.identifier.type,
value.value.identifier.type
);
}
break;
}
@@ -263,8 +271,12 @@ function* generateInstructionTypes(
}
case "TypeCastExpression": {
yield equation(value.type, value.value.identifier.type);
yield equation(left, value.type);
if (env.config.enableUseTypeAnnotations) {
yield equation(value.type, value.value.identifier.type);
yield equation(left, value.type);
} else {
yield equation(left, value.value.identifier.type);
}
break;
}
@@ -2,6 +2,7 @@
## Input
```javascript
// @enableUseTypeAnnotations
function useArray(items: Array<number>) {
// With type information we know that the callback cannot escape
// and does not need to be memoized, only the result needs to be
@@ -19,7 +20,7 @@ export const FIXTURE_ENTRYPOINT = {
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
import { unstable_useMemoCache as useMemoCache } from "react"; // @enableUseTypeAnnotations
function useArray(items) {
const $ = useMemoCache(3);
let t1;
@@ -1,3 +1,4 @@
// @enableUseTypeAnnotations
function useArray(items: Array<number>) {
// With type information we know that the callback cannot escape
// and does not need to be memoized, only the result needs to be
@@ -2,6 +2,7 @@
## Input
```javascript
// @enableUseTypeAnnotations
function Component(props: { id: number }) {
const x = makeArray(props.id) as number[];
const y = x.at(0);
@@ -22,7 +23,7 @@ export const FIXTURE_ENTRYPOINT = {
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
import { unstable_useMemoCache as useMemoCache } from "react"; // @enableUseTypeAnnotations
function Component(props) {
const $ = useMemoCache(4);
let t0;
@@ -1,3 +1,4 @@
// @enableUseTypeAnnotations
function Component(props: { id: number }) {
const x = makeArray(props.id) as number[];
const y = x.at(0);
@@ -2,7 +2,7 @@
## Input
```javascript
// @flow
// @flow @enableUseTypeAnnotations
import { identity, makeArray } from "shared-runtime";
function Component(props: { id: number }) {
@@ -1,4 +1,4 @@
// @flow
// @flow @enableUseTypeAnnotations
import { identity, makeArray } from "shared-runtime";
function Component(props: { id: number }) {
@@ -2,6 +2,7 @@
## Input
```javascript
// @enableUseTypeAnnotations
import { identity } from "shared-runtime";
function Component(props: { id: number }) {
@@ -20,6 +21,7 @@ export const FIXTURE_ENTRYPOINT = {
## Code
```javascript
// @enableUseTypeAnnotations
import { identity } from "shared-runtime";
function Component(props) {
@@ -1,3 +1,4 @@
// @enableUseTypeAnnotations
import { identity } from "shared-runtime";
function Component(props: { id: number }) {
@@ -2,7 +2,7 @@
## Input
```javascript
// @flow
// @flow @enableUseTypeAnnotations
import { identity } from "shared-runtime";
function Component(props: {id: number}) {
@@ -1,4 +1,4 @@
// @flow
// @flow @enableUseTypeAnnotations
import { identity } from "shared-runtime";
function Component(props: {id: number}) {
@@ -2,6 +2,7 @@
## Input
```javascript
// @enableUseTypeAnnotations
function Component(props: { id: number }) {
const x: number[] = makeArray(props.id);
const y = x.at(0);
@@ -22,7 +23,7 @@ export const FIXTURE_ENTRYPOINT = {
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
import { unstable_useMemoCache as useMemoCache } from "react"; // @enableUseTypeAnnotations
function Component(props) {
const $ = useMemoCache(4);
let t0;
@@ -1,3 +1,4 @@
// @enableUseTypeAnnotations
function Component(props: { id: number }) {
const x: number[] = makeArray(props.id);
const y = x.at(0);
@@ -2,7 +2,7 @@
## Input
```javascript
// @flow
// @flow @enableUseTypeAnnotations
import { identity } from "shared-runtime";
function Component(props: { id: number }) {
@@ -1,4 +1,4 @@
// @flow
// @flow @enableUseTypeAnnotations
import { identity } from "shared-runtime";
function Component(props: { id: number }) {