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

Avoid intermediate functions for private accessors with decs #16306

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
54 changes: 50 additions & 4 deletions packages/babel-helper-create-class-features-plugin/src/fields.ts
Expand Up @@ -81,9 +81,47 @@ export function buildPrivateNamesMap(
}
if (prop.isClassPrivateMethod()) {
if (prop.node.kind === "get") {
update.getId = prop.scope.generateUidIdentifier(`get_${name}`);
const { body } = prop.node.body;
let $: t.Node;
if (
// If we have
// get #foo() { return _some_fn(this); }
// we can use _some_fn directly.
body.length === 1 &&
t.isReturnStatement(($ = body[0])) &&
t.isCallExpression(($ = $.argument)) &&
$.arguments.length === 1 &&
t.isThisExpression($.arguments[0]) &&
t.isIdentifier(($ = $.callee))
) {
update.getId = t.cloneNode($);
update.getterDeclared = true;
} else {
update.getId = prop.scope.generateUidIdentifier(`get_${name}`);
}
} else if (prop.node.kind === "set") {
update.setId = prop.scope.generateUidIdentifier(`set_${name}`);
const { params } = prop.node;
const { body } = prop.node.body;
let $: t.Node;
if (
// If we have
// set #foo(val) { _some_fn(this, val); }
// we can use _some_fn directly.
body.length === 1 &&
t.isExpressionStatement(($ = body[0])) &&
t.isCallExpression(($ = $.expression)) &&
$.arguments.length === 2 &&
t.isThisExpression($.arguments[0]) &&
t.isIdentifier($.arguments[1], {
name: (params[0] as t.Identifier).name,
}) &&
t.isIdentifier(($ = $.callee))
) {
update.setId = t.cloneNode($);
update.setterDeclared = true;
} else {
update.setId = prop.scope.generateUidIdentifier(`set_${name}`);
}
} else if (prop.node.kind === "method") {
update.methodId = prop.scope.generateUidIdentifier(name);
}
Expand Down Expand Up @@ -1258,8 +1296,16 @@ function buildPrivateMethodDeclaration(
static: isStatic,
} = privateName;
const { params, body, generator, async } = prop.node;
const isGetter = getId && !getterDeclared && params.length === 0;
const isSetter = setId && !setterDeclared && params.length > 0;
const isGetter = getId && params.length === 0;
const isSetter = setId && params.length > 0;

if ((isGetter && getterDeclared) || (isSetter && setterDeclared)) {
privateNamesMap.set(prop.node.key.id.name, {
...privateName,
initAdded: true,
});
return null;
}

if (
(process.env.BABEL_8_BREAKING || newHelpers(file)) &&
Expand Down
Expand Up @@ -61,12 +61,6 @@ class Foo {
}
}
_Foo = Foo;
function _set_a2(_this, v) {
_set_a(_this, v);
}
function _get_a2(_this2) {
return _get_a(_this2);
}
(() => {
[_init_a, _init_a2, _get_a, _set_a, _init_computedKey, _init_computedKey2, _init_computedKey3, _init_computedKey4, _init_computedKey5, _init_computedKey6, _init_computedKey7, _initStatic] = babelHelpers.applyDecs(_Foo, [[dec, 6, "a"], [dec, 6, "a", function () {
return babelHelpers.assertClassBrand(_Foo, this, _B)._;
Expand Down
Expand Up @@ -11,18 +11,6 @@ class Foo {
}
}
_Foo = Foo;
function _set_a2(_this, v) {
_set_a(_this, v);
}
function _get_a2(_this2) {
return _get_a(_this2);
}
function _set_b2(_this3, v) {
_set_b(_this3, v);
}
function _get_b2(_this4) {
return _get_b(_this4);
}
[_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto] = babelHelpers.applyDecs(_Foo, [[dec, 1, "a", function () {
return babelHelpers.classPrivateFieldGet2(_A, this);
}, function (value) {
Expand Down
Expand Up @@ -2,18 +2,6 @@ var _initStatic, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _Foo;
const dec = () => {};
class Foo {}
_Foo = Foo;
function _set_a2(_this, v) {
_set_a(_this, v);
}
function _get_a2(_this2) {
return _get_a(_this2);
}
function _set_b2(_this3, v) {
_set_b(_this3, v);
}
function _get_b2(_this4) {
return _get_b(_this4);
}
(() => {
[_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initStatic] = babelHelpers.applyDecs(_Foo, [[dec, 6, "a", function () {
return babelHelpers.assertClassBrand(_Foo, this, _A)._;
Expand Down
Expand Up @@ -21,9 +21,6 @@ class Foo {
static get [_computedKey]() {}
}
_Foo = Foo;
function _get_a(_this) {
return _call_a(_this);
}
(() => {
[_call_a, _initStatic] = babelHelpers.applyDecs(_Foo, [[dec, 8, "a"], [dec, 8, "a", function () {}], [dec, 8, "b"], [dec, 8, "c"], [dec, 8, 0], [dec, 8, 1], [dec, 8, 2n], [dec, 8, 3n], [dec, 8, _computedKey]], []);
_initStatic(_Foo);
Expand Down
Expand Up @@ -7,13 +7,10 @@ class Foo {
babelHelpers.defineProperty(this, "value", (_initProto(this), 1));
}
getA() {
return babelHelpers.classPrivateGetter(_Foo_brand, this, _get_a);
return babelHelpers.classPrivateGetter(_Foo_brand, this, _call_a);
}
}
_Foo = Foo;
function _get_a(_this) {
return _call_a(_this);
}
[_call_a, _initProto] = babelHelpers.applyDecs(_Foo, [[dec, 3, "a", function () {
return this.value;
}]], []);
Expand Up @@ -2,13 +2,10 @@ var _initStatic, _call_a, _Foo;
const dec = () => {};
class Foo {
static getA() {
return babelHelpers.classPrivateGetter(Foo, this, _get_a);
return babelHelpers.classPrivateGetter(Foo, this, _call_a);
}
}
_Foo = Foo;
function _get_a(_this) {
return _call_a(_this);
}
(() => {
[_call_a, _initStatic] = babelHelpers.applyDecs(_Foo, [[dec, 8, "a", function () {
return this.value;
Expand Down
Expand Up @@ -7,19 +7,13 @@ class Foo {
babelHelpers.defineProperty(this, "value", (_initProto(this), 1));
}
getA() {
return babelHelpers.classPrivateGetter(_Foo_brand, this, _get_a);
return babelHelpers.classPrivateGetter(_Foo_brand, this, _call_a);
}
setA(v) {
babelHelpers.classPrivateSetter(_Foo_brand, _set_a, this, v);
babelHelpers.classPrivateSetter(_Foo_brand, _call_a2, this, v);
}
}
_Foo = Foo;
function _get_a(_this) {
return _call_a(_this);
}
function _set_a(_this2, v) {
_call_a2(_this2, v);
}
[_call_a, _call_a2, _initProto] = babelHelpers.applyDecs(_Foo, [[dec, 3, "a", function () {
return this.value;
}], [dec, 4, "a", function (v) {
Expand Down
Expand Up @@ -2,19 +2,13 @@ var _initStatic, _call_a, _call_a2, _Foo;
const dec = () => {};
class Foo {
static getA() {
return babelHelpers.classPrivateGetter(Foo, this, _get_a);
return babelHelpers.classPrivateGetter(Foo, this, _call_a);
}
static setA(v) {
babelHelpers.classPrivateSetter(Foo, _set_a, this, v);
babelHelpers.classPrivateSetter(Foo, _call_a2, this, v);
}
}
_Foo = Foo;
function _get_a(_this) {
return _call_a(_this);
}
function _set_a(_this2, v) {
_call_a2(_this2, v);
}
(() => {
[_call_a, _call_a2, _initStatic] = babelHelpers.applyDecs(_Foo, [[dec, 8, "a", function () {
return this.value;
Expand Down
Expand Up @@ -21,9 +21,6 @@ class Foo {
static set [_computedKey](v) {}
}
_Foo = Foo;
function _set_a(_this, v) {
_call_a(_this, v);
}
(() => {
[_call_a, _initStatic] = babelHelpers.applyDecs(_Foo, [[dec, 9, "a"], [dec, 9, "a", function (v) {}], [dec, 9, "b"], [dec, 9, "c"], [dec, 9, 0], [dec, 9, 1], [dec, 9, 2n], [dec, 9, 3n], [dec, 9, _computedKey]], []);
_initStatic(_Foo);
Expand Down
Expand Up @@ -7,13 +7,10 @@ class Foo {
babelHelpers.defineProperty(this, "value", (_initProto(this), 1));
}
setA(v) {
babelHelpers.classPrivateSetter(_Foo_brand, _set_a, this, v);
babelHelpers.classPrivateSetter(_Foo_brand, _call_a, this, v);
}
}
_Foo = Foo;
function _set_a(_this, v) {
_call_a(_this, v);
}
[_call_a, _initProto] = babelHelpers.applyDecs(_Foo, [[dec, 4, "a", function (v) {
return this.value = v;
}]], []);
Expand Up @@ -2,13 +2,10 @@ var _initStatic, _call_a, _Foo;
const dec = () => {};
class Foo {
static setA(v) {
babelHelpers.classPrivateSetter(Foo, _set_a, this, v);
babelHelpers.classPrivateSetter(Foo, _call_a, this, v);
}
}
_Foo = Foo;
function _set_a(_this, v) {
_call_a(_this, v);
}
(() => {
[_call_a, _initStatic] = babelHelpers.applyDecs(_Foo, [[dec, 9, "a", function (v) {
return this.value = v;
Expand Down
Expand Up @@ -61,12 +61,6 @@ class Foo {
}
}
_Foo = Foo;
function _set_a2(_this, v) {
_set_a(_this, v);
}
function _get_a2(_this2) {
return _get_a(_this2);
}
(() => {
[_init_a, _init_a2, _get_a, _set_a, _init_computedKey, _init_computedKey2, _init_computedKey3, _init_computedKey4, _init_computedKey5, _init_computedKey6, _init_computedKey7, _initStatic] = babelHelpers.applyDecs2203R(_Foo, [[dec, 6, "a"], [dec, 6, "a", function () {
return babelHelpers.assertClassBrand(_Foo, this, _B)._;
Expand Down
Expand Up @@ -11,18 +11,6 @@ class Foo {
}
}
_Foo = Foo;
function _set_a2(_this, v) {
_set_a(_this, v);
}
function _get_a2(_this2) {
return _get_a(_this2);
}
function _set_b2(_this3, v) {
_set_b(_this3, v);
}
function _get_b2(_this4) {
return _get_b(_this4);
}
[_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto] = babelHelpers.applyDecs2203R(_Foo, [[dec, 1, "a", function () {
return babelHelpers.classPrivateFieldGet2(_A, this);
}, function (value) {
Expand Down
Expand Up @@ -2,18 +2,6 @@ var _initStatic, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _Foo;
const dec = () => {};
class Foo {}
_Foo = Foo;
function _set_a2(_this, v) {
_set_a(_this, v);
}
function _get_a2(_this2) {
return _get_a(_this2);
}
function _set_b2(_this3, v) {
_set_b(_this3, v);
}
function _get_b2(_this4) {
return _get_b(_this4);
}
(() => {
[_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initStatic] = babelHelpers.applyDecs2203R(_Foo, [[dec, 6, "a", function () {
return babelHelpers.assertClassBrand(_Foo, this, _A)._;
Expand Down
Expand Up @@ -21,9 +21,6 @@ class Foo {
static get [_computedKey]() {}
}
_Foo = Foo;
function _get_a(_this) {
return _call_a(_this);
}
(() => {
[_call_a, _initStatic] = babelHelpers.applyDecs2203R(_Foo, [[dec, 8, "a"], [dec, 8, "a", function () {}], [dec, 8, "b"], [dec, 8, "c"], [dec, 8, 0], [dec, 8, 1], [dec, 8, 2n], [dec, 8, 3n], [dec, 8, _computedKey]], []).e;
_initStatic(_Foo);
Expand Down
Expand Up @@ -7,13 +7,10 @@ class Foo {
babelHelpers.defineProperty(this, "value", (_initProto(this), 1));
}
getA() {
return babelHelpers.classPrivateGetter(_Foo_brand, this, _get_a);
return babelHelpers.classPrivateGetter(_Foo_brand, this, _call_a);
}
}
_Foo = Foo;
function _get_a(_this) {
return _call_a(_this);
}
[_call_a, _initProto] = babelHelpers.applyDecs2203R(_Foo, [[dec, 3, "a", function () {
return this.value;
}]], []).e;
Expand Up @@ -2,13 +2,10 @@ var _initStatic, _call_a, _Foo;
const dec = () => {};
class Foo {
static getA() {
return babelHelpers.classPrivateGetter(Foo, this, _get_a);
return babelHelpers.classPrivateGetter(Foo, this, _call_a);
}
}
_Foo = Foo;
function _get_a(_this) {
return _call_a(_this);
}
(() => {
[_call_a, _initStatic] = babelHelpers.applyDecs2203R(_Foo, [[dec, 8, "a", function () {
return this.value;
Expand Down
Expand Up @@ -7,19 +7,13 @@ class Foo {
babelHelpers.defineProperty(this, "value", (_initProto(this), 1));
}
getA() {
return babelHelpers.classPrivateGetter(_Foo_brand, this, _get_a);
return babelHelpers.classPrivateGetter(_Foo_brand, this, _call_a);
}
setA(v) {
babelHelpers.classPrivateSetter(_Foo_brand, _set_a, this, v);
babelHelpers.classPrivateSetter(_Foo_brand, _call_a2, this, v);
}
}
_Foo = Foo;
function _get_a(_this) {
return _call_a(_this);
}
function _set_a(_this2, v) {
_call_a2(_this2, v);
}
[_call_a, _call_a2, _initProto] = babelHelpers.applyDecs2203R(_Foo, [[dec, 3, "a", function () {
return this.value;
}], [dec, 4, "a", function (v) {
Expand Down
Expand Up @@ -2,19 +2,13 @@ var _initStatic, _call_a, _call_a2, _Foo;
const dec = () => {};
class Foo {
static getA() {
return babelHelpers.classPrivateGetter(Foo, this, _get_a);
return babelHelpers.classPrivateGetter(Foo, this, _call_a);
}
static setA(v) {
babelHelpers.classPrivateSetter(Foo, _set_a, this, v);
babelHelpers.classPrivateSetter(Foo, _call_a2, this, v);
}
}
_Foo = Foo;
function _get_a(_this) {
return _call_a(_this);
}
function _set_a(_this2, v) {
_call_a2(_this2, v);
}
(() => {
[_call_a, _call_a2, _initStatic] = babelHelpers.applyDecs2203R(_Foo, [[dec, 8, "a", function () {
return this.value;
Expand Down
Expand Up @@ -21,9 +21,6 @@ class Foo {
static set [_computedKey](v) {}
}
_Foo = Foo;
function _set_a(_this, v) {
_call_a(_this, v);
}
(() => {
[_call_a, _initStatic] = babelHelpers.applyDecs2203R(_Foo, [[dec, 9, "a"], [dec, 9, "a", function (v) {}], [dec, 9, "b"], [dec, 9, "c"], [dec, 9, 0], [dec, 9, 1], [dec, 9, 2n], [dec, 9, 3n], [dec, 9, _computedKey]], []).e;
_initStatic(_Foo);
Expand Down