Skip to content

Commit

Permalink
Smaller output for accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 4, 2020
1 parent d3af56d commit 4602035
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 106 deletions.
41 changes: 32 additions & 9 deletions packages/babel-plugin-proposal-private-methods/src/index.js
Expand Up @@ -257,29 +257,52 @@ export default declare((api, options) => {

if (getter) {
descriptor.properties.push(
t.objectProperty(
t.identifier("get"),
toMemoizedFunction(getter.node, path.scope, `get_${name}`),
t.classMethod(
"get",
t.identifier("_"),
getter.node.params,
getter.node.body,
),
);
getter.remove();
}
if (setter) {
descriptor.properties.push(
t.objectProperty(
t.identifier("set"),
toMemoizedFunction(setter.node, path.scope, `set_${name}`),
t.classMethod(
"set",
t.identifier("_"),
setter.node.params,
setter.node.body,
),
);
setter.remove();
}

let initializer;

if (isStatic) {
descriptor.properties.push(
t.objectProperty(t.identifier("t"), t.thisExpression()),
);
initializer = descriptor;
} else {
const tmpId = path.scope.generateUidIdentifier(name);
path.scope.push({ id: tmpId });

initializer = template.expression.ast`
{
__proto__: ${t.cloneNode(tmpId)} || (
${t.cloneNode(tmpId)} = ${descriptor}
),
t: this
}
`;
}

newElements.push(
t.classPrivateProperty(
t.privateName(t.identifier(name)),
template.expression.ast`
Object.defineProperty({ t: this }, "_", ${descriptor})
`,
initializer,
null,
isStatic,
),
Expand Down
@@ -1,16 +1,19 @@
var _get_privateFieldValue, _set_privateFieldValue;
var _privateFieldValue;

class Cl {
#privateFieldValue = Object.defineProperty({
t: this
}, "_", {
get: _get_privateFieldValue || (_get_privateFieldValue = function () {
return this.t.#privateField;
#privateFieldValue = {
__proto__: _privateFieldValue || (_privateFieldValue = {
get _() {
return this.t.#privateField;
},

set _(newValue) {
this.t.#privateField = newValue;
}

}),
set: _set_privateFieldValue || (_set_privateFieldValue = function (newValue) {
this.t.#privateField = newValue;
})
});
t: this
};
#privateField = "top secret string";

constructor() {
Expand Down
@@ -1,13 +1,15 @@
var _set_privateFieldValue;
var _privateFieldValue;

class Cl {
#privateFieldValue = Object.defineProperty({
#privateFieldValue = {
__proto__: _privateFieldValue || (_privateFieldValue = {
set _(newValue) {
this.t.#privateField = newValue;
}

}),
t: this
}, "_", {
set: _set_privateFieldValue || (_set_privateFieldValue = function (newValue) {
this.t.#privateField = newValue;
})
});
};
#privateField = 0;

constructor() {
Expand Down
@@ -1,12 +1,15 @@
var _get_foo, _set_foo;
var _foo;

class Cl {
#foo = Object.defineProperty({
#foo = {
__proto__: _foo || (_foo = {
get _() {},

set _(x) {}

}),
t: this
}, "_", {
get: _get_foo || (_get_foo = function () {}),
set: _set_foo || (_set_foo = function (x) {})
});
};

test() {
this.#foo._?.bar;
Expand Down
@@ -1,13 +1,15 @@
var _get_privateFieldValue;
var _privateFieldValue;

class Cl {
#privateFieldValue = Object.defineProperty({
#privateFieldValue = {
__proto__: _privateFieldValue || (_privateFieldValue = {
get _() {
return this.t.#privateField;
}

}),
t: this
}, "_", {
get: _get_privateFieldValue || (_get_privateFieldValue = function () {
return this.t.#privateField;
})
});
};
#privateField = 0;

constructor() {
Expand Down
@@ -1,30 +1,39 @@
var _get_foo, _set_foo, _get_bar, _set_bar;
var _foo, _bar;

class A {
#foo = Object.defineProperty({
#foo = {
__proto__: _foo || (_foo = {
get _() {},

set _(x) {}

}),
t: this
}, "_", {
get: _get_foo || (_get_foo = function () {}),
set: _set_foo || (_set_foo = function (x) {})
});
#bar = Object.defineProperty({
};
#bar = {
__proto__: _bar || (_bar = {
get _() {},

set _(x) {}

}),
t: this
}, "_", {
get: _get_bar || (_get_bar = function () {}),
set: _set_bar || (_set_bar = function (x) {})
});
};
#baz;

