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 IIFE for single-expression class static blocks #14275

Merged
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
15 changes: 10 additions & 5 deletions packages/babel-helper-create-class-features-plugin/src/fields.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
class Foo {
static bar = 42;
static #_ = (() => {
this.foo = Foo.bar;
})();
static #_ = this.foo = Foo.bar;
}

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

expect(Foo.foo).toBe(42);
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
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;
}
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class Base {
constructor() {
this.Foo = class {
static #_ = (() => {
this.foo = new.target;
})();
static #_ = this.foo = new.target;
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
class Foo {}

Foo.bar = 42;

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

Foo.foo = Foo.bar;
expect(Foo.foo).toBe(42);
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
class Foo {}

Foo.bar = 42;

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

Foo.foo = Foo.bar;
expect(Foo.foo).toBe(42);
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,4 @@ Object.defineProperty(Foo, _bar, {
})();

Foo.qux = 21;

(() => {
Foo.qux2 = Foo.qux;
})();
Foo.qux2 = Foo.qux;
Original file line number Diff line number Diff line change
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);
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
class Foo {}

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

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

Foo.foo = Foo.bar;
expect(Foo.foo).toBe(42);
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
class Foo {}

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

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

Foo.foo = Foo.bar;
expect(Foo.foo).toBe(42);
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,4 @@ var _bar = {
})();

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

(() => {
Foo.qux2 = Foo.qux;
})();
Foo.qux2 = Foo.qux;
Original file line number Diff line number Diff line change
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);
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}]], []);
Original file line number Diff line number Diff line change
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]], []);
Original file line number Diff line number Diff line change
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;
}
Original file line number Diff line number Diff line change
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();