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