test() {
var _get_baz, _set_baz;
var _baz;

class B {
#baz = Object.defineProperty({
#baz = {
__proto__: _baz || (_baz = {
get _() {},

set _(x) {}

}),
t: this
}, "_", {
get: _get_baz || (_get_baz = function () {}),
set: _set_baz || (_set_baz = function (x) {})
});
};
#foo;

[this.#foo._]() {
Expand Down
@@ -1,20 +1,23 @@
var _get_foo, _set_foo;
var _foo;

class A {
#foo = Object.defineProperty({
t: this
}, "_", {
get: _get_foo || (_get_foo = function () {
this.t;
#foo = {
__proto__: _foo || (_foo = {
get _() {
this.t;

() => this.t;

() => this.t;
(function () {
this;
}).call(this.t);
this.t.#foo._;
this.t.#foo._ = 2;
},

set _(x) {}

(function () {
this;
}).call(this.t);
this.t.#foo._;
this.t.#foo._ = 2;
}),
set: _set_foo || (_set_foo = function (x) {})
});
t: this
};
}
@@ -1,16 +1,19 @@
var _get_privateFieldValue, _set_privateFieldValue;
var _privateFieldValue;

class Cl {
#privateFieldValue = Object.defineProperty({
t: this
}, "_", {
get: _get_privateFieldValue || (_get_privateFieldValue = function () {
return this.t.#privateField;
#privateFieldValue = {
__proto__: _privateFieldValue || (_privateFieldValue = {
get _() {
return this.t.#privateField;
},

set _(newValue) {
this.t.#privateField = newValue;
}

}),
set: _set_privateFieldValue || (_set_privateFieldValue = function (newValue) {
this.t.#privateField = newValue;
})
});
t: this
};
#privateField = "top secret string";

constructor() {
Expand Down
@@ -1,14 +1,15 @@
class Cl {
static #privateStaticFieldValue = Object.defineProperty({
t: this
}, "_", {
get: function () {
static #privateStaticFieldValue = {
get _() {
return Cl.#PRIVATE_STATIC_FIELD;
},
set: function (newValue) {

set _(newValue) {
Cl.#PRIVATE_STATIC_FIELD = `Updated: ${newValue}`;
}
});
},

t: this
};
static #PRIVATE_STATIC_FIELD = "top secret string";

static getValue() {
Expand Down
@@ -1,11 +1,11 @@
class Cl {
static #privateStaticFieldValue = Object.defineProperty({
t: this
}, "_", {
set: function (newValue) {
static #privateStaticFieldValue = {
set _(newValue) {
Cl.#PRIVATE_STATIC_FIELD = newValue;
}
});
},

t: this
};
static #PRIVATE_STATIC_FIELD = 0;

static getPrivateStaticFieldValue() {
Expand Down
@@ -1,11 +1,11 @@
class Cl {
static #privateStaticFieldValue = Object.defineProperty({
t: this
}, "_", {
get: function () {
static #privateStaticFieldValue = {
get _() {
return Cl.#PRIVATE_STATIC_FIELD;
}
});
},

t: this
};
static #PRIVATE_STATIC_FIELD = 0;

static setPrivateStaticFieldValue() {
Expand Down
@@ -1,14 +1,15 @@
class Cl {
static #privateFieldValue = Object.defineProperty({
t: this
}, "_", {
get: function () {
static #privateFieldValue = {
get _() {
return Cl.#privateField;
},
set: function (newValue) {

set _(newValue) {
Cl.#privateField = newValue;
}
});
},

t: this
};
static #privateField = "top secret string";
static publicField = "not secret string";

Expand Down

0 comments on commit 4602035

Please sign in to comment.