mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[hir] Attach fnType to HIRFunction
---
(This came out of running a sync and observing hundreds of bailouts due from
this validation)
Reading `fnType` from environment overgeneralizes, as inner functions are
usually not the type of the outer react function.
```
// Component type
function Component() {
// not Component type
const helper = () => {...};
}
```
Let's attach fnType to `HIRFunction` and use that for our inference +
validations
This commit is contained in:
@@ -210,6 +210,7 @@ export function lower(
|
||||
return Ok({
|
||||
id,
|
||||
params,
|
||||
fnType: parent == null ? env.fnType : "Other",
|
||||
returnType: null, // TODO: extract the actual return type node if present
|
||||
body: builder.build(),
|
||||
context,
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
import * as t from "@babel/types";
|
||||
import { CompilerError, CompilerErrorDetailOptions } from "../CompilerError";
|
||||
import { assertExhaustive } from "../Utils/utils";
|
||||
import { Environment } from "./Environment";
|
||||
import { Environment, ReactFunctionType } from "./Environment";
|
||||
import { HookKind } from "./ObjectShape";
|
||||
import { Type } from "./Types";
|
||||
|
||||
@@ -240,6 +240,7 @@ export type ReactiveTryTerminal = {
|
||||
export type HIRFunction = {
|
||||
loc: SourceLocation;
|
||||
id: string | null;
|
||||
fnType: ReactFunctionType;
|
||||
env: Environment;
|
||||
params: Array<Place | SpreadPattern>;
|
||||
returnType: t.FlowType | t.TSType | null;
|
||||
|
||||
@@ -137,7 +137,7 @@ export default function inferReferenceEffects(
|
||||
reason: new Set([ValueReason.ReactiveFunctionArgument]),
|
||||
};
|
||||
|
||||
if (fn.env.fnType === "Component") {
|
||||
if (fn.fnType === "Component") {
|
||||
CompilerError.invariant(fn.params.length <= 2, {
|
||||
reason:
|
||||
"Expected React component to have not more than two parameters: one for props and for ref",
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
import { Stringify } from "shared-runtime";
|
||||
function Component(props) {
|
||||
const cb = (x, y, z) => x + y + z;
|
||||
|
||||
return <Stringify cb={cb} id={props.id} />;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ id: 0 }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
import { Stringify } from "shared-runtime";
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(3);
|
||||
let t0;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
t0 = (x, y, z) => x + y + z;
|
||||
$[0] = t0;
|
||||
} else {
|
||||
t0 = $[0];
|
||||
}
|
||||
const cb = t0;
|
||||
let t1;
|
||||
if ($[1] !== props.id) {
|
||||
t1 = <Stringify cb={cb} id={props.id} />;
|
||||
$[1] = props.id;
|
||||
$[2] = t1;
|
||||
} else {
|
||||
t1 = $[2];
|
||||
}
|
||||
return t1;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ id: 0 }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
### Eval output
|
||||
(kind: ok) <div>{"cb":"[[ function params=3 ]]","id":0}</div>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { Stringify } from "shared-runtime";
|
||||
function Component(props) {
|
||||
const cb = (x, y, z) => x + y + z;
|
||||
|
||||
return <Stringify cb={cb} id={props.id} />;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ id: 0 }],
|
||||
};
|
||||
Reference in New Issue
Block a user