fix(react-compiler): optimize components declared with arrow function and implicit return and compilationMode: 'infer'#31792
Conversation
…s and implicit returns with `compilationMode: 'infer'`
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
compilationMode: 'infer'
compilationMode: 'infer'compilationMode: 'infer'
josephsavona
left a comment
There was a problem hiding this comment.
Thanks for the contribution! Please see comments
| if (node.type === 'ArrowFunctionExpression' && node.node.body.type === 'JSXElement') { | ||
| return false | ||
| } |
There was a problem hiding this comment.
I missed this PR the first time around, my apologies. This is an overly narrow solution and needs to be more robust. Let's do the following:
- Extract out a helper that takes an Expression and returns a boolean for whether its possibly a React node or not. This can reuse the
switch (argument.type) { ... }from the current ReturnStatement case. - Reuse this helper in the existing ReturnStatement case
- Expand the ArrowFunctionExpression case to check if the body is an expresssion, and if so use the new helper. otherwise traverse with skipNestedFunctions.
There was a problem hiding this comment.
No worries Joseph, thanks for your detailed feedback 🤩
Extract out a helper that takes an Expression and returns a boolean for whether its possibly a React node or not. This can reuse the switch (argument.type) { ... } from the current ReturnStatement case.
extracted as isNonNode function in d0f5b19
react/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts
Lines 996 to 1010 in d0f5b19
Reuse this helper in the existing ReturnStatement case
react/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts
Lines 1029 to 1032 in d0f5b19
Expand the ArrowFunctionExpression case to check if the body is an expresssion, and if so use the new helper. otherwise traverse with skipNestedFunctions.
I can't get this to work, seems node.traverse#ArrowFunctionExpression isn't called for the root node
There was a problem hiding this comment.
also removed useless hasReturn variable in e0c9364
|
@josephsavona friendly ping |
|
Thanks for the fix! |
fixes #31601 #31639 cc @josephsavona