Skip to content

Commit

Permalink
add support for logical assignments with private properties
Browse files Browse the repository at this point in the history
Patches the logic for handling assignment operators and adds support for
handling the logical assignment operators appropriately.

Fixes: #11646
  • Loading branch information
ryzokuken committed Jun 10, 2020
1 parent 36f9798 commit 95fec86
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/babel-helper-member-expression-to-functions/src/index.js
Expand Up @@ -306,11 +306,21 @@ const handle = {
// assignment.
this.memoise(member, 2);

value = t.binaryExpression(
operator.slice(0, -1),
this.get(member),
value,
);
const operatorTrunc = operator.slice(0, -1);
switch (operatorTrunc) {
case "??":
case "||":
case "&&":
value = t.logicalExpression(operatorTrunc, this.get(member), value);
break;
default:
value = t.binaryExpression(
operator.slice(0, -1),
this.get(member),
value,
);
break;
}
}

parentPath.replaceWith(this.set(member, value));
Expand Down
@@ -0,0 +1,7 @@
class Foo {
#foo = 0;

test() {
this.#foo ??= 42;
}
}
@@ -0,0 +1,6 @@
{
"plugins": [
"proposal-logical-assignment-operators",
"proposal-class-properties"
]
}
@@ -0,0 +1,19 @@
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = privateMap.get(receiver); if (!descriptor) { throw new TypeError("attempted to set private field on non-instance"); } if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } return value; }

function _classPrivateFieldGet(receiver, privateMap) { var descriptor = privateMap.get(receiver); if (!descriptor) { throw new TypeError("attempted to get private field on non-instance"); } if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }

var _foo = new WeakMap();

class Foo {
constructor() {
_foo.set(this, {
writable: true,
value: 0
});
}

test() {
_classPrivateFieldSet(this, _foo, _classPrivateFieldGet(this, _foo) ?? 42);
}

}

0 comments on commit 95fec86

Please sign in to comment.