[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = void 0;
|
---|
| 7 |
|
---|
| 8 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
| 9 |
|
---|
| 10 | var _pluginSyntaxClassStaticBlock = require("@babel/plugin-syntax-class-static-block");
|
---|
| 11 |
|
---|
| 12 | var _helperCreateClassFeaturesPlugin = require("@babel/helper-create-class-features-plugin");
|
---|
| 13 |
|
---|
| 14 | function generateUid(scope, denyList) {
|
---|
| 15 | const name = "";
|
---|
| 16 | let uid;
|
---|
| 17 | let i = 1;
|
---|
| 18 |
|
---|
| 19 | do {
|
---|
| 20 | uid = scope._generateUid(name, i);
|
---|
| 21 | i++;
|
---|
| 22 | } while (denyList.has(uid));
|
---|
| 23 |
|
---|
| 24 | return uid;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | var _default = (0, _helperPluginUtils.declare)(({
|
---|
| 28 | types: t,
|
---|
| 29 | template,
|
---|
| 30 | assertVersion
|
---|
| 31 | }) => {
|
---|
| 32 | assertVersion("^7.12.0");
|
---|
| 33 | return {
|
---|
| 34 | name: "proposal-class-static-block",
|
---|
| 35 | inherits: _pluginSyntaxClassStaticBlock.default,
|
---|
| 36 |
|
---|
| 37 | pre() {
|
---|
| 38 | (0, _helperCreateClassFeaturesPlugin.enableFeature)(this.file, _helperCreateClassFeaturesPlugin.FEATURES.staticBlocks, false);
|
---|
| 39 | },
|
---|
| 40 |
|
---|
| 41 | visitor: {
|
---|
| 42 | ClassBody(classBody) {
|
---|
| 43 | const {
|
---|
| 44 | scope
|
---|
| 45 | } = classBody;
|
---|
| 46 | const privateNames = new Set();
|
---|
| 47 | const body = classBody.get("body");
|
---|
| 48 |
|
---|
| 49 | for (const path of body) {
|
---|
| 50 | if (path.isPrivate()) {
|
---|
| 51 | privateNames.add(path.get("key.id").node.name);
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | for (const path of body) {
|
---|
| 56 | if (!path.isStaticBlock()) continue;
|
---|
| 57 | const staticBlockPrivateId = generateUid(scope, privateNames);
|
---|
| 58 | privateNames.add(staticBlockPrivateId);
|
---|
| 59 | const staticBlockRef = t.privateName(t.identifier(staticBlockPrivateId));
|
---|
| 60 | path.replaceWith(t.classPrivateProperty(staticBlockRef, template.expression.ast`(() => { ${path.node.body} })()`, [], true));
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | }
|
---|
| 65 | };
|
---|
| 66 | });
|
---|
| 67 |
|
---|
| 68 | exports.default = _default; |
---|