Skip to content
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

allowMutablePropsOnTags: cache JSX constant elements with function props #12975

Merged

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Mar 6, 2021

Q                       A
Fixed Issues?
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

@jridgewell You were right!


I had to split immutabiltyVisitor and targetScopeVisitor in two visitors because I had to skip functions when checking for immutability, but traverse them when collecting references.

@nicolo-ribaudo nicolo-ribaudo added PR: Output optimization 🔬 A type of pull request used for our changelog categories area: react labels Mar 6, 2021
@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 6, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 4f9b9c4:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@babel-bot
Copy link
Collaborator

babel-bot commented Mar 6, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/51305/

Copy link
Member

@jridgewell jridgewell left a comment

Choose a reason for hiding this comment

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

Can we get a few test cases for nested do expressions?

I think this series of PRs is really clever workaround for the hoisting issue. Thanks!

// If we allow mutable props, tags with function expressions can be
// safely hoisted.
const { mutablePropsAllowed } = state;
if (mutablePropsAllowed && path.isFunction()) return skip();
Copy link
Member

Choose a reason for hiding this comment

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

I'd propose we traverse with the targetScopeVisitor here, so we could keep the two traversals together.

@nicolo-ribaudo nicolo-ribaudo force-pushed the constant-elements-hoist-functions branch from 4f9b9c4 to 425f9f7 Compare February 16, 2022 00:19
if (path.isIdentifier()) {
const binding = path.scope.getBinding(path.node.name);
if (binding && binding.constant) return;
}

if (!path.isImmutable()) {
// If it's not immutable, it may still be a pure expression, such as string concatenation.
Copy link
Member Author

Choose a reason for hiding this comment

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

This was not actually changed; I suggest hiding whitespace changes when reviewing.

@nicolo-ribaudo nicolo-ribaudo changed the title Cache JSX constant elements with functions when allowMutablePropsOnTags allowMutablePropsOnTags: cache JSX const elements w/ fn props Feb 21, 2022
@nicolo-ribaudo nicolo-ribaudo merged commit 300e477 into babel:main Feb 21, 2022
@nicolo-ribaudo nicolo-ribaudo deleted the constant-elements-hoist-functions branch February 21, 2022 22:19
@nicolo-ribaudo nicolo-ribaudo changed the title allowMutablePropsOnTags: cache JSX const elements w/ fn props allowMutablePropsOnTags: cache JSX constant elements with function props Feb 21, 2022
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 24, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: react outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Output optimization 🔬 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants