Skip to content

fix(transform-fast-rest): rest parameters references with closures#798

Merged
developit merged 2 commits intodevelopit:masterfrom
gdorsi:master
May 5, 2021
Merged

fix(transform-fast-rest): rest parameters references with closures#798
developit merged 2 commits intodevelopit:masterfrom
gdorsi:master

Conversation

@gdorsi
Copy link
Copy Markdown
Contributor

@gdorsi gdorsi commented Feb 2, 2021

closes #797

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 2, 2021

⚠️ No Changeset found

Latest commit: ab39334

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Copy Markdown
Owner

@developit developit left a comment

Choose a reason for hiding this comment

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

This is great! Curious to get your thoughts on the arrow function bit, otherwise this looks ready to go.

Comment thread src/lib/transform-fast-rest.js Outdated
binding.constant &&
binding.referencePaths.length === 1 &&
// arguments access requires the same function scope
getParentFunction(binding.referencePaths[0], t) === func
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

nit: there's a builtin for this (though see my other comment for why you might actually have the better approach here!)

Suggested change
getParentFunction(binding.referencePaths[0], t) === func
binding.referencePaths[0].getFunctionParent() === func

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.

aww, that's nice!

Comment thread src/lib/transform-fast-rest.js Outdated

function getParentFunction(node, t) {
while ((node = node.parentPath)) {
if (t.isFunction(node)) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Arrow functions can access arguments from the enclosing scope, so we could allow them in the tree here:

Suggested change
if (t.isFunction(node)) {
if (t.isFunction(node) && !t.isArrowFunctionExpression(node)) {

Copy link
Copy Markdown
Contributor Author

@gdorsi gdorsi Feb 2, 2021

Choose a reason for hiding this comment

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

I thought about that, but if the rest parameters transformation is applied with the same criteria of the arrow function transformation there would be no gain in terms of bundlesize.

Instead if the [].slice.call is moved in the root function scope it would be cached in some cases.

What do you think?

Copy link
Copy Markdown
Contributor Author

@gdorsi gdorsi Feb 3, 2021

Choose a reason for hiding this comment

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

I've made the change and seems that without checking "arrow function" the bundle size of the test case is smaller:

With the arrow function check:

157 B: parameters-rest-closure.js.gz
115 B: parameters-rest-closure.js.br
165 B: parameters-rest-closure.esm.js.gz
124 B: parameters-rest-closure.esm.js.br
247 B: parameters-rest-closure.umd.js.gz
189 B: parameters-rest-closure.umd.js.br

With getFunctionParent:

146 B: parameters-rest-closure.js.gz
106 B: parameters-rest-closure.js.br
153 B: parameters-rest-closure.esm.js.gz
113 B: parameters-rest-closure.esm.js.br
238 B: parameters-rest-closure.umd.js.gz
177 B: parameters-rest-closure.umd.js.br

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

makes sense from a Gzip perspective (repetition is essentially free), just there's a runtime cost associated with materializing arguments via slice() where it wouldn't otherwise be necessary.

Copy link
Copy Markdown
Owner

@developit developit left a comment

Choose a reason for hiding this comment

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

LGTM!

@developit developit merged commit 7426218 into developit:master May 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Transpiling] Rest parameters doesn't work with non-arrow function closures

2 participants