-
Notifications
You must be signed in to change notification settings - Fork 49.9k
[compiler] Infer render helpers for additional validation #33647
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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
44 changes: 44 additions & 0 deletions
44
...compiler/error.invalid-mutate-global-in-render-helper-phi-return-prop.expect.md
This file contains hidden or 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,44 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| function Component() { | ||
| const renderItem = item => { | ||
| // Multiple returns so that the return type is a Phi (union) | ||
| if (item == null) { | ||
| return null; | ||
| } | ||
| // Normally we assume that it's safe to mutate globals in a function passed | ||
| // as a prop, because the prop could be used as an event handler or effect. | ||
| // But if the function returns JSX we can assume it's a render helper, ie | ||
| // called during render, and thus it's unsafe to mutate globals or call | ||
| // other impure code. | ||
| global.property = true; | ||
| return <Item item={item} value={rand} />; | ||
| }; | ||
| return <ItemList renderItem={renderItem} />; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| ## Error | ||
|
|
||
| ``` | ||
| Found 1 error: | ||
|
|
||
| Error: This value cannot be modified | ||
|
|
||
| Modifying a variable defined outside a component or hook is not allowed. Consider using an effect. | ||
|
|
||
| error.invalid-mutate-global-in-render-helper-phi-return-prop.ts:12:4 | ||
| 10 | // called during render, and thus it's unsafe to mutate globals or call | ||
| 11 | // other impure code. | ||
| > 12 | global.property = true; | ||
| | ^^^^^^ value cannot be modified | ||
| 13 | return <Item item={item} value={rand} />; | ||
| 14 | }; | ||
| 15 | return <ItemList renderItem={renderItem} />; | ||
| ``` | ||
|
|
||
|
|
16 changes: 16 additions & 0 deletions
16
...tests__/fixtures/compiler/error.invalid-mutate-global-in-render-helper-phi-return-prop.js
This file contains hidden or 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,16 @@ | ||
| function Component() { | ||
| const renderItem = item => { | ||
| // Multiple returns so that the return type is a Phi (union) | ||
| if (item == null) { | ||
| return null; | ||
| } | ||
| // Normally we assume that it's safe to mutate globals in a function passed | ||
| // as a prop, because the prop could be used as an event handler or effect. | ||
| // But if the function returns JSX we can assume it's a render helper, ie | ||
| // called during render, and thus it's unsafe to mutate globals or call | ||
| // other impure code. | ||
| global.property = true; | ||
| return <Item item={item} value={rand} />; | ||
| }; | ||
| return <ItemList renderItem={renderItem} />; | ||
| } |
40 changes: 40 additions & 0 deletions
40
..._/fixtures/compiler/error.invalid-mutate-global-in-render-helper-prop.expect.md
This file contains hidden or 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,40 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| function Component() { | ||
| const renderItem = item => { | ||
| // Normally we assume that it's safe to mutate globals in a function passed | ||
| // as a prop, because the prop could be used as an event handler or effect. | ||
| // But if the function returns JSX we can assume it's a render helper, ie | ||
| // called during render, and thus it's unsafe to mutate globals or call | ||
| // other impure code. | ||
| global.property = true; | ||
| return <Item item={item} value={rand} />; | ||
| }; | ||
| return <ItemList renderItem={renderItem} />; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| ## Error | ||
|
|
||
| ``` | ||
| Found 1 error: | ||
|
|
||
| Error: This value cannot be modified | ||
|
|
||
| Modifying a variable defined outside a component or hook is not allowed. Consider using an effect. | ||
|
|
||
| error.invalid-mutate-global-in-render-helper-prop.ts:8:4 | ||
| 6 | // called during render, and thus it's unsafe to mutate globals or call | ||
| 7 | // other impure code. | ||
| > 8 | global.property = true; | ||
| | ^^^^^^ value cannot be modified | ||
| 9 | return <Item item={item} value={rand} />; | ||
| 10 | }; | ||
| 11 | return <ItemList renderItem={renderItem} />; | ||
| ``` | ||
|
|
||
|
|
12 changes: 12 additions & 0 deletions
12
...iler/src/__tests__/fixtures/compiler/error.invalid-mutate-global-in-render-helper-prop.js
This file contains hidden or 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,12 @@ | ||
| function Component() { | ||
| const renderItem = item => { | ||
| // Normally we assume that it's safe to mutate globals in a function passed | ||
| // as a prop, because the prop could be used as an event handler or effect. | ||
| // But if the function returns JSX we can assume it's a render helper, ie | ||
| // called during render, and thus it's unsafe to mutate globals or call | ||
| // other impure code. | ||
| global.property = true; | ||
| return <Item item={item} value={rand} />; | ||
| }; | ||
| return <ItemList renderItem={renderItem} />; | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.