UNPKG

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