Transform class static block (#12143)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
Huáng Jùnliàng
2020-10-14 13:54:41 -04:00
committed by Nicolò Ribaudo
parent 3ccca88178
commit f697e7995d
85 changed files with 861 additions and 24 deletions

View File

@@ -0,0 +1,3 @@
src
test
*.log

View File

@@ -0,0 +1,19 @@
# @babel/plugin-proposal-class-static-block
> Allow transforming of class static blocks
See our website [@babel/plugin-proposal-class-static-block](https://babeljs.io/docs/en/next/babel-plugin-proposal-class-static-block.html) for more information.
## Install
Using npm:
```sh
npm install --save-dev @babel/plugin-proposal-class-static-block
```
or using yarn:
```sh
yarn add @babel/plugin-proposal-class-static-block --dev
```

View File

@@ -0,0 +1,31 @@
{
"name": "@babel/plugin-proposal-class-static-block",
"version": "7.11.0",
"description": "Allow parsing of class static blocks",
"repository": {
"type": "git",
"url": "https://github.com/babel/babel.git",
"directory": "packages/babel-plugin-proposal-class-static-block"
},
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "./lib/index.js",
"exports": {
".": "./lib/index.js"
},
"keywords": [
"babel-plugin"
],
"dependencies": {
"@babel/helper-plugin-utils": "workspace:^7.10.1",
"@babel/plugin-syntax-class-static-block": "workspace:^7.11.0"
},
"peerDependencies": {
"@babel/core": "^7.12.0"
},
"devDependencies": {
"@babel/core": "workspace:^7.11.6"
}
}

View File

@@ -0,0 +1,61 @@
import { declare } from "@babel/helper-plugin-utils";
import syntaxClassStaticBlock from "@babel/plugin-syntax-class-static-block";
/**
* Generate a uid that is not in `denyList`
*
* @param {*} scope
* @param {Set<string>} a deny list that the generated uid should avoid
* @returns
*/
function generateUid(scope, denyList: Set<string>) {
const name = "";
let uid;
let i = 1;
do {
uid = scope._generateUid(name, i);
i++;
} while (denyList.has(uid));
return uid;
}
export default declare(({ types: t, template, assertVersion }) => {
// todo: change to ^7.12.0 when it is published
assertVersion("^7.11.6");
return {
name: "proposal-class-static-block",
inherits: syntaxClassStaticBlock,
visitor: {
Class(path: NodePath<Class>) {
const { scope } = path;
const classBody = path.get("body");
const privateNames = new Set();
let staticBlockPath;
for (const path of classBody.get("body")) {
if (path.isPrivate()) {
privateNames.add(path.get("key.id").node.name);
} else if (path.isStaticBlock()) {
staticBlockPath = path;
}
}
if (!staticBlockPath) {
return;
}
const staticBlockRef = t.privateName(
t.identifier(generateUid(scope, privateNames)),
);
classBody.pushContainer(
"body",
t.classPrivateProperty(
staticBlockRef,
template.expression.ast`(() => { ${staticBlockPath.node.body} })()`,
[],
/* static */ true,
),
);
staticBlockPath.remove();
},
},
};
});

View File

@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = Foo.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = this.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = this.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);

View File

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

View File

@@ -0,0 +1,6 @@
class Foo {
static {
this.foo = 42;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,14 @@
class Foo extends class extends class Base {
static {
this.qux = 21;
}
} {
static {
this.bar = 21;
}
} {
static {
this.foo = this.bar + this.qux;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,14 @@
class Foo extends class extends class Base {
static {
this.qux = 21;
}
} {
static {
this.bar = 21;
}
} {
static {
this.foo = this.bar + this.qux;
}
}
expect(Foo.foo).toBe(42);

View File

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

View File

@@ -0,0 +1,8 @@
class Foo {
static #bar = 21;
static {
this.foo = this.#bar + this.qux;
}
static qux = 21;
}
expect(Foo.foo).toBe(42);

View File

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

View File

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

View File

@@ -0,0 +1,9 @@
class Foo {
static #_ = 42; // static block can not be tranformed as `#_` here
static #_2 = (() => {
this.foo = this.#_;
})();
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
bar = 21;
static {
this.foo = this.bar;
}
}
expect(Foo.foo).toBe(undefined);

View File

@@ -0,0 +1,4 @@
{
"plugins": ["proposal-class-static-block", "syntax-class-properties"],
"minNodeVersion": "12.0.0"
}

View File

@@ -0,0 +1,8 @@
let getFoo;
class Foo {
static #foo = 42;
static {
getFoo = () => this.#foo;
}
}
expect(getFoo()).toBe(42);

View File

@@ -0,0 +1,9 @@
class Foo extends class {
static bar = 42;
} {
static bar = 21;
static {
this.foo = super.bar;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,11 @@
class Foo extends class {
static {
this.bar = 42;
}
} {
static bar = 21;
static {
this.foo = super.bar;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = Foo.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = Foo.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,12 @@
var _ = babelHelpers.classPrivateFieldLooseKey("_");
class Foo {}
Foo.bar = 42;
Object.defineProperty(Foo, _, {
writable: true,
value: (() => {
Foo.foo = Foo.bar;
})()
});
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = this.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = this.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,12 @@
var _ = babelHelpers.classPrivateFieldLooseKey("_");
class Foo {}
Foo.bar = 42;
Object.defineProperty(Foo, _, {
writable: true,
value: (() => {
Foo.foo = Foo.bar;
})()
});
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,6 @@
class Foo {
static {
this.foo = 42;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,14 @@
class Foo extends class extends class Base {
static {
this.qux = 21;
}
} {
static {
this.bar = 21;
}
} {
static {
this.foo = this.bar + this.qux;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,14 @@
class Foo extends class extends class Base {
static {
this.qux = 21;
}
} {
static {
this.bar = 21;
}
} {
static {
this.foo = this.bar + this.qux;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,23 @@
var _class, _2, _temp, _class2, _3, _temp2;
var _ = babelHelpers.classPrivateFieldLooseKey("_");
class Foo extends (_temp = (_2 = babelHelpers.classPrivateFieldLooseKey("_"), _class = class extends (_temp2 = (_3 = babelHelpers.classPrivateFieldLooseKey("_"), _class2 = class Base {}), Object.defineProperty(_class2, _3, {
writable: true,
value: (() => {
_class2.qux = 21;
})()
}), _temp2) {}), Object.defineProperty(_class, _2, {
writable: true,
value: (() => {
_class.bar = 21;
})()
}), _temp) {}
Object.defineProperty(Foo, _, {
writable: true,
value: (() => {
Foo.foo = Foo.bar + Foo.qux;
})()
});
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,8 @@
class Foo {
static #bar = 21;
static {
this.foo = this.#bar + this.qux;
}
static qux = 21;
}
expect(Foo.foo).toBe(42);

View File

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

View File

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

View File

@@ -0,0 +1,17 @@
var _ = babelHelpers.classPrivateFieldLooseKey("_");
var _2 = babelHelpers.classPrivateFieldLooseKey("_2");
class Foo {}
Object.defineProperty(Foo, _, {
writable: true,
value: 42
});
Object.defineProperty(Foo, _2, {
writable: true,
value: (() => {
Foo.foo = babelHelpers.classPrivateFieldLooseBase(Foo, _)[_];
})()
});
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
bar = 21;
static {
this.foo = this.bar;
}
}
expect(Foo.foo).toBe(undefined);

View File

@@ -0,0 +1,9 @@
{
"plugins": [
"proposal-class-static-block",
["proposal-private-property-in-object", { "loose": true }],
["proposal-private-methods", { "loose": true }],
["proposal-class-properties", { "loose": true }],
"external-helpers"
]
}

View File

@@ -0,0 +1,8 @@
let getFoo;
class Foo {
static #foo = 42;
static {
getFoo = () => this.#foo;
}
}
expect(getFoo()).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static #bar = 21;
static {
this.foo = #bar in this;
}
}
expect(Foo.foo).toBe(true);

View File

@@ -0,0 +1,8 @@
let getFoo;
class Foo {
static #foo() { return 42 };
static {
getFoo = () => this.#foo();
}
}
expect(getFoo()).toBe(42);

View File

@@ -0,0 +1,9 @@
class Foo extends class {
static bar = 42;
} {
static bar = 21;
static {
this.foo = super.bar;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,11 @@
class Foo extends class {
static {
this.bar = 42;
}
} {
static bar = 21;
static {
this.foo = super.bar;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,11 @@
class Foo extends class {
static {
this.bar = 42;
}
} {
static bar = 21;
static {
this.foo = super.bar;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,26 @@
var _class, _2, _temp;
var _ = babelHelpers.classPrivateFieldLooseKey("_");
class Foo extends (_temp = (_2 = babelHelpers.classPrivateFieldLooseKey("_"), _class = class {}), Object.defineProperty(_class, _2, {
writable: true,
value: (() => {
_class.bar = 42;
})()
}), _temp) {}
Foo.bar = 21;
Object.defineProperty(Foo, _, {
writable: true,
value: (() => {
var _class2, _3, _temp2;
Foo.foo = (_temp2 = (_3 = babelHelpers.classPrivateFieldLooseKey("_"), _class2 = class {}), Object.defineProperty(_class2, _3, {
writable: true,
value: (() => {
_class2.bar = 42;
})()
}), _temp2).bar;
})()
});
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = Foo.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = Foo.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,10 @@
class Foo {}
babelHelpers.defineProperty(Foo, "bar", 42);
var _ = {
writable: true,
value: (() => {
Foo.foo = Foo.bar;
})()
};
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = this.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static {
this.foo = this.bar;
}
static bar = 42;
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,10 @@
class Foo {}
babelHelpers.defineProperty(Foo, "bar", 42);
var _ = {
writable: true,
value: (() => {
Foo.foo = Foo.bar;
})()
};
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,6 @@
class Foo {
static {
this.foo = 42;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,14 @@
class Foo extends class extends class Base {
static {
this.qux = 21;
}
} {
static {
this.bar = 21;
}
} {
static {
this.foo = this.bar + this.qux;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,14 @@
class Foo extends class extends class Base {
static {
this.qux = 21;
}
} {
static {
this.bar = 21;
}
} {
static {
this.foo = this.bar + this.qux;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,21 @@
var _class, _temp, _2, _class2, _temp2, _3;
class Foo extends (_temp = _class = class extends (_temp2 = _class2 = class Base {}, _3 = {
writable: true,
value: (() => {
_class2.qux = 21;
})()
}, _temp2) {}, _2 = {
writable: true,
value: (() => {
_class.bar = 21;
})()
}, _temp) {}
var _ = {
writable: true,
value: (() => {
Foo.foo = Foo.bar + Foo.qux;
})()
};
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,8 @@
class Foo {
static #bar = 21;
static {
this.foo = this.#bar + this.qux;
}
static qux = 21;
}
expect(Foo.foo).toBe(42);

View File

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

View File

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

View File

@@ -0,0 +1,15 @@
function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
class Foo {}
var _ = {
writable: true,
value: 42
};
var _2 = {
writable: true,
value: (() => {
Foo.foo = _classStaticPrivateFieldSpecGet(Foo, Foo, _);
})()
};
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
bar = 21;
static {
this.foo = this.bar;
}
}
expect(Foo.foo).toBe(undefined);

View File

@@ -0,0 +1,9 @@
{
"plugins": [
"proposal-class-static-block",
"proposal-private-property-in-object",
"proposal-private-methods",
"proposal-class-properties",
"external-helpers"
]
}

View File

@@ -0,0 +1,8 @@
let getFoo;
class Foo {
static #foo = 42;
static {
getFoo = () => this.#foo;
}
}
expect(getFoo()).toBe(42);

View File

@@ -0,0 +1,7 @@
class Foo {
static #bar = 21;
static {
this.foo = #bar in this;
}
}
expect(Foo.foo).toBe(true);

View File

@@ -0,0 +1,8 @@
let getFoo;
class Foo {
static #foo() { return 42 };
static {
getFoo = () => this.#foo();
}
}
expect(getFoo()).toBe(42);

View File

@@ -0,0 +1,9 @@
class Foo extends class {
static bar = 42;
} {
static bar = 21;
static {
this.foo = super.bar;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,11 @@
class Foo extends class {
static {
this.bar = 42;
}
} {
static bar = 21;
static {
this.foo = super.bar;
}
}
expect(Foo.foo).toBe(42);

View File

@@ -0,0 +1,3 @@
{
"plugins": ["proposal-class-static-block"]
}

View File

@@ -0,0 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);

View File

@@ -0,0 +1,41 @@
import * as babel from "@babel/core";
import proposalClassStaticBlock from "../lib/index.js";
describe("plugin ordering", () => {
it("should throw when @babel/plugin-proposal-class-static-block is after class features plugin", () => {
const source = `class Foo {
static {
this.foo = Foo.bar;
}
static bar = 42;
}
`;
expect(() => {
babel.transform(source, {
filename: "example.js",
highlightCode: false,
configFile: false,
babelrc: false,
plugins: [
"@babel/plugin-proposal-class-properties",
proposalClassStaticBlock,
],
});
})
.toThrow(`Incorrect plugin order, \`@babel/plugin-proposal-class-static-block\` should be placed before class features plugins
{
"plugins": [
"@babel/plugin-proposal-class-static-block",
"@babel/plugin-proposal-private-property-in-object",
"@babel/plugin-proposal-private-methods",
"@babel/plugin-proposal-class-properties",
]
}
1 | class Foo {
> 2 | static {
| ^
3 | this.foo = Foo.bar;
4 | }
5 | static bar = 42;`);
});
});