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

Correctly transpile when default parameter initializer references binding in rest pattern #11317

Closed
wants to merge 9 commits into from

Conversation

vedantroy
Copy link
Contributor

@vedantroy vedantroy commented Mar 22, 2020

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

Fixes the bug where ({ a, ...R }, b = R, c = a) => {} was not transpiled correctly if transform-function-parameters was not enabled.

I used solution 2 as described by @JLHwung in the issue post. This solution loops over each param with a default initializer a = b/nested default initializer (like {a = b} and roughly only transforms it if it is referencing a id created in an object rest pattern or if the previous parameter has been processed.

For example, only the last 2 parameters in this function will be transformed.
({...b}, e, c = 2, a = b, f) => {}

Looking at the "parameters-extra" tests will clarify. There are some edge cases.

Problem: Because I extracted convertParam from extractFunctionParams in the babel-plugin-transform-parameters package, I had to import that package into this plugin, and update the package.json for this plugin. However, I couldn't do that because that would break the package-lock.json. This is causing the CI to fail, even though the tests pass locally on my computer.

convertParam is extracted for use in
babel-plugin-proposal-object-rest-spread
The Function visitor now checks if a parameter with an initializer
references a id declared in a rest pattern in a different parameter.
It transforms this special case.
The original Function visitor looped over function parameters from
right to left. This visitor loops from left to right, which changes
the output code, without breaking it.
Add parameters without initializers to the parameters-extra
test to make more thorough.
The second parameter in f({...S}, {a} = S) is now
transformed.
The previous code fails on default param intializers, like
f({...R}, a = {R}) because getBindingIdentifiers does not retrieve
"R" in the 2nd parameter. Using visitors fixes this edge case.
@JLHwung
Copy link
Contributor

JLHwung commented Mar 23, 2020

@vedantroy Can you rebase on the upstream master?

@vedantroy
Copy link
Contributor Author

@JLHwung. I rebased. However, while rebasing I realized there was a much better way to do this. So I am closing this PR because the commit history is useless. I opened up this one #11326 instead, which has the, new, better way.

@vedantroy vedantroy closed this Mar 24, 2020
@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 Jun 23, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

object-rest-spread does not transpile correctly when binding is referenced in param initializer
2 participants