Skip to content

fix: add computed property support for object Ref#10863

Merged
nicolo-ribaudo merged 3 commits intobabel:masterfrom
JLHwung:fix-6341
Dec 19, 2019
Merged

fix: add computed property support for object Ref#10863
nicolo-ribaudo merged 3 commits intobabel:masterfrom
JLHwung:fix-6341

Conversation

@JLHwung
Copy link
Copy Markdown
Contributor

@JLHwung JLHwung commented Dec 12, 2019

Q                       A
Fixed Issues? Fixes #6341 (comment)
Patch: Bug Fix? Yes
Tests Added + Pass? Yes
License MIT

In this PR we add support for computed properties when generating objRef for object-rest-spread. In the following example

var prop = "a";
var obj = { a: { b: 42 } };
var {
  [prop]: { ...x }
} = obj;

// transformed on v7.7.5

var prop = "a";
var obj = {
  a: {
    b: 42
  }
};

var {} = obj,
    x = _extends({}, obj.prop);

Here the objRef is obj.prop, which is the parent path of the rest element ...x. Apparently obj.prop is incorrect because [prop] is a computed property. This is fixed by adding computed argument when building t.memberExpression for objRef.

However, the computed properties could be impure: which means we could not evaluate these properties twice. Therefore we refactored the interface of replaceImpureComputedKeys and reuse it to cache the computed properties.

Note that although this PR only addresses #6341 (comment), #6341 can also be closed after merging this PR because I can not reproduce OP's issue on REPL. (link)

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Object Rest/Spread labels Dec 12, 2019
@@ -0,0 +1,20 @@
var key, x, y, z;
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.

Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
@nicolo-ribaudo nicolo-ribaudo merged commit 9be27bc into babel:master Dec 19, 2019
@nicolo-ribaudo nicolo-ribaudo deleted the fix-6341 branch December 19, 2019 00:29
@nicolo-ribaudo
Copy link
Copy Markdown
Member

The failing tests are fixed in master

@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 Mar 19, 2020
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 19, 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 not behaving as expected with computed properties

3 participants