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