| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | Object.defineProperty(exports, "FEATURES", {
|
|---|
| 7 | enumerable: true,
|
|---|
| 8 | get: function () {
|
|---|
| 9 | return _features.FEATURES;
|
|---|
| 10 | }
|
|---|
| 11 | });
|
|---|
| 12 | Object.defineProperty(exports, "buildCheckInRHS", {
|
|---|
| 13 | enumerable: true,
|
|---|
| 14 | get: function () {
|
|---|
| 15 | return _fields.buildCheckInRHS;
|
|---|
| 16 | }
|
|---|
| 17 | });
|
|---|
| 18 | Object.defineProperty(exports, "buildNamedEvaluationVisitor", {
|
|---|
| 19 | enumerable: true,
|
|---|
| 20 | get: function () {
|
|---|
| 21 | return _decorators.buildNamedEvaluationVisitor;
|
|---|
| 22 | }
|
|---|
| 23 | });
|
|---|
| 24 | exports.createClassFeaturePlugin = createClassFeaturePlugin;
|
|---|
| 25 | Object.defineProperty(exports, "enableFeature", {
|
|---|
| 26 | enumerable: true,
|
|---|
| 27 | get: function () {
|
|---|
| 28 | return _features.enableFeature;
|
|---|
| 29 | }
|
|---|
| 30 | });
|
|---|
| 31 | Object.defineProperty(exports, "injectInitialization", {
|
|---|
| 32 | enumerable: true,
|
|---|
| 33 | get: function () {
|
|---|
| 34 | return _misc.injectInitialization;
|
|---|
| 35 | }
|
|---|
| 36 | });
|
|---|
| 37 | var _core = require("@babel/core");
|
|---|
| 38 | var _semver = require("semver");
|
|---|
| 39 | var _fields = require("./fields.js");
|
|---|
| 40 | var _decorators = require("./decorators.js");
|
|---|
| 41 | var _decorators2 = require("./decorators-2018-09.js");
|
|---|
| 42 | var _misc = require("./misc.js");
|
|---|
| 43 | var _features = require("./features.js");
|
|---|
| 44 | var _typescript = require("./typescript.js");
|
|---|
| 45 | const versionKey = "@babel/plugin-class-features/version";
|
|---|
| 46 | function createClassFeaturePlugin({
|
|---|
| 47 | name,
|
|---|
| 48 | feature,
|
|---|
| 49 | loose,
|
|---|
| 50 | manipulateOptions,
|
|---|
| 51 | api,
|
|---|
| 52 | inherits,
|
|---|
| 53 | decoratorVersion
|
|---|
| 54 | }) {
|
|---|
| 55 | var _api$assumption;
|
|---|
| 56 | if (feature & _features.FEATURES.decorators) {
|
|---|
| 57 | if (decoratorVersion === "2023-11" || decoratorVersion === "2023-05" || decoratorVersion === "2023-01" || decoratorVersion === "2022-03" || decoratorVersion === "2021-12") {
|
|---|
| 58 | return (0, _decorators.default)(api, {
|
|---|
| 59 | loose
|
|---|
| 60 | }, decoratorVersion, inherits);
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | api != null ? api : api = {
|
|---|
| 64 | assumption: () => void 0
|
|---|
| 65 | };
|
|---|
| 66 | const setPublicClassFields = api.assumption("setPublicClassFields");
|
|---|
| 67 | const privateFieldsAsSymbols = api.assumption("privateFieldsAsSymbols");
|
|---|
| 68 | const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");
|
|---|
| 69 | const noUninitializedPrivateFieldAccess = (_api$assumption = api.assumption("noUninitializedPrivateFieldAccess")) != null ? _api$assumption : false;
|
|---|
| 70 | const constantSuper = api.assumption("constantSuper");
|
|---|
| 71 | const noDocumentAll = api.assumption("noDocumentAll");
|
|---|
| 72 | if (privateFieldsAsProperties && privateFieldsAsSymbols) {
|
|---|
| 73 | throw new Error(`Cannot enable both the "privateFieldsAsProperties" and ` + `"privateFieldsAsSymbols" assumptions as the same time.`);
|
|---|
| 74 | }
|
|---|
| 75 | const privateFieldsAsSymbolsOrProperties = privateFieldsAsProperties || privateFieldsAsSymbols;
|
|---|
| 76 | if (loose === true) {
|
|---|
| 77 | const explicit = [];
|
|---|
| 78 | if (setPublicClassFields !== undefined) {
|
|---|
| 79 | explicit.push(`"setPublicClassFields"`);
|
|---|
| 80 | }
|
|---|
| 81 | if (privateFieldsAsProperties !== undefined) {
|
|---|
| 82 | explicit.push(`"privateFieldsAsProperties"`);
|
|---|
| 83 | }
|
|---|
| 84 | if (privateFieldsAsSymbols !== undefined) {
|
|---|
| 85 | explicit.push(`"privateFieldsAsSymbols"`);
|
|---|
| 86 | }
|
|---|
| 87 | if (explicit.length !== 0) {
|
|---|
| 88 | console.warn(`[${name}]: You are using the "loose: true" option and you are` + ` explicitly setting a value for the ${explicit.join(" and ")}` + ` assumption${explicit.length > 1 ? "s" : ""}. The "loose" option` + ` can cause incompatibilities with the other class features` + ` plugins, so it's recommended that you replace it with the` + ` following top-level option:\n` + `\t"assumptions": {\n` + `\t\t"setPublicClassFields": true,\n` + `\t\t"privateFieldsAsSymbols": true\n` + `\t}`);
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | return {
|
|---|
| 92 | name,
|
|---|
| 93 | manipulateOptions,
|
|---|
| 94 | inherits,
|
|---|
| 95 | pre(file) {
|
|---|
| 96 | (0, _features.enableFeature)(file, feature, loose);
|
|---|
| 97 | if (typeof file.get(versionKey) === "number") {
|
|---|
| 98 | file.set(versionKey, "7.29.3");
|
|---|
| 99 | return;
|
|---|
| 100 | }
|
|---|
| 101 | if (!file.get(versionKey) || _semver.lt(file.get(versionKey), "7.29.3")) {
|
|---|
| 102 | file.set(versionKey, "7.29.3");
|
|---|
| 103 | }
|
|---|
| 104 | },
|
|---|
| 105 | visitor: {
|
|---|
| 106 | Class(path, {
|
|---|
| 107 | file
|
|---|
| 108 | }) {
|
|---|
| 109 | if (file.get(versionKey) !== "7.29.3") return;
|
|---|
| 110 | if (!(0, _features.shouldTransform)(path, file)) return;
|
|---|
| 111 | const pathIsClassDeclaration = path.isClassDeclaration();
|
|---|
| 112 | if (pathIsClassDeclaration) (0, _typescript.assertFieldTransformed)(path);
|
|---|
| 113 | const loose = (0, _features.isLoose)(file, feature);
|
|---|
| 114 | let constructor;
|
|---|
| 115 | const isDecorated = (0, _decorators.hasDecorators)(path.node);
|
|---|
| 116 | const props = [];
|
|---|
| 117 | const elements = [];
|
|---|
| 118 | const computedPaths = [];
|
|---|
| 119 | const privateNames = new Set();
|
|---|
| 120 | const body = path.get("body");
|
|---|
| 121 | for (const path of body.get("body")) {
|
|---|
| 122 | if ((path.isClassProperty() || path.isClassMethod()) && path.node.computed) {
|
|---|
| 123 | computedPaths.push(path);
|
|---|
| 124 | }
|
|---|
| 125 | if (path.isPrivate()) {
|
|---|
| 126 | const {
|
|---|
| 127 | name
|
|---|
| 128 | } = path.node.key.id;
|
|---|
| 129 | const getName = `get ${name}`;
|
|---|
| 130 | const setName = `set ${name}`;
|
|---|
| 131 | if (path.isClassPrivateMethod()) {
|
|---|
| 132 | if (path.node.kind === "get") {
|
|---|
| 133 | if (privateNames.has(getName) || privateNames.has(name) && !privateNames.has(setName)) {
|
|---|
| 134 | throw path.buildCodeFrameError("Duplicate private field");
|
|---|
| 135 | }
|
|---|
| 136 | privateNames.add(getName).add(name);
|
|---|
| 137 | } else if (path.node.kind === "set") {
|
|---|
| 138 | if (privateNames.has(setName) || privateNames.has(name) && !privateNames.has(getName)) {
|
|---|
| 139 | throw path.buildCodeFrameError("Duplicate private field");
|
|---|
| 140 | }
|
|---|
| 141 | privateNames.add(setName).add(name);
|
|---|
| 142 | }
|
|---|
| 143 | } else {
|
|---|
| 144 | if (privateNames.has(name) && !privateNames.has(getName) && !privateNames.has(setName) || privateNames.has(name) && (privateNames.has(getName) || privateNames.has(setName))) {
|
|---|
| 145 | throw path.buildCodeFrameError("Duplicate private field");
|
|---|
| 146 | }
|
|---|
| 147 | privateNames.add(name);
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 | if (path.isClassMethod({
|
|---|
| 151 | kind: "constructor"
|
|---|
| 152 | })) {
|
|---|
| 153 | constructor = path;
|
|---|
| 154 | } else {
|
|---|
| 155 | elements.push(path);
|
|---|
| 156 | if (path.isProperty() || path.isPrivate() || path.isStaticBlock != null && path.isStaticBlock()) {
|
|---|
| 157 | props.push(path);
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 | if (!props.length && !isDecorated) return;
|
|---|
| 162 | const innerBinding = path.node.id;
|
|---|
| 163 | let ref;
|
|---|
| 164 | if (!innerBinding || !pathIsClassDeclaration) {
|
|---|
| 165 | var _path$ensureFunctionN;
|
|---|
| 166 | (_path$ensureFunctionN = path.ensureFunctionName) != null ? _path$ensureFunctionN : path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
|
|---|
| 167 | path.ensureFunctionName(false);
|
|---|
| 168 | ref = path.scope.generateUidIdentifier((innerBinding == null ? void 0 : innerBinding.name) || "Class");
|
|---|
| 169 | }
|
|---|
| 170 | const classRefForDefine = ref != null ? ref : _core.types.cloneNode(innerBinding);
|
|---|
| 171 | const privateNamesMap = (0, _fields.buildPrivateNamesMap)(classRefForDefine.name, privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose, props, file);
|
|---|
| 172 | const privateNamesNodes = (0, _fields.buildPrivateNamesNodes)(privateNamesMap, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, privateFieldsAsSymbols != null ? privateFieldsAsSymbols : false, file);
|
|---|
| 173 | (0, _fields.transformPrivateNamesUsage)(classRefForDefine, path, privateNamesMap, {
|
|---|
| 174 | privateFieldsAsProperties: privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose,
|
|---|
| 175 | noUninitializedPrivateFieldAccess,
|
|---|
| 176 | noDocumentAll,
|
|---|
| 177 | innerBinding
|
|---|
| 178 | }, file);
|
|---|
| 179 | let keysNodes, staticNodes, instanceNodes, lastInstanceNodeReturnsThis, pureStaticNodes, classBindingNode, wrapClass;
|
|---|
| 180 | if (isDecorated) {
|
|---|
| 181 | staticNodes = pureStaticNodes = keysNodes = [];
|
|---|
| 182 | ({
|
|---|
| 183 | instanceNodes,
|
|---|
| 184 | wrapClass
|
|---|
| 185 | } = (0, _decorators2.buildDecoratedClass)(classRefForDefine, path, elements, file));
|
|---|
| 186 | } else {
|
|---|
| 187 | keysNodes = (0, _misc.extractComputedKeys)(path, computedPaths, file);
|
|---|
| 188 | ({
|
|---|
| 189 | staticNodes,
|
|---|
| 190 | pureStaticNodes,
|
|---|
| 191 | instanceNodes,
|
|---|
| 192 | lastInstanceNodeReturnsThis,
|
|---|
| 193 | classBindingNode,
|
|---|
| 194 | wrapClass
|
|---|
| 195 | } = (0, _fields.buildFieldsInitNodes)(ref, path.node.superClass, props, privateNamesMap, file, setPublicClassFields != null ? setPublicClassFields : loose, privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose, noUninitializedPrivateFieldAccess, constantSuper != null ? constantSuper : loose, innerBinding));
|
|---|
| 196 | }
|
|---|
| 197 | if (instanceNodes.length > 0) {
|
|---|
| 198 | (0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => {
|
|---|
| 199 | if (isDecorated) return;
|
|---|
| 200 | for (const prop of props) {
|
|---|
| 201 | if (_core.types.isStaticBlock != null && _core.types.isStaticBlock(prop.node) || prop.node.static) continue;
|
|---|
| 202 | prop.traverse(referenceVisitor, state);
|
|---|
| 203 | }
|
|---|
| 204 | }, lastInstanceNodeReturnsThis);
|
|---|
| 205 | }
|
|---|
| 206 | const wrappedPath = wrapClass(path);
|
|---|
| 207 | wrappedPath.insertBefore([...privateNamesNodes, ...keysNodes]);
|
|---|
| 208 | if (staticNodes.length > 0) {
|
|---|
| 209 | wrappedPath.insertAfter(staticNodes);
|
|---|
| 210 | }
|
|---|
| 211 | if (pureStaticNodes.length > 0) {
|
|---|
| 212 | wrappedPath.find(parent => parent.isStatement() || parent.isDeclaration()).insertAfter(pureStaticNodes);
|
|---|
| 213 | }
|
|---|
| 214 | if (classBindingNode != null && pathIsClassDeclaration) {
|
|---|
| 215 | wrappedPath.insertAfter(classBindingNode);
|
|---|
| 216 | }
|
|---|
| 217 | },
|
|---|
| 218 | ExportDefaultDeclaration(path, {
|
|---|
| 219 | file
|
|---|
| 220 | }) {
|
|---|
| 221 | if (file.get(versionKey) !== "7.29.3") return;
|
|---|
| 222 | const decl = path.get("declaration");
|
|---|
| 223 | if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) {
|
|---|
| 224 | if (decl.node.id) {
|
|---|
| 225 | var _path$splitExportDecl;
|
|---|
| 226 | (_path$splitExportDecl = path.splitExportDeclaration) != null ? _path$splitExportDecl : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
|
|---|
| 227 | path.splitExportDeclaration();
|
|---|
| 228 | } else {
|
|---|
| 229 | decl.node.type = "ClassExpression";
|
|---|
| 230 | }
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 | };
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | //# sourceMappingURL=index.js.map
|
|---|