Skip to content

Commit

Permalink
refactor: use private static field initializer
Browse files Browse the repository at this point in the history
Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
JLHwung and nicolo-ribaudo committed Oct 6, 2020
1 parent ca389fb commit 8523873
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 116 deletions.
Expand Up @@ -23,6 +23,6 @@
"@babel/plugin-syntax-class-static-block": "workspace:^7.11.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
"@babel/core": "^7.12.0"
}
}
46 changes: 12 additions & 34 deletions packages/babel-plugin-proposal-class-static-block/src/index.js
Expand Up @@ -2,50 +2,28 @@ import { declare } from "@babel/helper-plugin-utils";
import syntaxClassStaticBlock from "@babel/plugin-syntax-class-static-block";

export default declare(({ types: t, template, assertVersion }) => {
assertVersion(7);
// todo remove this check after Babel 7.12.0 is published
if (process.env.NODE_ENV !== "test") {
assertVersion("^7.12.0");
}

return {
name: "proposal-class-static-block",
inherits: syntaxClassStaticBlock,
visitor: {
StaticBlock(path) {
const staticBlockRef = path.scope.generateUidIdentifier("init");
const classPath = path.parentPath.parentPath;
path.replaceWith(
t.classMethod(
"method",
staticBlockRef,
const staticBlockRef = path.scope.generateUidIdentifier("");
const classBody = path.parentPath;
classBody.pushContainer(
"body",
t.classPrivateProperty(
t.privateName(staticBlockRef),
template.expression.ast`(() => { ${path.node.body} })()`,
[],
t.BlockStatement(
// Add completion record. A static block can not contain directly
// `return`, `break`, `continue`. So `return this` must be executed.
path.node.body.concat([
template.ast`delete this.${t.cloneNode(staticBlockRef)};`,
template.ast`return this;`,
]),
),
/* computed */ false,
/* static */ true,
),
);
const classId = classPath.node.id;
if (!classId) {
// If `classId` is not defined, we don't have to preserve the class binding
// `class { }` transformed as `(class { })._init()`
classPath.replaceWith(
template.ast`(${classPath.node}).${t.cloneNode(staticBlockRef)}()`,
);
} else {
// If `classId` is defined, preserve the class binding.
// `class Foo { }` transformed as `(Foo = (class Foo { })._init())`
classPath.node.type = "ClassExpression";
classPath.replaceWith(
template.ast`${t.cloneNode(classId)} = (${
classPath.node
}).${t.cloneNode(staticBlockRef)}()`,
);
classPath.insertBefore(template.ast`let ${t.cloneNode(classId)};`);
}
path.remove();
},
},
};
Expand Down
@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = Foo.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);
@@ -1,6 +1,7 @@
class Foo {
static {
this.foo = 42;
this.foo = this.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);
@@ -1,6 +1,7 @@
class Foo {
static {
this.foo = 42;
this.foo = this.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);
@@ -1,10 +1,8 @@
let Foo;
Foo = class Foo {
static _init() {
this.foo = 42;
delete this._init;
return this;
}
class Foo {
static bar = 42;
static #_ = (() => {
this.foo = this.bar;
})();
}

}._init();
expect(Foo.foo).toBe(42);

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -1,10 +1,14 @@
class Foo extends class {
class Foo extends class extends class Base {
static {
this.qux = 21;
}
} {
static {
this.bar = 21;
}
} {
static {
this.foo = 2 * this.bar;
this.foo = this.bar + this.qux;
}
}
expect(Foo.foo).toBe(42);
@@ -1,10 +1,14 @@
class Foo extends class {
class Foo extends class extends class Base {
static {
this.qux = 21;
}
} {
static {
this.bar = 21;
}
} {
static {
this.foo = 2 * this.bar;
this.foo = this.bar + this.qux;
}
}
expect(Foo.foo).toBe(42);
@@ -1,17 +1,15 @@
let Foo;
Foo = class Foo extends class {
static _init2() {
class Foo extends class extends class Base {
static #_3 = (() => {
this.qux = 21;
})();
} {
static #_2 = (() => {
this.bar = 21;
delete this._init2;
return this;
}
})();
} {
static #_ = (() => {
this.foo = this.bar + this.qux;
})();
}

}._init2() {
static _init() {
this.foo = 2 * this.bar;
delete this._init;
return this;
}

}._init();
expect(Foo.foo).toBe(42);
@@ -0,0 +1,8 @@
class Foo {
static #bar = 21;
static {
this.foo = this.#bar + this.qux;
}
static qux = 21;
}
expect(Foo.foo).toBe(42);
@@ -1,11 +1,8 @@
class Foo extends class {
class Foo {
static #_ = 42;
// static block can not be tranformed as `#_` here
static {
this.bar = 42;
}
} {
static bar = 21;
static {
this.foo = super.bar;
this.foo = this.#_;
}
}
expect(Foo.foo).toBe(42);

This file was deleted.

2 changes: 1 addition & 1 deletion packages/babel-types/src/definitions/experimental.js
Expand Up @@ -108,7 +108,7 @@ defineType("PipelinePrimaryTopicReference", {

defineType("ClassPrivateProperty", {
visitor: ["key", "value", "decorators"],
builder: ["key", "value", "decorators"],
builder: ["key", "value", "decorators", "static"],
aliases: ["Property", "Private"],
fields: {
key: {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -1011,7 +1011,7 @@ __metadata:
"@babel/helper-plugin-utils": "workspace:^7.10.1"
"@babel/plugin-syntax-class-static-block": "workspace:^7.11.0"
peerDependencies:
"@babel/core": ^7.0.0-0
"@babel/core": ^7.12.0
languageName: unknown
linkType: soft

Expand Down

0 comments on commit 8523873

Please sign in to comment.