Put type-annotation-based inference behind feature flag
Joe Savona committed
Dec 11, 2023 at 11:34 UTC
a1c7a26fc62c4ef0fc2a5845eaba69b12ee8824c
16 files changed
+46
-17
compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts
+8
@@ -114,6 +114,14 @@ const EnvironmentConfigSchema = z.object({
114
*/
115
memoizeJsxElements: z.boolean().default(true),
116
117
+ /**
118
+ * Enable use of type annotations in the source to drive type inference. By default
119
+ * Forget attemps to infer types using only information that is guaranteed correct
120
+ * given the source, and does not trust user-supplied type annotations. This mode
121
+ * enables trusting user type annotations.
122
+ */
123
+ enableUseTypeAnnotations: z.boolean().default(false),
124
+
125
/*
126
* Enable validation of hooks to partially check that the component honors the rules of hooks.
127
* When disabled, the component is assumed to follow the rules (though the Babel plugin looks
compiler/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts
+20
-8
@@ -137,12 +137,20 @@ function* generateInstructionTypes(
137
}
138
139
case "StoreLocal": {
140
- yield equation(
141
- value.lvalue.place.identifier.type,
142
- value.value.identifier.type
143
- );
144
- yield equation(value.type, value.lvalue.place.identifier.type);
145
- yield equation(left, value.type);
140
+ if (env.config.enableUseTypeAnnotations) {
141
+ yield equation(
142
+ value.lvalue.place.identifier.type,
143
+ value.value.identifier.type
144
+ );
145
+ yield equation(value.type, value.lvalue.place.identifier.type);
146
+ yield equation(left, value.type);
147
+ } else {
148
+ yield equation(left, value.value.identifier.type);
149
+ yield equation(
150
+ value.lvalue.place.identifier.type,
151
+ value.value.identifier.type
152
+ );
153
+ }
154
break;
155
}
156
@@ -263,8 +271,12 @@ function* generateInstructionTypes(
271
}
272
273
case "TypeCastExpression": {
266
- yield equation(value.type, value.value.identifier.type);
267
- yield equation(left, value.type);
274
+ if (env.config.enableUseTypeAnnotations) {
275
+ yield equation(value.type, value.value.identifier.type);
276
+ yield equation(left, value.type);
277
+ } else {
278
+ yield equation(left, value.value.identifier.type);
279
+ }
280
break;
281
}
282
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/todo_type-annotations-props.expect.md
+2
-1
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @enableUseTypeAnnotations
6
function useArray(items: Array<number>) {
7
// With type information we know that the callback cannot escape
8
// and does not need to be memoized, only the result needs to be
@@ -19,7 +20,7 @@ export const FIXTURE_ENTRYPOINT = {
20
## Code
21
22
```javascript
22
-import { unstable_useMemoCache as useMemoCache } from "react";
23
+import { unstable_useMemoCache as useMemoCache } from "react"; // @enableUseTypeAnnotations
24
function useArray(items) {
25
const $ = useMemoCache(3);
26
let t1;
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/todo_type-annotations-props.ts
+1
@@ -1,3 +1,4 @@
1
+// @enableUseTypeAnnotations
2
function useArray(items: Array<number>) {
3
// With type information we know that the callback cannot escape
4
// and does not need to be memoized, only the result needs to be
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array.expect.md
+2
-1
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @enableUseTypeAnnotations
6
function Component(props: { id: number }) {
7
const x = makeArray(props.id) as number[];
8
const y = x.at(0);
@@ -22,7 +23,7 @@ export const FIXTURE_ENTRYPOINT = {
23
## Code
24
25
```javascript
25
-import { unstable_useMemoCache as useMemoCache } from "react";
26
+import { unstable_useMemoCache as useMemoCache } from "react"; // @enableUseTypeAnnotations
27
function Component(props) {
28
const $ = useMemoCache(4);
29
let t0;
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array.ts
+1
@@ -1,3 +1,4 @@
1
+// @enableUseTypeAnnotations
2
function Component(props: { id: number }) {
3
const x = makeArray(props.id) as number[];
4
const y = x.at(0);
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array_.flow.expect.md
+1
-1
@@ -2,7 +2,7 @@
2
## Input
3
4
```javascript
5
-// @flow
5
+// @flow @enableUseTypeAnnotations
6
import { identity, makeArray } from "shared-runtime";
7
8
function Component(props: { id: number }) {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array_.flow.js
+1
-1
@@ -1,4 +1,4 @@
1
-// @flow
1
+// @flow @enableUseTypeAnnotations
2
import { identity, makeArray } from "shared-runtime";
3
4
function Component(props: { id: number }) {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number.expect.md
+2
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @enableUseTypeAnnotations
6
import { identity } from "shared-runtime";
7
8
function Component(props: { id: number }) {
@@ -20,6 +21,7 @@ export const FIXTURE_ENTRYPOINT = {
21
## Code
22
23
```javascript
24
+// @enableUseTypeAnnotations
25
import { identity } from "shared-runtime";
26
27
function Component(props) {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number.ts
+1
@@ -1,3 +1,4 @@
1
+// @enableUseTypeAnnotations
2
import { identity } from "shared-runtime";
3
4
function Component(props: { id: number }) {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number_.flow.expect.md
+1
-1
@@ -2,7 +2,7 @@
2
## Input
3
4
```javascript
5
-// @flow
5
+// @flow @enableUseTypeAnnotations
6
import { identity } from "shared-runtime";
7
8
function Component(props: {id: number}) {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number_.flow.js
+1
-1
@@ -1,4 +1,4 @@
1
-// @flow
1
+// @flow @enableUseTypeAnnotations
2
import { identity } from "shared-runtime";
3
4
function Component(props: {id: number}) {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array.expect.md
+2
-1
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @enableUseTypeAnnotations
6
function Component(props: { id: number }) {
7
const x: number[] = makeArray(props.id);
8
const y = x.at(0);
@@ -22,7 +23,7 @@ export const FIXTURE_ENTRYPOINT = {
23
## Code
24
25
```javascript
25
-import { unstable_useMemoCache as useMemoCache } from "react";
26
+import { unstable_useMemoCache as useMemoCache } from "react"; // @enableUseTypeAnnotations
27
function Component(props) {
28
const $ = useMemoCache(4);
29
let t0;
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array.ts
+1
@@ -1,3 +1,4 @@
1
+// @enableUseTypeAnnotations
2
function Component(props: { id: number }) {
3
const x: number[] = makeArray(props.id);
4
const y = x.at(0);
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array_.flow.expect.md
+1
-1
@@ -2,7 +2,7 @@
2
## Input
3
4
```javascript
5
-// @flow
5
+// @flow @enableUseTypeAnnotations
6
import { identity } from "shared-runtime";
7
8
function Component(props: { id: number }) {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array_.flow.js
+1
-1
@@ -1,4 +1,4 @@
1
-// @flow
1
+// @flow @enableUseTypeAnnotations
2
import { identity } from "shared-runtime";
3
4
function Component(props: { id: number }) {