Skip to content

Commit 5cd5be5

Browse files
committed
[compiler][newinference] Update fixtures
Updates fixtures whose output I've double-checked should change ghstack-source-id: bf3d5a6 Pull Request resolved: #33471
1 parent 4059aeb commit 5cd5be5

File tree

24 files changed

+296
-124
lines changed

24 files changed

+296
-124
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
## Input
3+
4+
```javascript
5+
import {useEffect, useState} from 'react';
6+
import {Stringify} from 'shared-runtime';
7+
8+
function Foo() {
9+
/**
10+
* Previously, this lowered to
11+
* $1 = LoadContext capture setState
12+
* $2 = FunctionExpression deps=$1 context=setState
13+
* [[ at this point, we freeze the `LoadContext setState` instruction, but it will never be referenced again ]]
14+
*
15+
* Now, this function expression directly references `setState`, which freezes
16+
* the source `DeclareContext HoistedConst setState`. Freezing source identifiers
17+
* (instead of the one level removed `LoadContext`) is more semantically correct
18+
* for everything *other* than hoisted context declarations.
19+
*
20+
* $2 = Function context=setState
21+
*/
22+
useEffect(() => setState(2), []);
23+
24+
const [state, setState] = useState(0);
25+
return <Stringify state={state} />;
26+
}
27+
28+
export const FIXTURE_ENTRYPOINT = {
29+
fn: Foo,
30+
params: [{}],
31+
sequentialRenders: [{}, {}],
32+
};
33+
34+
```
35+
36+
37+
## Error
38+
39+
```
40+
18 | useEffect(() => setState(2), []);
41+
19 |
42+
> 20 | const [state, setState] = useState(0);
43+
| ^^^^^^^^ InvalidReact: Updating a value used previously in an effect function or as an effect dependency is not allowed. Consider moving the mutation before calling useEffect(). Found mutation of `setState` (20:20)
44+
21 | return <Stringify state={state} />;
45+
22 | }
46+
23 |
47+
```
48+
49+

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-hook-function-argument-mutates-local-variable.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function useFoo() {
2424
> 6 | cache.set('key', 'value');
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626
> 7 | });
27-
| ^^^^ InvalidReact: This argument is a function which modifies local variables when called, which can bypass memoization and cause the UI not to update. Functions that are returned from hooks, passed as arguments to hooks, or passed as props to components may not mutate local variables (5:7)
27+
| ^^^^ InvalidReact: This argument is a function which may reassign or mutate local variables after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead (5:7)
2828
2929
InvalidReact: The function modifies a local variable here (6:6)
3030
8 | }
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
## Input
3+
4+
```javascript
5+
import {Stringify, useIdentity} from 'shared-runtime';
6+
7+
function Component({prop1, prop2}) {
8+
'use memo';
9+
10+
const data = useIdentity(
11+
new Map([
12+
[0, 'value0'],
13+
[1, 'value1'],
14+
])
15+
);
16+
let i = 0;
17+
const items = [];
18+
items.push(
19+
<Stringify
20+
key={i}
21+
onClick={() => data.get(i) + prop1}
22+
shouldInvokeFns={true}
23+
/>
24+
);
25+
i = i + 1;
26+
items.push(
27+
<Stringify
28+
key={i}
29+
onClick={() => data.get(i) + prop2}
30+
shouldInvokeFns={true}
31+
/>
32+
);
33+
return <>{items}</>;
34+
}
35+
36+
export const FIXTURE_ENTRYPOINT = {
37+
fn: Component,
38+
params: [{prop1: 'prop1', prop2: 'prop2'}],
39+
sequentialRenders: [
40+
{prop1: 'prop1', prop2: 'prop2'},
41+
{prop1: 'prop1', prop2: 'prop2'},
42+
{prop1: 'changed', prop2: 'prop2'},
43+
],
44+
};
45+
46+
```
47+
48+
49+
## Error
50+
51+
```
52+
19 | />
53+
20 | );
54+
> 21 | i = i + 1;
55+
| ^ InvalidReact: Updating a value used previously in JSX is not allowed. Consider moving the mutation before the JSX. Found mutation of `i` (21:21)
56+
22 | items.push(
57+
23 | <Stringify
58+
24 | key={i}
59+
```
60+
61+

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-pass-mutable-function-as-prop.expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function Component() {
2020
5 | cache.set('key', 'value');
2121
6 | };
2222
> 7 | return <Foo fn={fn} />;
23-
| ^^ InvalidReact: This argument is a function which modifies local variables when called, which can bypass memoization and cause the UI not to update. Functions that are returned from hooks, passed as arguments to hooks, or passed as props to components may not mutate local variables (7:7)
23+
| ^^ InvalidReact: This argument is a function which may reassign or mutate local variables after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead (7:7)
2424
2525
InvalidReact: The function modifies a local variable here (5:5)
2626
8 | }

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-in-hook-return-value.expect.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ function useFoo() {
1515
## Error
1616

1717
```
18+
1 | function useFoo() {
1819
2 | let x = 0;
19-
3 | return value => {
20+
> 3 | return value => {
21+
| ^^^^^^^^^^
2022
> 4 | x = value;
21-
| ^ InvalidReact: Reassigning a variable after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead. Variable `x` cannot be reassigned after render (4:4)
22-
5 | };
23+
| ^^^^^^^^^^^^^^
24+
> 5 | };
25+
| ^^^^ InvalidReact: This argument is a function which may reassign or mutate local variables after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead (3:5)
26+
27+
InvalidReact: The function modifies a local variable here (4:4)
2328
6 | }
2429
7 |
2530
```

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-effect.expect.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@ function Component() {
4747
## Error
4848

4949
```
50-
5 |
51-
6 | const reassignLocal = newValue => {
52-
> 7 | local = newValue;
53-
| ^^^^^ InvalidReact: Reassigning a variable after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead. Variable `local` cannot be reassigned after render (7:7)
54-
8 | };
55-
9 |
56-
10 | const onMount = newValue => {
50+
31 | };
51+
32 |
52+
> 33 | useEffect(() => {
53+
| ^^^^^^^
54+
> 34 | onMount();
55+
| ^^^^^^^^^^^^^^
56+
> 35 | }, [onMount]);
57+
| ^^^^ InvalidReact: This argument is a function which may reassign or mutate local variables after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead (33:35)
58+
59+
InvalidReact: The function modifies a local variable here (7:7)
60+
36 |
61+
37 | return 'ok';
62+
38 | }
5763
```
5864
5965

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-hook-argument.expect.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,19 @@ function Component() {
4848
## Error
4949

5050
```
51-
6 |
52-
7 | const reassignLocal = newValue => {
53-
> 8 | local = newValue;
54-
| ^^^^^ InvalidReact: Reassigning a variable after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead. Variable `local` cannot be reassigned after render (8:8)
55-
9 | };
56-
10 |
57-
11 | const callback = newValue => {
51+
32 | };
52+
33 |
53+
> 34 | useIdentity(() => {
54+
| ^^^^^^^
55+
> 35 | callback();
56+
| ^^^^^^^^^^^^^^^
57+
> 36 | });
58+
| ^^^^ InvalidReact: This argument is a function which may reassign or mutate local variables after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead (34:36)
59+
60+
InvalidReact: The function modifies a local variable here (8:8)
61+
37 |
62+
38 | return 'ok';
63+
39 | }
5864
```
5965
6066

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-jsx-callback.expect.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ function Component() {
4141
## Error
4242

4343
```
44-
3 |
45-
4 | const reassignLocal = newValue => {
46-
> 5 | local = newValue;
47-
| ^^^^^ InvalidReact: Reassigning a variable after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead. Variable `local` cannot be reassigned after render (5:5)
48-
6 | };
49-
7 |
50-
8 | const onClick = newValue => {
44+
29 | };
45+
30 |
46+
> 31 | return <button onClick={onClick}>Submit</button>;
47+
| ^^^^^^^ InvalidReact: This argument is a function which may reassign or mutate local variables after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead (31:31)
48+
49+
InvalidReact: The function modifies a local variable here (5:5)
50+
32 | }
51+
33 |
5152
```
5253
5354

0 commit comments

Comments
 (0)