Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for logical assignments with private properties (#11702)
* add support for logical assignments with private properties Patches the logic for handling assignment operators and adds support for handling the logical assignment operators appropriately. Fixes: #11646 * replace hardcoded logical assignment operators with constant Replace a hardcoded check for logical assignment operators with the LOGICAL_OPERATORS constant in plugin-proposal-logical-assignment-operators. Refs: #11702 (comment)
- Loading branch information
Showing
8 changed files
with
161 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...-plugin-proposal-class-properties/test/fixtures/private-loose/logical-assignment/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Foo { | ||
#nullish = 0; | ||
#and = 0; | ||
#or = 0; | ||
|
||
self() { | ||
return this; | ||
} | ||
|
||
test() { | ||
this.#nullish ??= 42; | ||
this.#and &&= 0; | ||
this.#or ||= 0; | ||
|
||
this.self().#nullish ??= 42; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...gin-proposal-class-properties/test/fixtures/private-loose/logical-assignment/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": [ | ||
["proposal-logical-assignment-operators", { "loose": true }], | ||
["proposal-class-properties", { "loose": true }] | ||
] | ||
} |
42 changes: 42 additions & 0 deletions
42
...plugin-proposal-class-properties/test/fixtures/private-loose/logical-assignment/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
function _classPrivateFieldLooseBase(receiver, privateKey) { if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) { throw new TypeError("attempted to use private field on non-instance"); } return receiver; } | ||
|
||
var id = 0; | ||
|
||
function _classPrivateFieldLooseKey(name) { return "__private_" + id++ + "_" + name; } | ||
|
||
var _nullish = _classPrivateFieldLooseKey("nullish"); | ||
|
||
var _and = _classPrivateFieldLooseKey("and"); | ||
|
||
var _or = _classPrivateFieldLooseKey("or"); | ||
|
||
class Foo { | ||
constructor() { | ||
Object.defineProperty(this, _nullish, { | ||
writable: true, | ||
value: 0 | ||
}); | ||
Object.defineProperty(this, _and, { | ||
writable: true, | ||
value: 0 | ||
}); | ||
Object.defineProperty(this, _or, { | ||
writable: true, | ||
value: 0 | ||
}); | ||
} | ||
|
||
self() { | ||
return this; | ||
} | ||
|
||
test() { | ||
var _classPrivateFieldLoo, _classPrivateFieldLoo2, _classPrivateFieldLoo3, _classPrivateFieldLoo4; | ||
|
||
(_classPrivateFieldLoo = _classPrivateFieldLooseBase(this, _nullish))[_nullish] ?? (_classPrivateFieldLoo[_nullish] = 42); | ||
(_classPrivateFieldLoo2 = _classPrivateFieldLooseBase(this, _and))[_and] && (_classPrivateFieldLoo2[_and] = 0); | ||
(_classPrivateFieldLoo3 = _classPrivateFieldLooseBase(this, _or))[_or] || (_classPrivateFieldLoo3[_or] = 0); | ||
(_classPrivateFieldLoo4 = _classPrivateFieldLooseBase(this.self(), _nullish))[_nullish] ?? (_classPrivateFieldLoo4[_nullish] = 42); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
.../babel-plugin-proposal-class-properties/test/fixtures/private/logical-assignment/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Foo { | ||
#nullish = 0; | ||
#and = 0; | ||
#or = 0; | ||
|
||
self() { | ||
return this; | ||
} | ||
|
||
test() { | ||
this.#nullish ??= 42; | ||
this.#and &&= 0; | ||
this.#or ||= 0; | ||
|
||
this.self().#nullish ??= 42; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...el-plugin-proposal-class-properties/test/fixtures/private/logical-assignment/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": [ | ||
"proposal-logical-assignment-operators", | ||
"proposal-class-properties" | ||
] | ||
} |
42 changes: 42 additions & 0 deletions
42
...babel-plugin-proposal-class-properties/test/fixtures/private/logical-assignment/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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 | ||
}); | ||
} | ||
|
||
self() { | ||
return this; | ||
} | ||
|
||
test() { | ||
var _this$self; | ||
|
||
_classPrivateFieldGet(this, _nullish) ?? _classPrivateFieldSet(this, _nullish, 42); | ||
_classPrivateFieldGet(this, _and) && _classPrivateFieldSet(this, _and, 0); | ||
_classPrivateFieldGet(this, _or) || _classPrivateFieldSet(this, _or, 0); | ||
_classPrivateFieldGet(_this$self = this.self(), _nullish) ?? _classPrivateFieldSet(_this$self, _nullish, 42); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters