-
Notifications
You must be signed in to change notification settings - Fork 47.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compiler: Allow global mutation in jsx props
Fixes https://x.com/raibima/status/1794395807216738792 The issue is that if you pass a global-modifying function as prop to JSX, we currently report that it's invalid to modify a global during rendering. The problem is that we don't really know when/if the child component will actually call that function prop. It would be against the rules to call the function during render, but it's totally fine to call it during an event handler or from a useEffect. Since we don't know at the call-site how the child will use the function, we should allow such calls. In the future we could improve this in a few ways: * For all functions that modify globals, codegen an assertion or warning into the function that fires if it's called "during render". We'd have to precisely define what "during render" is, but this would at least help developers catch this dynamically. * Use the type system to distinguish "event/effect" and "render" functions to help developers avoid accidentally mutating globals during render. ghstack-source-id: 4aba4e6d214fd6c062e4029294efe9b8fe25cd83 Pull Request resolved: #29591
- Loading branch information
1 parent
e2e12f3
commit 49ed6f0
Showing
13 changed files
with
331 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...r/src/__tests__/fixtures/compiler/allow-modify-global-in-callback-jsx.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import { useMemo } from "react"; | ||
|
||
const someGlobal = { value: 0 }; | ||
|
||
function Component({ value }) { | ||
const onClick = () => { | ||
someGlobal.value = value; | ||
}; | ||
return useMemo(() => { | ||
return <div onClick={onClick}>{someGlobal.value}</div>; | ||
}, []); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ value: 0 }], | ||
sequentialRenders: [ | ||
{ value: 1 }, | ||
{ value: 1 }, | ||
{ value: 42 }, | ||
{ value: 42 }, | ||
{ value: 0 }, | ||
], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { useMemo } from "react"; | ||
|
||
const someGlobal = { value: 0 }; | ||
|
||
function Component(t0) { | ||
const $ = _c(4); | ||
const { value } = t0; | ||
let t1; | ||
if ($[0] !== value) { | ||
t1 = () => { | ||
someGlobal.value = value; | ||
}; | ||
$[0] = value; | ||
$[1] = t1; | ||
} else { | ||
t1 = $[1]; | ||
} | ||
const onClick = t1; | ||
let t2; | ||
let t3; | ||
if ($[2] !== onClick) { | ||
t3 = <div onClick={onClick}>{someGlobal.value}</div>; | ||
$[2] = onClick; | ||
$[3] = t3; | ||
} else { | ||
t3 = $[3]; | ||
} | ||
t2 = t3; | ||
return t2; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ value: 0 }], | ||
sequentialRenders: [ | ||
{ value: 1 }, | ||
{ value: 1 }, | ||
{ value: 42 }, | ||
{ value: 42 }, | ||
{ value: 0 }, | ||
], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <div>0</div> | ||
<div>0</div> | ||
<div>0</div> | ||
<div>0</div> | ||
<div>0</div> |
24 changes: 24 additions & 0 deletions
24
...gin-react-compiler/src/__tests__/fixtures/compiler/allow-modify-global-in-callback-jsx.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useMemo } from "react"; | ||
|
||
const someGlobal = { value: 0 }; | ||
|
||
function Component({ value }) { | ||
const onClick = () => { | ||
someGlobal.value = value; | ||
}; | ||
return useMemo(() => { | ||
return <div onClick={onClick}>{someGlobal.value}</div>; | ||
}, []); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ value: 0 }], | ||
sequentialRenders: [ | ||
{ value: 1 }, | ||
{ value: 1 }, | ||
{ value: 42 }, | ||
{ value: 42 }, | ||
{ value: 0 }, | ||
], | ||
}; |
53 changes: 53 additions & 0 deletions
53
...ts__/fixtures/compiler/allow-reassignment-to-global-function-jsx-prop.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
function Component() { | ||
const onClick = () => { | ||
// Cannot assign to globals | ||
someUnknownGlobal = true; | ||
moduleLocal = true; | ||
}; | ||
// It's possible that this could be an event handler / effect function, | ||
// but we don't know that and optimistically assume it will only be | ||
// called by an event handler or effect, where it is allowed to modify globals | ||
return <div onClick={onClick} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
function Component() { | ||
const $ = _c(1); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
const onClick = () => { | ||
someUnknownGlobal = true; | ||
moduleLocal = true; | ||
}; | ||
|
||
t0 = <div onClick={onClick} />; | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
return t0; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <div></div> |
16 changes: 16 additions & 0 deletions
16
...ompiler/src/__tests__/fixtures/compiler/allow-reassignment-to-global-function-jsx-prop.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function Component() { | ||
const onClick = () => { | ||
// Cannot assign to globals | ||
someUnknownGlobal = true; | ||
moduleLocal = true; | ||
}; | ||
// It's possible that this could be an event handler / effect function, | ||
// but we don't know that and optimistically assume it will only be | ||
// called by an event handler or effect, where it is allowed to modify globals | ||
return <div onClick={onClick} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; |
27 changes: 27 additions & 0 deletions
27
...sts__/fixtures/compiler/error.assign-global-in-component-tag-function.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
function Component() { | ||
const Foo = () => { | ||
someGlobal = true; | ||
}; | ||
return <Foo />; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
1 | function Component() { | ||
2 | const Foo = () => { | ||
> 3 | someGlobal = true; | ||
| ^^^^^^^^^^ InvalidReact: Unexpected reassignment of a variable which was defined outside of the component. Components and hooks should be pure and side-effect free, but variable reassignment is a form of side-effect. If this variable is used in rendering, use useState instead. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render) (3:3) | ||
4 | }; | ||
5 | return <Foo />; | ||
6 | } | ||
``` | ||
6 changes: 6 additions & 0 deletions
6
...compiler/src/__tests__/fixtures/compiler/error.assign-global-in-component-tag-function.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
function Component() { | ||
const Foo = () => { | ||
someGlobal = true; | ||
}; | ||
return <Foo />; | ||
} |
30 changes: 30 additions & 0 deletions
30
...r/src/__tests__/fixtures/compiler/error.assign-global-in-jsx-children.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
function Component() { | ||
const foo = () => { | ||
someGlobal = true; | ||
}; | ||
// Children are generally access/called during render, so | ||
// modifying a global in a children function is almost | ||
// certainly a mistake. | ||
return <Foo>{foo}</Foo>; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
1 | function Component() { | ||
2 | const foo = () => { | ||
> 3 | someGlobal = true; | ||
| ^^^^^^^^^^ InvalidReact: Unexpected reassignment of a variable which was defined outside of the component. Components and hooks should be pure and side-effect free, but variable reassignment is a form of side-effect. If this variable is used in rendering, use useState instead. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render) (3:3) | ||
4 | }; | ||
5 | // Children are generally access/called during render, so | ||
6 | // modifying a global in a children function is almost | ||
``` | ||
9 changes: 9 additions & 0 deletions
9
...gin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-jsx-children.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function Component() { | ||
const foo = () => { | ||
someGlobal = true; | ||
}; | ||
// Children are generally access/called during render, so | ||
// modifying a global in a children function is almost | ||
// certainly a mistake. | ||
return <Foo>{foo}</Foo>; | ||
} |
27 changes: 27 additions & 0 deletions
27
...tests__/fixtures/compiler/error.assign-global-in-jsx-spread-attribute.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
function Component() { | ||
const foo = () => { | ||
someGlobal = true; | ||
}; | ||
return <div {...foo} />; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
1 | function Component() { | ||
2 | const foo = () => { | ||
> 3 | someGlobal = true; | ||
| ^^^^^^^^^^ InvalidReact: Unexpected reassignment of a variable which was defined outside of the component. Components and hooks should be pure and side-effect free, but variable reassignment is a form of side-effect. If this variable is used in rendering, use useState instead. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render) (3:3) | ||
4 | }; | ||
5 | return <div {...foo} />; | ||
6 | } | ||
``` | ||
6 changes: 6 additions & 0 deletions
6
...t-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-jsx-spread-attribute.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
function Component() { | ||
const foo = () => { | ||
someGlobal = true; | ||
}; | ||
return <div {...foo} />; | ||
} |
32 changes: 0 additions & 32 deletions
32
..._tests__/fixtures/compiler/error.reassignment-to-global-function-prop.expect.md
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.