-
Notifications
You must be signed in to change notification settings - Fork 51k
[compiler] Validate static components #32683
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
josephsavona
merged 5 commits into
gh/josephsavona/71/base
from
gh/josephsavona/71/head
Mar 20, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1bb9d1d
[compiler] Validate static components
josephsavona 78f6f04
Update on "[compiler] Validate static components"
josephsavona 3c6eddb
Update on "[compiler] Validate static components"
josephsavona 47ea4c7
Update on "[compiler] Validate static components"
josephsavona 9cc3c7e
Update on "[compiler] Validate static components"
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 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
87 changes: 87 additions & 0 deletions
87
compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateStaticComponents.ts
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,87 @@ | ||
| /** | ||
| * 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 '../CompilerError'; | ||
| import {HIRFunction, IdentifierId, SourceLocation} from '../HIR'; | ||
| import {Err, Ok, Result} from '../Utils/Result'; | ||
|
|
||
| /** | ||
| * Validates against components that are created dynamically and whose identity is not guaranteed | ||
| * to be stable (which would cause the component to reset on each re-render). | ||
| */ | ||
| export function validateStaticComponents( | ||
| fn: HIRFunction, | ||
| ): Result<void, CompilerError> { | ||
| const error = new CompilerError(); | ||
| const knownDynamicComponents = new Map<IdentifierId, SourceLocation>(); | ||
| for (const block of fn.body.blocks.values()) { | ||
| phis: for (const phi of block.phis) { | ||
| for (const operand of phi.operands.values()) { | ||
| const loc = knownDynamicComponents.get(operand.identifier.id); | ||
| if (loc != null) { | ||
| knownDynamicComponents.set(phi.place.identifier.id, loc); | ||
| continue phis; | ||
| } | ||
| } | ||
| } | ||
| for (const instr of block.instructions) { | ||
| const {lvalue, value} = instr; | ||
| switch (value.kind) { | ||
| case 'FunctionExpression': | ||
| case 'NewExpression': | ||
| case 'MethodCall': | ||
| case 'CallExpression': { | ||
| knownDynamicComponents.set(lvalue.identifier.id, value.loc); | ||
| break; | ||
| } | ||
| case 'LoadLocal': { | ||
| const loc = knownDynamicComponents.get(value.place.identifier.id); | ||
| if (loc != null) { | ||
| knownDynamicComponents.set(lvalue.identifier.id, loc); | ||
| } | ||
| break; | ||
| } | ||
| case 'StoreLocal': { | ||
| const loc = knownDynamicComponents.get(value.value.identifier.id); | ||
| if (loc != null) { | ||
| knownDynamicComponents.set(lvalue.identifier.id, loc); | ||
| knownDynamicComponents.set(value.lvalue.place.identifier.id, loc); | ||
| } | ||
| break; | ||
| } | ||
| case 'JsxExpression': { | ||
| if (value.tag.kind === 'Identifier') { | ||
| const location = knownDynamicComponents.get( | ||
| value.tag.identifier.id, | ||
| ); | ||
| if (location != null) { | ||
| error.push({ | ||
| reason: `Components created during render will reset their state each time they are created. Declare components outside of render. `, | ||
| severity: ErrorSeverity.InvalidReact, | ||
| loc: value.tag.loc, | ||
| description: null, | ||
| suggestions: null, | ||
| }); | ||
| error.push({ | ||
| reason: `The component may be created during render`, | ||
| severity: ErrorSeverity.InvalidReact, | ||
| loc: location, | ||
| description: null, | ||
| suggestions: null, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (error.hasErrors()) { | ||
| return Err(error); | ||
| } else { | ||
| return Ok(undefined); | ||
| } | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
...id-conditionally-assigned-dynamically-constructed-component-in-render.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,59 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| let Component; | ||
| if (props.cond) { | ||
| Component = createComponent(); | ||
| } else { | ||
| Component = DefaultComponent; | ||
| } | ||
| return <Component />; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { c as _c } from "react/compiler-runtime"; // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| const $ = _c(3); | ||
| let Component; | ||
| if (props.cond) { | ||
| let t0; | ||
| if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
| t0 = createComponent(); | ||
| $[0] = t0; | ||
| } else { | ||
| t0 = $[0]; | ||
| } | ||
| Component = t0; | ||
| } else { | ||
| Component = DefaultComponent; | ||
| } | ||
| let t0; | ||
| if ($[1] !== Component) { | ||
| t0 = <Component />; | ||
| $[1] = Component; | ||
| $[2] = t0; | ||
| } else { | ||
| t0 = $[2]; | ||
| } | ||
| return t0; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ## Logs | ||
|
|
||
| ``` | ||
| {"kind":"CompileError","detail":{"options":{"reason":"Components created during render will reset their state each time they are created. Declare components outside of render. ","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":9,"column":10,"index":194},"end":{"line":9,"column":19,"index":203},"filename":"invalid-conditionally-assigned-dynamically-constructed-component-in-render.ts"}}},"fnLoc":null} | ||
| {"kind":"CompileError","detail":{"options":{"reason":"The component may be created during render","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":5,"column":16,"index":116},"end":{"line":5,"column":33,"index":133},"filename":"invalid-conditionally-assigned-dynamically-constructed-component-in-render.ts"}}},"fnLoc":null} | ||
| {"kind":"CompileSuccess","fnLoc":{"start":{"line":2,"column":0,"index":37},"end":{"line":10,"column":1,"index":209},"filename":"invalid-conditionally-assigned-dynamically-constructed-component-in-render.ts"},"fnName":"Example","memoSlots":3,"memoBlocks":2,"memoValues":2,"prunedMemoBlocks":0,"prunedMemoValues":0} | ||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: exception) Fixture not implemented |
10 changes: 10 additions & 0 deletions
10
...-components/invalid-conditionally-assigned-dynamically-constructed-component-in-render.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,10 @@ | ||
| // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| let Component; | ||
| if (props.cond) { | ||
| Component = createComponent(); | ||
| } else { | ||
| Component = DefaultComponent; | ||
| } | ||
| return <Component />; | ||
| } |
41 changes: 41 additions & 0 deletions
41
...r/static-components/invalid-dynamically-construct-component-in-render.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,41 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| const Component = createComponent(); | ||
| return <Component />; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { c as _c } from "react/compiler-runtime"; // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| const $ = _c(1); | ||
| let t0; | ||
| if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
| const Component = createComponent(); | ||
| t0 = <Component />; | ||
| $[0] = t0; | ||
| } else { | ||
| t0 = $[0]; | ||
| } | ||
| return t0; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ## Logs | ||
|
|
||
| ``` | ||
| {"kind":"CompileError","detail":{"options":{"reason":"Components created during render will reset their state each time they are created. Declare components outside of render. ","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":4,"column":10,"index":112},"end":{"line":4,"column":19,"index":121},"filename":"invalid-dynamically-construct-component-in-render.ts"}}},"fnLoc":null} | ||
| {"kind":"CompileError","detail":{"options":{"reason":"The component may be created during render","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":3,"column":20,"index":83},"end":{"line":3,"column":37,"index":100},"filename":"invalid-dynamically-construct-component-in-render.ts"}}},"fnLoc":null} | ||
| {"kind":"CompileSuccess","fnLoc":{"start":{"line":2,"column":0,"index":37},"end":{"line":5,"column":1,"index":127},"filename":"invalid-dynamically-construct-component-in-render.ts"},"fnName":"Example","memoSlots":1,"memoBlocks":1,"memoValues":1,"prunedMemoBlocks":0,"prunedMemoValues":0} | ||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: exception) Fixture not implemented |
5 changes: 5 additions & 0 deletions
5
.../fixtures/compiler/static-components/invalid-dynamically-construct-component-in-render.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,5 @@ | ||
| // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| const Component = createComponent(); | ||
| return <Component />; | ||
| } |
46 changes: 46 additions & 0 deletions
46
.../static-components/invalid-dynamically-constructed-component-function.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,46 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| function Component() { | ||
| return <div />; | ||
| } | ||
| return <Component />; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { c as _c } from "react/compiler-runtime"; // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| const $ = _c(1); | ||
| let t0; | ||
| if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
| const Component = function Component() { | ||
| return <div />; | ||
| }; | ||
|
|
||
| t0 = <Component />; | ||
| $[0] = t0; | ||
| } else { | ||
| t0 = $[0]; | ||
| } | ||
| return t0; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ## Logs | ||
|
|
||
| ``` | ||
| {"kind":"CompileError","detail":{"options":{"reason":"Components created during render will reset their state each time they are created. Declare components outside of render. ","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":6,"column":10,"index":122},"end":{"line":6,"column":19,"index":131},"filename":"invalid-dynamically-constructed-component-function.ts"}}},"fnLoc":null} | ||
| {"kind":"CompileError","detail":{"options":{"reason":"The component may be created during render","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":3,"column":2,"index":65},"end":{"line":5,"column":3,"index":111},"filename":"invalid-dynamically-constructed-component-function.ts"}}},"fnLoc":null} | ||
| {"kind":"CompileSuccess","fnLoc":{"start":{"line":2,"column":0,"index":37},"end":{"line":7,"column":1,"index":137},"filename":"invalid-dynamically-constructed-component-function.ts"},"fnName":"Example","memoSlots":1,"memoBlocks":1,"memoValues":1,"prunedMemoBlocks":0,"prunedMemoValues":0} | ||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: exception) Fixture not implemented |
7 changes: 7 additions & 0 deletions
7
...fixtures/compiler/static-components/invalid-dynamically-constructed-component-function.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,7 @@ | ||
| // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| function Component() { | ||
| return <div />; | ||
| } | ||
| return <Component />; | ||
| } |
50 changes: 50 additions & 0 deletions
50
...atic-components/invalid-dynamically-constructed-component-method-call.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,50 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| const Component = props.foo.bar(); | ||
| return <Component />; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { c as _c } from "react/compiler-runtime"; // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| const $ = _c(4); | ||
| let t0; | ||
| if ($[0] !== props.foo) { | ||
| t0 = props.foo.bar(); | ||
| $[0] = props.foo; | ||
| $[1] = t0; | ||
| } else { | ||
| t0 = $[1]; | ||
| } | ||
| const Component = t0; | ||
| let t1; | ||
| if ($[2] !== Component) { | ||
| t1 = <Component />; | ||
| $[2] = Component; | ||
| $[3] = t1; | ||
| } else { | ||
| t1 = $[3]; | ||
| } | ||
| return t1; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ## Logs | ||
|
|
||
| ``` | ||
| {"kind":"CompileError","detail":{"options":{"reason":"Components created during render will reset their state each time they are created. Declare components outside of render. ","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":4,"column":10,"index":110},"end":{"line":4,"column":19,"index":119},"filename":"invalid-dynamically-constructed-component-method-call.ts"}}},"fnLoc":null} | ||
| {"kind":"CompileError","detail":{"options":{"reason":"The component may be created during render","description":null,"severity":"InvalidReact","suggestions":null,"loc":{"start":{"line":3,"column":20,"index":83},"end":{"line":3,"column":35,"index":98},"filename":"invalid-dynamically-constructed-component-method-call.ts"}}},"fnLoc":null} | ||
| {"kind":"CompileSuccess","fnLoc":{"start":{"line":2,"column":0,"index":37},"end":{"line":5,"column":1,"index":125},"filename":"invalid-dynamically-constructed-component-method-call.ts"},"fnName":"Example","memoSlots":4,"memoBlocks":2,"memoValues":2,"prunedMemoBlocks":0,"prunedMemoValues":0} | ||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: exception) Fixture not implemented |
5 changes: 5 additions & 0 deletions
5
...tures/compiler/static-components/invalid-dynamically-constructed-component-method-call.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,5 @@ | ||
| // @logger @validateStaticComponents | ||
| function Example(props) { | ||
| const Component = props.foo.bar(); | ||
| return <Component />; | ||
| } |
Oops, something went wrong.
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.
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.
per feedback i switched to return a Result here. this lets us easily refactor later to throw the error if we want, without rewriting the validation pass itself