@babel/plugin-proposal-decorators 7.21.4-esm.4 → 7.22.3
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 +20 -13
- package/lib/index.js.map +1 -1
- package/lib/{transformer-2023-01.js → transformer-2023-05.js} +176 -108
- package/lib/transformer-2023-05.js.map +1 -0
- package/lib/transformer-legacy.js +35 -28
- package/lib/transformer-legacy.js.map +1 -1
- package/package.json +12 -12
- package/lib/package.json +0 -1
- package/lib/transformer-2023-01.js.map +0 -1
package/lib/index.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
|
8
|
+
var _pluginSyntaxDecorators = require("@babel/plugin-syntax-decorators");
|
|
9
|
+
var _helperCreateClassFeaturesPlugin = require("@babel/helper-create-class-features-plugin");
|
|
10
|
+
var _transformerLegacy = require("./transformer-legacy");
|
|
11
|
+
var _transformer = require("./transformer-2023-05");
|
|
12
|
+
var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
|
7
13
|
api.assertVersion(7);
|
|
8
14
|
{
|
|
9
15
|
var {
|
|
@@ -16,20 +22,21 @@ export default declare((api, options) => {
|
|
|
16
22
|
if (legacy || version === "legacy") {
|
|
17
23
|
return {
|
|
18
24
|
name: "proposal-decorators",
|
|
19
|
-
inherits:
|
|
20
|
-
visitor:
|
|
25
|
+
inherits: _pluginSyntaxDecorators.default,
|
|
26
|
+
visitor: _transformerLegacy.default
|
|
21
27
|
};
|
|
22
|
-
} else if (version === "2021-12" || version === "2022-03" || version === "2023-01") {
|
|
23
|
-
return
|
|
28
|
+
} else if (version === "2021-12" || version === "2022-03" || version === "2023-01" || version === "2023-05") {
|
|
29
|
+
return (0, _transformer.default)(api, options, version);
|
|
24
30
|
} else {
|
|
25
31
|
api.assertVersion("^7.0.2");
|
|
26
|
-
return createClassFeaturePlugin({
|
|
32
|
+
return (0, _helperCreateClassFeaturesPlugin.createClassFeaturePlugin)({
|
|
27
33
|
name: "proposal-decorators",
|
|
28
34
|
api,
|
|
29
|
-
feature: FEATURES.decorators,
|
|
30
|
-
inherits:
|
|
35
|
+
feature: _helperCreateClassFeaturesPlugin.FEATURES.decorators,
|
|
36
|
+
inherits: _pluginSyntaxDecorators.default
|
|
31
37
|
});
|
|
32
38
|
}
|
|
33
39
|
});
|
|
40
|
+
exports.default = _default;
|
|
34
41
|
|
|
35
42
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["_helperPluginUtils","require","_pluginSyntaxDecorators","_helperCreateClassFeaturesPlugin","_transformerLegacy","_transformer","_default","declare","api","options","assertVersion","legacy","version","name","inherits","syntaxDecorators","visitor","legacyVisitor","transformer2023_05","createClassFeaturePlugin","feature","FEATURES","decorators","exports","default"],"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 transformer2023_05 from \"./transformer-2023-05\";\nimport type { Options as SyntaxOptions } from \"@babel/plugin-syntax-decorators\";\n\ninterface Options extends SyntaxOptions {\n /** @deprecated 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 (\n version === \"2021-12\" ||\n version === \"2022-03\" ||\n version === \"2023-01\" ||\n version === \"2023-05\"\n ) {\n return transformer2023_05(api, options, version);\n } else if (!process.env.BABEL_8_BREAKING) {\n api.assertVersion(\"^7.0.2\");\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', '2022-03', or '2023-01'.\",\n );\n }\n});\n"],"mappings":";;;;;;AAEA,IAAAA,kBAAA,GAAAC,OAAA;AACA,IAAAC,uBAAA,GAAAD,OAAA;AACA,IAAAE,gCAAA,GAAAF,OAAA;AAIA,IAAAG,kBAAA,GAAAH,OAAA;AACA,IAAAI,YAAA,GAAAJ,OAAA;AAAuD,IAAAK,QAAA,GAUxC,IAAAC,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,IACLL,OAAO,KAAK,SAAS,IACrBA,OAAO,KAAK,SAAS,IACrBA,OAAO,KAAK,SAAS,IACrBA,OAAO,KAAK,SAAS,EACrB;IACA,OAAO,IAAAM,oBAAkB,EAACV,GAAG,EAAEC,OAAO,EAAEG,OAAO,CAAC;EAClD,CAAC,MAAyC;IACxCJ,GAAG,CAACE,aAAa,CAAC,QAAQ,CAAC;IAC3B,OAAO,IAAAS,yDAAwB,EAAC;MAC9BN,IAAI,EAAE,qBAAqB;MAE3BL,GAAG;MACHY,OAAO,EAAEC,yCAAQ,CAACC,UAAU;MAC5BR,QAAQ,EAAEC;IAEZ,CAAC,CAAC;EACJ;AAKF,CAAC,CAAC;AAAAQ,OAAA,CAAAC,OAAA,GAAAlB,QAAA"}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = _default;
|
|
7
|
+
var _core = require("@babel/core");
|
|
8
|
+
var _pluginSyntaxDecorators = require("@babel/plugin-syntax-decorators");
|
|
9
|
+
var _helperReplaceSupers = require("@babel/helper-replace-supers");
|
|
10
|
+
var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
|
|
5
11
|
function incrementId(id, idx = id.length - 1) {
|
|
6
12
|
if (idx === -1) {
|
|
7
13
|
id.unshift(65);
|
|
@@ -31,7 +37,7 @@ function createPrivateUidGeneratorForClass(classPath) {
|
|
|
31
37
|
incrementId(currentPrivateId);
|
|
32
38
|
reifiedId = String.fromCharCode(...currentPrivateId);
|
|
33
39
|
} while (privateNames.has(reifiedId));
|
|
34
|
-
return
|
|
40
|
+
return _core.types.privateName(_core.types.identifier(reifiedId));
|
|
35
41
|
};
|
|
36
42
|
}
|
|
37
43
|
function createLazyPrivateUidGeneratorForClass(classPath) {
|
|
@@ -46,11 +52,11 @@ function createLazyPrivateUidGeneratorForClass(classPath) {
|
|
|
46
52
|
function replaceClassWithVar(path) {
|
|
47
53
|
if (path.type === "ClassDeclaration") {
|
|
48
54
|
const varId = path.scope.generateUidIdentifierBasedOnNode(path.node.id);
|
|
49
|
-
const classId =
|
|
55
|
+
const classId = _core.types.identifier(path.node.id.name);
|
|
50
56
|
path.scope.rename(classId.name, varId.name);
|
|
51
|
-
path.insertBefore(
|
|
57
|
+
path.insertBefore(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(varId)]));
|
|
52
58
|
path.get("id").replaceWith(classId);
|
|
53
|
-
return [
|
|
59
|
+
return [_core.types.cloneNode(varId), path];
|
|
54
60
|
} else {
|
|
55
61
|
let className;
|
|
56
62
|
let varId;
|
|
@@ -64,51 +70,52 @@ function replaceClassWithVar(path) {
|
|
|
64
70
|
} else {
|
|
65
71
|
varId = path.scope.parent.generateDeclaredUidIdentifier("decorated_class");
|
|
66
72
|
}
|
|
67
|
-
const newClassExpr =
|
|
68
|
-
const [newPath] = path.replaceWith(
|
|
69
|
-
return [
|
|
73
|
+
const newClassExpr = _core.types.classExpression(className && _core.types.identifier(className), path.node.superClass, path.node.body);
|
|
74
|
+
const [newPath] = path.replaceWith(_core.types.sequenceExpression([newClassExpr, varId]));
|
|
75
|
+
return [_core.types.cloneNode(varId), newPath.get("expressions.0")];
|
|
70
76
|
}
|
|
71
77
|
}
|
|
72
78
|
function generateClassProperty(key, value, isStatic) {
|
|
73
79
|
if (key.type === "PrivateName") {
|
|
74
|
-
return
|
|
80
|
+
return _core.types.classPrivateProperty(key, value, undefined, isStatic);
|
|
75
81
|
} else {
|
|
76
|
-
return
|
|
82
|
+
return _core.types.classProperty(key, value, undefined, undefined, isStatic);
|
|
77
83
|
}
|
|
78
84
|
}
|
|
79
|
-
function addProxyAccessorsFor(element, originalKey, targetKey, isComputed = false) {
|
|
85
|
+
function addProxyAccessorsFor(className, element, originalKey, targetKey, version, isComputed = false) {
|
|
80
86
|
const {
|
|
81
87
|
static: isStatic
|
|
82
88
|
} = element.node;
|
|
83
|
-
const
|
|
84
|
-
const
|
|
89
|
+
const thisArg = version === "2023-05" && isStatic ? className : _core.types.thisExpression();
|
|
90
|
+
const getterBody = _core.types.blockStatement([_core.types.returnStatement(_core.types.memberExpression(_core.types.cloneNode(thisArg), _core.types.cloneNode(targetKey)))]);
|
|
91
|
+
const setterBody = _core.types.blockStatement([_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.cloneNode(thisArg), _core.types.cloneNode(targetKey)), _core.types.identifier("v")))]);
|
|
85
92
|
let getter, setter;
|
|
86
93
|
if (originalKey.type === "PrivateName") {
|
|
87
|
-
getter =
|
|
88
|
-
setter =
|
|
94
|
+
getter = _core.types.classPrivateMethod("get", _core.types.cloneNode(originalKey), [], getterBody, isStatic);
|
|
95
|
+
setter = _core.types.classPrivateMethod("set", _core.types.cloneNode(originalKey), [_core.types.identifier("v")], setterBody, isStatic);
|
|
89
96
|
} else {
|
|
90
|
-
getter =
|
|
91
|
-
setter =
|
|
97
|
+
getter = _core.types.classMethod("get", _core.types.cloneNode(originalKey), [], getterBody, isComputed, isStatic);
|
|
98
|
+
setter = _core.types.classMethod("set", _core.types.cloneNode(originalKey), [_core.types.identifier("v")], setterBody, isComputed, isStatic);
|
|
92
99
|
}
|
|
93
100
|
element.insertAfter(setter);
|
|
94
101
|
element.insertAfter(getter);
|
|
95
102
|
}
|
|
96
103
|
function extractProxyAccessorsFor(targetKey, version) {
|
|
97
|
-
if (version !== "2023-01") {
|
|
98
|
-
return [template.expression.ast`
|
|
104
|
+
if (version !== "2023-05" && version !== "2023-01") {
|
|
105
|
+
return [_core.template.expression.ast`
|
|
99
106
|
function () {
|
|
100
|
-
return this.${
|
|
107
|
+
return this.${_core.types.cloneNode(targetKey)};
|
|
101
108
|
}
|
|
102
|
-
`, template.expression.ast`
|
|
109
|
+
`, _core.template.expression.ast`
|
|
103
110
|
function (value) {
|
|
104
|
-
this.${
|
|
111
|
+
this.${_core.types.cloneNode(targetKey)} = value;
|
|
105
112
|
}
|
|
106
113
|
`];
|
|
107
114
|
}
|
|
108
|
-
return [template.expression.ast`
|
|
109
|
-
o => o.${
|
|
110
|
-
`, template.expression.ast`
|
|
111
|
-
(o, v) => o.${
|
|
115
|
+
return [_core.template.expression.ast`
|
|
116
|
+
o => o.${_core.types.cloneNode(targetKey)}
|
|
117
|
+
`, _core.template.expression.ast`
|
|
118
|
+
(o, v) => o.${_core.types.cloneNode(targetKey)} = v
|
|
112
119
|
`];
|
|
113
120
|
}
|
|
114
121
|
const FIELD = 0;
|
|
@@ -116,7 +123,9 @@ const ACCESSOR = 1;
|
|
|
116
123
|
const METHOD = 2;
|
|
117
124
|
const GETTER = 3;
|
|
118
125
|
const SETTER = 4;
|
|
119
|
-
const
|
|
126
|
+
const STATIC_OLD_VERSION = 5;
|
|
127
|
+
const STATIC = 8;
|
|
128
|
+
const DECORATORS_HAVE_THIS = 16;
|
|
120
129
|
function getElementKind(element) {
|
|
121
130
|
switch (element.node.type) {
|
|
122
131
|
case "ClassProperty":
|
|
@@ -142,11 +151,33 @@ function filteredOrderedDecoratorInfo(info) {
|
|
|
142
151
|
const filtered = info.filter(isDecoratorInfo);
|
|
143
152
|
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)];
|
|
144
153
|
}
|
|
145
|
-
function
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
154
|
+
function generateDecorationList(decorators, decoratorsThis, version) {
|
|
155
|
+
const decsCount = decorators.length;
|
|
156
|
+
const hasOneThis = decoratorsThis.some(Boolean);
|
|
157
|
+
const decs = [];
|
|
158
|
+
for (let i = 0; i < decsCount; i++) {
|
|
159
|
+
if (version === "2023-05" && hasOneThis) {
|
|
160
|
+
decs.push(decoratorsThis[i] || _core.types.unaryExpression("void", _core.types.numericLiteral(0)));
|
|
161
|
+
}
|
|
162
|
+
decs.push(decorators[i]);
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
hasThis: hasOneThis,
|
|
166
|
+
decs
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
function generateDecorationExprs(info, version) {
|
|
170
|
+
return _core.types.arrayExpression(filteredOrderedDecoratorInfo(info).map(el => {
|
|
171
|
+
const {
|
|
172
|
+
decs,
|
|
173
|
+
hasThis
|
|
174
|
+
} = generateDecorationList(el.decorators, el.decoratorsThis, version);
|
|
175
|
+
let flag = el.kind;
|
|
176
|
+
if (el.isStatic) {
|
|
177
|
+
flag += version === "2023-05" ? STATIC : STATIC_OLD_VERSION;
|
|
178
|
+
}
|
|
179
|
+
if (hasThis) flag += DECORATORS_HAVE_THIS;
|
|
180
|
+
return _core.types.arrayExpression([decs.length === 1 ? decs[0] : _core.types.arrayExpression(decs), _core.types.numericLiteral(flag), el.name, ...(el.privateMethods || [])]);
|
|
150
181
|
}));
|
|
151
182
|
}
|
|
152
183
|
function extractElementLocalAssignments(decorationInfo) {
|
|
@@ -164,8 +195,8 @@ function extractElementLocalAssignments(decorationInfo) {
|
|
|
164
195
|
return localIds;
|
|
165
196
|
}
|
|
166
197
|
function addCallAccessorsFor(element, key, getId, setId) {
|
|
167
|
-
element.insertAfter(
|
|
168
|
-
element.insertAfter(
|
|
198
|
+
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()]))])));
|
|
199
|
+
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")]))])));
|
|
169
200
|
}
|
|
170
201
|
function isNotTsParameter(node) {
|
|
171
202
|
return node.type !== "TSParameterProperty";
|
|
@@ -174,13 +205,13 @@ function movePrivateAccessor(element, key, methodLocalVar, isStatic) {
|
|
|
174
205
|
let params;
|
|
175
206
|
let block;
|
|
176
207
|
if (element.node.kind === "set") {
|
|
177
|
-
params = [
|
|
178
|
-
block = [
|
|
208
|
+
params = [_core.types.identifier("v")];
|
|
209
|
+
block = [_core.types.expressionStatement(_core.types.callExpression(methodLocalVar, [_core.types.thisExpression(), _core.types.identifier("v")]))];
|
|
179
210
|
} else {
|
|
180
211
|
params = [];
|
|
181
|
-
block = [
|
|
212
|
+
block = [_core.types.returnStatement(_core.types.callExpression(methodLocalVar, [_core.types.thisExpression()]))];
|
|
182
213
|
}
|
|
183
|
-
element.replaceWith(
|
|
214
|
+
element.replaceWith(_core.types.classPrivateMethod(element.node.kind, _core.types.cloneNode(key), params, _core.types.blockStatement(block), isStatic));
|
|
184
215
|
}
|
|
185
216
|
function isClassDecoratableElementPath(path) {
|
|
186
217
|
const {
|
|
@@ -189,12 +220,12 @@ function isClassDecoratableElementPath(path) {
|
|
|
189
220
|
return type !== "TSDeclareMethod" && type !== "TSIndexSignature" && type !== "StaticBlock";
|
|
190
221
|
}
|
|
191
222
|
function staticBlockToIIFE(block) {
|
|
192
|
-
return
|
|
223
|
+
return _core.types.callExpression(_core.types.arrowFunctionExpression([], _core.types.blockStatement(block.body)), []);
|
|
193
224
|
}
|
|
194
225
|
function maybeSequenceExpression(exprs) {
|
|
195
|
-
if (exprs.length === 0) return
|
|
226
|
+
if (exprs.length === 0) return _core.types.unaryExpression("void", _core.types.numericLiteral(0));
|
|
196
227
|
if (exprs.length === 1) return exprs[0];
|
|
197
|
-
return
|
|
228
|
+
return _core.types.sequenceExpression(exprs);
|
|
198
229
|
}
|
|
199
230
|
function transformClass(path, state, constantSuper, version) {
|
|
200
231
|
const body = path.get("body.body");
|
|
@@ -215,10 +246,10 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
215
246
|
computed
|
|
216
247
|
} = element.node;
|
|
217
248
|
const newId = generateClassPrivateUid();
|
|
218
|
-
const valueNode = value ?
|
|
249
|
+
const valueNode = value ? _core.types.cloneNode(value) : undefined;
|
|
219
250
|
const newField = generateClassProperty(newId, valueNode, isStatic);
|
|
220
251
|
const [newPath] = element.replaceWith(newField);
|
|
221
|
-
addProxyAccessorsFor(newPath, key, newId, computed);
|
|
252
|
+
addProxyAccessorsFor(path.node.id, newPath, key, newId, version, computed);
|
|
222
253
|
}
|
|
223
254
|
}
|
|
224
255
|
if (!classDecorators && !hasElementDecorators) return;
|
|
@@ -233,8 +264,29 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
233
264
|
const scopeParent = path.scope.parent;
|
|
234
265
|
const memoiseExpression = (expression, hint) => {
|
|
235
266
|
const localEvaluatedId = scopeParent.generateDeclaredUidIdentifier(hint);
|
|
236
|
-
assignments.push(
|
|
237
|
-
return
|
|
267
|
+
assignments.push(_core.types.assignmentExpression("=", localEvaluatedId, expression));
|
|
268
|
+
return _core.types.cloneNode(localEvaluatedId);
|
|
269
|
+
};
|
|
270
|
+
const decoratorsThis = new Map();
|
|
271
|
+
const maybeExtractDecorator = decorator => {
|
|
272
|
+
const {
|
|
273
|
+
expression
|
|
274
|
+
} = decorator;
|
|
275
|
+
if (version === "2023-05" && _core.types.isMemberExpression(expression)) {
|
|
276
|
+
let object;
|
|
277
|
+
if (_core.types.isSuper(expression.object) || _core.types.isThisExpression(expression.object)) {
|
|
278
|
+
object = memoiseExpression(_core.types.thisExpression(), "obj");
|
|
279
|
+
} else if (!scopeParent.isStatic(expression.object)) {
|
|
280
|
+
object = memoiseExpression(expression.object, "obj");
|
|
281
|
+
expression.object = object;
|
|
282
|
+
} else {
|
|
283
|
+
object = expression.object;
|
|
284
|
+
}
|
|
285
|
+
decoratorsThis.set(decorator, _core.types.cloneNode(object));
|
|
286
|
+
}
|
|
287
|
+
if (!scopeParent.isStatic(expression)) {
|
|
288
|
+
decorator.expression = memoiseExpression(expression, "dec");
|
|
289
|
+
}
|
|
238
290
|
};
|
|
239
291
|
if (classDecorators) {
|
|
240
292
|
classInitLocal = scopeParent.generateDeclaredUidIdentifier("initClass");
|
|
@@ -243,15 +295,13 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
243
295
|
classIdLocal = classId;
|
|
244
296
|
path.node.decorators = null;
|
|
245
297
|
for (const classDecorator of classDecorators) {
|
|
246
|
-
|
|
247
|
-
classDecorator.expression = memoiseExpression(classDecorator.expression, "dec");
|
|
248
|
-
}
|
|
298
|
+
maybeExtractDecorator(classDecorator);
|
|
249
299
|
}
|
|
250
300
|
} else {
|
|
251
301
|
if (!path.node.id) {
|
|
252
302
|
path.node.id = path.scope.generateUidIdentifier("Class");
|
|
253
303
|
}
|
|
254
|
-
classIdLocal =
|
|
304
|
+
classIdLocal = _core.types.cloneNode(path.node.id);
|
|
255
305
|
}
|
|
256
306
|
let lastInstancePrivateName;
|
|
257
307
|
let needsInstancePrivateBrandCheck = false;
|
|
@@ -267,9 +317,7 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
267
317
|
const hasDecorators = Array.isArray(decorators) && decorators.length > 0;
|
|
268
318
|
if (hasDecorators) {
|
|
269
319
|
for (const decoratorPath of decorators) {
|
|
270
|
-
|
|
271
|
-
decoratorPath.node.expression = memoiseExpression(decoratorPath.node.expression, "dec");
|
|
272
|
-
}
|
|
320
|
+
maybeExtractDecorator(decoratorPath.node);
|
|
273
321
|
}
|
|
274
322
|
}
|
|
275
323
|
const isComputed = "computed" in element.node && element.node.computed === true;
|
|
@@ -294,7 +342,7 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
294
342
|
if (hasDecorators) {
|
|
295
343
|
needsInstancePrivateBrandCheck = true;
|
|
296
344
|
}
|
|
297
|
-
if (
|
|
345
|
+
if (_core.types.isClassPrivateProperty(node) || !lastInstancePrivateName) {
|
|
298
346
|
lastInstancePrivateName = key;
|
|
299
347
|
}
|
|
300
348
|
}
|
|
@@ -310,13 +358,13 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
310
358
|
const {
|
|
311
359
|
value
|
|
312
360
|
} = element.node;
|
|
313
|
-
const params = [
|
|
361
|
+
const params = [_core.types.thisExpression()];
|
|
314
362
|
if (value) {
|
|
315
|
-
params.push(
|
|
363
|
+
params.push(_core.types.cloneNode(value));
|
|
316
364
|
}
|
|
317
365
|
const newId = generateClassPrivateUid();
|
|
318
366
|
const newFieldInitId = element.scope.parent.generateDeclaredUidIdentifier(`init_${name}`);
|
|
319
|
-
const newValue =
|
|
367
|
+
const newValue = _core.types.callExpression(_core.types.cloneNode(newFieldInitId), params);
|
|
320
368
|
const newField = generateClassProperty(newId, newValue, isStatic);
|
|
321
369
|
const [newPath] = element.replaceWith(newField);
|
|
322
370
|
if (isPrivate) {
|
|
@@ -326,20 +374,20 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
326
374
|
addCallAccessorsFor(newPath, key, getId, setId);
|
|
327
375
|
locals = [newFieldInitId, getId, setId];
|
|
328
376
|
} else {
|
|
329
|
-
addProxyAccessorsFor(newPath, key, newId, isComputed);
|
|
377
|
+
addProxyAccessorsFor(path.node.id, newPath, key, newId, version, isComputed);
|
|
330
378
|
locals = newFieldInitId;
|
|
331
379
|
}
|
|
332
380
|
} else if (kind === FIELD) {
|
|
333
381
|
const initId = element.scope.parent.generateDeclaredUidIdentifier(`init_${name}`);
|
|
334
382
|
const valuePath = element.get("value");
|
|
335
|
-
valuePath.replaceWith(
|
|
383
|
+
valuePath.replaceWith(_core.types.callExpression(_core.types.cloneNode(initId), [_core.types.thisExpression(), valuePath.node].filter(v => v)));
|
|
336
384
|
locals = initId;
|
|
337
385
|
if (isPrivate) {
|
|
338
386
|
privateMethods = extractProxyAccessorsFor(key, version);
|
|
339
387
|
}
|
|
340
388
|
} else if (isPrivate) {
|
|
341
389
|
locals = element.scope.parent.generateDeclaredUidIdentifier(`call_${name}`);
|
|
342
|
-
const replaceSupers = new
|
|
390
|
+
const replaceSupers = new _helperReplaceSupers.default({
|
|
343
391
|
constantSuper,
|
|
344
392
|
methodPath: element,
|
|
345
393
|
objectRef: classIdLocal,
|
|
@@ -353,29 +401,30 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
353
401
|
body,
|
|
354
402
|
async: isAsync
|
|
355
403
|
} = element.node;
|
|
356
|
-
privateMethods = [
|
|
404
|
+
privateMethods = [_core.types.functionExpression(undefined, params.filter(isNotTsParameter), body, isAsync)];
|
|
357
405
|
if (kind === GETTER || kind === SETTER) {
|
|
358
|
-
movePrivateAccessor(element,
|
|
406
|
+
movePrivateAccessor(element, _core.types.cloneNode(key), _core.types.cloneNode(locals), isStatic);
|
|
359
407
|
} else {
|
|
360
408
|
const node = element.node;
|
|
361
|
-
path.node.body.body.unshift(
|
|
409
|
+
path.node.body.body.unshift(_core.types.classPrivateProperty(key, _core.types.cloneNode(locals), [], node.static));
|
|
362
410
|
decoratedPrivateMethods.add(key.id.name);
|
|
363
411
|
element.remove();
|
|
364
412
|
}
|
|
365
413
|
}
|
|
366
414
|
let nameExpr;
|
|
367
415
|
if (isComputed) {
|
|
368
|
-
nameExpr =
|
|
416
|
+
nameExpr = _core.types.cloneNode(key);
|
|
369
417
|
} else if (key.type === "PrivateName") {
|
|
370
|
-
nameExpr =
|
|
418
|
+
nameExpr = _core.types.stringLiteral(key.id.name);
|
|
371
419
|
} else if (key.type === "Identifier") {
|
|
372
|
-
nameExpr =
|
|
420
|
+
nameExpr = _core.types.stringLiteral(key.name);
|
|
373
421
|
} else {
|
|
374
|
-
nameExpr =
|
|
422
|
+
nameExpr = _core.types.cloneNode(key);
|
|
375
423
|
}
|
|
376
424
|
elementDecoratorInfo.push({
|
|
377
425
|
kind,
|
|
378
426
|
decorators: decorators.map(d => d.node.expression),
|
|
427
|
+
decoratorsThis: decorators.map(d => decoratorsThis.get(d.node)),
|
|
379
428
|
name: nameExpr,
|
|
380
429
|
isStatic,
|
|
381
430
|
privateMethods,
|
|
@@ -397,40 +446,49 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
397
446
|
}
|
|
398
447
|
}
|
|
399
448
|
}
|
|
400
|
-
const elementDecorations = generateDecorationExprs(elementDecoratorInfo);
|
|
401
|
-
|
|
449
|
+
const elementDecorations = generateDecorationExprs(elementDecoratorInfo, version);
|
|
450
|
+
let classDecorationsFlag = 0;
|
|
451
|
+
let classDecorations = [];
|
|
452
|
+
if (classDecorators) {
|
|
453
|
+
const {
|
|
454
|
+
hasThis,
|
|
455
|
+
decs
|
|
456
|
+
} = generateDecorationList(classDecorators.map(el => el.expression), classDecorators.map(dec => decoratorsThis.get(dec)), version);
|
|
457
|
+
classDecorationsFlag = hasThis ? 1 : 0;
|
|
458
|
+
classDecorations = decs;
|
|
459
|
+
}
|
|
402
460
|
const elementLocals = extractElementLocalAssignments(elementDecoratorInfo);
|
|
403
461
|
if (requiresProtoInit) {
|
|
404
462
|
protoInitLocal = scopeParent.generateDeclaredUidIdentifier("initProto");
|
|
405
463
|
elementLocals.push(protoInitLocal);
|
|
406
|
-
const protoInitCall =
|
|
464
|
+
const protoInitCall = _core.types.callExpression(_core.types.cloneNode(protoInitLocal), [_core.types.thisExpression()]);
|
|
407
465
|
if (firstFieldPath) {
|
|
408
466
|
const value = firstFieldPath.get("value");
|
|
409
467
|
const body = [protoInitCall];
|
|
410
468
|
if (value.node) {
|
|
411
469
|
body.push(value.node);
|
|
412
470
|
}
|
|
413
|
-
value.replaceWith(
|
|
471
|
+
value.replaceWith(_core.types.sequenceExpression(body));
|
|
414
472
|
} else if (constructorPath) {
|
|
415
473
|
if (path.node.superClass) {
|
|
416
474
|
path.traverse({
|
|
417
475
|
CallExpression: {
|
|
418
476
|
exit(path) {
|
|
419
477
|
if (!path.get("callee").isSuper()) return;
|
|
420
|
-
path.replaceWith(
|
|
478
|
+
path.replaceWith(_core.types.callExpression(_core.types.cloneNode(protoInitLocal), [path.node]));
|
|
421
479
|
path.skip();
|
|
422
480
|
}
|
|
423
481
|
}
|
|
424
482
|
});
|
|
425
483
|
} else {
|
|
426
|
-
constructorPath.node.body.body.unshift(
|
|
484
|
+
constructorPath.node.body.body.unshift(_core.types.expressionStatement(protoInitCall));
|
|
427
485
|
}
|
|
428
486
|
} else {
|
|
429
|
-
const body = [
|
|
487
|
+
const body = [_core.types.expressionStatement(protoInitCall)];
|
|
430
488
|
if (path.node.superClass) {
|
|
431
|
-
body.unshift(
|
|
489
|
+
body.unshift(_core.types.expressionStatement(_core.types.callExpression(_core.types.super(), [_core.types.spreadElement(_core.types.identifier("args"))])));
|
|
432
490
|
}
|
|
433
|
-
path.node.body.body.unshift(
|
|
491
|
+
path.node.body.body.unshift(_core.types.classMethod("constructor", _core.types.identifier("constructor"), [_core.types.restElement(_core.types.identifier("args"))], _core.types.blockStatement(body)));
|
|
434
492
|
}
|
|
435
493
|
}
|
|
436
494
|
if (requiresStaticInit) {
|
|
@@ -451,7 +509,7 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
451
509
|
}
|
|
452
510
|
const classLocals = [];
|
|
453
511
|
let classInitInjected = false;
|
|
454
|
-
const classInitCall = classInitLocal &&
|
|
512
|
+
const classInitCall = classInitLocal && _core.types.callExpression(_core.types.cloneNode(classInitLocal), []);
|
|
455
513
|
const originalClass = path.node;
|
|
456
514
|
if (classDecorators) {
|
|
457
515
|
classLocals.push(classIdLocal, classInitLocal);
|
|
@@ -477,12 +535,12 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
477
535
|
}
|
|
478
536
|
});
|
|
479
537
|
if (statics.length > 0 || staticBlocks.length > 0) {
|
|
480
|
-
const staticsClass = template.expression.ast`
|
|
538
|
+
const staticsClass = _core.template.expression.ast`
|
|
481
539
|
class extends ${state.addHelper("identity")} {}
|
|
482
540
|
`;
|
|
483
|
-
staticsClass.body.body = [
|
|
541
|
+
staticsClass.body.body = [_core.types.staticBlock([_core.types.toStatement(originalClass, true) || _core.types.expressionStatement(originalClass)]), ...statics];
|
|
484
542
|
const constructorBody = [];
|
|
485
|
-
const newExpr =
|
|
543
|
+
const newExpr = _core.types.newExpression(staticsClass, []);
|
|
486
544
|
if (staticBlocks.length > 0) {
|
|
487
545
|
constructorBody.push(...staticBlocks.map(staticBlockToIIFE));
|
|
488
546
|
}
|
|
@@ -491,61 +549,71 @@ function transformClass(path, state, constantSuper, version) {
|
|
|
491
549
|
constructorBody.push(classInitCall);
|
|
492
550
|
}
|
|
493
551
|
if (constructorBody.length > 0) {
|
|
494
|
-
constructorBody.unshift(
|
|
495
|
-
staticsClass.body.body.push(
|
|
552
|
+
constructorBody.unshift(_core.types.callExpression(_core.types.super(), [_core.types.cloneNode(classIdLocal)]));
|
|
553
|
+
staticsClass.body.body.push(_core.types.classMethod("constructor", _core.types.identifier("constructor"), [], _core.types.blockStatement([_core.types.expressionStatement(_core.types.sequenceExpression(constructorBody))])));
|
|
496
554
|
} else {
|
|
497
|
-
newExpr.arguments.push(
|
|
555
|
+
newExpr.arguments.push(_core.types.cloneNode(classIdLocal));
|
|
498
556
|
}
|
|
499
557
|
path.replaceWith(newExpr);
|
|
500
558
|
}
|
|
501
559
|
}
|
|
502
560
|
if (!classInitInjected && classInitCall) {
|
|
503
|
-
path.node.body.body.push(
|
|
561
|
+
path.node.body.body.push(_core.types.staticBlock([_core.types.expressionStatement(classInitCall)]));
|
|
504
562
|
}
|
|
505
|
-
originalClass.body.body.unshift(
|
|
506
|
-
path.insertBefore(assignments.map(expr =>
|
|
563
|
+
originalClass.body.body.unshift(_core.types.staticBlock([_core.types.expressionStatement(createLocalsAssignment(elementLocals, classLocals, elementDecorations, _core.types.arrayExpression(classDecorations), _core.types.numericLiteral(classDecorationsFlag), needsInstancePrivateBrandCheck ? lastInstancePrivateName : null, state, version)), requiresStaticInit && _core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(staticInitLocal), [_core.types.thisExpression()]))].filter(Boolean)));
|
|
564
|
+
path.insertBefore(assignments.map(expr => _core.types.expressionStatement(expr)));
|
|
507
565
|
path.scope.crawl();
|
|
508
566
|
return path;
|
|
509
567
|
}
|
|
510
|
-
function createLocalsAssignment(elementLocals, classLocals, elementDecorations, classDecorations, maybePrivateBranName, state, version) {
|
|
568
|
+
function createLocalsAssignment(elementLocals, classLocals, elementDecorations, classDecorations, classDecorationsFlag, maybePrivateBranName, state, version) {
|
|
511
569
|
let lhs, rhs;
|
|
512
|
-
const args = [
|
|
570
|
+
const args = [_core.types.thisExpression(), elementDecorations, classDecorations];
|
|
513
571
|
if (version === "2021-12" || version === "2022-03" && !state.availableHelper("applyDecs2203R")) {
|
|
514
|
-
lhs =
|
|
515
|
-
rhs =
|
|
572
|
+
lhs = _core.types.arrayPattern([...elementLocals, ...classLocals]);
|
|
573
|
+
rhs = _core.types.callExpression(state.addHelper(version === "2021-12" ? "applyDecs" : "applyDecs2203"), args);
|
|
516
574
|
} else {
|
|
517
|
-
if (version === "2023-
|
|
575
|
+
if (version === "2023-05") {
|
|
576
|
+
if (maybePrivateBranName || classDecorationsFlag.value !== 0) {
|
|
577
|
+
args.push(classDecorationsFlag);
|
|
578
|
+
}
|
|
579
|
+
if (maybePrivateBranName) {
|
|
580
|
+
args.push(_core.template.expression.ast`
|
|
581
|
+
_ => ${_core.types.cloneNode(maybePrivateBranName)} in _
|
|
582
|
+
`);
|
|
583
|
+
}
|
|
584
|
+
rhs = _core.types.callExpression(state.addHelper("applyDecs2305"), args);
|
|
585
|
+
} else if (version === "2023-01") {
|
|
518
586
|
if (maybePrivateBranName) {
|
|
519
|
-
args.push(template.expression.ast`
|
|
520
|
-
_ => ${
|
|
587
|
+
args.push(_core.template.expression.ast`
|
|
588
|
+
_ => ${_core.types.cloneNode(maybePrivateBranName)} in _
|
|
521
589
|
`);
|
|
522
590
|
}
|
|
523
|
-
rhs =
|
|
591
|
+
rhs = _core.types.callExpression(state.addHelper("applyDecs2301"), args);
|
|
524
592
|
} else {
|
|
525
|
-
rhs =
|
|
593
|
+
rhs = _core.types.callExpression(state.addHelper("applyDecs2203R"), args);
|
|
526
594
|
}
|
|
527
595
|
if (elementLocals.length > 0) {
|
|
528
596
|
if (classLocals.length > 0) {
|
|
529
|
-
lhs =
|
|
597
|
+
lhs = _core.types.objectPattern([_core.types.objectProperty(_core.types.identifier("e"), _core.types.arrayPattern(elementLocals)), _core.types.objectProperty(_core.types.identifier("c"), _core.types.arrayPattern(classLocals))]);
|
|
530
598
|
} else {
|
|
531
|
-
lhs =
|
|
532
|
-
rhs =
|
|
599
|
+
lhs = _core.types.arrayPattern(elementLocals);
|
|
600
|
+
rhs = _core.types.memberExpression(rhs, _core.types.identifier("e"), false, false);
|
|
533
601
|
}
|
|
534
602
|
} else {
|
|
535
|
-
lhs =
|
|
536
|
-
rhs =
|
|
603
|
+
lhs = _core.types.arrayPattern(classLocals);
|
|
604
|
+
rhs = _core.types.memberExpression(rhs, _core.types.identifier("c"), false, false);
|
|
537
605
|
}
|
|
538
606
|
}
|
|
539
|
-
return
|
|
607
|
+
return _core.types.assignmentExpression("=", lhs, rhs);
|
|
540
608
|
}
|
|
541
|
-
|
|
609
|
+
function _default({
|
|
542
610
|
assertVersion,
|
|
543
611
|
assumption
|
|
544
612
|
}, {
|
|
545
613
|
loose
|
|
546
614
|
}, version) {
|
|
547
615
|
var _assumption;
|
|
548
|
-
if (version === "2023-01") {
|
|
616
|
+
if (version === "2023-05" || version === "2023-01") {
|
|
549
617
|
assertVersion("^7.21.0");
|
|
550
618
|
} else if (version === "2021-12") {
|
|
551
619
|
assertVersion("^7.16.0");
|
|
@@ -556,7 +624,7 @@ export default function ({
|
|
|
556
624
|
const constantSuper = (_assumption = assumption("constantSuper")) != null ? _assumption : loose;
|
|
557
625
|
return {
|
|
558
626
|
name: "proposal-decorators",
|
|
559
|
-
inherits:
|
|
627
|
+
inherits: _pluginSyntaxDecorators.default,
|
|
560
628
|
visitor: {
|
|
561
629
|
"ExportNamedDeclaration|ExportDefaultDeclaration"(path) {
|
|
562
630
|
var _declaration$decorato;
|
|
@@ -564,7 +632,7 @@ export default function ({
|
|
|
564
632
|
declaration
|
|
565
633
|
} = path.node;
|
|
566
634
|
if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0) {
|
|
567
|
-
|
|
635
|
+
(0, _helperSplitExportDeclaration.default)(path);
|
|
568
636
|
}
|
|
569
637
|
},
|
|
570
638
|
Class(path, state) {
|
|
@@ -576,4 +644,4 @@ export default function ({
|
|
|
576
644
|
};
|
|
577
645
|
}
|
|
578
646
|
|
|
579
|
-
//# sourceMappingURL=transformer-2023-
|
|
647
|
+
//# sourceMappingURL=transformer-2023-05.js.map
|