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 11, 2020
1 parent 36f9798 commit f781b05
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
Expand Up @@ -306,11 +306,12 @@ const handle = {
// assignment.
this.memoise(member, 2);

value = t.binaryExpression(
operator.slice(0, -1),
this.get(member),
value,
);
const operatorTrunc = operator.slice(0, -1);
if (t.LOGICAL_OPERATORS.includes(operatorTrunc)) {
value = t.logicalExpression(operatorTrunc, this.get(member), value);
} else {
value = t.binaryExpression(operatorTrunc, this.get(member), value);
}
}

parentPath.replaceWith(this.set(member, value));
Expand Down
@@ -0,0 +1,11 @@
class Foo {
#nullish = 0;
#and = 0;
#or = 0;

test() {
this.#nullish ??= 42;
this.#and &&= 0;
this.#or ||= 0;
}
}
@@ -0,0 +1,6 @@
{
"plugins": [
"proposal-logical-assignment-operators",
"proposal-class-properties"
]
}
@@ -0,0 +1,37 @@
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 _nullish = new WeakMap();

var _and = new WeakMap();

var _or = new WeakMap();

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

_and.set(this, {
writable: true,
value: 0
});

_or.set(this, {
writable: true,
value: 0
});
}

test() {
_classPrivateFieldSet(this, _nullish, _classPrivateFieldGet(this, _nullish) ?? 42);

_classPrivateFieldSet(this, _and, _classPrivateFieldGet(this, _and) && 0);

_classPrivateFieldSet(this, _or, _classPrivateFieldGet(this, _or) || 0);
}

}

0 comments on commit f781b05

Please sign in to comment.