Skip to content

Commit

Permalink
destrucuring and rest for private properties
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed May 26, 2019
1 parent 3b4a973 commit b374e44
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 106 deletions.
13 changes: 13 additions & 0 deletions packages/babel-helper-create-class-features-plugin/src/fields.js
Expand Up @@ -211,6 +211,19 @@ const privateNameHandlerSpec = {
]);
},

destructureSet(member) {
const { privateNamesMap, file } = this;
const { name } = member.node.property.id;
const { id } = privateNamesMap.get(name);
return t.memberExpression(
t.callExpression(file.addHelper("classPrivateFieldDestructureSet"), [
this.receiver(member),
t.cloneNode(id),
]),
t.identifier("value"),
);
},

call(member, args) {
// The first access (the get) should do the memo assignment.
this.memoise(member, 1);
Expand Down
20 changes: 7 additions & 13 deletions packages/babel-helper-member-expression-to-functions/src/index.js
Expand Up @@ -100,23 +100,17 @@ const handle = {
return;
}

// { KEY: MEMBER } = OBJ -> { KEY: _ref } = OBJ; _set(MEMBER, _ref);
// [MEMBER] = ARR -> [_ref] = ARR; _set(MEMBER, _ref);
// { KEY: MEMBER } = OBJ -> { KEY: _destructureSet(MEMBER) } = OBJ
// [MEMBER] = ARR -> [_destructureSet(MEMBER)] = ARR
// [...MEMBER] -> [..._destructureSet(MEMBER)]
// {...MEMBER} -> {..._destructureSet(MEMBER)}
if (
(parentPath.isObjectProperty({ value: node }) &&
parentPath.parentPath.isObjectPattern()) ||
parentPath.isArrayPattern()
parentPath.isArrayPattern() ||
parentPath.isRestElement()
) {
const { scope } = member;
const ref = scope.generateUidIdentifierBasedOnNode(node);
scope.push({ id: ref });

const assignmentParent = parentPath.getStatementParent();
assignmentParent.insertAfter(
t.expressionStatement(this.set(member, t.cloneNode(ref))),
);
member.replaceWith(t.cloneNode(ref));

member.replaceWith(this.destructureSet(member));
return;
}

Expand Down
28 changes: 28 additions & 0 deletions packages/babel-helpers/src/helpers.js
Expand Up @@ -1104,6 +1104,34 @@ helpers.classPrivateFieldSet = helper("7.0.0-beta.0")`
}
`;

helpers.classPrivateFieldDestructureSet = helper("7.4.4")`
export default function _classPrivateFieldDestructureSet(receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
var descriptor = privateMap.get(receiver);
if (descriptor.set) {
if (!("__destrObj" in descriptor)) {
descriptor.__destrObj = {
set value(v) {
descriptor.set.call(receiver, v)
},
};
}
return descriptor.__destrObj;
} else {
if (!descriptor.writable) {
// This should only throw in strict mode, but class bodies are
// always strict and private fields can only be used inside
// class bodies.
throw new TypeError("attempted to set read only private field");
}
return descriptor;
}
}
`;

helpers.classStaticPrivateFieldSpecGet = helper("7.0.2")`
export default function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) {
if (receiver !== classConstructor) {
Expand Down
@@ -1,30 +1,18 @@
var Foo =
/*#__PURE__*/
function () {
"use strict";

function Foo(props) {
var _this$client;
function _classPrivateFieldDestructureSet(receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } var descriptor = privateMap.get(receiver); if (descriptor.set) { if (!("__destrObj" in descriptor)) { descriptor.__destrObj = { set value(v) { descriptor.set.call(receiver, v); } }; } return descriptor.__destrObj; } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } return descriptor; } }

babelHelpers.classCallCheck(this, Foo);
var Foo = function Foo(props) {
"use strict";

_client.set(this, {
writable: true,
value: void 0
});
babelHelpers.classCallCheck(this, Foo);

;
[_this$client] = props;
babelHelpers.classPrivateFieldSet(this, _client, _this$client);
}
_client.set(this, {
writable: true,
value: void 0
});

babelHelpers.createClass(Foo, [{
key: "getClient",
value: function getClient() {
return babelHelpers.classPrivateFieldGet(this, _client);
}
}]);
return Foo;
}();
babelHelpers.classPrivateFieldSet(this, _client, 1);
;
[this.x = babelHelpers.classPrivateFieldGet(this, _client), _classPrivateFieldDestructureSet(this, _client).value, this.y = babelHelpers.classPrivateFieldGet(this, _client)] = props;
};

var _client = new WeakMap();
Expand Up @@ -2,6 +2,7 @@ class Foo {
#client

constructor(props) {
let x;
;([x, ...this.#client] = props);
}

Expand All @@ -11,4 +12,4 @@ class Foo {
}

const foo = new Foo(['foo', 'bar', 'baz', 'quu']);
expect(foo.getClient()).toBe(['bar', 'baz', 'quu']);
expect(foo.getClient()).toEqual(['bar', 'baz', 'quu']);
@@ -1,30 +1,17 @@
var Foo =
/*#__PURE__*/
function () {
"use strict";

function Foo(props) {
var _this$client;
function _classPrivateFieldDestructureSet(receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } var descriptor = privateMap.get(receiver); if (descriptor.set) { if (!("__destrObj" in descriptor)) { descriptor.__destrObj = { set value(v) { descriptor.set.call(receiver, v); } }; } return descriptor.__destrObj; } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } return descriptor; } }

babelHelpers.classCallCheck(this, Foo);
var Foo = function Foo(props) {
"use strict";

_client.set(this, {
writable: true,
value: void 0
});
babelHelpers.classCallCheck(this, Foo);

;
[_this$client] = props;
babelHelpers.classPrivateFieldSet(this, _client, _this$client);
}
_client.set(this, {
writable: true,
value: void 0
});

babelHelpers.createClass(Foo, [{
key: "getClient",
value: function getClient() {
return babelHelpers.classPrivateFieldGet(this, _client);
}
}]);
return Foo;
}();
;
[x, ..._classPrivateFieldDestructureSet(this, _client).value] = props;
};

var _client = new WeakMap();
@@ -1,30 +1,17 @@
var Foo =
/*#__PURE__*/
function () {
"use strict";

function Foo(props) {
var _this$client;
function _classPrivateFieldDestructureSet(receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } var descriptor = privateMap.get(receiver); if (descriptor.set) { if (!("__destrObj" in descriptor)) { descriptor.__destrObj = { set value(v) { descriptor.set.call(receiver, v); } }; } return descriptor.__destrObj; } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } return descriptor; } }

babelHelpers.classCallCheck(this, Foo);
var Foo = function Foo(props) {
"use strict";

_client.set(this, {
writable: true,
value: void 0
});
babelHelpers.classCallCheck(this, Foo);

;
[_this$client] = props;
babelHelpers.classPrivateFieldSet(this, _client, _this$client);
}
_client.set(this, {
writable: true,
value: void 0
});

babelHelpers.createClass(Foo, [{
key: "getClient",
value: function getClient() {
return babelHelpers.classPrivateFieldGet(this, _client);
}
}]);
return Foo;
}();
;
[_classPrivateFieldDestructureSet(this, _client).value] = props;
};

var _client = new WeakMap();
Expand Up @@ -2,13 +2,16 @@ class Foo {
#client

constructor(props) {
;({ client: this.#client } = props)
this.#client = 'foo';
;({ x: this.x = this.#client, y: this.#client, z: this.z = this.#client } = props)
}

getClient() {
return this.#client;
}
}

const foo = new Foo({ client: 'bar' });
const foo = new Foo({ y: 'bar' });
expect(foo.getClient()).toBe('bar');
expect(foo.x).toBe('foo');
expect(foo.z).toBe('bar');
@@ -1,20 +1,22 @@
function _classPrivateFieldDestructureSet(receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } var descriptor = privateMap.get(receiver); if (descriptor.set) { if (!("__destrObj" in descriptor)) { descriptor.__destrObj = { set value(v) { descriptor.set.call(receiver, v); } }; } return descriptor.__destrObj; } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } return descriptor; } }

var Foo = function Foo(props) {
"use strict";

var _this$client;

babelHelpers.classCallCheck(this, Foo);

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

babelHelpers.classPrivateFieldSet(this, _client, 'foo');
;
({
client: _this$client
x: this.x = babelHelpers.classPrivateFieldGet(this, _client),
y: _classPrivateFieldDestructureSet(this, _client).value,
z: this.z = babelHelpers.classPrivateFieldGet(this, _client)
} = props);
babelHelpers.classPrivateFieldSet(this, _client, _this$client);
};

var _client = new WeakMap();
Expand Up @@ -2,6 +2,7 @@ class Foo {
#client

constructor(props) {
let x;
;({ x, ...this.#client } = props)
}

Expand All @@ -11,4 +12,4 @@ class Foo {
}

const foo = new Foo({ x: 'foo', y: 'bar', z: 'baz' });
expect(foo.getClient()).toBe({ y: 'bar', z: 'baz' });
expect(foo.getClient()).toEqual({ y: 'bar', z: 'baz' });
@@ -1,8 +1,8 @@
function _classPrivateFieldDestructureSet(receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } var descriptor = privateMap.get(receiver); if (descriptor.set) { if (!("__destrObj" in descriptor)) { descriptor.__destrObj = { set value(v) { descriptor.set.call(receiver, v); } }; } return descriptor.__destrObj; } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } return descriptor; } }

var Foo = function Foo(props) {
"use strict";

var _this$client;

babelHelpers.classCallCheck(this, Foo);

_client.set(this, {
Expand All @@ -12,9 +12,9 @@ var Foo = function Foo(props) {

;
({
client: _this$client
x,
..._classPrivateFieldDestructureSet(this, _client).value
} = props);
babelHelpers.classPrivateFieldSet(this, _client, _this$client);
};

var _client = new WeakMap();
Expand Up @@ -2,16 +2,13 @@ class Foo {
#client

constructor(props) {
this.#client = 'foo';
;({ x: this.x = this.#client, y: this.#client, z: this.z = this.#client } = props)
;({ client: this.#client } = props)
}

getClient() {
return this.#client;
}
}

const foo = new Foo({ y: 'bar' });
const foo = new Foo({ client: 'bar' });
expect(foo.getClient()).toBe('bar');
expect(foo.x).toBe('foo');
expect(foo.z).toBe('bar');
@@ -1,8 +1,8 @@
function _classPrivateFieldDestructureSet(receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } var descriptor = privateMap.get(receiver); if (descriptor.set) { if (!("__destrObj" in descriptor)) { descriptor.__destrObj = { set value(v) { descriptor.set.call(receiver, v); } }; } return descriptor.__destrObj; } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } return descriptor; } }

var Foo = function Foo(props) {
"use strict";

var _this$client;

babelHelpers.classCallCheck(this, Foo);

_client.set(this, {
Expand All @@ -12,9 +12,8 @@ var Foo = function Foo(props) {

;
({
client: _this$client
client: _classPrivateFieldDestructureSet(this, _client).value
} = props);
babelHelpers.classPrivateFieldSet(this, _client, _this$client);
};

var _client = new WeakMap();

0 comments on commit b374e44

Please sign in to comment.