source: trip-planner-front/node_modules/@babel/helper-create-class-features-plugin/lib/index.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 7.8 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createClassFeaturePlugin = createClassFeaturePlugin;
7Object.defineProperty(exports, "injectInitialization", {
8 enumerable: true,
9 get: function () {
10 return _misc.injectInitialization;
11 }
12});
13Object.defineProperty(exports, "enableFeature", {
14 enumerable: true,
15 get: function () {
16 return _features.enableFeature;
17 }
18});
19Object.defineProperty(exports, "FEATURES", {
20 enumerable: true,
21 get: function () {
22 return _features.FEATURES;
23 }
24});
25
26var _core = require("@babel/core");
27
28var _helperFunctionName = require("@babel/helper-function-name");
29
30var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
31
32var _fields = require("./fields");
33
34var _decorators = require("./decorators");
35
36var _misc = require("./misc");
37
38var _features = require("./features");
39
40const version = "7.15.4".split(".").reduce((v, x) => v * 1e5 + +x, 0);
41const versionKey = "@babel/plugin-class-features/version";
42
43function createClassFeaturePlugin({
44 name,
45 feature,
46 loose,
47 manipulateOptions,
48 api = {
49 assumption: () => void 0
50 }
51}) {
52 const setPublicClassFields = api.assumption("setPublicClassFields");
53 const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");
54 const constantSuper = api.assumption("constantSuper");
55 const noDocumentAll = api.assumption("noDocumentAll");
56
57 if (loose === true) {
58 const explicit = [];
59
60 if (setPublicClassFields !== undefined) {
61 explicit.push(`"setPublicClassFields"`);
62 }
63
64 if (privateFieldsAsProperties !== undefined) {
65 explicit.push(`"privateFieldsAsProperties"`);
66 }
67
68 if (explicit.length !== 0) {
69 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"privateFieldsAsProperties": true\n` + `\t}`);
70 }
71 }
72
73 return {
74 name,
75 manipulateOptions,
76
77 pre() {
78 (0, _features.enableFeature)(this.file, feature, loose);
79
80 if (!this.file.get(versionKey) || this.file.get(versionKey) < version) {
81 this.file.set(versionKey, version);
82 }
83 },
84
85 visitor: {
86 Class(path, state) {
87 if (this.file.get(versionKey) !== version) return;
88 (0, _features.verifyUsedFeatures)(path, this.file);
89 const loose = (0, _features.isLoose)(this.file, feature);
90 let constructor;
91 const isDecorated = (0, _decorators.hasDecorators)(path.node);
92 const props = [];
93 const elements = [];
94 const computedPaths = [];
95 const privateNames = new Set();
96 const body = path.get("body");
97
98 for (const path of body.get("body")) {
99 (0, _features.verifyUsedFeatures)(path, this.file);
100
101 if ((path.isClassProperty() || path.isClassMethod()) && path.node.computed) {
102 computedPaths.push(path);
103 }
104
105 if (path.isPrivate()) {
106 const {
107 name
108 } = path.node.key.id;
109 const getName = `get ${name}`;
110 const setName = `set ${name}`;
111
112 if (path.isClassPrivateMethod()) {
113 if (path.node.kind === "get") {
114 if (privateNames.has(getName) || privateNames.has(name) && !privateNames.has(setName)) {
115 throw path.buildCodeFrameError("Duplicate private field");
116 }
117
118 privateNames.add(getName).add(name);
119 } else if (path.node.kind === "set") {
120 if (privateNames.has(setName) || privateNames.has(name) && !privateNames.has(getName)) {
121 throw path.buildCodeFrameError("Duplicate private field");
122 }
123
124 privateNames.add(setName).add(name);
125 }
126 } else {
127 if (privateNames.has(name) && !privateNames.has(getName) && !privateNames.has(setName) || privateNames.has(name) && (privateNames.has(getName) || privateNames.has(setName))) {
128 throw path.buildCodeFrameError("Duplicate private field");
129 }
130
131 privateNames.add(name);
132 }
133 }
134
135 if (path.isClassMethod({
136 kind: "constructor"
137 })) {
138 constructor = path;
139 } else {
140 elements.push(path);
141
142 if (path.isProperty() || path.isPrivate() || path.isStaticBlock != null && path.isStaticBlock()) {
143 props.push(path);
144 }
145 }
146 }
147
148 if (!props.length && !isDecorated) return;
149 const innerBinding = path.node.id;
150 let ref;
151
152 if (!innerBinding || path.isClassExpression()) {
153 (0, _helperFunctionName.default)(path);
154 ref = path.scope.generateUidIdentifier("class");
155 } else {
156 ref = _core.types.cloneNode(path.node.id);
157 }
158
159 const privateNamesMap = (0, _fields.buildPrivateNamesMap)(props);
160 const privateNamesNodes = (0, _fields.buildPrivateNamesNodes)(privateNamesMap, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, state);
161 (0, _fields.transformPrivateNamesUsage)(ref, path, privateNamesMap, {
162 privateFieldsAsProperties: privateFieldsAsProperties != null ? privateFieldsAsProperties : loose,
163 noDocumentAll,
164 innerBinding
165 }, state);
166 let keysNodes, staticNodes, instanceNodes, pureStaticNodes, wrapClass;
167
168 if (isDecorated) {
169 staticNodes = pureStaticNodes = keysNodes = [];
170 ({
171 instanceNodes,
172 wrapClass
173 } = (0, _decorators.buildDecoratedClass)(ref, path, elements, this.file));
174 } else {
175 keysNodes = (0, _misc.extractComputedKeys)(ref, path, computedPaths, this.file);
176 ({
177 staticNodes,
178 pureStaticNodes,
179 instanceNodes,
180 wrapClass
181 } = (0, _fields.buildFieldsInitNodes)(ref, path.node.superClass, props, privateNamesMap, state, setPublicClassFields != null ? setPublicClassFields : loose, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, constantSuper != null ? constantSuper : loose, innerBinding));
182 }
183
184 if (instanceNodes.length > 0) {
185 (0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => {
186 if (isDecorated) return;
187
188 for (const prop of props) {
189 if (prop.node.static) continue;
190 prop.traverse(referenceVisitor, state);
191 }
192 });
193 }
194
195 const wrappedPath = wrapClass(path);
196 wrappedPath.insertBefore([...privateNamesNodes, ...keysNodes]);
197
198 if (staticNodes.length > 0) {
199 wrappedPath.insertAfter(staticNodes);
200 }
201
202 if (pureStaticNodes.length > 0) {
203 wrappedPath.find(parent => parent.isStatement() || parent.isDeclaration()).insertAfter(pureStaticNodes);
204 }
205 },
206
207 PrivateName(path) {
208 if (this.file.get(versionKey) !== version || path.parentPath.isPrivate({
209 key: path.node
210 })) {
211 return;
212 }
213
214 throw path.buildCodeFrameError(`Unknown PrivateName "${path}"`);
215 },
216
217 ExportDefaultDeclaration(path) {
218 if (this.file.get(versionKey) !== version) return;
219 const decl = path.get("declaration");
220
221 if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) {
222 if (decl.node.id) {
223 (0, _helperSplitExportDeclaration.default)(path);
224 } else {
225 decl.node.type = "ClassExpression";
226 }
227 }
228 }
229
230 }
231 };
232}
Note: See TracBrowser for help on using the repository browser.