Skip to content

Commit

Permalink
Avoid IIFE for single-expression class static blocks (#14275)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 18, 2022
1 parent 8b69dae commit 1937284
Show file tree
Hide file tree
Showing 50 changed files with 122 additions and 286 deletions.
15 changes: 10 additions & 5 deletions packages/babel-helper-create-class-features-plugin/src/fields.ts
Expand Up @@ -993,12 +993,17 @@ export function buildFieldsInitNodes(
// a `NodePath<t.StaticBlock>`
// this maybe a bug for ts
switch (true) {
case isStaticBlock:
staticNodes.push(
// @ts-expect-error prop is `StaticBlock` here
template.statement.ast`(() => ${t.blockStatement(prop.node.body)})()`,
);
case isStaticBlock: {
const blockBody = (prop.node as t.StaticBlock).body;
// We special-case the single expression case to avoid the iife, since
// it's common.
if (blockBody.length === 1 && t.isExpressionStatement(blockBody[0])) {
staticNodes.push(blockBody[0] as t.ExpressionStatement);
} else {
staticNodes.push(template.statement.ast`(() => { ${blockBody} })()`);
}
break;
}
case isStatic && isPrivate && isField && privateFieldsAsProperties:
needsClassRef = true;
staticNodes.push(
Expand Down
15 changes: 12 additions & 3 deletions packages/babel-plugin-proposal-class-static-block/src/index.ts
Expand Up @@ -59,12 +59,21 @@ export default declare(({ types: t, template, assertVersion }) => {
const staticBlockRef = t.privateName(
t.identifier(staticBlockPrivateId),
);

let replacement;
const blockBody = path.node.body;
// We special-case the single expression case to avoid the iife, since
// it's common.
if (blockBody.length === 1 && t.isExpressionStatement(blockBody[0])) {
replacement = (blockBody[0] as t.ExpressionStatement).expression;
} else {
replacement = template.expression.ast`(() => { ${blockBody} })()`;
}

path.replaceWith(
t.classPrivateProperty(
staticBlockRef,
template.expression.ast`(() => { ${
(path.node as t.StaticBlock).body
} })()`,
replacement,
[],
/* static */ true,
),
Expand Down
@@ -1,8 +1,6 @@
class Foo {
static bar = 42;
static #_ = (() => {
this.foo = Foo.bar;
})();
static #_ = this.foo = Foo.bar;
}

expect(Foo.foo).toBe(42);
@@ -1,8 +1,6 @@
class Foo {
static bar = 42;
static #_ = (() => {
this.foo = this.bar;
})();
static #_ = this.foo = this.bar;
}

expect(Foo.foo).toBe(42);
@@ -1,15 +1,9 @@
class Foo extends class extends class Base {
static #_ = (() => {
this.qux = 21;
})();
static #_ = this.qux = 21;
} {
static #_ = (() => {
this.bar = 21;
})();
static #_ = this.bar = 21;
} {
static #_ = (() => {
this.foo = this.bar + this.qux;
})();
static #_ = this.foo = this.bar + this.qux;
}

expect(Foo.foo).toBe(42);
Expand Up @@ -5,7 +5,5 @@ class Foo {
this.qux1 = this.qux;
})();
static qux = 21;
static #_2 = (() => {
this.qux2 = this.qux;
})();
static #_2 = this.qux2 = this.qux;
}
@@ -1,9 +1,7 @@
class Foo {
static #_ = 42; // static block can not be tranformed as `#_` here

static #_2 = (() => {
this.foo = this.#_;
})();
static #_2 = this.foo = this.#_;
}

expect(Foo.foo).toBe(42);
@@ -1,9 +1,7 @@
class Base {
constructor() {
this.Foo = class {
static #_ = (() => {
this.foo = new.target;
})();
static #_ = this.foo = new.target;
};
}

Expand Down
@@ -1,9 +1,5 @@
class Foo {}

Foo.bar = 42;

(() => {
Foo.foo = Foo.bar;
})();

Foo.foo = Foo.bar;
expect(Foo.foo).toBe(42);
@@ -1,9 +1,5 @@
class Foo {}

Foo.bar = 42;

(() => {
Foo.foo = Foo.bar;
})();

Foo.foo = Foo.bar;
expect(Foo.foo).toBe(42);
@@ -1,13 +1,6 @@
var _class, _class2;

class Foo extends (_class = class extends (_class2 = class Base {}, (() => {
_class2.qux = 21;
})(), _class2) {}, (() => {
_class.bar = 21;
})(), _class) {}

(() => {
Foo.foo = Foo.bar + Foo.qux;
})();
class Foo extends (_class = class extends (_class2 = class Base {}, _class2.qux = 21, _class2) {}, _class.bar = 21, _class) {}

Foo.foo = Foo.bar + Foo.qux;
expect(Foo.foo).toBe(42);
Expand Up @@ -13,7 +13,4 @@ Object.defineProperty(Foo, _bar, {
})();

Foo.qux = 21;

(() => {
Foo.qux2 = Foo.qux;
})();
Foo.qux2 = Foo.qux;
Expand Up @@ -6,9 +6,5 @@ Object.defineProperty(Foo, _, {
writable: true,
value: 42
});

(() => {
Foo.foo = babelHelpers.classPrivateFieldLooseBase(Foo, _)[_];
})();

Foo.foo = babelHelpers.classPrivateFieldLooseBase(Foo, _)[_];
expect(Foo.foo).toBe(42);
@@ -1,13 +1,7 @@
var _ref, _class;

class Foo extends (_ref = (_class = class _ref {}, (() => {
_class.bar = 42;
})(), _class)) {}
class Foo extends (_ref = (_class = class _ref {}, _class.bar = 42, _class)) {}

Foo.bar = 21;

(() => {
Foo.foo = _ref.bar;
})();

Foo.foo = _ref.bar;
expect(Foo.foo).toBe(42);
@@ -1,9 +1,5 @@
class Foo {}

babelHelpers.defineProperty(Foo, "bar", 42);

(() => {
Foo.foo = Foo.bar;
})();

Foo.foo = Foo.bar;
expect(Foo.foo).toBe(42);
@@ -1,9 +1,5 @@
class Foo {}

babelHelpers.defineProperty(Foo, "bar", 42);

(() => {
Foo.foo = Foo.bar;
})();

Foo.foo = Foo.bar;
expect(Foo.foo).toBe(42);
@@ -1,13 +1,6 @@
var _class, _class2;

class Foo extends (_class = class extends (_class2 = class Base {}, (() => {
_class2.qux = 21;
})(), _class2) {}, (() => {
_class.bar = 21;
})(), _class) {}

(() => {
Foo.foo = Foo.bar + Foo.qux;
})();
class Foo extends (_class = class extends (_class2 = class Base {}, _class2.qux = 21, _class2) {}, _class.bar = 21, _class) {}

Foo.foo = Foo.bar + Foo.qux;
expect(Foo.foo).toBe(42);
Expand Up @@ -11,7 +11,4 @@ var _bar = {
})();

babelHelpers.defineProperty(Foo, "qux", 21);

(() => {
Foo.qux2 = Foo.qux;
})();
Foo.qux2 = Foo.qux;
Expand Up @@ -4,9 +4,5 @@ var _ = {
writable: true,
value: 42
};

(() => {
Foo.foo = babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _);
})();

