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