Skip to content

Commit 1937284

Browse files
Avoid IIFE for single-expression class static blocks (#14275)
1 parent 8b69dae commit 1937284

50 files changed

Lines changed: 122 additions & 286 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/babel-helper-create-class-features-plugin/src/fields.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -993,12 +993,17 @@ export function buildFieldsInitNodes(
993993
// a `NodePath<t.StaticBlock>`
994994
// this maybe a bug for ts
995995
switch (true) {
996-
case isStaticBlock:
997-
staticNodes.push(
998-
// @ts-expect-error prop is `StaticBlock` here
999-
template.statement.ast`(() => ${t.blockStatement(prop.node.body)})()`,
1000-
);
996+
case isStaticBlock: {
997+
const blockBody = (prop.node as t.StaticBlock).body;
998+
// We special-case the single expression case to avoid the iife, since
999+
// it's common.
1000+
if (blockBody.length === 1 && t.isExpressionStatement(blockBody[0])) {
1001+
staticNodes.push(blockBody[0] as t.ExpressionStatement);
1002+
} else {
1003+
staticNodes.push(template.statement.ast`(() => { ${blockBody} })()`);
1004+
}
10011005
break;
1006+
}
10021007
case isStatic && isPrivate && isField && privateFieldsAsProperties:
10031008
needsClassRef = true;
10041009
staticNodes.push(

packages/babel-plugin-proposal-class-static-block/src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,21 @@ export default declare(({ types: t, template, assertVersion }) => {
5959
const staticBlockRef = t.privateName(
6060
t.identifier(staticBlockPrivateId),
6161
);
62+
63+
let replacement;
64+
const blockBody = path.node.body;
65+
// We special-case the single expression case to avoid the iife, since
66+
// it's common.
67+
if (blockBody.length === 1 && t.isExpressionStatement(blockBody[0])) {
68+
replacement = (blockBody[0] as t.ExpressionStatement).expression;
69+
} else {
70+
replacement = template.expression.ast`(() => { ${blockBody} })()`;
71+
}
72+
6273
path.replaceWith(
6374
t.classPrivateProperty(
6475
staticBlockRef,
65-
template.expression.ast`(() => { ${
66-
(path.node as t.StaticBlock).body
67-
} })()`,
76+
replacement,
6877
[],
6978
/* static */ true,
7079
),
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
class Foo {
22
static bar = 42;
3-
static #_ = (() => {
4-
this.foo = Foo.bar;
5-
})();
3+
static #_ = this.foo = Foo.bar;
64
}
75

86
expect(Foo.foo).toBe(42);
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
class Foo {
22
static bar = 42;
3-
static #_ = (() => {
4-
this.foo = this.bar;
5-
})();
3+
static #_ = this.foo = this.bar;
64
}
75

86
expect(Foo.foo).toBe(42);
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
class Foo extends class extends class Base {
2-
static #_ = (() => {
3-
this.qux = 21;
4-
})();
2+
static #_ = this.qux = 21;
53
} {
6-
static #_ = (() => {
7-
this.bar = 21;
8-
})();
4+
static #_ = this.bar = 21;
95
} {
10-
static #_ = (() => {
11-
this.foo = this.bar + this.qux;
12-
})();
6+
static #_ = this.foo = this.bar + this.qux;
137
}
148

159
expect(Foo.foo).toBe(42);

packages/babel-plugin-proposal-class-static-block/test/fixtures/class-static-block/multiple-static-initializers/output.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ class Foo {
55
this.qux1 = this.qux;
66
})();
77
static qux = 21;
8-
static #_2 = (() => {
9-
this.qux2 = this.qux;
10-
})();
8+
static #_2 = this.qux2 = this.qux;
119
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class Foo {
22
static #_ = 42; // static block can not be tranformed as `#_` here
33

4-
static #_2 = (() => {
5-
this.foo = this.#_;
6-
})();
4+
static #_2 = this.foo = this.#_;
75
}
86

97
expect(Foo.foo).toBe(42);

packages/babel-plugin-proposal-class-static-block/test/fixtures/class-static-block/new-target/output.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class Base {
22
constructor() {
33
this.Foo = class {
4-
static #_ = (() => {
5-
this.foo = new.target;
6-
})();
4+
static #_ = this.foo = new.target;
75
};
86
}
97

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
class Foo {}
22

33
Foo.bar = 42;
4-
5-
(() => {
6-
Foo.foo = Foo.bar;
7-
})();
8-
4+
Foo.foo = Foo.bar;
95
expect(Foo.foo).toBe(42);
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
class Foo {}
22

33
Foo.bar = 42;
4-
5-
(() => {
6-
Foo.foo = Foo.bar;
7-
})();
8-
4+
Foo.foo = Foo.bar;
95
expect(Foo.foo).toBe(42);

0 commit comments

Comments
 (0)