[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 _pluginSyntaxPrivatePropertyInObject = require("@babel/plugin-syntax-private-property-in-object");
|
---|
| 11 |
|
---|
| 12 | var _helperCreateClassFeaturesPlugin = require("@babel/helper-create-class-features-plugin");
|
---|
| 13 |
|
---|
| 14 | var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
|
---|
| 15 |
|
---|
| 16 | var _default = (0, _helperPluginUtils.declare)(({
|
---|
| 17 | assertVersion,
|
---|
| 18 | types: t,
|
---|
| 19 | template
|
---|
| 20 | }, {
|
---|
| 21 | loose
|
---|
| 22 | }) => {
|
---|
| 23 | assertVersion(7);
|
---|
| 24 | const classWeakSets = new WeakMap();
|
---|
| 25 | const fieldsWeakSets = new WeakMap();
|
---|
| 26 |
|
---|
| 27 | function unshadow(name, targetScope, scope) {
|
---|
| 28 | while (scope !== targetScope) {
|
---|
| 29 | if (scope.hasOwnBinding(name)) scope.rename(name);
|
---|
| 30 | scope = scope.parent;
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | function injectToFieldInit(fieldPath, expr, before = false) {
|
---|
| 35 | if (fieldPath.node.value) {
|
---|
| 36 | if (before) {
|
---|
| 37 | fieldPath.get("value").insertBefore(expr);
|
---|
| 38 | } else {
|
---|
| 39 | fieldPath.get("value").insertAfter(expr);
|
---|
| 40 | }
|
---|
| 41 | } else {
|
---|
| 42 | fieldPath.set("value", t.unaryExpression("void", expr));
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | function injectInitialization(classPath, init) {
|
---|
| 47 | let firstFieldPath;
|
---|
| 48 | let consturctorPath;
|
---|
| 49 |
|
---|
| 50 | for (const el of classPath.get("body.body")) {
|
---|
| 51 | if ((el.isClassProperty() || el.isClassPrivateProperty()) && !el.node.static) {
|
---|
| 52 | firstFieldPath = el;
|
---|
| 53 | break;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | if (!consturctorPath && el.isClassMethod({
|
---|
| 57 | kind: "constructor"
|
---|
| 58 | })) {
|
---|
| 59 | consturctorPath = el;
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | if (firstFieldPath) {
|
---|
| 64 | injectToFieldInit(firstFieldPath, init, true);
|
---|
| 65 | } else {
|
---|
| 66 | (0, _helperCreateClassFeaturesPlugin.injectInitialization)(classPath, consturctorPath, [t.expressionStatement(init)]);
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | function getWeakSetId(weakSets, outerClass, reference, name = "", inject) {
|
---|
| 71 | let id = classWeakSets.get(reference.node);
|
---|
| 72 |
|
---|
| 73 | if (!id) {
|
---|
| 74 | id = outerClass.scope.generateUidIdentifier(`${name || ""} brandCheck`);
|
---|
| 75 | classWeakSets.set(reference.node, id);
|
---|
| 76 | inject(reference, template.expression.ast`${t.cloneNode(id)}.add(this)`);
|
---|
| 77 | const newExpr = t.newExpression(t.identifier("WeakSet"), []);
|
---|
| 78 | (0, _helperAnnotateAsPure.default)(newExpr);
|
---|
| 79 | outerClass.insertBefore(template.ast`var ${id} = ${newExpr}`);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | return t.cloneNode(id);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | return {
|
---|
| 86 | name: "proposal-private-property-in-object",
|
---|
| 87 | inherits: _pluginSyntaxPrivatePropertyInObject.default,
|
---|
| 88 |
|
---|
| 89 | pre() {
|
---|
| 90 | (0, _helperCreateClassFeaturesPlugin.enableFeature)(this.file, _helperCreateClassFeaturesPlugin.FEATURES.privateIn, loose);
|
---|
| 91 | },
|
---|
| 92 |
|
---|
| 93 | visitor: {
|
---|
| 94 | BinaryExpression(path) {
|
---|
| 95 | const {
|
---|
| 96 | node
|
---|
| 97 | } = path;
|
---|
| 98 | if (node.operator !== "in") return;
|
---|
| 99 | if (!t.isPrivateName(node.left)) return;
|
---|
| 100 | const {
|
---|
| 101 | name
|
---|
| 102 | } = node.left.id;
|
---|
| 103 | let privateElement;
|
---|
| 104 | const outerClass = path.findParent(path => {
|
---|
| 105 | if (!path.isClass()) return false;
|
---|
| 106 | privateElement = path.get("body.body").find(({
|
---|
| 107 | node
|
---|
| 108 | }) => t.isPrivate(node) && node.key.id.name === name);
|
---|
| 109 | return !!privateElement;
|
---|
| 110 | });
|
---|
| 111 |
|
---|
| 112 | if (outerClass.parentPath.scope.path.isPattern()) {
|
---|
| 113 | outerClass.replaceWith(template.ast`(() => ${outerClass.node})()`);
|
---|
| 114 | return;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | if (privateElement.isMethod()) {
|
---|
| 118 | if (privateElement.node.static) {
|
---|
| 119 | if (outerClass.node.id) {
|
---|
| 120 | unshadow(outerClass.node.id.name, outerClass.scope, path.scope);
|
---|
| 121 | } else {
|
---|
| 122 | outerClass.set("id", path.scope.generateUidIdentifier("class"));
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | path.replaceWith(template.expression.ast`
|
---|
| 126 | ${t.cloneNode(outerClass.node.id)} === ${path.node.right}
|
---|
| 127 | `);
|
---|
| 128 | } else {
|
---|
| 129 | var _outerClass$node$id;
|
---|
| 130 |
|
---|
| 131 | const id = getWeakSetId(classWeakSets, outerClass, outerClass, (_outerClass$node$id = outerClass.node.id) == null ? void 0 : _outerClass$node$id.name, injectInitialization);
|
---|
| 132 | path.replaceWith(template.expression.ast`${id}.has(${path.node.right})`);
|
---|
| 133 | }
|
---|
| 134 | } else {
|
---|
| 135 | const id = getWeakSetId(fieldsWeakSets, outerClass, privateElement, privateElement.node.key.id.name, injectToFieldInit);
|
---|
| 136 | path.replaceWith(template.expression.ast`${id}.has(${path.node.right})`);
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | }
|
---|
| 141 | };
|
---|
| 142 | });
|
---|
| 143 |
|
---|
| 144 | exports.default = _default; |
---|