@babel/plugin-proposal-decorators 8.0.0-alpha.1 → 8.0.0-alpha.11
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.d.ts +12 -0
- package/lib/index.js +12 -623
- package/lib/index.js.map +1 -1
- package/package.json +14 -15
- package/tsconfig.json +23 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/lib/transformer-2023-05.js +0 -631
- package/lib/transformer-2023-05.js.map +0 -1
- package/lib/transformer-legacy.js +0 -155
- package/lib/transformer-legacy.js.map +0 -1
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as _babel_core from '@babel/core';
|
|
2
|
+
import { Options as Options$1 } from '@babel/plugin-syntax-decorators';
|
|
3
|
+
|
|
4
|
+
interface Options extends Options$1 {
|
|
5
|
+
/** @deprecated use `constantSuper` assumption instead. Only supported in 2021-12 version. */
|
|
6
|
+
loose?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare const _default: (api: _babel_core.PluginAPI, options: Options, dirname: string) => _babel_core.PluginObject<_babel_core.PluginPass<{}>>;
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
11
|
+
|
|
12
|
+
export { type Options, _default as default };
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { declare } from '@babel/helper-plugin-utils';
|
|
2
2
|
import syntaxDecorators from '@babel/plugin-syntax-decorators';
|
|
3
|
-
import '@babel/helper-create-class-features-plugin';
|
|
3
|
+
import { createClassFeaturePlugin, FEATURES } from '@babel/helper-create-class-features-plugin';
|
|
4
4
|
import { template, types } from '@babel/core';
|
|
5
|
-
import ReplaceSupers from '@babel/helper-replace-supers';
|
|
6
|
-
import splitExportDeclaration from '@babel/helper-split-export-declaration';
|
|
7
5
|
|
|
8
6
|
const buildClassDecorator = template.statement(`
|
|
9
7
|
DECORATOR(CLASS_REF = INNER) || CLASS_REF;
|
|
@@ -157,624 +155,8 @@ const visitor = {
|
|
|
157
155
|
}
|
|
158
156
|
};
|
|
159
157
|
|
|
160
|
-
function incrementId(id, idx = id.length - 1) {
|
|
161
|
-
if (idx === -1) {
|
|
162
|
-
id.unshift(65);
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
const current = id[idx];
|
|
166
|
-
if (current === 90) {
|
|
167
|
-
id[idx] = 97;
|
|
168
|
-
} else if (current === 122) {
|
|
169
|
-
id[idx] = 65;
|
|
170
|
-
incrementId(id, idx - 1);
|
|
171
|
-
} else {
|
|
172
|
-
id[idx] = current + 1;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
function createPrivateUidGeneratorForClass(classPath) {
|
|
176
|
-
const currentPrivateId = [];
|
|
177
|
-
const privateNames = new Set();
|
|
178
|
-
classPath.traverse({
|
|
179
|
-
PrivateName(path) {
|
|
180
|
-
privateNames.add(path.node.id.name);
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
return () => {
|
|
184
|
-
let reifiedId;
|
|
185
|
-
do {
|
|
186
|
-
incrementId(currentPrivateId);
|
|
187
|
-
reifiedId = String.fromCharCode(...currentPrivateId);
|
|
188
|
-
} while (privateNames.has(reifiedId));
|
|
189
|
-
return types.privateName(types.identifier(reifiedId));
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
function createLazyPrivateUidGeneratorForClass(classPath) {
|
|
193
|
-
let generator;
|
|
194
|
-
return () => {
|
|
195
|
-
if (!generator) {
|
|
196
|
-
generator = createPrivateUidGeneratorForClass(classPath);
|
|
197
|
-
}
|
|
198
|
-
return generator();
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
function replaceClassWithVar(path) {
|
|
202
|
-
if (path.type === "ClassDeclaration") {
|
|
203
|
-
const varId = path.scope.generateUidIdentifierBasedOnNode(path.node.id);
|
|
204
|
-
const classId = types.identifier(path.node.id.name);
|
|
205
|
-
path.scope.rename(classId.name, varId.name);
|
|
206
|
-
path.insertBefore(types.variableDeclaration("let", [types.variableDeclarator(varId)]));
|
|
207
|
-
path.get("id").replaceWith(classId);
|
|
208
|
-
return [types.cloneNode(varId), path];
|
|
209
|
-
} else {
|
|
210
|
-
let className;
|
|
211
|
-
let varId;
|
|
212
|
-
if (path.node.id) {
|
|
213
|
-
className = path.node.id.name;
|
|
214
|
-
varId = path.scope.parent.generateDeclaredUidIdentifier(className);
|
|
215
|
-
path.scope.rename(className, varId.name);
|
|
216
|
-
} else if (path.parentPath.node.type === "VariableDeclarator" && path.parentPath.node.id.type === "Identifier") {
|
|
217
|
-
className = path.parentPath.node.id.name;
|
|
218
|
-
varId = path.scope.parent.generateDeclaredUidIdentifier(className);
|
|
219
|
-
} else {
|
|
220
|
-
varId = path.scope.parent.generateDeclaredUidIdentifier("decorated_class");
|
|
221
|
-
}
|
|
222
|
-
const newClassExpr = types.classExpression(className && types.identifier(className), path.node.superClass, path.node.body);
|
|
223
|
-
const [newPath] = path.replaceWith(types.sequenceExpression([newClassExpr, varId]));
|
|
224
|
-
return [types.cloneNode(varId), newPath.get("expressions.0")];
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
function generateClassProperty(key, value, isStatic) {
|
|
228
|
-
if (key.type === "PrivateName") {
|
|
229
|
-
return types.classPrivateProperty(key, value, undefined, isStatic);
|
|
230
|
-
} else {
|
|
231
|
-
return types.classProperty(key, value, undefined, undefined, isStatic);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
function addProxyAccessorsFor(className, element, originalKey, targetKey, version, isComputed = false) {
|
|
235
|
-
const {
|
|
236
|
-
static: isStatic
|
|
237
|
-
} = element.node;
|
|
238
|
-
const thisArg = version === "2023-05" && isStatic ? className : types.thisExpression();
|
|
239
|
-
const getterBody = types.blockStatement([types.returnStatement(types.memberExpression(types.cloneNode(thisArg), types.cloneNode(targetKey)))]);
|
|
240
|
-
const setterBody = types.blockStatement([types.expressionStatement(types.assignmentExpression("=", types.memberExpression(types.cloneNode(thisArg), types.cloneNode(targetKey)), types.identifier("v")))]);
|
|
241
|
-
let getter, setter;
|
|
242
|
-
if (originalKey.type === "PrivateName") {
|
|
243
|
-
getter = types.classPrivateMethod("get", types.cloneNode(originalKey), [], getterBody, isStatic);
|
|
244
|
-
setter = types.classPrivateMethod("set", types.cloneNode(originalKey), [types.identifier("v")], setterBody, isStatic);
|
|
245
|
-
} else {
|
|
246
|
-
getter = types.classMethod("get", types.cloneNode(originalKey), [], getterBody, isComputed, isStatic);
|
|
247
|
-
setter = types.classMethod("set", types.cloneNode(originalKey), [types.identifier("v")], setterBody, isComputed, isStatic);
|
|
248
|
-
}
|
|
249
|
-
element.insertAfter(setter);
|
|
250
|
-
element.insertAfter(getter);
|
|
251
|
-
}
|
|
252
|
-
function extractProxyAccessorsFor(targetKey, version) {
|
|
253
|
-
if (version !== "2023-05" && version !== "2023-01") {
|
|
254
|
-
return [template.expression.ast`
|
|
255
|
-
function () {
|
|
256
|
-
return this.${types.cloneNode(targetKey)};
|
|
257
|
-
}
|
|
258
|
-
`, template.expression.ast`
|
|
259
|
-
function (value) {
|
|
260
|
-
this.${types.cloneNode(targetKey)} = value;
|
|
261
|
-
}
|
|
262
|
-
`];
|
|
263
|
-
}
|
|
264
|
-
return [template.expression.ast`
|
|
265
|
-
o => o.${types.cloneNode(targetKey)}
|
|
266
|
-
`, template.expression.ast`
|
|
267
|
-
(o, v) => o.${types.cloneNode(targetKey)} = v
|
|
268
|
-
`];
|
|
269
|
-
}
|
|
270
|
-
const FIELD = 0;
|
|
271
|
-
const ACCESSOR = 1;
|
|
272
|
-
const METHOD = 2;
|
|
273
|
-
const GETTER = 3;
|
|
274
|
-
const SETTER = 4;
|
|
275
|
-
const STATIC_OLD_VERSION = 5;
|
|
276
|
-
const STATIC = 8;
|
|
277
|
-
const DECORATORS_HAVE_THIS = 16;
|
|
278
|
-
function getElementKind(element) {
|
|
279
|
-
switch (element.node.type) {
|
|
280
|
-
case "ClassProperty":
|
|
281
|
-
case "ClassPrivateProperty":
|
|
282
|
-
return FIELD;
|
|
283
|
-
case "ClassAccessorProperty":
|
|
284
|
-
return ACCESSOR;
|
|
285
|
-
case "ClassMethod":
|
|
286
|
-
case "ClassPrivateMethod":
|
|
287
|
-
if (element.node.kind === "get") {
|
|
288
|
-
return GETTER;
|
|
289
|
-
} else if (element.node.kind === "set") {
|
|
290
|
-
return SETTER;
|
|
291
|
-
} else {
|
|
292
|
-
return METHOD;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
function isDecoratorInfo(info) {
|
|
297
|
-
return "decorators" in info;
|
|
298
|
-
}
|
|
299
|
-
function filteredOrderedDecoratorInfo(info) {
|
|
300
|
-
const filtered = info.filter(isDecoratorInfo);
|
|
301
|
-
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)];
|
|
302
|
-
}
|
|
303
|
-
function generateDecorationList(decorators, decoratorsThis, version) {
|
|
304
|
-
const decsCount = decorators.length;
|
|
305
|
-
const hasOneThis = decoratorsThis.some(Boolean);
|
|
306
|
-
const decs = [];
|
|
307
|
-
for (let i = 0; i < decsCount; i++) {
|
|
308
|
-
if (version === "2023-05" && hasOneThis) {
|
|
309
|
-
decs.push(decoratorsThis[i] || types.unaryExpression("void", types.numericLiteral(0)));
|
|
310
|
-
}
|
|
311
|
-
decs.push(decorators[i]);
|
|
312
|
-
}
|
|
313
|
-
return {
|
|
314
|
-
hasThis: hasOneThis,
|
|
315
|
-
decs
|
|
316
|
-
};
|
|
317
|
-
}
|
|
318
|
-
function generateDecorationExprs(info, version) {
|
|
319
|
-
return types.arrayExpression(filteredOrderedDecoratorInfo(info).map(el => {
|
|
320
|
-
const {
|
|
321
|
-
decs,
|
|
322
|
-
hasThis
|
|
323
|
-
} = generateDecorationList(el.decorators, el.decoratorsThis, version);
|
|
324
|
-
let flag = el.kind;
|
|
325
|
-
if (el.isStatic) {
|
|
326
|
-
flag += version === "2023-05" ? STATIC : STATIC_OLD_VERSION;
|
|
327
|
-
}
|
|
328
|
-
if (hasThis) flag += DECORATORS_HAVE_THIS;
|
|
329
|
-
return types.arrayExpression([decs.length === 1 ? decs[0] : types.arrayExpression(decs), types.numericLiteral(flag), el.name, ...(el.privateMethods || [])]);
|
|
330
|
-
}));
|
|
331
|
-
}
|
|
332
|
-
function extractElementLocalAssignments(decorationInfo) {
|
|
333
|
-
const localIds = [];
|
|
334
|
-
for (const el of filteredOrderedDecoratorInfo(decorationInfo)) {
|
|
335
|
-
const {
|
|
336
|
-
locals
|
|
337
|
-
} = el;
|
|
338
|
-
if (Array.isArray(locals)) {
|
|
339
|
-
localIds.push(...locals);
|
|
340
|
-
} else if (locals !== undefined) {
|
|
341
|
-
localIds.push(locals);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
return localIds;
|
|
345
|
-
}
|
|
346
|
-
function addCallAccessorsFor(element, key, getId, setId) {
|
|
347
|
-
element.insertAfter(types.classPrivateMethod("get", types.cloneNode(key), [], types.blockStatement([types.returnStatement(types.callExpression(types.cloneNode(getId), [types.thisExpression()]))])));
|
|
348
|
-
element.insertAfter(types.classPrivateMethod("set", types.cloneNode(key), [types.identifier("v")], types.blockStatement([types.expressionStatement(types.callExpression(types.cloneNode(setId), [types.thisExpression(), types.identifier("v")]))])));
|
|
349
|
-
}
|
|
350
|
-
function isNotTsParameter(node) {
|
|
351
|
-
return node.type !== "TSParameterProperty";
|
|
352
|
-
}
|
|
353
|
-
function movePrivateAccessor(element, key, methodLocalVar, isStatic) {
|
|
354
|
-
let params;
|
|
355
|
-
let block;
|
|
356
|
-
if (element.node.kind === "set") {
|
|
357
|
-
params = [types.identifier("v")];
|
|
358
|
-
block = [types.expressionStatement(types.callExpression(methodLocalVar, [types.thisExpression(), types.identifier("v")]))];
|
|
359
|
-
} else {
|
|
360
|
-
params = [];
|
|
361
|
-
block = [types.returnStatement(types.callExpression(methodLocalVar, [types.thisExpression()]))];
|
|
362
|
-
}
|
|
363
|
-
element.replaceWith(types.classPrivateMethod(element.node.kind, types.cloneNode(key), params, types.blockStatement(block), isStatic));
|
|
364
|
-
}
|
|
365
|
-
function isClassDecoratableElementPath(path) {
|
|
366
|
-
const {
|
|
367
|
-
type
|
|
368
|
-
} = path;
|
|
369
|
-
return type !== "TSDeclareMethod" && type !== "TSIndexSignature" && type !== "StaticBlock";
|
|
370
|
-
}
|
|
371
|
-
function staticBlockToIIFE(block) {
|
|
372
|
-
return types.callExpression(types.arrowFunctionExpression([], types.blockStatement(block.body)), []);
|
|
373
|
-
}
|
|
374
|
-
function maybeSequenceExpression(exprs) {
|
|
375
|
-
if (exprs.length === 0) return types.unaryExpression("void", types.numericLiteral(0));
|
|
376
|
-
if (exprs.length === 1) return exprs[0];
|
|
377
|
-
return types.sequenceExpression(exprs);
|
|
378
|
-
}
|
|
379
|
-
function transformClass(path, state, constantSuper, version) {
|
|
380
|
-
const body = path.get("body.body");
|
|
381
|
-
const classDecorators = path.node.decorators;
|
|
382
|
-
let hasElementDecorators = false;
|
|
383
|
-
const generateClassPrivateUid = createLazyPrivateUidGeneratorForClass(path);
|
|
384
|
-
for (const element of body) {
|
|
385
|
-
if (!isClassDecoratableElementPath(element)) {
|
|
386
|
-
continue;
|
|
387
|
-
}
|
|
388
|
-
if (element.node.decorators && element.node.decorators.length > 0) {
|
|
389
|
-
hasElementDecorators = true;
|
|
390
|
-
} else if (element.node.type === "ClassAccessorProperty") {
|
|
391
|
-
const {
|
|
392
|
-
key,
|
|
393
|
-
value,
|
|
394
|
-
static: isStatic,
|
|
395
|
-
computed
|
|
396
|
-
} = element.node;
|
|
397
|
-
const newId = generateClassPrivateUid();
|
|
398
|
-
const valueNode = value ? types.cloneNode(value) : undefined;
|
|
399
|
-
const newField = generateClassProperty(newId, valueNode, isStatic);
|
|
400
|
-
const [newPath] = element.replaceWith(newField);
|
|
401
|
-
addProxyAccessorsFor(path.node.id, newPath, key, newId, version, computed);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
if (!classDecorators && !hasElementDecorators) return;
|
|
405
|
-
const elementDecoratorInfo = [];
|
|
406
|
-
let firstFieldPath;
|
|
407
|
-
let constructorPath;
|
|
408
|
-
let requiresProtoInit = false;
|
|
409
|
-
let requiresStaticInit = false;
|
|
410
|
-
const decoratedPrivateMethods = new Set();
|
|
411
|
-
let protoInitLocal, staticInitLocal, classInitLocal, classIdLocal;
|
|
412
|
-
const assignments = [];
|
|
413
|
-
const scopeParent = path.scope.parent;
|
|
414
|
-
const memoiseExpression = (expression, hint) => {
|
|
415
|
-
const localEvaluatedId = scopeParent.generateDeclaredUidIdentifier(hint);
|
|
416
|
-
assignments.push(types.assignmentExpression("=", localEvaluatedId, expression));
|
|
417
|
-
return types.cloneNode(localEvaluatedId);
|
|
418
|
-
};
|
|
419
|
-
const decoratorsThis = new Map();
|
|
420
|
-
const maybeExtractDecorator = decorator => {
|
|
421
|
-
const {
|
|
422
|
-
expression
|
|
423
|
-
} = decorator;
|
|
424
|
-
if (version === "2023-05" && types.isMemberExpression(expression)) {
|
|
425
|
-
let object;
|
|
426
|
-
if (types.isSuper(expression.object) || types.isThisExpression(expression.object)) {
|
|
427
|
-
object = memoiseExpression(types.thisExpression(), "obj");
|
|
428
|
-
} else if (!scopeParent.isStatic(expression.object)) {
|
|
429
|
-
object = memoiseExpression(expression.object, "obj");
|
|
430
|
-
expression.object = object;
|
|
431
|
-
} else {
|
|
432
|
-
object = expression.object;
|
|
433
|
-
}
|
|
434
|
-
decoratorsThis.set(decorator, types.cloneNode(object));
|
|
435
|
-
}
|
|
436
|
-
if (!scopeParent.isStatic(expression)) {
|
|
437
|
-
decorator.expression = memoiseExpression(expression, "dec");
|
|
438
|
-
}
|
|
439
|
-
};
|
|
440
|
-
if (classDecorators) {
|
|
441
|
-
classInitLocal = scopeParent.generateDeclaredUidIdentifier("initClass");
|
|
442
|
-
const [classId, classPath] = replaceClassWithVar(path);
|
|
443
|
-
path = classPath;
|
|
444
|
-
classIdLocal = classId;
|
|
445
|
-
path.node.decorators = null;
|
|
446
|
-
for (const classDecorator of classDecorators) {
|
|
447
|
-
maybeExtractDecorator(classDecorator);
|
|
448
|
-
}
|
|
449
|
-
} else {
|
|
450
|
-
if (!path.node.id) {
|
|
451
|
-
path.node.id = path.scope.generateUidIdentifier("Class");
|
|
452
|
-
}
|
|
453
|
-
classIdLocal = types.cloneNode(path.node.id);
|
|
454
|
-
}
|
|
455
|
-
let lastInstancePrivateName;
|
|
456
|
-
let needsInstancePrivateBrandCheck = false;
|
|
457
|
-
if (hasElementDecorators) {
|
|
458
|
-
for (const element of body) {
|
|
459
|
-
if (!isClassDecoratableElementPath(element)) {
|
|
460
|
-
continue;
|
|
461
|
-
}
|
|
462
|
-
const {
|
|
463
|
-
node
|
|
464
|
-
} = element;
|
|
465
|
-
const decorators = element.get("decorators");
|
|
466
|
-
const hasDecorators = Array.isArray(decorators) && decorators.length > 0;
|
|
467
|
-
if (hasDecorators) {
|
|
468
|
-
for (const decoratorPath of decorators) {
|
|
469
|
-
maybeExtractDecorator(decoratorPath.node);
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
const isComputed = "computed" in element.node && element.node.computed === true;
|
|
473
|
-
if (isComputed) {
|
|
474
|
-
if (!scopeParent.isStatic(node.key)) {
|
|
475
|
-
node.key = memoiseExpression(node.key, "computedKey");
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
const kind = getElementKind(element);
|
|
479
|
-
const {
|
|
480
|
-
key
|
|
481
|
-
} = node;
|
|
482
|
-
const isPrivate = key.type === "PrivateName";
|
|
483
|
-
const isStatic = !!element.node.static;
|
|
484
|
-
let name = "computedKey";
|
|
485
|
-
if (isPrivate) {
|
|
486
|
-
name = key.id.name;
|
|
487
|
-
} else if (!isComputed && key.type === "Identifier") {
|
|
488
|
-
name = key.name;
|
|
489
|
-
}
|
|
490
|
-
if (isPrivate && !isStatic) {
|
|
491
|
-
if (hasDecorators) {
|
|
492
|
-
needsInstancePrivateBrandCheck = true;
|
|
493
|
-
}
|
|
494
|
-
if (types.isClassPrivateProperty(node) || !lastInstancePrivateName) {
|
|
495
|
-
lastInstancePrivateName = key;
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
if (element.isClassMethod({
|
|
499
|
-
kind: "constructor"
|
|
500
|
-
})) {
|
|
501
|
-
constructorPath = element;
|
|
502
|
-
}
|
|
503
|
-
if (hasDecorators) {
|
|
504
|
-
let locals;
|
|
505
|
-
let privateMethods;
|
|
506
|
-
if (kind === ACCESSOR) {
|
|
507
|
-
const {
|
|
508
|
-
value
|
|
509
|
-
} = element.node;
|
|
510
|
-
const params = [types.thisExpression()];
|
|
511
|
-
if (value) {
|
|
512
|
-
params.push(types.cloneNode(value));
|
|
513
|
-
}
|
|
514
|
-
const newId = generateClassPrivateUid();
|
|
515
|
-
const newFieldInitId = element.scope.parent.generateDeclaredUidIdentifier(`init_${name}`);
|
|
516
|
-
const newValue = types.callExpression(types.cloneNode(newFieldInitId), params);
|
|
517
|
-
const newField = generateClassProperty(newId, newValue, isStatic);
|
|
518
|
-
const [newPath] = element.replaceWith(newField);
|
|
519
|
-
if (isPrivate) {
|
|
520
|
-
privateMethods = extractProxyAccessorsFor(newId, version);
|
|
521
|
-
const getId = newPath.scope.parent.generateDeclaredUidIdentifier(`get_${name}`);
|
|
522
|
-
const setId = newPath.scope.parent.generateDeclaredUidIdentifier(`set_${name}`);
|
|
523
|
-
addCallAccessorsFor(newPath, key, getId, setId);
|
|
524
|
-
locals = [newFieldInitId, getId, setId];
|
|
525
|
-
} else {
|
|
526
|
-
addProxyAccessorsFor(path.node.id, newPath, key, newId, version, isComputed);
|
|
527
|
-
locals = newFieldInitId;
|
|
528
|
-
}
|
|
529
|
-
} else if (kind === FIELD) {
|
|
530
|
-
const initId = element.scope.parent.generateDeclaredUidIdentifier(`init_${name}`);
|
|
531
|
-
const valuePath = element.get("value");
|
|
532
|
-
valuePath.replaceWith(types.callExpression(types.cloneNode(initId), [types.thisExpression(), valuePath.node].filter(v => v)));
|
|
533
|
-
locals = initId;
|
|
534
|
-
if (isPrivate) {
|
|
535
|
-
privateMethods = extractProxyAccessorsFor(key, version);
|
|
536
|
-
}
|
|
537
|
-
} else if (isPrivate) {
|
|
538
|
-
locals = element.scope.parent.generateDeclaredUidIdentifier(`call_${name}`);
|
|
539
|
-
const replaceSupers = new ReplaceSupers({
|
|
540
|
-
constantSuper,
|
|
541
|
-
methodPath: element,
|
|
542
|
-
objectRef: classIdLocal,
|
|
543
|
-
superRef: path.node.superClass,
|
|
544
|
-
file: state.file,
|
|
545
|
-
refToPreserve: classIdLocal
|
|
546
|
-
});
|
|
547
|
-
replaceSupers.replace();
|
|
548
|
-
const {
|
|
549
|
-
params,
|
|
550
|
-
body,
|
|
551
|
-
async: isAsync
|
|
552
|
-
} = element.node;
|
|
553
|
-
privateMethods = [types.functionExpression(undefined, params.filter(isNotTsParameter), body, isAsync)];
|
|
554
|
-
if (kind === GETTER || kind === SETTER) {
|
|
555
|
-
movePrivateAccessor(element, types.cloneNode(key), types.cloneNode(locals), isStatic);
|
|
556
|
-
} else {
|
|
557
|
-
const node = element.node;
|
|
558
|
-
path.node.body.body.unshift(types.classPrivateProperty(key, types.cloneNode(locals), [], node.static));
|
|
559
|
-
decoratedPrivateMethods.add(key.id.name);
|
|
560
|
-
element.remove();
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
let nameExpr;
|
|
564
|
-
if (isComputed) {
|
|
565
|
-
nameExpr = types.cloneNode(key);
|
|
566
|
-
} else if (key.type === "PrivateName") {
|
|
567
|
-
nameExpr = types.stringLiteral(key.id.name);
|
|
568
|
-
} else if (key.type === "Identifier") {
|
|
569
|
-
nameExpr = types.stringLiteral(key.name);
|
|
570
|
-
} else {
|
|
571
|
-
nameExpr = types.cloneNode(key);
|
|
572
|
-
}
|
|
573
|
-
elementDecoratorInfo.push({
|
|
574
|
-
kind,
|
|
575
|
-
decorators: decorators.map(d => d.node.expression),
|
|
576
|
-
decoratorsThis: decorators.map(d => decoratorsThis.get(d.node)),
|
|
577
|
-
name: nameExpr,
|
|
578
|
-
isStatic,
|
|
579
|
-
privateMethods,
|
|
580
|
-
locals
|
|
581
|
-
});
|
|
582
|
-
if (kind !== FIELD) {
|
|
583
|
-
if (isStatic) {
|
|
584
|
-
requiresStaticInit = true;
|
|
585
|
-
} else {
|
|
586
|
-
requiresProtoInit = true;
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
if (element.node) {
|
|
590
|
-
element.node.decorators = null;
|
|
591
|
-
}
|
|
592
|
-
if (!firstFieldPath && !isStatic && (kind === FIELD || kind === ACCESSOR)) {
|
|
593
|
-
firstFieldPath = element;
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
const elementDecorations = generateDecorationExprs(elementDecoratorInfo, version);
|
|
599
|
-
let classDecorationsFlag = 0;
|
|
600
|
-
let classDecorations = [];
|
|
601
|
-
if (classDecorators) {
|
|
602
|
-
const {
|
|
603
|
-
hasThis,
|
|
604
|
-
decs
|
|
605
|
-
} = generateDecorationList(classDecorators.map(el => el.expression), classDecorators.map(dec => decoratorsThis.get(dec)), version);
|
|
606
|
-
classDecorationsFlag = hasThis ? 1 : 0;
|
|
607
|
-
classDecorations = decs;
|
|
608
|
-
}
|
|
609
|
-
const elementLocals = extractElementLocalAssignments(elementDecoratorInfo);
|
|
610
|
-
if (requiresProtoInit) {
|
|
611
|
-
protoInitLocal = scopeParent.generateDeclaredUidIdentifier("initProto");
|
|
612
|
-
elementLocals.push(protoInitLocal);
|
|
613
|
-
const protoInitCall = types.callExpression(types.cloneNode(protoInitLocal), [types.thisExpression()]);
|
|
614
|
-
if (firstFieldPath) {
|
|
615
|
-
const value = firstFieldPath.get("value");
|
|
616
|
-
const body = [protoInitCall];
|
|
617
|
-
if (value.node) {
|
|
618
|
-
body.push(value.node);
|
|
619
|
-
}
|
|
620
|
-
value.replaceWith(types.sequenceExpression(body));
|
|
621
|
-
} else if (constructorPath) {
|
|
622
|
-
if (path.node.superClass) {
|
|
623
|
-
path.traverse({
|
|
624
|
-
CallExpression: {
|
|
625
|
-
exit(path) {
|
|
626
|
-
if (!path.get("callee").isSuper()) return;
|
|
627
|
-
path.replaceWith(types.callExpression(types.cloneNode(protoInitLocal), [path.node]));
|
|
628
|
-
path.skip();
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
});
|
|
632
|
-
} else {
|
|
633
|
-
constructorPath.node.body.body.unshift(types.expressionStatement(protoInitCall));
|
|
634
|
-
}
|
|
635
|
-
} else {
|
|
636
|
-
const body = [types.expressionStatement(protoInitCall)];
|
|
637
|
-
if (path.node.superClass) {
|
|
638
|
-
body.unshift(types.expressionStatement(types.callExpression(types.super(), [types.spreadElement(types.identifier("args"))])));
|
|
639
|
-
}
|
|
640
|
-
path.node.body.body.unshift(types.classMethod("constructor", types.identifier("constructor"), [types.restElement(types.identifier("args"))], types.blockStatement(body)));
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
if (requiresStaticInit) {
|
|
644
|
-
staticInitLocal = scopeParent.generateDeclaredUidIdentifier("initStatic");
|
|
645
|
-
elementLocals.push(staticInitLocal);
|
|
646
|
-
}
|
|
647
|
-
if (decoratedPrivateMethods.size > 0) {
|
|
648
|
-
path.traverse({
|
|
649
|
-
PrivateName(path) {
|
|
650
|
-
if (!decoratedPrivateMethods.has(path.node.id.name)) return;
|
|
651
|
-
const parentPath = path.parentPath;
|
|
652
|
-
const parentParentPath = parentPath.parentPath;
|
|
653
|
-
if (parentParentPath.node.type === "AssignmentExpression" && parentParentPath.node.left === parentPath.node || parentParentPath.node.type === "UpdateExpression" || parentParentPath.node.type === "RestElement" || parentParentPath.node.type === "ArrayPattern" || parentParentPath.node.type === "ObjectProperty" && parentParentPath.node.value === parentPath.node && parentParentPath.parentPath.type === "ObjectPattern" || parentParentPath.node.type === "ForOfStatement" && parentParentPath.node.left === parentPath.node) {
|
|
654
|
-
throw path.buildCodeFrameError(`Decorated private methods are not updatable, but "#${path.node.id.name}" is updated via this expression.`);
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
});
|
|
658
|
-
}
|
|
659
|
-
const classLocals = [];
|
|
660
|
-
let classInitInjected = false;
|
|
661
|
-
const classInitCall = classInitLocal && types.callExpression(types.cloneNode(classInitLocal), []);
|
|
662
|
-
const originalClass = path.node;
|
|
663
|
-
if (classDecorators) {
|
|
664
|
-
classLocals.push(classIdLocal, classInitLocal);
|
|
665
|
-
const statics = [];
|
|
666
|
-
let staticBlocks = [];
|
|
667
|
-
path.get("body.body").forEach(element => {
|
|
668
|
-
if (element.isStaticBlock()) {
|
|
669
|
-
staticBlocks.push(element.node);
|
|
670
|
-
element.remove();
|
|
671
|
-
return;
|
|
672
|
-
}
|
|
673
|
-
const isProperty = element.isClassProperty() || element.isClassPrivateProperty();
|
|
674
|
-
if ((isProperty || element.isClassPrivateMethod()) && element.node.static) {
|
|
675
|
-
if (isProperty && staticBlocks.length > 0) {
|
|
676
|
-
const allValues = staticBlocks.map(staticBlockToIIFE);
|
|
677
|
-
if (element.node.value) allValues.push(element.node.value);
|
|
678
|
-
element.node.value = maybeSequenceExpression(allValues);
|
|
679
|
-
staticBlocks = [];
|
|
680
|
-
}
|
|
681
|
-
element.node.static = false;
|
|
682
|
-
statics.push(element.node);
|
|
683
|
-
element.remove();
|
|
684
|
-
}
|
|
685
|
-
});
|
|
686
|
-
if (statics.length > 0 || staticBlocks.length > 0) {
|
|
687
|
-
const staticsClass = template.expression.ast`
|
|
688
|
-
class extends ${state.addHelper("identity")} {}
|
|
689
|
-
`;
|
|
690
|
-
staticsClass.body.body = [types.staticBlock([types.toStatement(originalClass, true) || types.expressionStatement(originalClass)]), ...statics];
|
|
691
|
-
const constructorBody = [];
|
|
692
|
-
const newExpr = types.newExpression(staticsClass, []);
|
|
693
|
-
if (staticBlocks.length > 0) {
|
|
694
|
-
constructorBody.push(...staticBlocks.map(staticBlockToIIFE));
|
|
695
|
-
}
|
|
696
|
-
if (classInitCall) {
|
|
697
|
-
classInitInjected = true;
|
|
698
|
-
constructorBody.push(classInitCall);
|
|
699
|
-
}
|
|
700
|
-
if (constructorBody.length > 0) {
|
|
701
|
-
constructorBody.unshift(types.callExpression(types.super(), [types.cloneNode(classIdLocal)]));
|
|
702
|
-
staticsClass.body.body.push(types.classMethod("constructor", types.identifier("constructor"), [], types.blockStatement([types.expressionStatement(types.sequenceExpression(constructorBody))])));
|
|
703
|
-
} else {
|
|
704
|
-
newExpr.arguments.push(types.cloneNode(classIdLocal));
|
|
705
|
-
}
|
|
706
|
-
path.replaceWith(newExpr);
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
if (!classInitInjected && classInitCall) {
|
|
710
|
-
path.node.body.body.push(types.staticBlock([types.expressionStatement(classInitCall)]));
|
|
711
|
-
}
|
|
712
|
-
originalClass.body.body.unshift(types.staticBlock([types.expressionStatement(createLocalsAssignment(elementLocals, classLocals, elementDecorations, types.arrayExpression(classDecorations), types.numericLiteral(classDecorationsFlag), needsInstancePrivateBrandCheck ? lastInstancePrivateName : null, state)), requiresStaticInit && types.expressionStatement(types.callExpression(types.cloneNode(staticInitLocal), [types.thisExpression()]))].filter(Boolean)));
|
|
713
|
-
path.insertBefore(assignments.map(expr => types.expressionStatement(expr)));
|
|
714
|
-
path.scope.crawl();
|
|
715
|
-
return path;
|
|
716
|
-
}
|
|
717
|
-
function createLocalsAssignment(elementLocals, classLocals, elementDecorations, classDecorations, classDecorationsFlag, maybePrivateBranName, state, version) {
|
|
718
|
-
let lhs, rhs;
|
|
719
|
-
const args = [types.thisExpression(), elementDecorations, classDecorations];
|
|
720
|
-
{
|
|
721
|
-
if (maybePrivateBranName || classDecorationsFlag.value !== 0) {
|
|
722
|
-
args.push(classDecorationsFlag);
|
|
723
|
-
}
|
|
724
|
-
if (maybePrivateBranName) {
|
|
725
|
-
args.push(template.expression.ast`
|
|
726
|
-
_ => ${types.cloneNode(maybePrivateBranName)} in _
|
|
727
|
-
`);
|
|
728
|
-
}
|
|
729
|
-
rhs = types.callExpression(state.addHelper("applyDecs2305"), args);
|
|
730
|
-
}
|
|
731
|
-
if (elementLocals.length > 0) {
|
|
732
|
-
if (classLocals.length > 0) {
|
|
733
|
-
lhs = types.objectPattern([types.objectProperty(types.identifier("e"), types.arrayPattern(elementLocals)), types.objectProperty(types.identifier("c"), types.arrayPattern(classLocals))]);
|
|
734
|
-
} else {
|
|
735
|
-
lhs = types.arrayPattern(elementLocals);
|
|
736
|
-
rhs = types.memberExpression(rhs, types.identifier("e"), false, false);
|
|
737
|
-
}
|
|
738
|
-
} else {
|
|
739
|
-
lhs = types.arrayPattern(classLocals);
|
|
740
|
-
rhs = types.memberExpression(rhs, types.identifier("c"), false, false);
|
|
741
|
-
}
|
|
742
|
-
return types.assignmentExpression("=", lhs, rhs);
|
|
743
|
-
}
|
|
744
|
-
function transformer2023_05 ({
|
|
745
|
-
assertVersion,
|
|
746
|
-
assumption
|
|
747
|
-
}, {
|
|
748
|
-
loose
|
|
749
|
-
}, version) {
|
|
750
|
-
{
|
|
751
|
-
assertVersion("^7.21.0");
|
|
752
|
-
}
|
|
753
|
-
const VISITED = new WeakSet();
|
|
754
|
-
const constantSuper = assumption("constantSuper") ?? loose;
|
|
755
|
-
return {
|
|
756
|
-
name: "proposal-decorators",
|
|
757
|
-
inherits: syntaxDecorators,
|
|
758
|
-
visitor: {
|
|
759
|
-
"ExportNamedDeclaration|ExportDefaultDeclaration"(path) {
|
|
760
|
-
const {
|
|
761
|
-
declaration
|
|
762
|
-
} = path.node;
|
|
763
|
-
if (declaration?.type === "ClassDeclaration" && declaration.decorators?.length > 0) {
|
|
764
|
-
splitExportDeclaration(path);
|
|
765
|
-
}
|
|
766
|
-
},
|
|
767
|
-
Class(path, state) {
|
|
768
|
-
if (VISITED.has(path)) return;
|
|
769
|
-
const newPath = transformClass(path, state, constantSuper, version);
|
|
770
|
-
if (newPath) VISITED.add(newPath);
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
};
|
|
774
|
-
}
|
|
775
|
-
|
|
776
158
|
var index = declare((api, options) => {
|
|
777
|
-
api.assertVersion(
|
|
159
|
+
api.assertVersion("8.0.0-alpha.11");
|
|
778
160
|
const {
|
|
779
161
|
version
|
|
780
162
|
} = options;
|
|
@@ -784,10 +166,17 @@ var index = declare((api, options) => {
|
|
|
784
166
|
inherits: syntaxDecorators,
|
|
785
167
|
visitor: visitor
|
|
786
168
|
};
|
|
787
|
-
} else if (version === "2021-12" || version === "2022-03" || version === "2023-01" || version === "2023-05") {
|
|
788
|
-
|
|
169
|
+
} else if (!version || version === "2018-09" || version === "2021-12" || version === "2022-03" || version === "2023-01" || version === "2023-05" || version === "2023-11") {
|
|
170
|
+
api.assertVersion("8.0.0-alpha.11");
|
|
171
|
+
return createClassFeaturePlugin({
|
|
172
|
+
name: "proposal-decorators",
|
|
173
|
+
api,
|
|
174
|
+
feature: FEATURES.decorators,
|
|
175
|
+
inherits: syntaxDecorators,
|
|
176
|
+
decoratorVersion: version
|
|
177
|
+
});
|
|
789
178
|
} else {
|
|
790
|
-
throw new Error("The '.version' option must be one of 'legacy', '
|
|
179
|
+
throw new Error("The '.version' option must be one of 'legacy', '2023-11', '2023-05', '2023-01', '2022-03', or '2021-12'.");
|
|
791
180
|
}
|
|
792
181
|
});
|
|
793
182
|
|