Skip to content

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

Merged
josephsavona merged 11 commits intofacebook:mainfrom
dimaMachina:arrow-function-with-implicit-return
Mar 21, 2025
Merged

fix(react-compiler): optimize components declared with arrow function and implicit return and compilationMode: 'infer'#31792
josephsavona merged 11 commits intofacebook:mainfrom
dimaMachina:arrow-function-with-implicit-return

Conversation

@dimaMachina
Copy link
Copy Markdown
Contributor

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

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

@josephsavona friendly ping

@josephsavona
Copy link
Copy Markdown
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