source: imaps-frontend/node_modules/@babel/plugin-transform-private-property-in-object/lib/index.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.6 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7var _helperPluginUtils = require("@babel/helper-plugin-utils");
8var _helperCreateClassFeaturesPlugin = require("@babel/helper-create-class-features-plugin");
9var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
10var _default = exports.default = (0, _helperPluginUtils.declare)((api, opt) => {
11 api.assertVersion("^7.0.0-0 || >8.0.0-alpha <8.0.0-beta");
12 const {
13 types: t,
14 template
15 } = api;
16 const {
17 loose
18 } = opt;
19 const classWeakSets = new WeakMap();
20 const fieldsWeakSets = new WeakMap();
21 function unshadow(name, targetScope, scope) {
22 while (scope !== targetScope) {
23 if (scope.hasOwnBinding(name)) scope.rename(name);
24 scope = scope.parent;
25 }
26 }
27 function injectToFieldInit(fieldPath, expr, before = false) {
28 if (fieldPath.node.value) {
29 const value = fieldPath.get("value");
30 if (before) {
31 value.insertBefore(expr);
32 } else {
33 value.insertAfter(expr);
34 }
35 } else {
36 fieldPath.set("value", t.unaryExpression("void", expr));
37 }
38 }
39 function injectInitialization(classPath, init) {
40 let firstFieldPath;
41 let constructorPath;
42 for (const el of classPath.get("body.body")) {
43 if ((el.isClassProperty() || el.isClassPrivateProperty()) && !el.node.static) {
44 firstFieldPath = el;
45 break;
46 }
47 if (!constructorPath && el.isClassMethod({
48 kind: "constructor"
49 })) {
50 constructorPath = el;
51 }
52 }
53 if (firstFieldPath) {
54 injectToFieldInit(firstFieldPath, init, true);
55 } else {
56 (0, _helperCreateClassFeaturesPlugin.injectInitialization)(classPath, constructorPath, [t.expressionStatement(init)]);
57 }
58 }
59 function getWeakSetId(weakSets, outerClass, reference, name = "", inject) {
60 let id = weakSets.get(reference.node);
61 if (!id) {
62 id = outerClass.scope.generateUidIdentifier(`${name || ""} brandCheck`);
63 weakSets.set(reference.node, id);
64 inject(reference, template.expression.ast`${t.cloneNode(id)}.add(this)`);
65 const newExpr = t.newExpression(t.identifier("WeakSet"), []);
66 (0, _helperAnnotateAsPure.default)(newExpr);
67 outerClass.insertBefore(template.ast`var ${id} = ${newExpr}`);
68 }
69 return t.cloneNode(id);
70 }
71 return {
72 name: "transform-private-property-in-object",
73 manipulateOptions: (_, parser) => parser.plugins.push("privateIn"),
74 pre() {
75 (0, _helperCreateClassFeaturesPlugin.enableFeature)(this.file, _helperCreateClassFeaturesPlugin.FEATURES.privateIn, loose);
76 },
77 visitor: {
78 BinaryExpression(path, state) {
79 const {
80 node
81 } = path;
82 const {
83 file
84 } = state;
85 if (node.operator !== "in") return;
86 if (!t.isPrivateName(node.left)) return;
87 const {
88 name
89 } = node.left.id;
90 let privateElement;
91 const outerClass = path.findParent(path => {
92 if (!path.isClass()) return false;
93 privateElement = path.get("body.body").find(({
94 node
95 }) => t.isPrivate(node) && node.key.id.name === name);
96 return !!privateElement;
97 });
98 if (outerClass.parentPath.scope.path.isPattern()) {
99 outerClass.replaceWith(template.ast`(() => ${outerClass.node})()`);
100 return;
101 }
102 if (privateElement.node.type === "ClassPrivateMethod") {
103 if (privateElement.node.static) {
104 if (outerClass.node.id) {
105 unshadow(outerClass.node.id.name, outerClass.scope, path.scope);
106 } else {
107 outerClass.set("id", path.scope.generateUidIdentifier("class"));
108 }
109 path.replaceWith(template.expression.ast`
110 ${t.cloneNode(outerClass.node.id)} === ${(0, _helperCreateClassFeaturesPlugin.buildCheckInRHS)(node.right, file)}
111 `);
112 } else {
113 var _outerClass$node$id;
114 const id = getWeakSetId(classWeakSets, outerClass, outerClass, (_outerClass$node$id = outerClass.node.id) == null ? void 0 : _outerClass$node$id.name, injectInitialization);
115 path.replaceWith(template.expression.ast`${id}.has(${(0, _helperCreateClassFeaturesPlugin.buildCheckInRHS)(node.right, file)})`);
116 }
117 } else {
118 const id = getWeakSetId(fieldsWeakSets, outerClass, privateElement, privateElement.node.key.id.name, injectToFieldInit);
119 path.replaceWith(template.expression.ast`${id}.has(${(0, _helperCreateClassFeaturesPlugin.buildCheckInRHS)(node.right, file)})`);
120 }
121 }
122 }
123 };
124});
125
126//# sourceMappingURL=index.js.map
Note: See TracBrowser for help on using the repository browser.