| [9af201e] | 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 | function mapLast(arr, fn) {
|
|---|
| 21 | if (arr.length === 0) return arr;
|
|---|
| 22 | return [...arr.slice(0, -1), fn(arr[arr.length - 1])];
|
|---|
| 23 | }
|
|---|
| 24 | var _default = exports.default = (0, _helperPluginUtils.declare)(({
|
|---|
| 25 | types: t,
|
|---|
| 26 | template,
|
|---|
| 27 | traverse,
|
|---|
| 28 | assertVersion
|
|---|
| 29 | }) => {
|
|---|
| 30 | assertVersion("^7.12.0 || ^8.0.0-0 || >8.0.0-alpha <8.0.0-beta");
|
|---|
| 31 | const rawNamedEvaluationVisitor = (0, _helperCreateClassFeaturesPlugin.buildNamedEvaluationVisitor)(path => {
|
|---|
| 32 | if (!path.isClassExpression()) return false;
|
|---|
| 33 | for (let i = path.node.body.body.length - 1; i >= 0; i--) {
|
|---|
| 34 | const el = path.node.body.body[i];
|
|---|
| 35 | if (t.isStaticBlock(el)) {
|
|---|
| 36 | return true;
|
|---|
| 37 | }
|
|---|
| 38 | if ((t.isClassProperty(el) || t.isClassPrivateProperty(el)) && el.static) {
|
|---|
| 39 | break;
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 | return false;
|
|---|
| 43 | }, (classPath, state, name) => {
|
|---|
| 44 | const nameNode = typeof name === "string" ? t.stringLiteral(name) : name;
|
|---|
| 45 | classPath.get("body").unshiftContainer("body", t.staticBlock([template.statement.ast`
|
|---|
| 46 | ${state.addHelper("setFunctionName")}(this, ${nameNode});
|
|---|
| 47 | `]));
|
|---|
| 48 | });
|
|---|
| 49 | if (!t.classAccessorProperty) {
|
|---|
| 50 | delete rawNamedEvaluationVisitor.ClassAccessorProperty;
|
|---|
| 51 | }
|
|---|
| 52 | const namedEvaluationVisitor = traverse.visitors.explode(rawNamedEvaluationVisitor);
|
|---|
| 53 | const maybeSequenceExpression = expressions => {
|
|---|
| 54 | if (expressions.length === 1) {
|
|---|
| 55 | return expressions[0];
|
|---|
| 56 | } else {
|
|---|
| 57 | return t.sequenceExpression(expressions);
|
|---|
| 58 | }
|
|---|
| 59 | };
|
|---|
| 60 | const blocksToExpressions = blocks => blocks.map(block => {
|
|---|
| 61 | const {
|
|---|
| 62 | body
|
|---|
| 63 | } = block;
|
|---|
| 64 | if (body.length === 1 && t.isExpressionStatement(body[0])) {
|
|---|
| 65 | return t.inheritsComments(t.inheritsComments(body[0].expression, body[0]), block);
|
|---|
| 66 | }
|
|---|
| 67 | return t.inheritsComments(template.expression.ast`(() => { ${body} })()`, block);
|
|---|
| 68 | });
|
|---|
| 69 | const prependToInitializer = (prop, expressions) => {
|
|---|
| 70 | prop.value = prop.value ? t.sequenceExpression([...expressions, prop.value]) : maybeSequenceExpression(mapLast(expressions, expr => t.unaryExpression("void", expr)));
|
|---|
| 71 | };
|
|---|
| 72 | return {
|
|---|
| 73 | name: "transform-class-static-block",
|
|---|
| 74 | manipulateOptions: (_, parser) => parser.plugins.push("classStaticBlock"),
|
|---|
| 75 | pre() {
|
|---|
| 76 | (0, _helperCreateClassFeaturesPlugin.enableFeature)(this.file, _helperCreateClassFeaturesPlugin.FEATURES.staticBlocks, false);
|
|---|
| 77 | },
|
|---|
| 78 | visitor: {
|
|---|
| 79 | ClassBody(classBody) {
|
|---|
| 80 | const {
|
|---|
| 81 | scope
|
|---|
| 82 | } = classBody;
|
|---|
| 83 | let parentPath = classBody.parentPath;
|
|---|
| 84 | if (parentPath.isClassExpression() && !parentPath.node.id) {
|
|---|
| 85 | do ({
|
|---|
| 86 | parentPath
|
|---|
| 87 | } = parentPath); while (parentPath && !namedEvaluationVisitor[parentPath.type] && !parentPath.isStatement());
|
|---|
| 88 | if (parentPath) {
|
|---|
| 89 | var _namedEvaluationVisit;
|
|---|
| 90 | (_namedEvaluationVisit = namedEvaluationVisitor[parentPath.type]) == null || _namedEvaluationVisit.enter.forEach(f => f.call(this, parentPath, this));
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | const pendingStaticBlocks = [];
|
|---|
| 94 | let lastStaticProp = null;
|
|---|
| 95 | for (const path of classBody.get("body")) {
|
|---|
| 96 | if (path.isStaticBlock()) {
|
|---|
| 97 | pendingStaticBlocks.push(path.node);
|
|---|
| 98 | path.remove();
|
|---|
| 99 | } else if (path.isClassProperty({
|
|---|
| 100 | static: true
|
|---|
| 101 | }) || path.isClassPrivateProperty({
|
|---|
| 102 | static: true
|
|---|
| 103 | })) {
|
|---|
| 104 | lastStaticProp = path;
|
|---|
| 105 | if (pendingStaticBlocks.length > 0) {
|
|---|
| 106 | prependToInitializer(path.node, blocksToExpressions(pendingStaticBlocks));
|
|---|
| 107 | pendingStaticBlocks.length = 0;
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | if (pendingStaticBlocks.length > 0) {
|
|---|
| 112 | const tmp = scope.generateDeclaredUidIdentifier("staticBlock");
|
|---|
| 113 | let arrowBody;
|
|---|
| 114 | const needsCompletionValue = classBody.parentPath.isExpression();
|
|---|
| 115 | if (pendingStaticBlocks.length > 1 || pendingStaticBlocks[0].body.length === 1 && t.isExpressionStatement(pendingStaticBlocks[0].body[0])) {
|
|---|
| 116 | const expressions = blocksToExpressions(pendingStaticBlocks);
|
|---|
| 117 | if (needsCompletionValue) {
|
|---|
| 118 | expressions.push(t.thisExpression());
|
|---|
| 119 | }
|
|---|
| 120 | arrowBody = maybeSequenceExpression(expressions);
|
|---|
| 121 | } else {
|
|---|
| 122 | arrowBody = t.blockStatement(pendingStaticBlocks[0].body);
|
|---|
| 123 | if (needsCompletionValue) {
|
|---|
| 124 | arrowBody.body.push(t.returnStatement(t.thisExpression()));
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 | const init = template.expression.ast`${tmp} = () => ${arrowBody}`;
|
|---|
| 128 | if (lastStaticProp) {
|
|---|
| 129 | prependToInitializer(lastStaticProp.node, [init]);
|
|---|
| 130 | } else {
|
|---|
| 131 | const privateNames = new Set();
|
|---|
| 132 | for (const path of classBody.get("body")) {
|
|---|
| 133 | if (path.isPrivate()) {
|
|---|
| 134 | privateNames.add(path.get("key.id").node.name);
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 | const staticBlockPrivateId = generateUid(scope, privateNames);
|
|---|
| 138 | const staticBlockRef = t.privateName(t.identifier(staticBlockPrivateId));
|
|---|
| 139 | classBody.pushContainer("body", [t.classPrivateProperty(staticBlockRef, init, [], true)]);
|
|---|
| 140 | }
|
|---|
| 141 | const staticBlockClosureCall = t.callExpression(t.cloneNode(tmp), []);
|
|---|
| 142 | if (classBody.parentPath.isClassExpression()) {
|
|---|
| 143 | classBody.parentPath.replaceWith(t.sequenceExpression([classBody.parent, staticBlockClosureCall]));
|
|---|
| 144 | } else {
|
|---|
| 145 | classBody.parentPath.insertAfter(t.expressionStatement(staticBlockClosureCall));
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 | };
|
|---|
| 151 | });
|
|---|
| 152 |
|
|---|
| 153 | //# sourceMappingURL=index.js.map
|
|---|