Skip to content

Commit 98ea57b

Browse files
committed
[compiler] Update ValidateNoDerivedComputationsInEffects_exp to log the error instead of throwing
1 parent 776d44a commit 98ea57b

File tree

41 files changed

+827
-460
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+827
-460
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ function runWithEnvironment(
277277
}
278278

279279
if (env.config.validateNoDerivedComputationsInEffects_exp) {
280-
validateNoDerivedComputationsInEffects_exp(hir);
280+
env.logErrors(validateNoDerivedComputationsInEffects_exp(hir));
281281
}
282282

283283
if (env.config.validateNoSetStateInEffects) {

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects_exp.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {Result} from '../Utils/Result';
89
import {CompilerDiagnostic, CompilerError, Effect} from '..';
910
import {ErrorCategory} from '../CompilerError';
1011
import {
@@ -146,7 +147,7 @@ class DerivationCache {
146147
*/
147148
export function validateNoDerivedComputationsInEffects_exp(
148149
fn: HIRFunction,
149-
): void {
150+
): Result<void, CompilerError> {
150151
const functions: Map<IdentifierId, FunctionExpression> = new Map();
151152
const derivationCache = new DerivationCache();
152153
const errors = new CompilerError();
@@ -206,9 +207,7 @@ export function validateNoDerivedComputationsInEffects_exp(
206207
validateEffect(effect, context);
207208
}
208209

209-
if (errors.hasAnyErrors()) {
210-
throw errors;
211-
}
210+
return errors.asResult();
212211
}
213212

214213
function recordPhiDerivations(
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly
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_exp @loggerTestOnly
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+
## Logs
79+
80+
```
81+
{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":107},"end":{"line":16,"column":1,"index":378},"filename":"derived-state-conditionally-in-effect.ts"},"fnName":"Component","memoSlots":6,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0}
82+
```
83+
84+
### Eval output
85+
(kind: ok) <div>test</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @validateNoDerivedComputationsInEffects_exp
1+
// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly
22
import {useEffect, useState} from 'react';
33

44
function Component({value, enabled}) {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly
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_exp @loggerTestOnly
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+
## Logs
71+
72+
```
73+
{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":15,"index":122},"end":{"line":13,"column":1,"index":372},"filename":"derived-state-from-default-props.ts"},"fnName":"Component","memoSlots":5,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0}
74+
```
75+
76+
### Eval output
77+
(kind: ok) <div>testlocal const</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @validateNoDerivedComputationsInEffects_exp
1+
// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly
22
import {useEffect, useState} from 'react';
33

44
export default function Component({input = 'empty'}) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly
6+
7+
import {useEffect, useState} from 'react';
8+
9+
function Component({shouldChange}) {
10+
const [count, setCount] = useState(0);
11+
12+
useEffect(() => {
13+
if (shouldChange) {
14+
setCount(count + 1);
15+
}
16+
}, [count]);
17+
18+
return <div>{count}</div>;
19+
}
20+
21+
```
22+
23+
## Code
24+
25+
```javascript
26+
import { c as _c } from "react/compiler-runtime"; // @validateNoDerivedComputationsInEffects_exp @loggerTestOnly
27+
28+
import { useEffect, useState } from "react";
29+
30+
function Component(t0) {
31+
const $ = _c(7);
32+
const { shouldChange } = t0;
33+
const [count, setCount] = useState(0);
34+
let t1;
35+
if ($[0] !== count || $[1] !== shouldChange) {
36+
t1 = () => {
37+
if (shouldChange) {
38+
setCount(count + 1);
39+
}
40+
};
41+
$[0] = count;
42+
$[1] = shouldChange;
43+
$[2] = t1;
44+
} else {
45+
t1 = $[2];
46+
}
47+
let t2;
48+
if ($[3] !== count) {
49+
t2 = [count];
50+
$[3] = count;
51+
$[4] = t2;
52+
} else {
53+
t2 = $[4];
54+
}
55+
useEffect(t1, t2);
56+
let t3;
57+
if ($[5] !== count) {
58+
t3 = <div>{count}</div>;
59+
$[5] = count;
60+
$[6] = t3;
61+
} else {
62+
t3 = $[6];
63+
}
64+
return t3;
65+
}
66+
67+
```
68+
69+
## Logs
70+
71+
```
72+
{"kind":"CompileSuccess","fnLoc":{"start":{"line":5,"column":0,"index":108},"end":{"line":15,"column":1,"index":310},"filename":"derived-state-from-local-state-in-effect.ts"},"fnName":"Component","memoSlots":7,"memoBlocks":3,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0}
73+
```
74+
75+
### Eval output
76+
(kind: exception) Fixture not implemented
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @validateNoDerivedComputationsInEffects_exp
1+
// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly
22

33
import {useEffect, useState} from 'react';
44

0 commit comments

Comments
 (0)