Skip to content

Commit 822a629

Browse files
committed
[compiler] Validate against JSX in try statements
Per comments on the new validation pass, this disallows creating JSX (expression/fragment) within a try statement. Developers sometimes use this pattern thinking that they can catch errors during the rendering of the element, without realizing that rendering is lazy. The validation allows us to teach developers about the error boundary pattern. ghstack-source-id: 5f00467 Pull Request resolved: #30725
1 parent 5edbe29 commit 822a629

10 files changed

Lines changed: 265 additions & 0 deletions

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ import {outlineFunctions} from '../Optimization/OutlineFunctions';
105105
import {propagatePhiTypes} from '../TypeInference/PropagatePhiTypes';
106106
import {lowerContextAccess} from '../Optimization/LowerContextAccess';
107107
import {validateNoSetStateInPassiveEffects} from '../Validation/ValidateNoSetStateInPassiveEffects';
108+
import {validateNoJSXInTryStatement} from '../Validation/ValidateNoJSXInTryStatement';
108109

109110
export type CompilerPipelineValue =
110111
| {kind: 'ast'; name: string; value: CodegenFunction}
@@ -249,6 +250,8 @@ function* runWithEnvironment(
249250
validateNoSetStateInPassiveEffects(hir);
250251
}
251252

253+
validateNoJSXInTryStatement(hir);
254+
252255
inferReactivePlaces(hir);
253256
yield log({kind: 'hir', name: 'InferReactivePlaces', value: hir});
254257

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {CompilerError, ErrorSeverity} from '..';
9+
import {BlockId, HIRFunction} from '../HIR';
10+
import {retainWhere} from '../Utils/utils';
11+
12+
/**
13+
* Developers may not be aware of error boundaries and lazy evaluation of JSX, leading them
14+
* to use patterns such as `let el; try { el = <Component /> } catch { ... }` to attempt to
15+
* catch rendering errors. Such code will fail to catch errors in rendering, but developers
16+
* may not realize this right away.
17+
*
18+
* This validation pass validates against this pattern: specifically, it errors for JSX
19+
* created within a try block. JSX is allowed within a catch statement, unless that catch
20+
* is itself nested inside an outer try.
21+
*/
22+
export function validateNoJSXInTryStatement(fn: HIRFunction): void {
23+
const activeTryBlocks: Array<BlockId> = [];
24+
const errors = new CompilerError();
25+
for (const [, block] of fn.body.blocks) {
26+
retainWhere(activeTryBlocks, id => id !== block.id);
27+
28+
if (activeTryBlocks.length !== 0) {
29+
for (const instr of block.instructions) {
30+
const {value} = instr;
31+
switch (value.kind) {
32+
case 'JsxExpression':
33+
case 'JsxFragment': {
34+
errors.push({
35+
severity: ErrorSeverity.InvalidReact,
36+
reason: `Unexpected JSX element within a try statement. To catch errors in rendering a given component, wrap that component in an error boundary. (https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary)`,
37+
loc: value.loc,
38+
});
39+
break;
40+
}
41+
}
42+
}
43+
}
44+
45+
if (block.terminal.kind === 'try') {
46+
activeTryBlocks.push(block.terminal.handler);
47+
}
48+
}
49+
if (errors.hasErrors()) {
50+
throw errors;
51+
}
52+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
## Input
3+
4+
```javascript
5+
import {identity} from 'shared-runtime';
6+
7+
function Component(props) {
8+
let el;
9+
try {
10+
let value;
11+
try {
12+
value = identity(props.foo);
13+
} catch {
14+
el = <div value={value} />;
15+
}
16+
} catch {
17+
return null;
18+
}
19+
return el;
20+
}
21+
22+
```
23+
24+
25+
## Error
26+
27+
```
28+
8 | value = identity(props.foo);
29+
9 | } catch {
30+
> 10 | el = <div value={value} />;
31+
| ^^^^^^^^^^^^^^^^^^^^^ InvalidReact: Unexpected JSX element within a try statement. To catch errors in rendering a given component, wrap that component in an error boundary. (https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary) (10:10)
32+
11 | }
33+
12 | } catch {
34+
13 | return null;
35+
```
36+
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {identity} from 'shared-runtime';
2+
3+
function Component(props) {
4+
let el;
5+
try {
6+
let value;
7+
try {
8+
value = identity(props.foo);
9+
} catch {
10+
el = <div value={value} />;
11+
}
12+
} catch {
13+
return null;
14+
}
15+
return el;
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
## Input
3+
4+
```javascript
5+
function Component(props) {
6+
let el;
7+
try {
8+
el = <div />;
9+
} catch {
10+
return null;
11+
}
12+
return el;
13+
}
14+
15+
```
16+
17+
18+
## Error
19+
20+
```
21+
2 | let el;
22+
3 | try {
23+
> 4 | el = <div />;
24+
| ^^^^^^^ InvalidReact: Unexpected JSX element within a try statement. To catch errors in rendering a given component, wrap that component in an error boundary. (https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary) (4:4)
25+
5 | } catch {
26+
6 | return null;
27+
7 | }
28+
```
29+
30+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function Component(props) {
2+
let el;
3+
try {
4+
el = <div />;
5+
} catch {
6+
return null;
7+
}
8+
return el;
9+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
## Input
3+
4+
```javascript
5+
import {identity} from 'shared-runtime';
6+
7+
function Component(props) {
8+
let el;
9+
try {
10+
let value;
11+
try {
12+
value = identity(props.foo);
13+
} catch {
14+
el = <div value={value} />;
15+
}
16+
} finally {
17+
console.log(el);
18+
}
19+
return el;
20+
}
21+
22+
```
23+
24+
25+
## Error
26+
27+
```
28+
3 | function Component(props) {
29+
4 | let el;
30+
> 5 | try {
31+
| ^^^^^
32+
> 6 | let value;
33+
| ^^^^^^^^^^^^^^
34+
> 7 | try {
35+
| ^^^^^^^^^^^^^^
36+
> 8 | value = identity(props.foo);
37+
| ^^^^^^^^^^^^^^
38+
> 9 | } catch {
39+
| ^^^^^^^^^^^^^^
40+
> 10 | el = <div value={value} />;
41+
| ^^^^^^^^^^^^^^
42+
> 11 | }
43+
| ^^^^^^^^^^^^^^
44+
> 12 | } finally {
45+
| ^^^^^^^^^^^^^^
46+
> 13 | console.log(el);
47+
| ^^^^^^^^^^^^^^
48+
> 14 | }
49+
| ^^^^ Todo: (BuildHIR::lowerStatement) Handle TryStatement without a catch clause (5:14)
50+
15 | return el;
51+
16 | }
52+
17 |
53+
```
54+
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {identity} from 'shared-runtime';
2+
3+
function Component(props) {
4+
let el;
5+
try {
6+
let value;
7+
try {
8+
value = identity(props.foo);
9+
} catch {
10+
el = <div value={value} />;
11+
}
12+
} finally {
13+
console.log(el);
14+
}
15+
return el;
16+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
## Input
3+
4+
```javascript
5+
function Component(props) {
6+
let el;
7+
try {
8+
el = <div />;
9+
} finally {
10+
console.log(el);
11+
}
12+
return el;
13+
}
14+
15+
```
16+
17+
18+
## Error
19+
20+
```
21+
1 | function Component(props) {
22+
2 | let el;
23+
> 3 | try {
24+
| ^^^^^
25+
> 4 | el = <div />;
26+
| ^^^^^^^^^^^^^^^^^
27+
> 5 | } finally {
28+
| ^^^^^^^^^^^^^^^^^
29+
> 6 | console.log(el);
30+
| ^^^^^^^^^^^^^^^^^
31+
> 7 | }
32+
| ^^^^ Todo: (BuildHIR::lowerStatement) Handle TryStatement without a catch clause (3:7)
33+
8 | return el;
34+
9 | }
35+
10 |
36+
```
37+
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function Component(props) {
2+
let el;
3+
try {
4+
el = <div />;
5+
} finally {
6+
console.log(el);
7+
}
8+
return el;
9+
}

0 commit comments

Comments
 (0)