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鈥檒l 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 #11326

Merged
merged 7 commits into from Apr 7, 2020
Merged

Conversation

vedantroy
Copy link
Contributor

@vedantroy vedantroy commented Mar 24, 2020

Note: If the title seems familiar, it is because I copied it from #11317, which I closed because I rewrote the code to a large degree, so the commit history was useless. Third time is the charm 馃檭

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 fixed it by modifying convertFunctionParams to accept a function to transform rest elements.

Fix issue 11281. Transform parameters with default initializers that
have ids that are also in a parameter with a rest element.
Before, these parameters were not transformed.
@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 馃悰 A type of pull request used for our changelog categories Spec: Object Rest/Spread labels Mar 24, 2020
@@ -213,8 +218,67 @@ export default declare((api, opts) => {
// function a({ b, ...c }) {}
Function(path) {
const params = path.get("params");
for (let i = params.length - 1; i >= 0; i--) {
replaceRestElement(params[i].parentPath, params[i]);
const paramHasRestElement = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

It suffices to model paramHasRestElement as Set<number> and to only include the index of which has rest element.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. I used an array of booleans instead of a set since the code was about the same length and I figured array access would be (very marginally) faster than set access.

@vedantroy
Copy link
Contributor Author

Any more feedback?

Checking the RHS of an assignment pattern/checking the parent of
an identifier node fails on cases like "({...R}, a = f(R))" or
"({...R}, {[R.key]: a = 42})".

Also refactor tests by removing unecessary tests and
separating "should transform" from "should not transform" tests.
Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

Awesome!

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Thank you!

@nicolo-ribaudo nicolo-ribaudo merged commit aea0fcd into babel:master Apr 7, 2020
@vedantroy vedantroy deleted the fix-11281-real branch April 7, 2020 19:41
@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 Jul 8, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 8, 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 PR: Bug Fix 馃悰 A type of pull request used for our changelog categories Spec: Object Rest/Spread
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
3 participants