Skip to content

allowMutablePropsOnTags: cache JSX constant elements with function props - #12975

Merged
nicolo-ribaudo merged 3 commits into
babel:mainfrom
nicolo-ribaudo:constant-elements-hoist-functions
Feb 21, 2022
Merged

nicolo-ribaudo merged 3 commits into
babel:mainfrom
nicolo-ribaudo:constant-elements-hoist-functions

Conversation

@nicolo-ribaudo

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

Copy link
Copy Markdown
Member
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

codesandbox-ci Bot commented Mar 6, 2021

Copy link
Copy Markdown

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

babel-bot commented Mar 6, 2021 •

Copy link
Copy Markdown
Collaborator

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

@jridgewell jridgewell left a comment

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.

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
Copy Markdown
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.isImmutable()) {
// If it's not immutable, it may still be a pure expression, such as string concatenation.

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