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:
parent
3ccca88178
commit
f697e7995d
8
Makefile
8
Makefile
@ -220,6 +220,14 @@ prepublish:
|
||||
|
||||
# --exclude-dependents support is added by .yarn-patches/@lerna/version
|
||||
new-version:
|
||||
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||
@echo "!!!!!! !!!!!!"
|
||||
@echo "!!!!!! Enable the check in proposal-class-static-block !!!!!!"
|
||||
@echo "!!!!!! !!!!!!"
|
||||
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||
@exit 1
|
||||
git pull --rebase
|
||||
$(YARN) lerna version --exclude-dependents --force-publish=$(FORCE_PUBLISH)
|
||||
|
||||
|
||||
@ -31,6 +31,16 @@ const pluginNameMap = {
|
||||
url: "https://git.io/JvpRG",
|
||||
},
|
||||
},
|
||||
classStaticBlock: {
|
||||
syntax: {
|
||||
name: "@babel/plugin-syntax-class-static-block",
|
||||
url: "https://git.io/JTLB6",
|
||||
},
|
||||
transform: {
|
||||
name: "@babel/plugin-proposal-class-static-block",
|
||||
url: "https://git.io/JTLBP",
|
||||
},
|
||||
},
|
||||
decimal: {
|
||||
syntax: {
|
||||
name: "@babel/plugin-syntax-decimal",
|
||||
|
||||
@ -120,6 +120,18 @@ export function createClassFeaturePlugin({
|
||||
}
|
||||
|
||||
if (!isDecorated) isDecorated = hasOwnDecorators(path.node);
|
||||
|
||||
if (path.isStaticBlock?.()) {
|
||||
throw path.buildCodeFrameError(`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",
|
||||
]
|
||||
}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (!props.length && !isDecorated) return;
|
||||
@ -188,7 +200,12 @@ export function createClassFeaturePlugin({
|
||||
},
|
||||
|
||||
PrivateName(path) {
|
||||
if (this.file.get(versionKey) !== version) return;
|
||||
if (
|
||||
this.file.get(versionKey) !== version ||
|
||||
path.parentPath.isPrivate({ key: path.node })
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw path.buildCodeFrameError(`Unknown PrivateName "${path}"`);
|
||||
},
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
"@babel/helper-split-export-declaration": "workspace:^7.11.0",
|
||||
"@babel/helper-validator-identifier": "workspace:^7.10.4",
|
||||
"@babel/template": "workspace:^7.10.4",
|
||||
"@babel/traverse": "workspace:^7.11.5",
|
||||
"@babel/types": "workspace:^7.11.0",
|
||||
"lodash": "^4.17.19"
|
||||
}
|
||||
|
||||
@ -1,25 +1,21 @@
|
||||
import { skipAllButComputedKey } from "@babel/helper-replace-supers";
|
||||
import { environmentVisitor } from "@babel/helper-replace-supers";
|
||||
import traverse from "@babel/traverse";
|
||||
import * as t from "@babel/types";
|
||||
|
||||
export default function rewriteThis(programPath: NodePath) {
|
||||
// Rewrite "this" to be "undefined".
|
||||
programPath.traverse(rewriteThisVisitor);
|
||||
traverse(programPath.node, { ...rewriteThisVisitor, noScope: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* A visitor to walk the tree, rewriting all `this` references in the top-level scope to be
|
||||
* `undefined`.
|
||||
* `void 0` (undefined).
|
||||
*/
|
||||
const rewriteThisVisitor = {
|
||||
const rewriteThisVisitor = traverse.visitors.merge([
|
||||
environmentVisitor,
|
||||
{
|
||||
ThisExpression(path) {
|
||||
path.replaceWith(path.scope.buildUndefinedNode());
|
||||
path.replaceWith(t.unaryExpression("void", t.numericLiteral(0), true));
|
||||
},
|
||||
Function(path) {
|
||||
if (path.isMethod()) skipAllButComputedKey(path);
|
||||
else if (!path.isArrowFunctionExpression()) path.skip();
|
||||
},
|
||||
ClassProperty(path) {
|
||||
skipAllButComputedKey(path);
|
||||
},
|
||||
ClassPrivateProperty(path) {
|
||||
path.skip();
|
||||
},
|
||||
};
|
||||
]);
|
||||
|
||||
@ -42,8 +42,13 @@ export function skipAllButComputedKey(path: NodePath) {
|
||||
}
|
||||
|
||||
// environmentVisitor should be used when traversing the whole class and not for specific class elements/methods.
|
||||
// For perf reasons, the environmentVisitor will be traversed with `{ noScope: true }`, which means `path.scope` is undefined.
|
||||
// Avoid using `path.scope` here
|
||||
export const environmentVisitor = {
|
||||
TypeAnnotation(path: NodePath) {
|
||||
// todo (Babel 8): remove StaticBlock brand checks
|
||||
[`${t.StaticBlock ? "StaticBlock|" : ""}ClassPrivateProperty|TypeAnnotation`](
|
||||
path: NodePath,
|
||||
) {
|
||||
path.skip();
|
||||
},
|
||||
|
||||
@ -55,7 +60,7 @@ export const environmentVisitor = {
|
||||
path.skip();
|
||||
},
|
||||
|
||||
"Method|ClassProperty|ClassPrivateProperty"(path: NodePath) {
|
||||
"Method|ClassProperty"(path: NodePath) {
|
||||
skipAllButComputedKey(path);
|
||||
},
|
||||
};
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
src
|
||||
test
|
||||
*.log
|
||||
19
packages/babel-plugin-proposal-class-static-block/README.md
Normal file
19
packages/babel-plugin-proposal-class-static-block/README.md
Normal 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
|
||||
```
|
||||
@ -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"
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = Foo.bar;
|
||||
}
|
||||
static bar = 42;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = this.bar;
|
||||
}
|
||||
static bar = 42;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = this.bar;
|
||||
}
|
||||
static bar = 42;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,8 @@
|
||||
class Foo {
|
||||
static bar = 42;
|
||||
static #_ = (() => {
|
||||
this.foo = this.bar;
|
||||
})();
|
||||
}
|
||||
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = 42;
|
||||
}
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -0,0 +1,8 @@
|
||||
class Foo {
|
||||
static #bar = 21;
|
||||
static {
|
||||
this.foo = this.#bar + this.qux;
|
||||
}
|
||||
static qux = 21;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
bar = 21;
|
||||
static {
|
||||
this.foo = this.bar;
|
||||
}
|
||||
}
|
||||
expect(Foo.foo).toBe(undefined);
|
||||
@ -0,0 +1,4 @@
|
||||
{
|
||||
"plugins": ["proposal-class-static-block", "syntax-class-properties"],
|
||||
"minNodeVersion": "12.0.0"
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
let getFoo;
|
||||
class Foo {
|
||||
static #foo = 42;
|
||||
static {
|
||||
getFoo = () => this.#foo;
|
||||
}
|
||||
}
|
||||
expect(getFoo()).toBe(42);
|
||||
@ -0,0 +1,9 @@
|
||||
class Foo extends class {
|
||||
static bar = 42;
|
||||
} {
|
||||
static bar = 21;
|
||||
static {
|
||||
this.foo = super.bar;
|
||||
}
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -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);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = Foo.bar;
|
||||
}
|
||||
static bar = 42;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = Foo.bar;
|
||||
}
|
||||
static bar = 42;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -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);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = this.bar;
|
||||
}
|
||||
static bar = 42;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = this.bar;
|
||||
}
|
||||
static bar = 42;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -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);
|
||||
@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = 42;
|
||||
}
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -0,0 +1,8 @@
|
||||
class Foo {
|
||||
static #bar = 21;
|
||||
static {
|
||||
this.foo = this.#bar + this.qux;
|
||||
}
|
||||
static qux = 21;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
bar = 21;
|
||||
static {
|
||||
this.foo = this.bar;
|
||||
}
|
||||
}
|
||||
expect(Foo.foo).toBe(undefined);
|
||||
@ -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"
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
let getFoo;
|
||||
class Foo {
|
||||
static #foo = 42;
|
||||
static {
|
||||
getFoo = () => this.#foo;
|
||||
}
|
||||
}
|
||||
expect(getFoo()).toBe(42);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static #bar = 21;
|
||||
static {
|
||||
this.foo = #bar in this;
|
||||
}
|
||||
}
|
||||
expect(Foo.foo).toBe(true);
|
||||
@ -0,0 +1,8 @@
|
||||
let getFoo;
|
||||
class Foo {
|
||||
static #foo() { return 42 };
|
||||
static {
|
||||
getFoo = () => this.#foo();
|
||||
}
|
||||
}
|
||||
expect(getFoo()).toBe(42);
|
||||
@ -0,0 +1,9 @@
|
||||
class Foo extends class {
|
||||
static bar = 42;
|
||||
} {
|
||||
static bar = 21;
|
||||
static {
|
||||
this.foo = super.bar;
|
||||
}
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = Foo.bar;
|
||||
}
|
||||
static bar = 42;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = Foo.bar;
|
||||
}
|
||||
static bar = 42;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,10 @@
|
||||
class Foo {}
|
||||
|
||||
babelHelpers.defineProperty(Foo, "bar", 42);
|
||||
var _ = {
|
||||
writable: true,
|
||||
value: (() => {
|
||||
Foo.foo = Foo.bar;
|
||||
})()
|
||||
};
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = this.bar;
|
||||
}
|
||||
static bar = 42;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = this.bar;
|
||||
}
|
||||
static bar = 42;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,10 @@
|
||||
class Foo {}
|
||||
|
||||
babelHelpers.defineProperty(Foo, "bar", 42);
|
||||
var _ = {
|
||||
writable: true,
|
||||
value: (() => {
|
||||
Foo.foo = Foo.bar;
|
||||
})()
|
||||
};
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
static {
|
||||
this.foo = 42;
|
||||
}
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -0,0 +1,8 @@
|
||||
class Foo {
|
||||
static #bar = 21;
|
||||
static {
|
||||
this.foo = this.#bar + this.qux;
|
||||
}
|
||||
static qux = 21;
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
bar = 21;
|
||||
static {
|
||||
this.foo = this.bar;
|
||||
}
|
||||
}
|
||||
expect(Foo.foo).toBe(undefined);
|
||||
9
packages/babel-plugin-proposal-class-static-block/test/fixtures/integration/options.json
vendored
Normal file
9
packages/babel-plugin-proposal-class-static-block/test/fixtures/integration/options.json
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"plugins": [
|
||||
"proposal-class-static-block",
|
||||
"proposal-private-property-in-object",
|
||||
"proposal-private-methods",
|
||||
"proposal-class-properties",
|
||||
"external-helpers"
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
let getFoo;
|
||||
class Foo {
|
||||
static #foo = 42;
|
||||
static {
|
||||
getFoo = () => this.#foo;
|
||||
}
|
||||
}
|
||||
expect(getFoo()).toBe(42);
|
||||
@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static #bar = 21;
|
||||
static {
|
||||
this.foo = #bar in this;
|
||||
}
|
||||
}
|
||||
expect(Foo.foo).toBe(true);
|
||||
@ -0,0 +1,8 @@
|
||||
let getFoo;
|
||||
class Foo {
|
||||
static #foo() { return 42 };
|
||||
static {
|
||||
getFoo = () => this.#foo();
|
||||
}
|
||||
}
|
||||
expect(getFoo()).toBe(42);
|
||||
@ -0,0 +1,9 @@
|
||||
class Foo extends class {
|
||||
static bar = 42;
|
||||
} {
|
||||
static bar = 21;
|
||||
static {
|
||||
this.foo = super.bar;
|
||||
}
|
||||
}
|
||||
expect(Foo.foo).toBe(42);
|
||||
@ -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);
|
||||
3
packages/babel-plugin-proposal-class-static-block/test/fixtures/options.json
vendored
Normal file
3
packages/babel-plugin-proposal-class-static-block/test/fixtures/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["proposal-class-static-block"]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
import runner from "@babel/helper-plugin-test-runner";
|
||||
|
||||
runner(__dirname);
|
||||
@ -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;`);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,5 @@
|
||||
class foo {
|
||||
static {
|
||||
this // should not be replaced by undefined
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-modules-commonjs", "syntax-class-static-block"]
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
class foo {
|
||||
static {
|
||||
this; // should not be replaced by undefined
|
||||
|
||||
}
|
||||
}
|
||||
@ -13,6 +13,7 @@
|
||||
"@babel/plugin-external-helpers": "workspace:^7.10.4",
|
||||
"@babel/plugin-proposal-async-generator-functions": "workspace:^7.10.5",
|
||||
"@babel/plugin-proposal-class-properties": "workspace:^7.10.4",
|
||||
"@babel/plugin-proposal-class-static-block": "workspace:^7.11.0",
|
||||
"@babel/plugin-proposal-decorators": "workspace:^7.10.5",
|
||||
"@babel/plugin-proposal-do-expressions": "workspace:^7.10.4",
|
||||
"@babel/plugin-proposal-dynamic-import": "workspace:^7.10.4",
|
||||
@ -34,6 +35,7 @@
|
||||
"@babel/plugin-proposal-unicode-property-regex": "workspace:^7.10.4",
|
||||
"@babel/plugin-syntax-async-generators": "^7.8.0",
|
||||
"@babel/plugin-syntax-class-properties": "workspace:^7.10.4",
|
||||
"@babel/plugin-syntax-class-static-block": "workspace:^7.11.0",
|
||||
"@babel/plugin-syntax-decimal": "workspace:^7.11.0",
|
||||
"@babel/plugin-syntax-decorators": "workspace:^7.10.4",
|
||||
"@babel/plugin-syntax-do-expressions": "workspace:^7.10.4",
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
"external-helpers",
|
||||
"syntax-async-generators",
|
||||
"syntax-class-properties",
|
||||
"syntax-class-static-block",
|
||||
"syntax-decimal",
|
||||
"syntax-decorators",
|
||||
"syntax-do-expressions",
|
||||
@ -20,6 +21,7 @@
|
||||
"syntax-typescript",
|
||||
"proposal-async-generator-functions",
|
||||
"proposal-class-properties",
|
||||
"proposal-class-static-block",
|
||||
"proposal-decorators",
|
||||
"proposal-do-expressions",
|
||||
"proposal-dynamic-import",
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
import externalHelpers from "@babel/plugin-external-helpers";
|
||||
import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";
|
||||
import syntaxClassProperties from "@babel/plugin-syntax-class-properties";
|
||||
import syntaxClassStaticBlock from "@babel/plugin-syntax-class-static-block";
|
||||
import syntaxDecimal from "@babel/plugin-syntax-decimal";
|
||||
import syntaxDecorators from "@babel/plugin-syntax-decorators";
|
||||
import syntaxDoExpressions from "@babel/plugin-syntax-do-expressions";
|
||||
@ -23,6 +24,7 @@ import syntaxTopLevelAwait from "@babel/plugin-syntax-top-level-await";
|
||||
import syntaxTypescript from "@babel/plugin-syntax-typescript";
|
||||
import proposalAsyncGeneratorFunctions from "@babel/plugin-proposal-async-generator-functions";
|
||||
import proposalClassProperties from "@babel/plugin-proposal-class-properties";
|
||||
import proposalClassStaticBlock from "@babel/plugin-proposal-class-static-block";
|
||||
import proposalDecorators from "@babel/plugin-proposal-decorators";
|
||||
import proposalDoExpressions from "@babel/plugin-proposal-do-expressions";
|
||||
import proposalDynamicImport from "@babel/plugin-proposal-dynamic-import";
|
||||
@ -98,6 +100,7 @@ export {
|
||||
externalHelpers,
|
||||
syntaxAsyncGenerators,
|
||||
syntaxClassProperties,
|
||||
syntaxClassStaticBlock,
|
||||
syntaxDecimal,
|
||||
syntaxDecorators,
|
||||
syntaxDoExpressions,
|
||||
@ -116,6 +119,7 @@ export {
|
||||
syntaxTypescript,
|
||||
proposalAsyncGeneratorFunctions,
|
||||
proposalClassProperties,
|
||||
proposalClassStaticBlock,
|
||||
proposalDecorators,
|
||||
proposalDoExpressions,
|
||||
proposalDynamicImport,
|
||||
@ -192,6 +196,7 @@ export const all = {
|
||||
"external-helpers": externalHelpers,
|
||||
"syntax-async-generators": syntaxAsyncGenerators,
|
||||
"syntax-class-properties": syntaxClassProperties,
|
||||
"syntax-class-static-block": syntaxClassStaticBlock,
|
||||
"syntax-decimal": syntaxDecimal,
|
||||
"syntax-decorators": syntaxDecorators,
|
||||
"syntax-do-expressions": syntaxDoExpressions,
|
||||
@ -210,6 +215,7 @@ export const all = {
|
||||
"syntax-typescript": syntaxTypescript,
|
||||
"proposal-async-generator-functions": proposalAsyncGeneratorFunctions,
|
||||
"proposal-class-properties": proposalClassProperties,
|
||||
"proposal-class-static-block": proposalClassStaticBlock,
|
||||
"proposal-decorators": proposalDecorators,
|
||||
"proposal-do-expressions": proposalDoExpressions,
|
||||
"proposal-dynamic-import": proposalDynamicImport,
|
||||
|
||||
@ -29,7 +29,6 @@ export default (_: any, opts: Object = {}) => {
|
||||
[babelPlugins.syntaxRecordAndTuple, { syntaxType: recordAndTupleSyntax }],
|
||||
babelPlugins.proposalExportDefaultFrom,
|
||||
[babelPlugins.proposalPipelineOperator, { proposal: pipelineProposal }],
|
||||
babelPlugins.proposalPrivatePropertyInObject,
|
||||
babelPlugins.proposalDoExpressions,
|
||||
],
|
||||
};
|
||||
|
||||
@ -13,11 +13,13 @@ export default (_: any, opts: Object = {}) => {
|
||||
return {
|
||||
presets: [[presetStage3, { loose, useBuiltIns }]],
|
||||
plugins: [
|
||||
babelPlugins.proposalClassStaticBlock,
|
||||
[
|
||||
babelPlugins.proposalDecorators,
|
||||
{ legacy: decoratorsLegacy, decoratorsBeforeExport },
|
||||
],
|
||||
babelPlugins.proposalFunctionSent,
|
||||
babelPlugins.proposalPrivatePropertyInObject,
|
||||
babelPlugins.proposalThrowExpressions,
|
||||
],
|
||||
};
|
||||
|
||||
@ -367,7 +367,7 @@ export default class Scope {
|
||||
.replace(/[0-9]+$/g, "");
|
||||
|
||||
let uid;
|
||||
let i = 0;
|
||||
let i = 1;
|
||||
do {
|
||||
uid = this._generateUid(name, i);
|
||||
i++;
|
||||
|
||||
@ -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: {
|
||||
|
||||
17
yarn.lock
17
yarn.lock
@ -595,6 +595,7 @@ __metadata:
|
||||
"@babel/helper-split-export-declaration": "workspace:^7.11.0"
|
||||
"@babel/helper-validator-identifier": "workspace:^7.10.4"
|
||||
"@babel/template": "workspace:^7.10.4"
|
||||
"@babel/traverse": "workspace:^7.11.5"
|
||||
"@babel/types": "workspace:^7.11.0"
|
||||
lodash: ^4.17.19
|
||||
languageName: unknown
|
||||
@ -998,6 +999,18 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@babel/plugin-proposal-class-static-block@workspace:^7.11.0, @babel/plugin-proposal-class-static-block@workspace:packages/babel-plugin-proposal-class-static-block":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@babel/plugin-proposal-class-static-block@workspace:packages/babel-plugin-proposal-class-static-block"
|
||||
dependencies:
|
||||
"@babel/core": "workspace:^7.11.6"
|
||||
"@babel/helper-plugin-utils": "workspace:^7.10.1"
|
||||
"@babel/plugin-syntax-class-static-block": "workspace:^7.11.0"
|
||||
peerDependencies:
|
||||
"@babel/core": ^7.12.0
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@babel/plugin-proposal-decorators@workspace:^7.10.4, @babel/plugin-proposal-decorators@workspace:^7.10.5, @babel/plugin-proposal-decorators@workspace:packages/babel-plugin-proposal-decorators":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@babel/plugin-proposal-decorators@workspace:packages/babel-plugin-proposal-decorators"
|
||||
@ -1432,7 +1445,7 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@babel/plugin-syntax-class-static-block@workspace:packages/babel-plugin-syntax-class-static-block":
|
||||
"@babel/plugin-syntax-class-static-block@workspace:^7.11.0, @babel/plugin-syntax-class-static-block@workspace:packages/babel-plugin-syntax-class-static-block":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@babel/plugin-syntax-class-static-block@workspace:packages/babel-plugin-syntax-class-static-block"
|
||||
dependencies:
|
||||
@ -3176,6 +3189,7 @@ __metadata:
|
||||
"@babel/plugin-external-helpers": "workspace:^7.10.4"
|
||||
"@babel/plugin-proposal-async-generator-functions": "workspace:^7.10.5"
|
||||
"@babel/plugin-proposal-class-properties": "workspace:^7.10.4"
|
||||
"@babel/plugin-proposal-class-static-block": "workspace:^7.11.0"
|
||||
"@babel/plugin-proposal-decorators": "workspace:^7.10.5"
|
||||
"@babel/plugin-proposal-do-expressions": "workspace:^7.10.4"
|
||||
"@babel/plugin-proposal-dynamic-import": "workspace:^7.10.4"
|
||||
@ -3197,6 +3211,7 @@ __metadata:
|
||||
"@babel/plugin-proposal-unicode-property-regex": "workspace:^7.10.4"
|
||||
"@babel/plugin-syntax-async-generators": ^7.8.0
|
||||
"@babel/plugin-syntax-class-properties": "workspace:^7.10.4"
|
||||
"@babel/plugin-syntax-class-static-block": "workspace:^7.11.0"
|
||||
"@babel/plugin-syntax-decimal": "workspace:^7.11.0"
|
||||
"@babel/plugin-syntax-decorators": "workspace:^7.10.4"
|
||||
"@babel/plugin-syntax-do-expressions": "workspace:^7.10.4"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user