@babel/plugin-proposal-decorators 7.19.6 → 7.20.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +1 -8
- package/lib/index.js.map +1 -1
- package/lib/transformer-2022-03.js +15 -113
- package/lib/transformer-2022-03.js.map +1 -1
- package/lib/transformer-legacy.js +18 -30
- package/lib/transformer-legacy.js.map +1 -1
- package/package.json +5 -5
- package/CONTRIB.md +0 -396
package/lib/index.js
CHANGED
|
@@ -4,19 +4,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
|
9
|
-
|
|
10
8
|
var _pluginSyntaxDecorators = require("@babel/plugin-syntax-decorators");
|
|
11
|
-
|
|
12
9
|
var _helperCreateClassFeaturesPlugin = require("@babel/helper-create-class-features-plugin");
|
|
13
|
-
|
|
14
10
|
var _transformerLegacy = require("./transformer-legacy");
|
|
15
|
-
|
|
16
11
|
var _transformer = require("./transformer-2022-03");
|
|
17
|
-
|
|
18
12
|
var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
|
19
13
|
api.assertVersion(7);
|
|
14
|
+
|
|
20
15
|
{
|
|
21
16
|
var {
|
|
22
17
|
legacy
|
|
@@ -25,7 +20,6 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
|
|
25
20
|
const {
|
|
26
21
|
version
|
|
27
22
|
} = options;
|
|
28
|
-
|
|
29
23
|
if (legacy || version === "legacy") {
|
|
30
24
|
return {
|
|
31
25
|
name: "proposal-decorators",
|
|
@@ -43,7 +37,6 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
|
|
43
37
|
});
|
|
44
38
|
}
|
|
45
39
|
});
|
|
46
|
-
|
|
47
40
|
exports.default = _default;
|
|
48
41
|
|
|
49
42
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["declare","api","options","assertVersion","legacy","version","name","inherits","syntaxDecorators","visitor","legacyVisitor","transformer2022_03","createClassFeaturePlugin","feature","FEATURES","decorators"],"sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @babel/development/plugin-name */\n\nimport { declare } from \"@babel/helper-plugin-utils\";\nimport syntaxDecorators from \"@babel/plugin-syntax-decorators\";\nimport {\n createClassFeaturePlugin,\n FEATURES,\n} from \"@babel/helper-create-class-features-plugin\";\nimport legacyVisitor from \"./transformer-legacy\";\nimport transformer2022_03 from \"./transformer-2022-03\";\nimport type { Options as SyntaxOptions } from \"@babel/plugin-syntax-decorators\";\n\ninterface Options extends SyntaxOptions {\n /** @depreated use `constantSuper` assumption instead. Only supported in 2021-12 version. */\n loose?: boolean;\n}\n\nexport type { Options };\n\nexport default declare((api, options: Options) => {\n api.assertVersion(7);\n\n // Options are validated in @babel/plugin-syntax-decorators\n if (!process.env.BABEL_8_BREAKING) {\n // eslint-disable-next-line no-var\n var { legacy } = options;\n }\n const { version } = options;\n\n if (\n process.env.BABEL_8_BREAKING\n ? version === \"legacy\"\n : legacy || version === \"legacy\"\n ) {\n return {\n name: \"proposal-decorators\",\n inherits: syntaxDecorators,\n visitor: legacyVisitor,\n };\n } else if (version === \"2021-12\" || version === \"2022-03\") {\n return transformer2022_03(api, options, version);\n } else if (!process.env.BABEL_8_BREAKING) {\n return createClassFeaturePlugin({\n name: \"proposal-decorators\",\n\n api,\n feature: FEATURES.decorators,\n inherits: syntaxDecorators,\n // loose: options.loose, Not supported\n });\n } else {\n throw new Error(\n \"The '.version' option must be one of 'legacy', '2021-12' or '2022-03'.\",\n );\n }\n});\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["declare","api","options","assertVersion","legacy","version","name","inherits","syntaxDecorators","visitor","legacyVisitor","transformer2022_03","createClassFeaturePlugin","feature","FEATURES","decorators"],"sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @babel/development/plugin-name */\n\nimport { declare } from \"@babel/helper-plugin-utils\";\nimport syntaxDecorators from \"@babel/plugin-syntax-decorators\";\nimport {\n createClassFeaturePlugin,\n FEATURES,\n} from \"@babel/helper-create-class-features-plugin\";\nimport legacyVisitor from \"./transformer-legacy\";\nimport transformer2022_03 from \"./transformer-2022-03\";\nimport type { Options as SyntaxOptions } from \"@babel/plugin-syntax-decorators\";\n\ninterface Options extends SyntaxOptions {\n /** @depreated use `constantSuper` assumption instead. Only supported in 2021-12 version. */\n loose?: boolean;\n}\n\nexport type { Options };\n\nexport default declare((api, options: Options) => {\n api.assertVersion(7);\n\n // Options are validated in @babel/plugin-syntax-decorators\n if (!process.env.BABEL_8_BREAKING) {\n // eslint-disable-next-line no-var\n var { legacy } = options;\n }\n const { version } = options;\n\n if (\n process.env.BABEL_8_BREAKING\n ? version === \"legacy\"\n : legacy || version === \"legacy\"\n ) {\n return {\n name: \"proposal-decorators\",\n inherits: syntaxDecorators,\n visitor: legacyVisitor,\n };\n } else if (version === \"2021-12\" || version === \"2022-03\") {\n return transformer2022_03(api, options, version);\n } else if (!process.env.BABEL_8_BREAKING) {\n return createClassFeaturePlugin({\n name: \"proposal-decorators\",\n\n api,\n feature: FEATURES.decorators,\n inherits: syntaxDecorators,\n // loose: options.loose, Not supported\n });\n } else {\n throw new Error(\n \"The '.version' option must be one of 'legacy', '2021-12' or '2022-03'.\",\n );\n }\n});\n"],"mappings":";;;;;;AAEA;AACA;AACA;AAIA;AACA;AAAuD,eAUxC,IAAAA,0BAAO,EAAC,CAACC,GAAG,EAAEC,OAAgB,KAAK;EAChDD,GAAG,CAACE,aAAa,CAAC,CAAC,CAAC;;EAGe;IAEjC,IAAI;MAAEC;IAAO,CAAC,GAAGF,OAAO;EAC1B;EACA,MAAM;IAAEG;EAAQ,CAAC,GAAGH,OAAO;EAE3B,IAGME,MAAM,IAAIC,OAAO,KAAK,QAAQ,EAClC;IACA,OAAO;MACLC,IAAI,EAAE,qBAAqB;MAC3BC,QAAQ,EAAEC,+BAAgB;MAC1BC,OAAO,EAAEC;IACX,CAAC;EACH,CAAC,MAAM,IAAIL,OAAO,KAAK,SAAS,IAAIA,OAAO,KAAK,SAAS,EAAE;IACzD,OAAO,IAAAM,oBAAkB,EAACV,GAAG,EAAEC,OAAO,EAAEG,OAAO,CAAC;EAClD,CAAC,MAAyC;IACxC,OAAO,IAAAO,yDAAwB,EAAC;MAC9BN,IAAI,EAAE,qBAAqB;MAE3BL,GAAG;MACHY,OAAO,EAAEC,yCAAQ,CAACC,UAAU;MAC5BR,QAAQ,EAAEC;IAEZ,CAAC,CAAC;EACJ;AAKF,CAAC,CAAC;AAAA"}
|
|
@@ -4,23 +4,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = _default;
|
|
7
|
-
|
|
8
7
|
var _core = require("@babel/core");
|
|
9
|
-
|
|
10
8
|
var _pluginSyntaxDecorators = require("@babel/plugin-syntax-decorators");
|
|
11
|
-
|
|
12
9
|
var _helperReplaceSupers = require("@babel/helper-replace-supers");
|
|
13
|
-
|
|
14
10
|
var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
|
|
15
|
-
|
|
16
11
|
function incrementId(id, idx = id.length - 1) {
|
|
17
12
|
if (idx === -1) {
|
|
18
13
|
id.unshift(65);
|
|
19
14
|
return;
|
|
20
15
|
}
|
|
21
|
-
|
|
22
16
|
const current = id[idx];
|
|
23
|
-
|
|
24
17
|
if (current === 90) {
|
|
25
18
|
id[idx] = 97;
|
|
26
19
|
} else if (current === 122) {
|
|
@@ -38,16 +31,13 @@ function createPrivateUidGeneratorForClass(classPath) {
|
|
|
38
31
|
PrivateName(path) {
|
|
39
32
|
privateNames.add(path.node.id.name);
|
|
40
33
|
}
|
|
41
|
-
|
|
42
34
|
});
|
|
43
35
|
return () => {
|
|
44
36
|
let reifiedId;
|
|
45
|
-
|
|
46
37
|
do {
|
|
47
38
|
incrementId(currentPrivateId);
|
|
48
39
|
reifiedId = String.fromCharCode(...currentPrivateId);
|
|
49
40
|
} while (privateNames.has(reifiedId));
|
|
50
|
-
|
|
51
41
|
return _core.types.privateName(_core.types.identifier(reifiedId));
|
|
52
42
|
};
|
|
53
43
|
}
|
|
@@ -58,7 +48,6 @@ function createLazyPrivateUidGeneratorForClass(classPath) {
|
|
|
58
48
|
if (!generator) {
|
|
59
49
|
generator = createPrivateUidGeneratorForClass(classPath);
|
|
60
50
|
}
|
|
61
|
-
|
|
62
51
|
return generator();
|
|
63
52
|
};
|
|
64
53
|
}
|
|
@@ -66,9 +55,7 @@ function createLazyPrivateUidGeneratorForClass(classPath) {
|
|
|
66
55
|
function replaceClassWithVar(path) {
|
|
67
56
|
if (path.type === "ClassDeclaration") {
|
|
68
57
|
const varId = path.scope.generateUidIdentifierBasedOnNode(path.node.id);
|
|
69
|
-
|
|
70
58
|
const classId = _core.types.identifier(path.node.id.name);
|
|
71
|
-
|
|
72
59
|
path.scope.rename(classId.name, varId.name);
|
|
73
60
|
path.insertBefore(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(varId)]));
|
|
74
61
|
path.get("id").replaceWith(classId);
|
|
@@ -76,7 +63,6 @@ function replaceClassWithVar(path) {
|
|
|
76
63
|
} else {
|
|
77
64
|
let className;
|
|
78
65
|
let varId;
|
|
79
|
-
|
|
80
66
|
if (path.node.id) {
|
|
81
67
|
className = path.node.id.name;
|
|
82
68
|
varId = path.scope.parent.generateDeclaredUidIdentifier(className);
|
|
@@ -87,14 +73,11 @@ function replaceClassWithVar(path) {
|
|
|
87
73
|
} else {
|
|
88
74
|
varId = path.scope.parent.generateDeclaredUidIdentifier("decorated_class");
|
|
89
75
|
}
|
|
90
|
-
|
|
91
76
|
const newClassExpr = _core.types.classExpression(className && _core.types.identifier(className), path.node.superClass, path.node.body);
|
|
92
|
-
|
|
93
77
|
const [newPath] = path.replaceWith(_core.types.sequenceExpression([newClassExpr, varId]));
|
|
94
78
|
return [_core.types.cloneNode(varId), newPath.get("expressions.0")];
|
|
95
79
|
}
|
|
96
80
|
}
|
|
97
|
-
|
|
98
81
|
function generateClassProperty(key, value, isStatic) {
|
|
99
82
|
if (key.type === "PrivateName") {
|
|
100
83
|
return _core.types.classPrivateProperty(key, value, undefined, isStatic);
|
|
@@ -102,18 +85,13 @@ function generateClassProperty(key, value, isStatic) {
|
|
|
102
85
|
return _core.types.classProperty(key, value, undefined, undefined, isStatic);
|
|
103
86
|
}
|
|
104
87
|
}
|
|
105
|
-
|
|
106
88
|
function addProxyAccessorsFor(element, originalKey, targetKey, isComputed = false) {
|
|
107
89
|
const {
|
|
108
90
|
static: isStatic
|
|
109
91
|
} = element.node;
|
|
110
|
-
|
|
111
92
|
const getterBody = _core.types.blockStatement([_core.types.returnStatement(_core.types.memberExpression(_core.types.thisExpression(), _core.types.cloneNode(targetKey)))]);
|
|
112
|
-
|
|
113
93
|
const setterBody = _core.types.blockStatement([_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.thisExpression(), _core.types.cloneNode(targetKey)), _core.types.identifier("v")))]);
|
|
114
|
-
|
|
115
94
|
let getter, setter;
|
|
116
|
-
|
|
117
95
|
if (originalKey.type === "PrivateName") {
|
|
118
96
|
getter = _core.types.classPrivateMethod("get", _core.types.cloneNode(originalKey), [], getterBody, isStatic);
|
|
119
97
|
setter = _core.types.classPrivateMethod("set", _core.types.cloneNode(originalKey), [_core.types.identifier("v")], setterBody, isStatic);
|
|
@@ -121,31 +99,25 @@ function addProxyAccessorsFor(element, originalKey, targetKey, isComputed = fals
|
|
|
121
99
|
getter = _core.types.classMethod("get", _core.types.cloneNode(originalKey), [], getterBody, isComputed, isStatic);
|
|
122
100
|
setter = _core.types.classMethod("set", _core.types.cloneNode(originalKey), [_core.types.identifier("v")], setterBody, isComputed, isStatic);
|
|
123
101
|
}
|
|
124
|
-
|
|
125
102
|
element.insertAfter(setter);
|
|
126
103
|
element.insertAfter(getter);
|
|
127
104
|
}
|
|
128
|
-
|
|
129
105
|
function extractProxyAccessorsFor(targetKey) {
|
|
130
106
|
return [_core.types.functionExpression(undefined, [], _core.types.blockStatement([_core.types.returnStatement(_core.types.memberExpression(_core.types.thisExpression(), _core.types.cloneNode(targetKey)))])), _core.types.functionExpression(undefined, [_core.types.identifier("value")], _core.types.blockStatement([_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.thisExpression(), _core.types.cloneNode(targetKey)), _core.types.identifier("value")))]))];
|
|
131
107
|
}
|
|
132
|
-
|
|
133
108
|
const FIELD = 0;
|
|
134
109
|
const ACCESSOR = 1;
|
|
135
110
|
const METHOD = 2;
|
|
136
111
|
const GETTER = 3;
|
|
137
112
|
const SETTER = 4;
|
|
138
113
|
const STATIC = 5;
|
|
139
|
-
|
|
140
114
|
function getElementKind(element) {
|
|
141
115
|
switch (element.node.type) {
|
|
142
116
|
case "ClassProperty":
|
|
143
117
|
case "ClassPrivateProperty":
|
|
144
118
|
return FIELD;
|
|
145
|
-
|
|
146
119
|
case "ClassAccessorProperty":
|
|
147
120
|
return ACCESSOR;
|
|
148
|
-
|
|
149
121
|
case "ClassMethod":
|
|
150
122
|
case "ClassPrivateMethod":
|
|
151
123
|
if (element.node.kind === "get") {
|
|
@@ -155,19 +127,16 @@ function getElementKind(element) {
|
|
|
155
127
|
} else {
|
|
156
128
|
return METHOD;
|
|
157
129
|
}
|
|
158
|
-
|
|
159
130
|
}
|
|
160
131
|
}
|
|
161
132
|
|
|
162
133
|
function isDecoratorInfo(info) {
|
|
163
134
|
return "decorators" in info;
|
|
164
135
|
}
|
|
165
|
-
|
|
166
136
|
function filteredOrderedDecoratorInfo(info) {
|
|
167
137
|
const filtered = info.filter(isDecoratorInfo);
|
|
168
138
|
return [...filtered.filter(el => el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER), ...filtered.filter(el => !el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER), ...filtered.filter(el => el.isStatic && el.kind === FIELD), ...filtered.filter(el => !el.isStatic && el.kind === FIELD)];
|
|
169
139
|
}
|
|
170
|
-
|
|
171
140
|
function generateDecorationExprs(info) {
|
|
172
141
|
return _core.types.arrayExpression(filteredOrderedDecoratorInfo(info).map(el => {
|
|
173
142
|
const decs = el.decorators.length > 1 ? _core.types.arrayExpression(el.decorators) : el.decorators[0];
|
|
@@ -176,48 +145,38 @@ function generateDecorationExprs(info) {
|
|
|
176
145
|
const {
|
|
177
146
|
privateMethods
|
|
178
147
|
} = el;
|
|
179
|
-
|
|
180
148
|
if (Array.isArray(privateMethods)) {
|
|
181
149
|
decInfo.push(...privateMethods);
|
|
182
150
|
} else if (privateMethods) {
|
|
183
151
|
decInfo.push(privateMethods);
|
|
184
152
|
}
|
|
185
|
-
|
|
186
153
|
return _core.types.arrayExpression(decInfo);
|
|
187
154
|
}));
|
|
188
155
|
}
|
|
189
|
-
|
|
190
156
|
function extractElementLocalAssignments(decorationInfo) {
|
|
191
157
|
const localIds = [];
|
|
192
|
-
|
|
193
158
|
for (const el of filteredOrderedDecoratorInfo(decorationInfo)) {
|
|
194
159
|
const {
|
|
195
160
|
locals
|
|
196
161
|
} = el;
|
|
197
|
-
|
|
198
162
|
if (Array.isArray(locals)) {
|
|
199
163
|
localIds.push(...locals);
|
|
200
164
|
} else if (locals !== undefined) {
|
|
201
165
|
localIds.push(locals);
|
|
202
166
|
}
|
|
203
167
|
}
|
|
204
|
-
|
|
205
168
|
return localIds;
|
|
206
169
|
}
|
|
207
|
-
|
|
208
170
|
function addCallAccessorsFor(element, key, getId, setId) {
|
|
209
171
|
element.insertAfter(_core.types.classPrivateMethod("get", _core.types.cloneNode(key), [], _core.types.blockStatement([_core.types.returnStatement(_core.types.callExpression(_core.types.cloneNode(getId), [_core.types.thisExpression()]))])));
|
|
210
172
|
element.insertAfter(_core.types.classPrivateMethod("set", _core.types.cloneNode(key), [_core.types.identifier("v")], _core.types.blockStatement([_core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(setId), [_core.types.thisExpression(), _core.types.identifier("v")]))])));
|
|
211
173
|
}
|
|
212
|
-
|
|
213
174
|
function isNotTsParameter(node) {
|
|
214
175
|
return node.type !== "TSParameterProperty";
|
|
215
176
|
}
|
|
216
|
-
|
|
217
177
|
function movePrivateAccessor(element, key, methodLocalVar, isStatic) {
|
|
218
178
|
let params;
|
|
219
179
|
let block;
|
|
220
|
-
|
|
221
180
|
if (element.node.kind === "set") {
|
|
222
181
|
params = [_core.types.identifier("v")];
|
|
223
182
|
block = [_core.types.expressionStatement(_core.types.callExpression(methodLocalVar, [_core.types.thisExpression(), _core.types.identifier("v")]))];
|
|
@@ -225,27 +184,22 @@ function movePrivateAccessor(element, key, methodLocalVar, isStatic) {
|
|
|
225
184
|
params = [];
|
|
226
185
|
block = [_core.types.returnStatement(_core.types.callExpression(methodLocalVar, [_core.types.thisExpression()]))];
|
|
227
186
|
}
|
|
228
|
-
|
|
229
187
|
element.replaceWith(_core.types.classPrivateMethod(element.node.kind, _core.types.cloneNode(key), params, _core.types.blockStatement(block), isStatic));
|
|
230
188
|
}
|
|
231
|
-
|
|
232
189
|
function isClassDecoratableElementPath(path) {
|
|
233
190
|
const {
|
|
234
191
|
type
|
|
235
192
|
} = path;
|
|
236
193
|
return type !== "TSDeclareMethod" && type !== "TSIndexSignature" && type !== "StaticBlock";
|
|
237
194
|
}
|
|
238
|
-
|
|
239
195
|
function staticBlockToIIFE(block) {
|
|
240
196
|
return _core.types.callExpression(_core.types.arrowFunctionExpression([], _core.types.blockStatement(block.body)), []);
|
|
241
197
|
}
|
|
242
|
-
|
|
243
198
|
function maybeSequenceExpression(exprs) {
|
|
244
199
|
if (exprs.length === 0) return _core.types.unaryExpression("void", _core.types.numericLiteral(0));
|
|
245
200
|
if (exprs.length === 1) return exprs[0];
|
|
246
201
|
return _core.types.sequenceExpression(exprs);
|
|
247
202
|
}
|
|
248
|
-
|
|
249
203
|
function transformClass(path, state, constantSuper, version) {
|
|
250
204
|
const body = path.get("body.body");
|
|
251
205
|
const classDecorators = path.node.decorators;
|
|
@@ -256,7 +210,6 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
256
210
|
if (!isClassDecoratableElementPath(element)) {
|
|
257
211
|
continue;
|
|
258
212
|
}
|
|
259
|
-
|
|
260
213
|
if (element.node.decorators && element.node.decorators.length > 0) {
|
|
261
214
|
hasElementDecorators = true;
|
|
262
215
|
} else if (element.node.type === "ClassAccessorProperty") {
|
|
@@ -276,6 +229,7 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
276
229
|
|
|
277
230
|
if (!classDecorators && !hasElementDecorators) return;
|
|
278
231
|
const elementDecoratorInfo = [];
|
|
232
|
+
|
|
279
233
|
let firstFieldPath;
|
|
280
234
|
let constructorPath;
|
|
281
235
|
let requiresProtoInit = false;
|
|
@@ -284,20 +238,17 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
284
238
|
let protoInitLocal, staticInitLocal, classInitLocal, classLocal;
|
|
285
239
|
const assignments = [];
|
|
286
240
|
const scopeParent = path.scope.parent;
|
|
287
|
-
|
|
288
241
|
const memoiseExpression = (expression, hint) => {
|
|
289
242
|
const localEvaluatedId = scopeParent.generateDeclaredUidIdentifier(hint);
|
|
290
243
|
assignments.push(_core.types.assignmentExpression("=", localEvaluatedId, expression));
|
|
291
244
|
return _core.types.cloneNode(localEvaluatedId);
|
|
292
245
|
};
|
|
293
|
-
|
|
294
246
|
if (classDecorators) {
|
|
295
247
|
classInitLocal = scopeParent.generateDeclaredUidIdentifier("initClass");
|
|
296
248
|
const [localId, classPath] = replaceClassWithVar(path);
|
|
297
249
|
path = classPath;
|
|
298
250
|
classLocal = localId;
|
|
299
251
|
path.node.decorators = null;
|
|
300
|
-
|
|
301
252
|
for (const classDecorator of classDecorators) {
|
|
302
253
|
if (!scopeParent.isStatic(classDecorator.expression)) {
|
|
303
254
|
classDecorator.expression = memoiseExpression(classDecorator.expression, "dec");
|
|
@@ -307,22 +258,18 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
307
258
|
if (!path.node.id) {
|
|
308
259
|
path.node.id = path.scope.generateUidIdentifier("Class");
|
|
309
260
|
}
|
|
310
|
-
|
|
311
261
|
classLocal = _core.types.cloneNode(path.node.id);
|
|
312
262
|
}
|
|
313
|
-
|
|
314
263
|
if (hasElementDecorators) {
|
|
315
264
|
for (const element of body) {
|
|
316
265
|
if (!isClassDecoratableElementPath(element)) {
|
|
317
266
|
continue;
|
|
318
267
|
}
|
|
319
|
-
|
|
320
268
|
const {
|
|
321
269
|
node
|
|
322
270
|
} = element;
|
|
323
271
|
const decorators = element.get("decorators");
|
|
324
272
|
const hasDecorators = Array.isArray(decorators) && decorators.length > 0;
|
|
325
|
-
|
|
326
273
|
if (hasDecorators) {
|
|
327
274
|
for (const decoratorPath of decorators) {
|
|
328
275
|
if (!scopeParent.isStatic(decoratorPath.node.expression)) {
|
|
@@ -330,15 +277,12 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
330
277
|
}
|
|
331
278
|
}
|
|
332
279
|
}
|
|
333
|
-
|
|
334
280
|
const isComputed = "computed" in element.node && element.node.computed === true;
|
|
335
|
-
|
|
336
281
|
if (isComputed) {
|
|
337
282
|
if (!scopeParent.isStatic(node.key)) {
|
|
338
283
|
node.key = memoiseExpression(node.key, "computedKey");
|
|
339
284
|
}
|
|
340
285
|
}
|
|
341
|
-
|
|
342
286
|
const kind = getElementKind(element);
|
|
343
287
|
const {
|
|
344
288
|
key
|
|
@@ -346,41 +290,32 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
346
290
|
const isPrivate = key.type === "PrivateName";
|
|
347
291
|
const isStatic = !!element.node.static;
|
|
348
292
|
let name = "computedKey";
|
|
349
|
-
|
|
350
293
|
if (isPrivate) {
|
|
351
294
|
name = key.id.name;
|
|
352
295
|
} else if (!isComputed && key.type === "Identifier") {
|
|
353
296
|
name = key.name;
|
|
354
297
|
}
|
|
355
|
-
|
|
356
298
|
if (element.isClassMethod({
|
|
357
299
|
kind: "constructor"
|
|
358
300
|
})) {
|
|
359
301
|
constructorPath = element;
|
|
360
302
|
}
|
|
361
|
-
|
|
362
303
|
if (hasDecorators) {
|
|
363
304
|
let locals;
|
|
364
305
|
let privateMethods;
|
|
365
|
-
|
|
366
306
|
if (kind === ACCESSOR) {
|
|
367
307
|
const {
|
|
368
308
|
value
|
|
369
309
|
} = element.node;
|
|
370
310
|
const params = [_core.types.thisExpression()];
|
|
371
|
-
|
|
372
311
|
if (value) {
|
|
373
312
|
params.push(_core.types.cloneNode(value));
|
|
374
313
|
}
|
|
375
|
-
|
|
376
314
|
const newId = generateClassPrivateUid();
|
|
377
315
|
const newFieldInitId = element.scope.parent.generateDeclaredUidIdentifier(`init_${name}`);
|
|
378
|
-
|
|
379
316
|
const newValue = _core.types.callExpression(_core.types.cloneNode(newFieldInitId), params);
|
|
380
|
-
|
|
381
317
|
const newField = generateClassProperty(newId, newValue, isStatic);
|
|
382
318
|
const [newPath] = element.replaceWith(newField);
|
|
383
|
-
|
|
384
319
|
if (isPrivate) {
|
|
385
320
|
privateMethods = extractProxyAccessorsFor(newId);
|
|
386
321
|
const getId = newPath.scope.parent.generateDeclaredUidIdentifier(`get_${name}`);
|
|
@@ -396,7 +331,6 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
396
331
|
const valuePath = element.get("value");
|
|
397
332
|
valuePath.replaceWith(_core.types.callExpression(_core.types.cloneNode(initId), [_core.types.thisExpression(), valuePath.node].filter(v => v)));
|
|
398
333
|
locals = initId;
|
|
399
|
-
|
|
400
334
|
if (isPrivate) {
|
|
401
335
|
privateMethods = extractProxyAccessorsFor(key);
|
|
402
336
|
}
|
|
@@ -417,19 +351,17 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
417
351
|
async: isAsync
|
|
418
352
|
} = element.node;
|
|
419
353
|
privateMethods = _core.types.functionExpression(undefined, params.filter(isNotTsParameter), body, isAsync);
|
|
420
|
-
|
|
421
354
|
if (kind === GETTER || kind === SETTER) {
|
|
422
355
|
movePrivateAccessor(element, _core.types.cloneNode(key), _core.types.cloneNode(locals), isStatic);
|
|
423
356
|
} else {
|
|
424
357
|
const node = element.node;
|
|
358
|
+
|
|
425
359
|
path.node.body.body.unshift(_core.types.classPrivateProperty(key, _core.types.cloneNode(locals), [], node.static));
|
|
426
360
|
decoratedPrivateMethods.add(key.id.name);
|
|
427
361
|
element.remove();
|
|
428
362
|
}
|
|
429
363
|
}
|
|
430
|
-
|
|
431
364
|
let nameExpr;
|
|
432
|
-
|
|
433
365
|
if (isComputed) {
|
|
434
366
|
nameExpr = _core.types.cloneNode(key);
|
|
435
367
|
} else if (key.type === "PrivateName") {
|
|
@@ -439,7 +371,6 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
439
371
|
} else {
|
|
440
372
|
nameExpr = _core.types.cloneNode(key);
|
|
441
373
|
}
|
|
442
|
-
|
|
443
374
|
elementDecoratorInfo.push({
|
|
444
375
|
kind,
|
|
445
376
|
decorators: decorators.map(d => d.node.expression),
|
|
@@ -448,7 +379,6 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
448
379
|
privateMethods,
|
|
449
380
|
locals
|
|
450
381
|
});
|
|
451
|
-
|
|
452
382
|
if (kind !== FIELD) {
|
|
453
383
|
if (isStatic) {
|
|
454
384
|
requiresStaticInit = true;
|
|
@@ -456,38 +386,28 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
456
386
|
requiresProtoInit = true;
|
|
457
387
|
}
|
|
458
388
|
}
|
|
459
|
-
|
|
460
389
|
if (element.node) {
|
|
461
390
|
element.node.decorators = null;
|
|
462
391
|
}
|
|
463
|
-
|
|
464
392
|
if (!firstFieldPath && !isStatic && (kind === FIELD || kind === ACCESSOR)) {
|
|
465
393
|
firstFieldPath = element;
|
|
466
394
|
}
|
|
467
395
|
}
|
|
468
396
|
}
|
|
469
397
|
}
|
|
470
|
-
|
|
471
398
|
const elementDecorations = generateDecorationExprs(elementDecoratorInfo);
|
|
472
|
-
|
|
473
399
|
const classDecorations = _core.types.arrayExpression((classDecorators || []).map(d => d.expression));
|
|
474
|
-
|
|
475
400
|
const locals = extractElementLocalAssignments(elementDecoratorInfo);
|
|
476
|
-
|
|
477
401
|
if (requiresProtoInit) {
|
|
478
402
|
protoInitLocal = scopeParent.generateDeclaredUidIdentifier("initProto");
|
|
479
403
|
locals.push(protoInitLocal);
|
|
480
|
-
|
|
481
404
|
const protoInitCall = _core.types.callExpression(_core.types.cloneNode(protoInitLocal), [_core.types.thisExpression()]);
|
|
482
|
-
|
|
483
405
|
if (firstFieldPath) {
|
|
484
406
|
const value = firstFieldPath.get("value");
|
|
485
407
|
const body = [protoInitCall];
|
|
486
|
-
|
|
487
408
|
if (value.node) {
|
|
488
409
|
body.push(value.node);
|
|
489
410
|
}
|
|
490
|
-
|
|
491
411
|
value.replaceWith(_core.types.sequenceExpression(body));
|
|
492
412
|
} else if (constructorPath) {
|
|
493
413
|
if (path.node.superClass) {
|
|
@@ -498,7 +418,6 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
498
418
|
path.replaceWith(_core.types.callExpression(_core.types.cloneNode(protoInitLocal), [path.node]));
|
|
499
419
|
path.skip();
|
|
500
420
|
}
|
|
501
|
-
|
|
502
421
|
}
|
|
503
422
|
});
|
|
504
423
|
} else {
|
|
@@ -506,41 +425,37 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
506
425
|
}
|
|
507
426
|
} else {
|
|
508
427
|
const body = [_core.types.expressionStatement(protoInitCall)];
|
|
509
|
-
|
|
510
428
|
if (path.node.superClass) {
|
|
511
429
|
body.unshift(_core.types.expressionStatement(_core.types.callExpression(_core.types.super(), [_core.types.spreadElement(_core.types.identifier("args"))])));
|
|
512
430
|
}
|
|
513
|
-
|
|
514
431
|
path.node.body.body.unshift(_core.types.classMethod("constructor", _core.types.identifier("constructor"), [_core.types.restElement(_core.types.identifier("args"))], _core.types.blockStatement(body)));
|
|
515
432
|
}
|
|
516
433
|
}
|
|
517
|
-
|
|
518
434
|
if (requiresStaticInit) {
|
|
519
435
|
staticInitLocal = scopeParent.generateDeclaredUidIdentifier("initStatic");
|
|
520
436
|
locals.push(staticInitLocal);
|
|
521
437
|
}
|
|
522
|
-
|
|
523
438
|
if (decoratedPrivateMethods.size > 0) {
|
|
524
439
|
path.traverse({
|
|
525
440
|
PrivateName(path) {
|
|
526
441
|
if (!decoratedPrivateMethods.has(path.node.id.name)) return;
|
|
527
442
|
const parentPath = path.parentPath;
|
|
528
443
|
const parentParentPath = parentPath.parentPath;
|
|
529
|
-
|
|
530
|
-
|
|
444
|
+
if (
|
|
445
|
+
parentParentPath.node.type === "AssignmentExpression" && parentParentPath.node.left === parentPath.node ||
|
|
446
|
+
parentParentPath.node.type === "UpdateExpression" ||
|
|
447
|
+
parentParentPath.node.type === "RestElement" ||
|
|
448
|
+
parentParentPath.node.type === "ArrayPattern" ||
|
|
449
|
+
parentParentPath.node.type === "ObjectProperty" && parentParentPath.node.value === parentPath.node && parentParentPath.parentPath.type === "ObjectPattern" ||
|
|
450
|
+
parentParentPath.node.type === "ForOfStatement" && parentParentPath.node.left === parentPath.node) {
|
|
531
451
|
throw path.buildCodeFrameError(`Decorated private methods are not updatable, but "#${path.node.id.name}" is updated via this expression.`);
|
|
532
452
|
}
|
|
533
453
|
}
|
|
534
|
-
|
|
535
454
|
});
|
|
536
455
|
}
|
|
537
|
-
|
|
538
456
|
let classInitInjected = false;
|
|
539
|
-
|
|
540
457
|
const classInitCall = classInitLocal && _core.types.callExpression(_core.types.cloneNode(classInitLocal), []);
|
|
541
|
-
|
|
542
458
|
const originalClass = path.node;
|
|
543
|
-
|
|
544
459
|
if (classDecorators) {
|
|
545
460
|
locals.push(classLocal, classInitLocal);
|
|
546
461
|
const statics = [];
|
|
@@ -551,9 +466,7 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
551
466
|
element.remove();
|
|
552
467
|
return;
|
|
553
468
|
}
|
|
554
|
-
|
|
555
469
|
const isProperty = element.isClassProperty() || element.isClassPrivateProperty();
|
|
556
|
-
|
|
557
470
|
if ((isProperty || element.isClassPrivateMethod()) && element.node.static) {
|
|
558
471
|
if (isProperty && staticBlocks.length > 0) {
|
|
559
472
|
const allValues = staticBlocks.map(staticBlockToIIFE);
|
|
@@ -561,52 +474,45 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
561
474
|
element.node.value = maybeSequenceExpression(allValues);
|
|
562
475
|
staticBlocks = [];
|
|
563
476
|
}
|
|
564
|
-
|
|
565
477
|
element.node.static = false;
|
|
566
478
|
statics.push(element.node);
|
|
567
479
|
element.remove();
|
|
568
480
|
}
|
|
569
481
|
});
|
|
570
|
-
|
|
571
482
|
if (statics.length > 0 || staticBlocks.length > 0) {
|
|
572
483
|
const staticsClass = _core.template.expression.ast`
|
|
573
484
|
class extends ${state.addHelper("identity")} {}
|
|
574
485
|
`;
|
|
575
|
-
staticsClass.body.body = [_core.types.staticBlock([_core.types.toStatement(
|
|
486
|
+
staticsClass.body.body = [_core.types.staticBlock([_core.types.toStatement(originalClass, true) ||
|
|
487
|
+
_core.types.expressionStatement(originalClass)]), ...statics];
|
|
576
488
|
const constructorBody = [];
|
|
577
|
-
|
|
578
489
|
const newExpr = _core.types.newExpression(staticsClass, []);
|
|
579
|
-
|
|
580
490
|
if (staticBlocks.length > 0) {
|
|
581
491
|
constructorBody.push(...staticBlocks.map(staticBlockToIIFE));
|
|
582
492
|
}
|
|
583
|
-
|
|
584
493
|
if (classInitCall) {
|
|
585
494
|
classInitInjected = true;
|
|
586
495
|
constructorBody.push(classInitCall);
|
|
587
496
|
}
|
|
588
|
-
|
|
589
497
|
if (constructorBody.length > 0) {
|
|
590
498
|
constructorBody.unshift(_core.types.callExpression(_core.types.super(), [_core.types.cloneNode(classLocal)]));
|
|
591
499
|
staticsClass.body.body.push(_core.types.classMethod("constructor", _core.types.identifier("constructor"), [], _core.types.blockStatement([_core.types.expressionStatement(_core.types.sequenceExpression(constructorBody))])));
|
|
592
500
|
} else {
|
|
593
501
|
newExpr.arguments.push(_core.types.cloneNode(classLocal));
|
|
594
502
|
}
|
|
595
|
-
|
|
596
503
|
path.replaceWith(newExpr);
|
|
597
504
|
}
|
|
598
505
|
}
|
|
599
|
-
|
|
600
506
|
if (!classInitInjected && classInitCall) {
|
|
601
507
|
path.node.body.body.push(_core.types.staticBlock([_core.types.expressionStatement(classInitCall)]));
|
|
602
508
|
}
|
|
603
|
-
|
|
604
509
|
originalClass.body.body.unshift(_core.types.staticBlock([_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.arrayPattern(locals), _core.types.callExpression(state.addHelper(version === "2021-12" ? "applyDecs" : "applyDecs2203"), [_core.types.thisExpression(), elementDecorations, classDecorations]))), requiresStaticInit && _core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(staticInitLocal), [_core.types.thisExpression()]))].filter(Boolean)));
|
|
510
|
+
|
|
605
511
|
path.insertBefore(assignments.map(expr => _core.types.expressionStatement(expr)));
|
|
512
|
+
|
|
606
513
|
path.scope.crawl();
|
|
607
514
|
return path;
|
|
608
515
|
}
|
|
609
|
-
|
|
610
516
|
function _default({
|
|
611
517
|
assertVersion,
|
|
612
518
|
assumption
|
|
@@ -614,7 +520,6 @@ function _default({
|
|
|
614
520
|
loose
|
|
615
521
|
}, version) {
|
|
616
522
|
var _assumption;
|
|
617
|
-
|
|
618
523
|
assertVersion(version === "2021-12" ? "^7.16.0" : "^7.19.0");
|
|
619
524
|
const VISITED = new WeakSet();
|
|
620
525
|
const constantSuper = (_assumption = assumption("constantSuper")) != null ? _assumption : loose;
|
|
@@ -624,22 +529,19 @@ function _default({
|
|
|
624
529
|
visitor: {
|
|
625
530
|
"ExportNamedDeclaration|ExportDefaultDeclaration"(path) {
|
|
626
531
|
var _declaration$decorato;
|
|
627
|
-
|
|
628
532
|
const {
|
|
629
533
|
declaration
|
|
630
534
|
} = path.node;
|
|
631
|
-
|
|
632
|
-
|
|
535
|
+
if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" &&
|
|
536
|
+
((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0) {
|
|
633
537
|
(0, _helperSplitExportDeclaration.default)(path);
|
|
634
538
|
}
|
|
635
539
|
},
|
|
636
|
-
|
|
637
540
|
Class(path, state) {
|
|
638
541
|
if (VISITED.has(path)) return;
|
|
639
542
|
const newPath = transformClass(path, state, constantSuper, version);
|
|
640
543
|
if (newPath) VISITED.add(newPath);
|
|
641
544
|
}
|
|
642
|
-
|
|
643
545
|
}
|
|
644
546
|
};
|
|
645
547
|
}
|