-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Correctly insert (Arrow)FunctionExpressions
Previously we would insert new (Arrow)FunctionExpressions as a sibling of the original function. However this would break in the outlining case as it would cause the original function expression's parent to become a SequenceExpression, breaking a bunch of assumptions in the babel plugin. To get around this, we synthesize a new VariableDeclaration to contain the newly inserted function expression and therefore insert it as a true sibling to the original function. Yeah, it's kinda gross ghstack-source-id: cdf36f3913415ebad5a96896812ef58f3c5d0a26 Pull Request resolved: #30446
- Loading branch information
Showing
5 changed files
with
170 additions
and
37 deletions.
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
24 changes: 0 additions & 24 deletions
24
...iler/src/__tests__/fixtures/compiler/error.bug-outlining-in-func-expr.expect.md
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
...plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-outlining-in-func-expr.js
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
...react-compiler/src/__tests__/fixtures/compiler/outlining-in-func-expr.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,72 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
const Component2 = props => { | ||
return ( | ||
<ul> | ||
{props.items.map(item => ( | ||
<li key={item.id}>{item.name}</li> | ||
))} | ||
</ul> | ||
); | ||
}; | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component2, | ||
params: [ | ||
{ | ||
items: [ | ||
{id: 2, name: 'foo'}, | ||
{id: 3, name: 'bar'}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
const Component2 = (props) => { | ||
const $ = _c(4); | ||
let t0; | ||
if ($[0] !== props.items) { | ||
t0 = props.items.map(_temp); | ||
$[0] = props.items; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
let t1; | ||
if ($[2] !== t0) { | ||
t1 = <ul>{t0}</ul>; | ||
$[2] = t0; | ||
$[3] = t1; | ||
} else { | ||
t1 = $[3]; | ||
} | ||
return t1; | ||
}; | ||
const _temp = (item) => { | ||
return <li key={item.id}>{item.name}</li>; | ||
}; | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component2, | ||
params: [ | ||
{ | ||
items: [ | ||
{ id: 2, name: "foo" }, | ||
{ id: 3, name: "bar" }, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <ul><li>foo</li><li>bar</li></ul> |
21 changes: 21 additions & 0 deletions
21
...ges/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-func-expr.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,21 @@ | ||
const Component2 = props => { | ||
return ( | ||
<ul> | ||
{props.items.map(item => ( | ||
<li key={item.id}>{item.name}</li> | ||
))} | ||
</ul> | ||
); | ||
}; | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component2, | ||
params: [ | ||
{ | ||
items: [ | ||
{id: 2, name: 'foo'}, | ||
{id: 3, name: 'bar'}, | ||
], | ||
}, | ||
], | ||
}; |