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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/compat): Readonly and writeonly private field #6182

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/swc/tests/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn init_helpers() -> Arc<PathBuf> {
let helper_dir = project_root.join("packages").join("swc-helpers");

let yarn = find_executable("yarn").expect("failed to find yarn");
let npm = find_executable("npm").expect("failed to find yarn");
let npm = find_executable("npm").expect("failed to find npm");
{
let mut cmd = if cfg!(target_os = "windows") {
let mut c = Command::new("cmd");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import _class_private_field_get from "@swc/helpers/src/_class_private_field_get.mjs";
import _class_private_field_init from "@swc/helpers/src/_class_private_field_init.mjs";
import _class_private_field_set from "@swc/helpers/src/_class_private_field_set.mjs";
import _read_only_error from "@swc/helpers/src/_read_only_error.mjs";
var _prop = /*#__PURE__*/ new WeakMap(), _roProp = /*#__PURE__*/ new WeakMap();
class A1 {
constructor(name){
Expand All @@ -14,7 +15,7 @@ class A1 {
set: void 0
});
_class_private_field_set(this, _prop, "");
_class_private_field_set(this, _roProp, ""); // Error
this, _read_only_error("#roProp"); // Error
console.log(_class_private_field_get(this, _prop));
console.log(_class_private_field_get(this, _roProp));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//// [privateNameMethodAssignment.ts]
import _class_private_field_set from "@swc/helpers/src/_class_private_field_set.mjs";
import _class_private_field_update from "@swc/helpers/src/_class_private_field_update.mjs";
import _class_private_method_get from "@swc/helpers/src/_class_private_method_get.mjs";
import _class_private_method_init from "@swc/helpers/src/_class_private_method_init.mjs";
import _read_only_error from "@swc/helpers/src/_read_only_error.mjs";
import _class_private_field_destructure from "@swc/helpers/src/_class_private_field_destructure.mjs";
var _method = /*#__PURE__*/ new WeakSet();
class A3 {
constructor(a, b){
_class_private_method_init(this, _method);
_class_private_field_set(this, _method, ()=>{} // Error, not writable
);
_class_private_field_set(a, _method, ()=>{}); // Error, not writable
_class_private_field_set(b, _method, ()=>{} //Error, not writable
);
this, _read_only_error("#method") // Error, not writable
;
a, _read_only_error("#method"); // Error, not writable
b, _read_only_error("#method") //Error, not writable
;
({ x: _class_private_field_destructure(this, _method).value } = {
x: ()=>{}
}); //Error, not writable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//// [privateNameReadonly.ts]
import _class_private_field_set from "@swc/helpers/src/_class_private_field_set.mjs";
import _class_private_method_init from "@swc/helpers/src/_class_private_method_init.mjs";
import _read_only_error from "@swc/helpers/src/_read_only_error.mjs";
var _bar, _class;
const C = (_bar = /*#__PURE__*/ new WeakSet(), _class = class {
foo() {
_class_private_field_set(this, _bar, console.log("should log this then throw"));
this, console.log("should log this then throw"), _read_only_error("#bar");
}
constructor(){
_class_private_method_init(this, _bar);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//// [privateNameReadonly.ts]
var _bar;
import _class_private_field_set from "@swc/helpers/src/_class_private_field_set.mjs";
import _class_private_method_init from "@swc/helpers/src/_class_private_method_init.mjs";
import _read_only_error from "@swc/helpers/src/_read_only_error.mjs";
let C = (_bar = new WeakSet(), class {
foo() {
_class_private_field_set(this, _bar, console.log("should log this then throw"));
console.log("should log this then throw"), _read_only_error("#bar");
}
constructor(){
_class_private_method_init(this, _bar);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//// [privateNameSetterNoGetter.ts]
import _class_private_field_get from "@swc/helpers/src/_class_private_field_get.mjs";
import _class_private_field_init from "@swc/helpers/src/_class_private_field_init.mjs";
import _class_private_field_set from "@swc/helpers/src/_class_private_field_set.mjs";
import _write_only_error from "@swc/helpers/src/_write_only_error.mjs";
var _x, _class;
const C = (_x = /*#__PURE__*/ new WeakMap(), _class = class {
m() {
_class_private_field_set(this, _x, _class_private_field_get(this, _x) + 2); // Error
_class_private_field_set(this, _x, (this, _write_only_error("#x")) + 2); // Error
}
constructor(){
_class_private_field_init(this, _x, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//// [privateNameSetterNoGetter.ts]
var _x;
import _class_private_field_get from "@swc/helpers/src/_class_private_field_get.mjs";
import _class_private_field_init from "@swc/helpers/src/_class_private_field_init.mjs";
import _class_private_field_set from "@swc/helpers/src/_class_private_field_set.mjs";
import _write_only_error from "@swc/helpers/src/_write_only_error.mjs";
let C = (_x = new WeakMap(), class {
m() {
_class_private_field_set(this, _x, _class_private_field_get(this, _x) + 2);
_class_private_field_set(this, _x, _write_only_error("#x") + 2);
}
constructor(){
_class_private_field_init(this, _x, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
//// [privateWriteOnlyAccessorRead.ts]
import _class_private_field_get from "@swc/helpers/src/_class_private_field_get.mjs";
import _class_private_field_init from "@swc/helpers/src/_class_private_field_init.mjs";
import _class_private_field_set from "@swc/helpers/src/_class_private_field_set.mjs";
import _extends from "@swc/helpers/src/_extends.mjs";
import _write_only_error from "@swc/helpers/src/_write_only_error.mjs";
import _class_private_field_destructure from "@swc/helpers/src/_class_private_field_destructure.mjs";
var _value = /*#__PURE__*/ new WeakMap(), _valueRest = /*#__PURE__*/ new WeakMap(), _valueOne = /*#__PURE__*/ new WeakMap(), _valueCompound = /*#__PURE__*/ new WeakMap();
class Test {
m() {
const foo = {
bar: 1
};
console.log(_class_private_field_get(this, _value)); // error
console.log((this, _write_only_error("#value"))); // error
_class_private_field_set(this, _value, {
foo
}); // ok
_class_private_field_set(this, _value, {
foo
}); // ok
_class_private_field_get(this, _value).foo = foo; // error
(this, _write_only_error("#value")).foo = foo; // error
({ o: _class_private_field_destructure(this, _value).value } = {
o: {
foo
Expand All @@ -27,26 +27,26 @@ class Test {
_tmp = {
foo
}, _class_private_field_destructure(this, _value).value = _extends({}, _tmp), _tmp; //ok
({ foo: _class_private_field_get(this, _value).foo } = {
({ foo: (this, _write_only_error("#value")).foo } = {
foo
}); //error
var _tmp1;
_tmp1 = {
foo
}, _class_private_field_get(this, _value).foo = _extends({}, _tmp1.foo), ({ foo: {} } = _tmp1), _tmp1; //error
}, (this, _write_only_error("#value")).foo = _extends({}, _tmp1.foo), ({ foo: {} } = _tmp1), _tmp1; //error
let r = {
o: _class_private_field_get(this, _value)
o: (this, _write_only_error("#value"))
}; //error
[_class_private_field_destructure(this, _valueOne).value, ..._class_private_field_destructure(this, _valueRest).value] = [
1,
2,
3
];
let arr = [
_class_private_field_get(this, _valueOne),
..._class_private_field_get(this, _valueRest)
(this, _write_only_error("#valueOne")),
...(this, _write_only_error("#valueRest"))
];
_class_private_field_set(this, _valueCompound, _class_private_field_get(this, _valueCompound) + 3);
_class_private_field_set(this, _valueCompound, (this, _write_only_error("#valueCompound")) + 3);
}
constructor(){
_class_private_field_init(this, _value, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//// [privateWriteOnlyAccessorRead.ts]
import _class_private_field_get from "@swc/helpers/src/_class_private_field_get.mjs";
import _class_private_field_init from "@swc/helpers/src/_class_private_field_init.mjs";
import _class_private_field_set from "@swc/helpers/src/_class_private_field_set.mjs";
import _extends from "@swc/helpers/src/_extends.mjs";
import _write_only_error from "@swc/helpers/src/_write_only_error.mjs";
import _class_private_field_destructure from "@swc/helpers/src/_class_private_field_destructure.mjs";
var _value = new WeakMap(), _valueRest = new WeakMap(), _valueOne = new WeakMap(), _valueCompound = new WeakMap();
function set_value(v) {}
Expand All @@ -15,28 +15,28 @@ new class {
let foo = {
bar: 1
};
console.log(_class_private_field_get(this, _value)), _class_private_field_set(this, _value, {
console.log(_write_only_error("#value")), _class_private_field_set(this, _value, {
foo
}), _class_private_field_set(this, _value, {
foo
}), _class_private_field_get(this, _value).foo = foo, ({ o: _class_private_field_destructure(this, _value).value } = {
}), _write_only_error("#value").foo = foo, ({ o: _class_private_field_destructure(this, _value).value } = {
o: {
foo
}
}), _class_private_field_destructure(this, _value).value = _extends({}, {
foo
}), ({ foo: _class_private_field_get(this, _value).foo } = {
}), ({ foo: _write_only_error("#value").foo } = {
foo
}), _tmp = {
foo
}, _class_private_field_get(this, _value).foo = _extends({}, _tmp.foo), _class_private_field_get(this, _value), [_class_private_field_destructure(this, _valueOne).value, ..._class_private_field_destructure(this, _valueRest).value] = [
}, _write_only_error("#value").foo = _extends({}, _tmp.foo), _write_only_error("#value"), [_class_private_field_destructure(this, _valueOne).value, ..._class_private_field_destructure(this, _valueRest).value] = [
1,
2,
3
], [
_class_private_field_get(this, _valueOne),
..._class_private_field_get(this, _valueRest)
], _class_private_field_set(this, _valueCompound, _class_private_field_get(this, _valueCompound) + 3);
_write_only_error("#valueOne"),
..._write_only_error("#valueRest")
], _class_private_field_set(this, _valueCompound, _write_only_error("#valueCompound") + 3);
}
constructor(){
_class_private_field_init(this, _value, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function _classApplyDescriptorUpdate(receiver, descriptor) {
if (descriptor.set) {
if (!descriptor.get) {
throw new TypeError("attempted to read set only private field");
}
if (!("__destrWrapper" in descriptor)) {
descriptor.__destrWrapper = {
set value(v) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function _readOnlyError(name) {
throw new Error("\"" + name + "\" is read-only");
throw new TypeError("\"" + name + "\" is read-only");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function _writeOnlyError(name) {
throw new TypeError("\"" + name + "\" is write-only");
}
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_base/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ define_helpers!(Helpers {
set_prototype_of,
is_native_function
),
write_only_error: (),

class_private_field_destructure: (
class_extract_field_descriptor,
Expand Down