Skip to content

Commit f7bd2a9

Browse files
committed
[Compiler] ValidateNoDerivedComputationsInEffects test cases
Summary: This creates the test cases we expect this first iteration of calculate in render to catch Test Plan: Test cases
1 parent 720bb13 commit f7bd2a9

File tree

26 files changed

+1090
-9
lines changed

26 files changed

+1090
-9
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validateNoDerivedComputationsInEffects
6+
import {useEffect, useState} from 'react';
7+
8+
function Component({value, enabled}) {
9+
const [localValue, setLocalValue] = useState('');
10+
11+
useEffect(() => {
12+
if (enabled) {
13+
setLocalValue(value);
14+
} else {
15+
setLocalValue('disabled');
16+
}
17+
}, [value, enabled]);
18+
19+
return <div>{localValue}</div>;
20+
}
21+
22+
export const FIXTURE_ENTRYPOINT = {
23+
fn: Component,
24+
params: [{value: 'test', enabled: true}],
25+
};
26+
27+
```
28+
29+
## Code
30+
31+
```javascript
32+
import { c as _c } from "react/compiler-runtime"; // @validateNoDerivedComputationsInEffects
33+
import { useEffect, useState } from "react";
34+
35+
function Component(t0) {
36+
const $ = _c(6);
37+
const { value, enabled } = t0;
38+
const [localValue, setLocalValue] = useState("");
39+
let t1;
40+
let t2;
41+
if ($[0] !== enabled || $[1] !== value) {
42+
t1 = () => {
43+
if (enabled) {
44+
setLocalValue(value);
45+
} else {
46+
setLocalValue("disabled");
47+
}
48+
};
49+
50+
t2 = [value, enabled];
51+
$[0] = enabled;
52+
$[1] = value;
53+
$[2] = t1;
54+
$[3] = t2;
55+
} else {
56+
t1 = $[2];
57+
t2 = $[3];
58+
}
59+
useEffect(t1, t2);
60+
let t3;
61+
if ($[4] !== localValue) {
62+
t3 = <div>{localValue}</div>;
63+
$[4] = localValue;
64+
$[5] = t3;
65+
} else {
66+
t3 = $[5];
67+
}
68+
return t3;
69+
}
70+
71+
export const FIXTURE_ENTRYPOINT = {
72+
fn: Component,
73+
params: [{ value: "test", enabled: true }],
74+
};
75+
76+
```
77+
78+
### Eval output
79+
(kind: ok) <div>test</div>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @validateNoDerivedComputationsInEffects
2+
import {useEffect, useState} from 'react';
3+
4+
function Component({value, enabled}) {
5+
const [localValue, setLocalValue] = useState('');
6+
7+
useEffect(() => {
8+
if (enabled) {
9+
setLocalValue(value);
10+
} else {
11+
setLocalValue('disabled');
12+
}
13+
}, [value, enabled]);
14+
15+
return <div>{localValue}</div>;
16+
}
17+
18+
export const FIXTURE_ENTRYPOINT = {
19+
fn: Component,
20+
params: [{value: 'test', enabled: true}],
21+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validateNoDerivedComputationsInEffects
6+
import {useEffect, useState} from 'react';
7+
8+
export default function Component({input = 'empty'}) {
9+
const [currInput, setCurrInput] = useState(input);
10+
const localConst = 'local const';
11+
12+
useEffect(() => {
13+
setCurrInput(input + localConst);
14+
}, [input, localConst]);
15+
16+
return <div>{currInput}</div>;
17+
}
18+
19+
export const FIXTURE_ENTRYPOINT = {
20+
fn: Component,
21+
params: [{input: 'test'}],
22+
};
23+
24+
```
25+
26+
## Code
27+
28+
```javascript
29+
import { c as _c } from "react/compiler-runtime"; // @validateNoDerivedComputationsInEffects
30+
import { useEffect, useState } from "react";
31+
32+
export default function Component(t0) {
33+
const $ = _c(5);
34+
const { input: t1 } = t0;
35+
const input = t1 === undefined ? "empty" : t1;
36+
const [currInput, setCurrInput] = useState(input);
37+
let t2;
38+
let t3;
39+
if ($[0] !== input) {
40+
t2 = () => {
41+
setCurrInput(input + "local const");
42+
};
43+
t3 = [input, "local const"];
44+
$[0] = input;
45+
$[1] = t2;
46+
$[2] = t3;
47+
} else {
48+
t2 = $[1];
49+
t3 = $[2];
50+
}
51+
useEffect(t2, t3);
52+
let t4;
53+
if ($[3] !== currInput) {
54+
t4 = <div>{currInput}</div>;
55+
$[3] = currInput;
56+
$[4] = t4;
57+
} else {
58+
t4 = $[4];
59+
}
60+
return t4;
61+
}
62+
63+
export const FIXTURE_ENTRYPOINT = {
64+
fn: Component,
65+
params: [{ input: "test" }],
66+
};
67+
68+
```
69+
70+
### Eval output
71+
(kind: ok) <div>testlocal const</div>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @validateNoDerivedComputationsInEffects
2+
import {useEffect, useState} from 'react';
3+
4+
export default function Component({input = 'empty'}) {
5+
const [currInput, setCurrInput] = useState(input);
6+
const localConst = 'local const';
7+
8+
useEffect(() => {
9+
setCurrInput(input + localConst);
10+
}, [input, localConst]);
11+
12+
return <div>{currInput}</div>;
13+
}
14+
15+
export const FIXTURE_ENTRYPOINT = {
16+
fn: Component,
17+
params: [{input: 'test'}],
18+
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validateNoDerivedComputationsInEffects
6+
7+
import { useEffect, useState } from 'react';
8+
9+
function Component({shouldChange}) {
10+
11+
const [count, setCount] = useState(0);
12+
13+
useEffect(() => {
14+
if (shouldChange) {
15+
setCount(count + 1);
16+
}
17+
}, [count]);
18+
19+
return (<div>{count}</div>)
20+
}
21+
22+
```
23+
24+
## Code
25+
26+
```javascript
27+
import { c as _c } from "react/compiler-runtime"; // @validateNoDerivedComputationsInEffects
28+
29+
import { useEffect, useState } from "react";
30+
31+
function Component(t0) {
32+
const $ = _c(7);
33+
const { shouldChange } = t0;
34+
35+
const [count, setCount] = useState(0);
36+
let t1;
37+
if ($[0] !== count || $[1] !== shouldChange) {
38+
t1 = () => {
39+
if (shouldChange) {
40+
setCount(count + 1);
41+
}
42+
};
43+
$[0] = count;
44+
$[1] = shouldChange;
45+
$[2] = t1;
46+
} else {
47+
t1 = $[2];
48+
}
49+
let t2;
50+
if ($[3] !== count) {
51+
t2 = [count];
52+
$[3] = count;
53+
$[4] = t2;
54+
} else {
55+
t2 = $[4];
56+
}
57+
useEffect(t1, t2);
58+
let t3;
59+
if ($[5] !== count) {
60+
t3 = <div>{count}</div>;
61+
$[5] = count;
62+
$[6] = t3;
63+
} else {
64+
t3 = $[6];
65+
}
66+
return t3;
67+
}
68+
69+
```
70+
71+
### Eval output
72+
(kind: exception) Fixture not implemented
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @validateNoDerivedComputationsInEffects
2+
3+
import { useEffect, useState } from 'react';
4+
5+
function Component({shouldChange}) {
6+
7+
const [count, setCount] = useState(0);
8+
9+
useEffect(() => {
10+
if (shouldChange) {
11+
setCount(count + 1);
12+
}
13+
}, [count]);
14+
15+
return (<div>{count}</div>)
16+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validateNoDerivedComputationsInEffects
6+
import {useEffect, useState} from 'react';
7+
8+
function Component({firstName}) {
9+
const [lastName, setLastName] = useState('Doe');
10+
const [fullName, setFullName] = useState('John');
11+
12+
const middleName = 'D.';
13+
14+
useEffect(() => {
15+
setFullName(firstName + ' ' + middleName + ' ' + lastName);
16+
}, [firstName, middleName, lastName]);
17+
18+
return (
19+
<div>
20+
<input value={lastName} onChange={e => setLastName(e.target.value)} />
21+
<div>{fullName}</div>
22+
</div>
23+
);
24+
}
25+
26+
export const FIXTURE_ENTRYPOINT = {
27+
fn: Component,
28+
params: [{firstName: 'John'}],
29+
};
30+
31+
```
32+
33+
## Code
34+
35+
```javascript
36+
import { c as _c } from "react/compiler-runtime"; // @validateNoDerivedComputationsInEffects
37+
import { useEffect, useState } from "react";
38+
39+
function Component(t0) {
40+
const $ = _c(12);
41+
const { firstName } = t0;
42+
const [lastName, setLastName] = useState("Doe");
43+
const [fullName, setFullName] = useState("John");
44+
let t1;
45+
let t2;
46+
if ($[0] !== firstName || $[1] !== lastName) {
47+
t1 = () => {
48+
setFullName(firstName + " " + "D." + " " + lastName);
49+
};
50+
t2 = [firstName, "D.", lastName];
51+
$[0] = firstName;
52+
$[1] = lastName;
53+
$[2] = t1;
54+
$[3] = t2;
55+
} else {
56+
t1 = $[2];
57+
t2 = $[3];
58+
}
59+
useEffect(t1, t2);
60+
let t3;
61+
if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
62+
t3 = (e) => setLastName(e.target.value);
63+
$[4] = t3;
64+
} else {
65+
t3 = $[4];
66+
}
67+
let t4;
68+
if ($[5] !== lastName) {
69+
t4 = <input value={lastName} onChange={t3} />;
70+
$[5] = lastName;
71+
$[6] = t4;
72+
} else {
73+
t4 = $[6];
74+
}
75+
let t5;
76+
if ($[7] !== fullName) {
77+
t5 = <div>{fullName}</div>;
78+
$[7] = fullName;
79+
$[8] = t5;
80+
} else {
81+
t5 = $[8];
82+
}
83+
let t6;
84+
if ($[9] !== t4 || $[10] !== t5) {
85+
t6 = (
86+
<div>
87+
{t4}
88+
{t5}
89+
</div>
90+
);
91+
$[9] = t4;
92+
$[10] = t5;
93+
$[11] = t6;
94+
} else {
95+
t6 = $[11];
96+
}
97+
return t6;
98+
}
99+
100+
export const FIXTURE_ENTRYPOINT = {
101+
fn: Component,
102+
params: [{ firstName: "John" }],
103+
};
104+
105+
```
106+
107+
### Eval output
108+
(kind: ok) <div><input value="Doe"><div>John D. Doe</div></div>

0 commit comments

Comments
 (0)