mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Put type-annotation-based inference behind feature flag
This commit is contained in:
@@ -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
-1
@@ -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
@@ -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
-1
@@ -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
@@ -1,3 +1,4 @@
|
||||
// @enableUseTypeAnnotations
|
||||
function Component(props: { id: number }) {
|
||||
const x = makeArray(props.id) as number[];
|
||||
const y = x.at(0);
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
// @flow
|
||||
// @flow @enableUseTypeAnnotations
|
||||
import { identity, makeArray } from "shared-runtime";
|
||||
|
||||
function Component(props: { id: number }) {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// @flow
|
||||
// @flow @enableUseTypeAnnotations
|
||||
import { identity, makeArray } from "shared-runtime";
|
||||
|
||||
function Component(props: { id: number }) {
|
||||
|
||||
+2
@@ -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
@@ -1,3 +1,4 @@
|
||||
// @enableUseTypeAnnotations
|
||||
import { identity } from "shared-runtime";
|
||||
|
||||
function Component(props: { id: number }) {
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
// @flow
|
||||
// @flow @enableUseTypeAnnotations
|
||||
import { identity } from "shared-runtime";
|
||||
|
||||
function Component(props: {id: number}) {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// @flow
|
||||
// @flow @enableUseTypeAnnotations
|
||||
import { identity } from "shared-runtime";
|
||||
|
||||
function Component(props: {id: number}) {
|
||||
|
||||
+2
-1
@@ -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
@@ -1,3 +1,4 @@
|
||||
// @enableUseTypeAnnotations
|
||||
function Component(props: { id: number }) {
|
||||
const x: number[] = makeArray(props.id);
|
||||
const y = x.at(0);
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
// @flow
|
||||
// @flow @enableUseTypeAnnotations
|
||||
import { identity } from "shared-runtime";
|
||||
|
||||
function Component(props: { id: number }) {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// @flow
|
||||
// @flow @enableUseTypeAnnotations
|
||||
import { identity } from "shared-runtime";
|
||||
|
||||
function Component(props: { id: number }) {
|
||||
|
||||
Reference in New Issue
Block a user