Skip to content

fix(react-compiler): optimize components declared with arrow function and implicit return and compilationMode: 'infer' #31792

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

Conversation

dimaMachina
Copy link
Contributor

…s and implicit returns with `compilationMode: 'infer'`
Copy link

vercel bot commented Dec 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 30, 2024 9:16am

@dimaMachina dimaMachina changed the title fix(react-compiler): optimize components declared with arrow function… fix(react-compiler): optimize components declared with arrow function and implicit returns and compilationMode: 'infer' Dec 15, 2024
@dimaMachina dimaMachina changed the title fix(react-compiler): optimize components declared with arrow function and implicit returns and compilationMode: 'infer' fix(react-compiler): optimize components declared with arrow function and implicit return and compilationMode: 'infer' Dec 15, 2024
Copy link
Member

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! Please see comments

Comment on lines 1001 to 1003
if (node.type === 'ArrowFunctionExpression' && node.node.body.type === 'JSXElement') {
return false
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.
  2. Reuse this helper in the existing ReturnStatement case
  3. Expand the ArrowFunctionExpression case to check if the body is an expresssion, and if so use the new helper. otherwise traverse with skipNestedFunctions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

function isNonNode(node?: t.Expression | null): boolean {
if (!node) {
return true;
}
switch (node.type) {
case 'ObjectExpression':
case 'ArrowFunctionExpression':
case 'FunctionExpression':
case 'BigIntLiteral':
case 'ClassExpression':
case 'NewExpression': // technically `new Array()` is legit, but unlikely
return true;
}
return false;
}

Reuse this helper in the existing ReturnStatement case

ReturnStatement(ret) {
hasReturn = true;
returnsNonNode = isNonNode(ret.node.argument);
},

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also removed useless hasReturn variable in e0c9364

@dimaMachina
Copy link
Contributor Author

@josephsavona friendly ping

@josephsavona josephsavona merged commit 6b1a2c1 into facebook:main Mar 21, 2025
21 checks passed
@josephsavona
Copy link
Member

Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Playground Bug]: Unexpected token, expected "{"
3 participants