Foo.foo = babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _);
expect(Foo.foo).toBe(42);
Expand Up @@ -2,9 +2,7 @@ class Base {
constructor() {
var _class;

this.Foo = (_class = class {}, (() => {
_class.foo = void 0;
})(), _class);
this.Foo = (_class = class {}, _class.foo = void 0, _class);
}

}
Expand Down
Expand Up @@ -26,9 +26,7 @@ describe("plugin ordering", () => {
class Foo {}
(() => {
Foo.foo = Foo.bar;
})();
Foo.foo = Foo.bar;
_defineProperty(Foo, \\"bar\\", 42);"
`);
Expand Down
Expand Up @@ -48,14 +48,12 @@ function _get_b2() {
_get_b(this);
}

(() => {
[_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 1, "a", function () {
return babelHelpers.classPrivateFieldGet(this, _A);
}, function (value) {
babelHelpers.classPrivateFieldSet(this, _A, value);
}], [dec, 1, "b", function () {
return babelHelpers.classPrivateFieldGet(this, _B);
}, function (value) {
babelHelpers.classPrivateFieldSet(this, _B, value);
}]], []);
})();
[_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 1, "a", function () {
return babelHelpers.classPrivateFieldGet(this, _A);
}, function (value) {
babelHelpers.classPrivateFieldSet(this, _A, value);
}], [dec, 1, "b", function () {
return babelHelpers.classPrivateFieldGet(this, _B);
}, function (value) {
babelHelpers.classPrivateFieldSet(this, _B, value);
}]], []);
Expand Up @@ -52,6 +52,4 @@ class Foo {

}

(() => {
[_init_a, _init_b, _init_computedKey, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 1, "a"], [dec, 1, "b"], [dec, 1, _computedKey]], []);
})();
[_init_a, _init_b, _init_computedKey, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 1, "a"], [dec, 1, "b"], [dec, 1, _computedKey]], []);
Expand Up @@ -2,52 +2,16 @@ var _initClass, _A, _class, _initClass2, _C, _class2, _initClass3, _D, _class3,

const dec = () => {};

const A = ((_class = class A {}, (() => {
[_A, _initClass] = babelHelpers.applyDecs(_class, [], [dec]);
})(), (() => {
_initClass();
})()), _A);
const B = ((_class2 = class C {}, (() => {
[_C, _initClass2] = babelHelpers.applyDecs(_class2, [], [dec]);
})(), (() => {
_initClass2();
})()), _C);
const D = ((_class3 = class D {}, (() => {
[_D, _initClass3] = babelHelpers.applyDecs(_class3, [], [dec]);
})(), (() => {
_initClass3();
})()), _D);
const E = (((_class4 = class {}, (() => {
[_decorated_class, _initClass4] = babelHelpers.applyDecs(_class4, [], [dec]);
})(), (() => {
_initClass4();
})()), _decorated_class), 123);
const F = [((_class5 = class G {}, (() => {
[_G, _initClass5] = babelHelpers.applyDecs(_class5, [], [dec]);
})(), (() => {
_initClass5();
})()), _G), ((_class6 = class {}, (() => {
[_decorated_class2, _initClass6] = babelHelpers.applyDecs(_class6, [], [dec]);
})(), (() => {
_initClass6();
})()), _decorated_class2)];
const H = ((_class7 = class H extends I {}, (() => {
[_H, _initClass7] = babelHelpers.applyDecs(_class7, [], [dec]);
})(), (() => {
_initClass7();
})()), _H);
const J = ((_class8 = class K extends L {}, (() => {
[_K, _initClass8] = babelHelpers.applyDecs(_class8, [], [dec]);
})(), (() => {
_initClass8();
})()), _K);
const A = ((_class = class A {}, [_A, _initClass] = babelHelpers.applyDecs(_class, [], [dec]), _initClass()), _A);
const B = ((_class2 = class C {}, [_C, _initClass2] = babelHelpers.applyDecs(_class2, [], [dec]), _initClass2()), _C);
const D = ((_class3 = class D {}, [_D, _initClass3] = babelHelpers.applyDecs(_class3, [], [dec]), _initClass3()), _D);
const E = (((_class4 = class {}, [_decorated_class, _initClass4] = babelHelpers.applyDecs(_class4, [], [dec]), _initClass4()), _decorated_class), 123);
const F = [((_class5 = class G {}, [_G, _initClass5] = babelHelpers.applyDecs(_class5, [], [dec]), _initClass5()), _G), ((_class6 = class {}, [_decorated_class2, _initClass6] = babelHelpers.applyDecs(_class6, [], [dec]), _initClass6()), _decorated_class2)];
const H = ((_class7 = class H extends I {}, [_H, _initClass7] = babelHelpers.applyDecs(_class7, [], [dec]), _initClass7()), _H);
const J = ((_class8 = class K extends L {}, [_K, _initClass8] = babelHelpers.applyDecs(_class8, [], [dec]), _initClass8()), _K);

function classFactory() {
var _initClass9, _decorated_class3, _class9;

return (_class9 = class {}, (() => {
[_decorated_class3, _initClass9] = babelHelpers.applyDecs(_class9, [], [dec]);
})(), (() => {
_initClass9();
})()), _decorated_class3;
return (_class9 = class {}, [_decorated_class3, _initClass9] = babelHelpers.applyDecs(_class9, [], [dec]), _initClass9()), _decorated_class3;
}
Expand Up @@ -8,22 +8,14 @@ let _Bar;

class Bar {}

(() => {
[_Bar, _initClass] = babelHelpers.applyDecs(Bar, [], [dec1]);
})();
[_Bar, _initClass] = babelHelpers.applyDecs(Bar, [], [dec1]);

(() => {
_initClass();
})();
_initClass();

let _Foo;

class Foo extends _Bar {}

(() => {
[_Foo, _initClass2] = babelHelpers.applyDecs(Foo, [], [dec2]);
})();
[_Foo, _initClass2] = babelHelpers.applyDecs(Foo, [], [dec2]);

(() => {
_initClass2();
})();
_initClass2();

0 comments on commit 1937284

Please sign in to comment.