-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[compiler] Validate against JSX in try statements #30725
Merged
josephsavona
merged 4 commits into
gh/josephsavona/36/base
from
gh/josephsavona/36/head
Aug 19, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
302ca32
[compiler] Validate against JSX in try statements
josephsavona 1a37c1e
Update on "[compiler] Validate against JSX in try statements"
josephsavona 56d58aa
Update on "[compiler] Validate against JSX in try statements"
josephsavona 1c3d4bd
Update on "[compiler] Validate against JSX in try statements"
josephsavona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
52 changes: 52 additions & 0 deletions
52
compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoJSXInTryStatement.ts
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,52 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {CompilerError, ErrorSeverity} from '..'; | ||
import {BlockId, HIRFunction} from '../HIR'; | ||
import {retainWhere} from '../Utils/utils'; | ||
|
||
/** | ||
* Developers may not be aware of error boundaries and lazy evaluation of JSX, leading them | ||
* to use patterns such as `let el; try { el = <Component /> } catch { ... }` to attempt to | ||
* catch rendering errors. Such code will fail to catch errors in rendering, but developers | ||
* may not realize this right away. | ||
* | ||
* This validation pass validates against this pattern: specifically, it errors for JSX | ||
* created within a try block. JSX is allowed within a catch statement, unless that catch | ||
* is itself nested inside an outer try. | ||
*/ | ||
export function validateNoJSXInTryStatement(fn: HIRFunction): void { | ||
const activeTryBlocks: Array<BlockId> = []; | ||
const errors = new CompilerError(); | ||
for (const [, block] of fn.body.blocks) { | ||
retainWhere(activeTryBlocks, id => id !== block.id); | ||
|
||
if (activeTryBlocks.length !== 0) { | ||
for (const instr of block.instructions) { | ||
const {value} = instr; | ||
switch (value.kind) { | ||
case 'JsxExpression': | ||
case 'JsxFragment': { | ||
errors.push({ | ||
severity: ErrorSeverity.InvalidReact, | ||
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)`, | ||
loc: value.loc, | ||
}); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (block.terminal.kind === 'try') { | ||
activeTryBlocks.push(block.terminal.handler); | ||
} | ||
} | ||
if (errors.hasErrors()) { | ||
throw errors; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../fixtures/compiler/error.invalid-jsx-in-catch-in-outer-try-with-catch.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,38 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validateNoJSXInTryStatements | ||
import {identity} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
let el; | ||
try { | ||
let value; | ||
try { | ||
value = identity(props.foo); | ||
} catch { | ||
el = <div value={value} />; | ||
} | ||
} catch { | ||
return null; | ||
} | ||
return el; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
9 | value = identity(props.foo); | ||
10 | } catch { | ||
> 11 | el = <div value={value} />; | ||
| ^^^^^^^^^^^^^^^^^^^^^ 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) (11:11) | ||
12 | } | ||
13 | } catch { | ||
14 | return null; | ||
``` | ||
|
||
|
17 changes: 17 additions & 0 deletions
17
...ler/src/__tests__/fixtures/compiler/error.invalid-jsx-in-catch-in-outer-try-with-catch.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,17 @@ | ||
// @validateNoJSXInTryStatements | ||
import {identity} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
let el; | ||
try { | ||
let value; | ||
try { | ||
value = identity(props.foo); | ||
} catch { | ||
el = <div value={value} />; | ||
} | ||
} catch { | ||
return null; | ||
} | ||
return el; | ||
} |
31 changes: 31 additions & 0 deletions
31
...r/src/__tests__/fixtures/compiler/error.invalid-jsx-in-try-with-catch.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,31 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validateNoJSXInTryStatements | ||
function Component(props) { | ||
let el; | ||
try { | ||
el = <div />; | ||
} catch { | ||
return null; | ||
} | ||
return el; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
3 | let el; | ||
4 | try { | ||
> 5 | el = <div />; | ||
| ^^^^^^^ 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) (5:5) | ||
6 | } catch { | ||
7 | return null; | ||
8 | } | ||
``` | ||
|
||
|
10 changes: 10 additions & 0 deletions
10
...gin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-jsx-in-try-with-catch.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,10 @@ | ||
// @validateNoJSXInTryStatements | ||
function Component(props) { | ||
let el; | ||
try { | ||
el = <div />; | ||
} catch { | ||
return null; | ||
} | ||
return el; | ||
} |
56 changes: 56 additions & 0 deletions
56
...es/compiler/error.todo-invalid-jsx-in-catch-in-outer-try-with-finally.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,56 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validateNoJSXInTryStatements | ||
import {identity} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
let el; | ||
try { | ||
let value; | ||
try { | ||
value = identity(props.foo); | ||
} catch { | ||
el = <div value={value} />; | ||
} | ||
} finally { | ||
console.log(el); | ||
} | ||
return el; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
4 | function Component(props) { | ||
5 | let el; | ||
> 6 | try { | ||
| ^^^^^ | ||
> 7 | let value; | ||
| ^^^^^^^^^^^^^^ | ||
> 8 | try { | ||
| ^^^^^^^^^^^^^^ | ||
> 9 | value = identity(props.foo); | ||
| ^^^^^^^^^^^^^^ | ||
> 10 | } catch { | ||
| ^^^^^^^^^^^^^^ | ||
> 11 | el = <div value={value} />; | ||
| ^^^^^^^^^^^^^^ | ||
> 12 | } | ||
| ^^^^^^^^^^^^^^ | ||
> 13 | } finally { | ||
| ^^^^^^^^^^^^^^ | ||
> 14 | console.log(el); | ||
| ^^^^^^^^^^^^^^ | ||
> 15 | } | ||
| ^^^^ Todo: (BuildHIR::lowerStatement) Handle TryStatement without a catch clause (6:15) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm adding this pre-emptively so that when we support try w only a finally clause, we'll see this either continue to correctly fail or notice that the validation doesn't work and have a chance to fix it |
||
16 | return el; | ||
17 | } | ||
18 | | ||
``` | ||
|
||
|
17 changes: 17 additions & 0 deletions
17
.../__tests__/fixtures/compiler/error.todo-invalid-jsx-in-catch-in-outer-try-with-finally.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,17 @@ | ||
// @validateNoJSXInTryStatements | ||
import {identity} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
let el; | ||
try { | ||
let value; | ||
try { | ||
value = identity(props.foo); | ||
} catch { | ||
el = <div value={value} />; | ||
} | ||
} finally { | ||
console.log(el); | ||
} | ||
return el; | ||
} |
39 changes: 39 additions & 0 deletions
39
..._tests__/fixtures/compiler/error.todo-invalid-jsx-in-try-with-finally.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,39 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validateNoJSXInTryStatements | ||
function Component(props) { | ||
let el; | ||
try { | ||
el = <div />; | ||
} finally { | ||
console.log(el); | ||
} | ||
return el; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
2 | function Component(props) { | ||
3 | let el; | ||
> 4 | try { | ||
| ^^^^^ | ||
> 5 | el = <div />; | ||
| ^^^^^^^^^^^^^^^^^ | ||
> 6 | } finally { | ||
| ^^^^^^^^^^^^^^^^^ | ||
> 7 | console.log(el); | ||
| ^^^^^^^^^^^^^^^^^ | ||
> 8 | } | ||
| ^^^^ Todo: (BuildHIR::lowerStatement) Handle TryStatement without a catch clause (4:8) | ||
9 | return el; | ||
10 | } | ||
11 | | ||
``` | ||
|
||
|
10 changes: 10 additions & 0 deletions
10
...ct-compiler/src/__tests__/fixtures/compiler/error.todo-invalid-jsx-in-try-with-finally.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,10 @@ | ||
// @validateNoJSXInTryStatements | ||
function Component(props) { | ||
let el; | ||
try { | ||
el = <div />; | ||
} finally { | ||
console.log(el); | ||
} | ||
return el; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: should this be a set of ids? do duplicates matter?