12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.buildNamedEvaluationVisitor = buildNamedEvaluationVisitor;
- exports.default = _default;
- exports.hasDecorators = hasDecorators;
- exports.hasOwnDecorators = hasOwnDecorators;
- var _core = require("@babel/core");
- var _helperReplaceSupers = require("@babel/helper-replace-supers");
- var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
- var _fields = require("./fields.js");
- var _misc = require("./misc.js");
- function hasOwnDecorators(node) {
- var _node$decorators;
- return !!((_node$decorators = node.decorators) != null && _node$decorators.length);
- }
- function hasDecorators(node) {
- return hasOwnDecorators(node) || node.body.body.some(hasOwnDecorators);
- }
- function incrementId(id, idx = id.length - 1) {
- if (idx === -1) {
- id.unshift(65);
- return;
- }
- const current = id[idx];
- if (current === 90) {
- id[idx] = 97;
- } else if (current === 122) {
- id[idx] = 65;
- incrementId(id, idx - 1);
- } else {
- id[idx] = current + 1;
- }
- }
- function createPrivateUidGeneratorForClass(classPath) {
- const currentPrivateId = [];
- const privateNames = new Set();
- classPath.traverse({
- PrivateName(path) {
- privateNames.add(path.node.id.name);
- }
- });
- return () => {
- let reifiedId;
- do {
- incrementId(currentPrivateId);
- reifiedId = String.fromCharCode(...currentPrivateId);
- } while (privateNames.has(reifiedId));
- return _core.types.privateName(_core.types.identifier(reifiedId));
- };
- }
- function createLazyPrivateUidGeneratorForClass(classPath) {
- let generator;
- return () => {
- if (!generator) {
- generator = createPrivateUidGeneratorForClass(classPath);
- }
- return generator();
- };
- }
- function replaceClassWithVar(path, className) {
- const id = path.node.id;
- const scope = path.scope;
- if (path.type === "ClassDeclaration") {
- const className = id.name;
- const varId = scope.generateUidIdentifierBasedOnNode(id);
- const classId = _core.types.identifier(className);
- scope.rename(className, varId.name);
- path.get("id").replaceWith(classId);
- return {
- id: _core.types.cloneNode(varId),
- path
- };
- } else {
- let varId;
- if (id) {
- className = id.name;
- varId = generateLetUidIdentifier(scope.parent, className);
- scope.rename(className, varId.name);
- } else {
- varId = generateLetUidIdentifier(scope.parent, typeof className === "string" ? className : "decorated_class");
- }
- const newClassExpr = _core.types.classExpression(typeof className === "string" ? _core.types.identifier(className) : null, path.node.superClass, path.node.body);
- const [newPath] = path.replaceWith(_core.types.sequenceExpression([newClassExpr, varId]));
- return {
- id: _core.types.cloneNode(varId),
- path: newPath.get("expressions.0")
- };
- }
- }
- function generateClassProperty(key, value, isStatic) {
- if (key.type === "PrivateName") {
- return _core.types.classPrivateProperty(key, value, undefined, isStatic);
- } else {
- return _core.types.classProperty(key, value, undefined, undefined, isStatic);
- }
- }
- function assignIdForAnonymousClass(path, className) {
- if (!path.node.id) {
- path.node.id = typeof className === "string" ? _core.types.identifier(className) : path.scope.generateUidIdentifier("Class");
- }
- }
- function addProxyAccessorsFor(className, element, getterKey, setterKey, targetKey, isComputed, isStatic, version) {
- const thisArg = (version === "2023-11" || version === "2023-05") && isStatic ? className : _core.types.thisExpression();
- const getterBody = _core.types.blockStatement([_core.types.returnStatement(_core.types.memberExpression(_core.types.cloneNode(thisArg), _core.types.cloneNode(targetKey)))]);
- 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")))]);
- let getter, setter;
- if (getterKey.type === "PrivateName") {
- getter = _core.types.classPrivateMethod("get", getterKey, [], getterBody, isStatic);
- setter = _core.types.classPrivateMethod("set", setterKey, [_core.types.identifier("v")], setterBody, isStatic);
- } else {
- getter = _core.types.classMethod("get", getterKey, [], getterBody, isComputed, isStatic);
- setter = _core.types.classMethod("set", setterKey, [_core.types.identifier("v")], setterBody, isComputed, isStatic);
- }
- element.insertAfter(setter);
- element.insertAfter(getter);
- }
- function extractProxyAccessorsFor(targetKey, version) {
- if (version !== "2023-11" && version !== "2023-05" && version !== "2023-01") {
- return [_core.template.expression.ast`
- function () {
- return this.${_core.types.cloneNode(targetKey)};
- }
- `, _core.template.expression.ast`
- function (value) {
- this.${_core.types.cloneNode(targetKey)} = value;
- }
- `];
- }
- return [_core.template.expression.ast`
- o => o.${_core.types.cloneNode(targetKey)}
- `, _core.template.expression.ast`
- (o, v) => o.${_core.types.cloneNode(targetKey)} = v
- `];
- }
- function getComputedKeyLastElement(path) {
- path = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path);
- if (path.isSequenceExpression()) {
- const expressions = path.get("expressions");
- return getComputedKeyLastElement(expressions[expressions.length - 1]);
- }
- return path;
- }
- function getComputedKeyMemoiser(path) {
- const element = getComputedKeyLastElement(path);
- if (element.isConstantExpression()) {
- return _core.types.cloneNode(path.node);
- } else if (element.isIdentifier() && path.scope.hasUid(element.node.name)) {
- return _core.types.cloneNode(path.node);
- } else if (element.isAssignmentExpression() && element.get("left").isIdentifier()) {
- return _core.types.cloneNode(element.node.left);
- } else {
- throw new Error(`Internal Error: the computed key ${path.toString()} has not yet been memoised.`);
- }
- }
- function prependExpressionsToComputedKey(expressions, fieldPath) {
- const key = fieldPath.get("key");
- if (key.isSequenceExpression()) {
- expressions.push(...key.node.expressions);
- } else {
- expressions.push(key.node);
- }
- key.replaceWith(maybeSequenceExpression(expressions));
- }
- function appendExpressionsToComputedKey(expressions, fieldPath) {
- const key = fieldPath.get("key");
- const completion = getComputedKeyLastElement(key);
- if (completion.isConstantExpression()) {
- prependExpressionsToComputedKey(expressions, fieldPath);
- } else {
- const scopeParent = key.scope.parent;
- const maybeAssignment = (0, _misc.memoiseComputedKey)(completion.node, scopeParent, scopeParent.generateUid("computedKey"));
- if (!maybeAssignment) {
- prependExpressionsToComputedKey(expressions, fieldPath);
- } else {
- const expressionSequence = [...expressions, _core.types.cloneNode(maybeAssignment.left)];
- const completionParent = completion.parentPath;
- if (completionParent.isSequenceExpression()) {
- completionParent.pushContainer("expressions", expressionSequence);
- } else {
- completion.replaceWith(maybeSequenceExpression([_core.types.cloneNode(maybeAssignment), ...expressionSequence]));
- }
- }
- }
- }
- function prependExpressionsToFieldInitializer(expressions, fieldPath) {
- const initializer = fieldPath.get("value");
- if (initializer.node) {
- expressions.push(initializer.node);
- } else if (expressions.length > 0) {
- expressions[expressions.length - 1] = _core.types.unaryExpression("void", expressions[expressions.length - 1]);
- }
- initializer.replaceWith(maybeSequenceExpression(expressions));
- }
- function prependExpressionsToStaticBlock(expressions, blockPath) {
- blockPath.unshiftContainer("body", _core.types.expressionStatement(maybeSequenceExpression(expressions)));
- }
- function prependExpressionsToConstructor(expressions, constructorPath) {
- constructorPath.node.body.body.unshift(_core.types.expressionStatement(maybeSequenceExpression(expressions)));
- }
- function isProtoInitCallExpression(expression, protoInitCall) {
- return _core.types.isCallExpression(expression) && _core.types.isIdentifier(expression.callee, {
- name: protoInitCall.name
- });
- }
- function optimizeSuperCallAndExpressions(expressions, protoInitLocal) {
- if (protoInitLocal) {
- if (expressions.length >= 2 && isProtoInitCallExpression(expressions[1], protoInitLocal)) {
- const mergedSuperCall = _core.types.callExpression(_core.types.cloneNode(protoInitLocal), [expressions[0]]);
- expressions.splice(0, 2, mergedSuperCall);
- }
- if (expressions.length >= 2 && _core.types.isThisExpression(expressions[expressions.length - 1]) && isProtoInitCallExpression(expressions[expressions.length - 2], protoInitLocal)) {
- expressions.splice(expressions.length - 1, 1);
- }
- }
- return maybeSequenceExpression(expressions);
- }
- function insertExpressionsAfterSuperCallAndOptimize(expressions, constructorPath, protoInitLocal) {
- constructorPath.traverse({
- CallExpression: {
- exit(path) {
- if (!path.get("callee").isSuper()) return;
- const newNodes = [path.node, ...expressions.map(expr => _core.types.cloneNode(expr))];
- if (path.isCompletionRecord()) {
- newNodes.push(_core.types.thisExpression());
- }
- path.replaceWith(optimizeSuperCallAndExpressions(newNodes, protoInitLocal));
- path.skip();
- }
- },
- ClassMethod(path) {
- if (path.node.kind === "constructor") {
- path.skip();
- }
- }
- });
- }
- function createConstructorFromExpressions(expressions, isDerivedClass) {
- const body = [_core.types.expressionStatement(maybeSequenceExpression(expressions))];
- if (isDerivedClass) {
- body.unshift(_core.types.expressionStatement(_core.types.callExpression(_core.types.super(), [_core.types.spreadElement(_core.types.identifier("args"))])));
- }
- return _core.types.classMethod("constructor", _core.types.identifier("constructor"), isDerivedClass ? [_core.types.restElement(_core.types.identifier("args"))] : [], _core.types.blockStatement(body));
- }
- function createStaticBlockFromExpressions(expressions) {
- return _core.types.staticBlock([_core.types.expressionStatement(maybeSequenceExpression(expressions))]);
- }
- const FIELD = 0;
- const ACCESSOR = 1;
- const METHOD = 2;
- const GETTER = 3;
- const SETTER = 4;
- const STATIC_OLD_VERSION = 5;
- const STATIC = 8;
- const DECORATORS_HAVE_THIS = 16;
- function getElementKind(element) {
- switch (element.node.type) {
- case "ClassProperty":
- case "ClassPrivateProperty":
- return FIELD;
- case "ClassAccessorProperty":
- return ACCESSOR;
- case "ClassMethod":
- case "ClassPrivateMethod":
- if (element.node.kind === "get") {
- return GETTER;
- } else if (element.node.kind === "set") {
- return SETTER;
- } else {
- return METHOD;
- }
- }
- }
- function toSortedDecoratorInfo(info) {
- return [...info.filter(el => el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER), ...info.filter(el => !el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER), ...info.filter(el => el.isStatic && el.kind === FIELD), ...info.filter(el => !el.isStatic && el.kind === FIELD)];
- }
- function generateDecorationList(decorators, decoratorsThis, version) {
- const decsCount = decorators.length;
- const haveOneThis = decoratorsThis.some(Boolean);
- const decs = [];
- for (let i = 0; i < decsCount; i++) {
- if ((version === "2023-11" || version === "2023-05") && haveOneThis) {
- decs.push(decoratorsThis[i] || _core.types.unaryExpression("void", _core.types.numericLiteral(0)));
- }
- decs.push(decorators[i].expression);
- }
- return {
- haveThis: haveOneThis,
- decs
- };
- }
- function generateDecorationExprs(decorationInfo, version) {
- return _core.types.arrayExpression(decorationInfo.map(el => {
- let flag = el.kind;
- if (el.isStatic) {
- flag += version === "2023-11" || version === "2023-05" ? STATIC : STATIC_OLD_VERSION;
- }
- if (el.decoratorsHaveThis) flag += DECORATORS_HAVE_THIS;
- return _core.types.arrayExpression([el.decoratorsArray, _core.types.numericLiteral(flag), el.name, ...(el.privateMethods || [])]);
- }));
- }
- function extractElementLocalAssignments(decorationInfo) {
- const localIds = [];
- for (const el of decorationInfo) {
- const {
- locals
- } = el;
- if (Array.isArray(locals)) {
- localIds.push(...locals);
- } else if (locals !== undefined) {
- localIds.push(locals);
- }
- }
- return localIds;
- }
- function addCallAccessorsFor(version, element, key, getId, setId, isStatic) {
- element.insertAfter(_core.types.classPrivateMethod("get", _core.types.cloneNode(key), [], _core.types.blockStatement([_core.types.returnStatement(_core.types.callExpression(_core.types.cloneNode(getId), version === "2023-11" && isStatic ? [] : [_core.types.thisExpression()]))]), isStatic));
- 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), version === "2023-11" && isStatic ? [_core.types.identifier("v")] : [_core.types.thisExpression(), _core.types.identifier("v")]))]), isStatic));
- }
- function movePrivateAccessor(element, key, methodLocalVar, isStatic) {
- let params;
- let block;
- if (element.node.kind === "set") {
- params = [_core.types.identifier("v")];
- block = [_core.types.expressionStatement(_core.types.callExpression(methodLocalVar, [_core.types.thisExpression(), _core.types.identifier("v")]))];
- } else {
- params = [];
- block = [_core.types.returnStatement(_core.types.callExpression(methodLocalVar, [_core.types.thisExpression()]))];
- }
- element.replaceWith(_core.types.classPrivateMethod(element.node.kind, _core.types.cloneNode(key), params, _core.types.blockStatement(block), isStatic));
- }
- function isClassDecoratableElementPath(path) {
- const {
- type
- } = path;
- return type !== "TSDeclareMethod" && type !== "TSIndexSignature" && type !== "StaticBlock";
- }
- function staticBlockToIIFE(block) {
- return _core.types.callExpression(_core.types.arrowFunctionExpression([], _core.types.blockStatement(block.body)), []);
- }
- function staticBlockToFunctionClosure(block) {
- return _core.types.functionExpression(null, [], _core.types.blockStatement(block.body));
- }
- function fieldInitializerToClosure(value) {
- return _core.types.functionExpression(null, [], _core.types.blockStatement([_core.types.returnStatement(value)]));
- }
- function maybeSequenceExpression(exprs) {
- if (exprs.length === 0) return _core.types.unaryExpression("void", _core.types.numericLiteral(0));
- if (exprs.length === 1) return exprs[0];
- return _core.types.sequenceExpression(exprs);
- }
- function createFunctionExpressionFromPrivateMethod(node) {
- const {
- params,
- body,
- generator: isGenerator,
- async: isAsync
- } = node;
- return _core.types.functionExpression(undefined, params, body, isGenerator, isAsync);
- }
- function createSetFunctionNameCall(state, className) {
- return _core.types.callExpression(state.addHelper("setFunctionName"), [_core.types.thisExpression(), className]);
- }
- function createToPropertyKeyCall(state, propertyKey) {
- return _core.types.callExpression(state.addHelper("toPropertyKey"), [propertyKey]);
- }
- function createPrivateBrandCheckClosure(brandName) {
- return _core.types.arrowFunctionExpression([_core.types.identifier("_")], _core.types.binaryExpression("in", _core.types.cloneNode(brandName), _core.types.identifier("_")));
- }
- function usesPrivateField(expression) {
- {
- try {
- _core.types.traverseFast(expression, node => {
- if (_core.types.isPrivateName(node)) {
- throw null;
- }
- });
- return false;
- } catch (_unused) {
- return true;
- }
- }
- }
- function convertToComputedKey(path) {
- const {
- node
- } = path;
- node.computed = true;
- if (_core.types.isIdentifier(node.key)) {
- node.key = _core.types.stringLiteral(node.key.name);
- }
- }
- function hasInstancePrivateAccess(path, privateNames) {
- let containsInstancePrivateAccess = false;
- if (privateNames.length > 0) {
- const privateNameVisitor = (0, _fields.privateNameVisitorFactory)({
- PrivateName(path, state) {
- if (state.privateNamesMap.has(path.node.id.name)) {
- containsInstancePrivateAccess = true;
- path.stop();
- }
- }
- });
- const privateNamesMap = new Map();
- for (const name of privateNames) {
- privateNamesMap.set(name, null);
- }
- path.traverse(privateNameVisitor, {
- privateNamesMap: privateNamesMap
- });
- }
- return containsInstancePrivateAccess;
- }
- function checkPrivateMethodUpdateError(path, decoratedPrivateMethods) {
- const privateNameVisitor = (0, _fields.privateNameVisitorFactory)({
- PrivateName(path, state) {
- if (!state.privateNamesMap.has(path.node.id.name)) return;
- const parentPath = path.parentPath;
- const parentParentPath = parentPath.parentPath;
- 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) {
- throw path.buildCodeFrameError(`Decorated private methods are read-only, but "#${path.node.id.name}" is updated via this expression.`);
- }
- }
- });
- const privateNamesMap = new Map();
- for (const name of decoratedPrivateMethods) {
- privateNamesMap.set(name, null);
- }
- path.traverse(privateNameVisitor, {
- privateNamesMap: privateNamesMap
- });
- }
- function transformClass(path, state, constantSuper, ignoreFunctionLength, className, propertyVisitor, version) {
- var _path$node$id;
- const body = path.get("body.body");
- const classDecorators = path.node.decorators;
- let hasElementDecorators = false;
- let hasComputedKeysSideEffects = false;
- let elemDecsUseFnContext = false;
- const generateClassPrivateUid = createLazyPrivateUidGeneratorForClass(path);
- const classAssignments = [];
- const scopeParent = path.scope.parent;
- const memoiseExpression = (expression, hint, assignments) => {
- const localEvaluatedId = generateLetUidIdentifier(scopeParent, hint);
- assignments.push(_core.types.assignmentExpression("=", localEvaluatedId, expression));
- return _core.types.cloneNode(localEvaluatedId);
- };
- let protoInitLocal;
- let staticInitLocal;
- const classIdName = (_path$node$id = path.node.id) == null ? void 0 : _path$node$id.name;
- const setClassName = typeof className === "object" ? className : undefined;
- const usesFunctionContextOrYieldAwait = decorator => {
- {
- try {
- _core.types.traverseFast(decorator, node => {
- if (_core.types.isThisExpression(node) || _core.types.isSuper(node) || _core.types.isYieldExpression(node) || _core.types.isAwaitExpression(node) || _core.types.isIdentifier(node, {
- name: "arguments"
- }) || classIdName && _core.types.isIdentifier(node, {
- name: classIdName
- }) || _core.types.isMetaProperty(node) && node.meta.name !== "import") {
- throw null;
- }
- });
- return false;
- } catch (_unused2) {
- return true;
- }
- }
- };
- const instancePrivateNames = [];
- for (const element of body) {
- if (!isClassDecoratableElementPath(element)) {
- continue;
- }
- const elementNode = element.node;
- if (!elementNode.static && _core.types.isPrivateName(elementNode.key)) {
- instancePrivateNames.push(elementNode.key.id.name);
- }
- if (isDecorated(elementNode)) {
- switch (elementNode.type) {
- case "ClassProperty":
- propertyVisitor.ClassProperty(element, state);
- break;
- case "ClassPrivateProperty":
- propertyVisitor.ClassPrivateProperty(element, state);
- break;
- case "ClassAccessorProperty":
- propertyVisitor.ClassAccessorProperty(element, state);
- if (version === "2023-11") {
- break;
- }
- default:
- if (elementNode.static) {
- staticInitLocal != null ? staticInitLocal : staticInitLocal = generateLetUidIdentifier(scopeParent, "initStatic");
- } else {
- protoInitLocal != null ? protoInitLocal : protoInitLocal = generateLetUidIdentifier(scopeParent, "initProto");
- }
- break;
- }
- hasElementDecorators = true;
- elemDecsUseFnContext || (elemDecsUseFnContext = elementNode.decorators.some(usesFunctionContextOrYieldAwait));
- } else if (elementNode.type === "ClassAccessorProperty") {
- propertyVisitor.ClassAccessorProperty(element, state);
- const {
- key,
- value,
- static: isStatic,
- computed
- } = elementNode;
- const newId = generateClassPrivateUid();
- const newField = generateClassProperty(newId, value, isStatic);
- const keyPath = element.get("key");
- const [newPath] = element.replaceWith(newField);
- let getterKey, setterKey;
- if (computed && !keyPath.isConstantExpression()) {
- getterKey = (0, _misc.memoiseComputedKey)(createToPropertyKeyCall(state, key), scopeParent, scopeParent.generateUid("computedKey"));
- setterKey = _core.types.cloneNode(getterKey.left);
- } else {
- getterKey = _core.types.cloneNode(key);
- setterKey = _core.types.cloneNode(key);
- }
- assignIdForAnonymousClass(path, className);
- addProxyAccessorsFor(path.node.id, newPath, getterKey, setterKey, newId, computed, isStatic, version);
- }
- if ("computed" in element.node && element.node.computed) {
- hasComputedKeysSideEffects || (hasComputedKeysSideEffects = !scopeParent.isStatic(element.node.key));
- }
- }
- if (!classDecorators && !hasElementDecorators) {
- if (!path.node.id && typeof className === "string") {
- path.node.id = _core.types.identifier(className);
- }
- if (setClassName) {
- path.node.body.body.unshift(createStaticBlockFromExpressions([createSetFunctionNameCall(state, setClassName)]));
- }
- return;
- }
- const elementDecoratorInfo = [];
- let constructorPath;
- const decoratedPrivateMethods = new Set();
- let classInitLocal, classIdLocal;
- let decoratorReceiverId = null;
- function handleDecorators(decorators) {
- let hasSideEffects = false;
- let usesFnContext = false;
- const decoratorsThis = [];
- for (const decorator of decorators) {
- const {
- expression
- } = decorator;
- let object;
- if ((version === "2023-11" || version === "2023-05") && _core.types.isMemberExpression(expression)) {
- if (_core.types.isSuper(expression.object)) {
- object = _core.types.thisExpression();
- } else if (scopeParent.isStatic(expression.object)) {
- object = _core.types.cloneNode(expression.object);
- } else {
- decoratorReceiverId != null ? decoratorReceiverId : decoratorReceiverId = generateLetUidIdentifier(scopeParent, "obj");
- object = _core.types.assignmentExpression("=", _core.types.cloneNode(decoratorReceiverId), expression.object);
- expression.object = _core.types.cloneNode(decoratorReceiverId);
- }
- }
- decoratorsThis.push(object);
- hasSideEffects || (hasSideEffects = !scopeParent.isStatic(expression));
- usesFnContext || (usesFnContext = usesFunctionContextOrYieldAwait(decorator));
- }
- return {
- hasSideEffects,
- usesFnContext,
- decoratorsThis
- };
- }
- const willExtractSomeElemDecs = hasComputedKeysSideEffects || elemDecsUseFnContext || version !== "2023-11";
- let needsDeclarationForClassBinding = false;
- let classDecorationsFlag = 0;
- let classDecorations = [];
- let classDecorationsId;
- let computedKeyAssignments = [];
- if (classDecorators) {
- classInitLocal = generateLetUidIdentifier(scopeParent, "initClass");
- needsDeclarationForClassBinding = path.isClassDeclaration();
- ({
- id: classIdLocal,
- path
- } = replaceClassWithVar(path, className));
- path.node.decorators = null;
- const classDecsUsePrivateName = classDecorators.some(usesPrivateField);
- const {
- hasSideEffects,
- usesFnContext,
- decoratorsThis
- } = handleDecorators(classDecorators);
- const {
- haveThis,
- decs
- } = generateDecorationList(classDecorators, decoratorsThis, version);
- classDecorationsFlag = haveThis ? 1 : 0;
- classDecorations = decs;
- if (usesFnContext || hasSideEffects && willExtractSomeElemDecs || classDecsUsePrivateName) {
- classDecorationsId = memoiseExpression(_core.types.arrayExpression(classDecorations), "classDecs", classAssignments);
- }
- if (!hasElementDecorators) {
- for (const element of path.get("body.body")) {
- const {
- node
- } = element;
- const isComputed = "computed" in node && node.computed;
- if (isComputed) {
- if (element.isClassProperty({
- static: true
- })) {
- if (!element.get("key").isConstantExpression()) {
- const key = node.key;
- const maybeAssignment = (0, _misc.memoiseComputedKey)(key, scopeParent, scopeParent.generateUid("computedKey"));
- if (maybeAssignment != null) {
- node.key = _core.types.cloneNode(maybeAssignment.left);
- computedKeyAssignments.push(maybeAssignment);
- }
- }
- } else if (computedKeyAssignments.length > 0) {
- prependExpressionsToComputedKey(computedKeyAssignments, element);
- computedKeyAssignments = [];
- }
- }
- }
- }
- } else {
- assignIdForAnonymousClass(path, className);
- classIdLocal = _core.types.cloneNode(path.node.id);
- }
- let lastInstancePrivateName;
- let needsInstancePrivateBrandCheck = false;
- let fieldInitializerExpressions = [];
- let staticFieldInitializerExpressions = [];
- if (hasElementDecorators) {
- if (protoInitLocal) {
- const protoInitCall = _core.types.callExpression(_core.types.cloneNode(protoInitLocal), [_core.types.thisExpression()]);
- fieldInitializerExpressions.push(protoInitCall);
- }
- for (const element of body) {
- if (!isClassDecoratableElementPath(element)) {
- if (staticFieldInitializerExpressions.length > 0 && element.isStaticBlock()) {
- prependExpressionsToStaticBlock(staticFieldInitializerExpressions, element);
- staticFieldInitializerExpressions = [];
- }
- continue;
- }
- const {
- node
- } = element;
- const decorators = node.decorators;
- const hasDecorators = !!(decorators != null && decorators.length);
- const isComputed = "computed" in node && node.computed;
- let name = "computedKey";
- if (node.key.type === "PrivateName") {
- name = node.key.id.name;
- } else if (!isComputed && node.key.type === "Identifier") {
- name = node.key.name;
- }
- let decoratorsArray;
- let decoratorsHaveThis;
- if (hasDecorators) {
- const {
- hasSideEffects,
- usesFnContext,
- decoratorsThis
- } = handleDecorators(decorators);
- const {
- decs,
- haveThis
- } = generateDecorationList(decorators, decoratorsThis, version);
- decoratorsHaveThis = haveThis;
- decoratorsArray = decs.length === 1 ? decs[0] : _core.types.arrayExpression(decs);
- if (usesFnContext || hasSideEffects && willExtractSomeElemDecs) {
- decoratorsArray = memoiseExpression(decoratorsArray, name + "Decs", computedKeyAssignments);
- }
- }
- if (isComputed) {
- if (!element.get("key").isConstantExpression()) {
- const key = node.key;
- const maybeAssignment = (0, _misc.memoiseComputedKey)(hasDecorators ? createToPropertyKeyCall(state, key) : key, scopeParent, scopeParent.generateUid("computedKey"));
- if (maybeAssignment != null) {
- if (classDecorators && element.isClassProperty({
- static: true
- })) {
- node.key = _core.types.cloneNode(maybeAssignment.left);
- computedKeyAssignments.push(maybeAssignment);
- } else {
- node.key = maybeAssignment;
- }
- }
- }
- }
- const {
- key,
- static: isStatic
- } = node;
- const isPrivate = key.type === "PrivateName";
- const kind = getElementKind(element);
- if (isPrivate && !isStatic) {
- if (hasDecorators) {
- needsInstancePrivateBrandCheck = true;
- }
- if (_core.types.isClassPrivateProperty(node) || !lastInstancePrivateName) {
- lastInstancePrivateName = key;
- }
- }
- if (element.isClassMethod({
- kind: "constructor"
- })) {
- constructorPath = element;
- }
- let locals;
- if (hasDecorators) {
- let privateMethods;
- let nameExpr;
- if (isComputed) {
- nameExpr = getComputedKeyMemoiser(element.get("key"));
- } else if (key.type === "PrivateName") {
- nameExpr = _core.types.stringLiteral(key.id.name);
- } else if (key.type === "Identifier") {
- nameExpr = _core.types.stringLiteral(key.name);
- } else {
- nameExpr = _core.types.cloneNode(key);
- }
- if (kind === ACCESSOR) {
- const {
- value
- } = element.node;
- const params = version === "2023-11" && isStatic ? [] : [_core.types.thisExpression()];
- if (value) {
- params.push(_core.types.cloneNode(value));
- }
- const newId = generateClassPrivateUid();
- const newFieldInitId = generateLetUidIdentifier(scopeParent, `init_${name}`);
- const newValue = _core.types.callExpression(_core.types.cloneNode(newFieldInitId), params);
- const newField = generateClassProperty(newId, newValue, isStatic);
- const [newPath] = element.replaceWith(newField);
- if (isPrivate) {
- privateMethods = extractProxyAccessorsFor(newId, version);
- const getId = generateLetUidIdentifier(scopeParent, `get_${name}`);
- const setId = generateLetUidIdentifier(scopeParent, `set_${name}`);
- addCallAccessorsFor(version, newPath, key, getId, setId, isStatic);
- locals = [newFieldInitId, getId, setId];
- } else {
- assignIdForAnonymousClass(path, className);
- addProxyAccessorsFor(path.node.id, newPath, _core.types.cloneNode(key), _core.types.isAssignmentExpression(key) ? _core.types.cloneNode(key.left) : _core.types.cloneNode(key), newId, isComputed, isStatic, version);
- locals = [newFieldInitId];
- }
- } else if (kind === FIELD) {
- const initId = generateLetUidIdentifier(scopeParent, `init_${name}`);
- const valuePath = element.get("value");
- const args = version === "2023-11" && isStatic ? [] : [_core.types.thisExpression()];
- if (valuePath.node) args.push(valuePath.node);
- valuePath.replaceWith(_core.types.callExpression(_core.types.cloneNode(initId), args));
- locals = [initId];
- if (isPrivate) {
- privateMethods = extractProxyAccessorsFor(key, version);
- }
- } else if (isPrivate) {
- const callId = generateLetUidIdentifier(scopeParent, `call_${name}`);
- locals = [callId];
- const replaceSupers = new _helperReplaceSupers.default({
- constantSuper,
- methodPath: element,
- objectRef: classIdLocal,
- superRef: path.node.superClass,
- file: state.file,
- refToPreserve: classIdLocal
- });
- replaceSupers.replace();
- privateMethods = [createFunctionExpressionFromPrivateMethod(element.node)];
- if (kind === GETTER || kind === SETTER) {
- movePrivateAccessor(element, _core.types.cloneNode(key), _core.types.cloneNode(callId), isStatic);
- } else {
- const node = element.node;
- path.node.body.body.unshift(_core.types.classPrivateProperty(key, _core.types.cloneNode(callId), [], node.static));
- decoratedPrivateMethods.add(key.id.name);
- element.remove();
- }
- }
- elementDecoratorInfo.push({
- kind,
- decoratorsArray,
- decoratorsHaveThis,
- name: nameExpr,
- isStatic,
- privateMethods,
- locals
- });
- if (element.node) {
- element.node.decorators = null;
- }
- }
- if (isComputed && computedKeyAssignments.length > 0) {
- if (classDecorators && element.isClassProperty({
- static: true
- })) {} else {
- prependExpressionsToComputedKey(computedKeyAssignments, kind === ACCESSOR ? element.getNextSibling() : element);
- computedKeyAssignments = [];
- }
- }
- if (fieldInitializerExpressions.length > 0 && !isStatic && (kind === FIELD || kind === ACCESSOR)) {
- prependExpressionsToFieldInitializer(fieldInitializerExpressions, element);
- fieldInitializerExpressions = [];
- }
- if (staticFieldInitializerExpressions.length > 0 && isStatic && (kind === FIELD || kind === ACCESSOR)) {
- prependExpressionsToFieldInitializer(staticFieldInitializerExpressions, element);
- staticFieldInitializerExpressions = [];
- }
- if (hasDecorators && version === "2023-11") {
- if (kind === FIELD || kind === ACCESSOR) {
- const initExtraId = generateLetUidIdentifier(scopeParent, `init_extra_${name}`);
- locals.push(initExtraId);
- const initExtraCall = _core.types.callExpression(_core.types.cloneNode(initExtraId), isStatic ? [] : [_core.types.thisExpression()]);
- if (!isStatic) {
- fieldInitializerExpressions.push(initExtraCall);
- } else {
- staticFieldInitializerExpressions.push(initExtraCall);
- }
- }
- }
- }
- }
- if (computedKeyAssignments.length > 0) {
- const elements = path.get("body.body");
- let lastComputedElement;
- for (let i = elements.length - 1; i >= 0; i--) {
- const path = elements[i];
- const node = path.node;
- if (node.computed) {
- if (classDecorators && _core.types.isClassProperty(node, {
- static: true
- })) {
- continue;
- }
- lastComputedElement = path;
- break;
- }
- }
- if (lastComputedElement != null) {
- appendExpressionsToComputedKey(computedKeyAssignments, lastComputedElement);
- computedKeyAssignments = [];
- } else {}
- }
- if (fieldInitializerExpressions.length > 0) {
- const isDerivedClass = !!path.node.superClass;
- if (constructorPath) {
- if (isDerivedClass) {
- insertExpressionsAfterSuperCallAndOptimize(fieldInitializerExpressions, constructorPath, protoInitLocal);
- } else {
- prependExpressionsToConstructor(fieldInitializerExpressions, constructorPath);
- }
- } else {
- path.node.body.body.unshift(createConstructorFromExpressions(fieldInitializerExpressions, isDerivedClass));
- }
- fieldInitializerExpressions = [];
- }
- if (staticFieldInitializerExpressions.length > 0) {
- path.node.body.body.push(createStaticBlockFromExpressions(staticFieldInitializerExpressions));
- staticFieldInitializerExpressions = [];
- }
- const sortedElementDecoratorInfo = toSortedDecoratorInfo(elementDecoratorInfo);
- const elementDecorations = generateDecorationExprs(version === "2023-11" ? elementDecoratorInfo : sortedElementDecoratorInfo, version);
- const elementLocals = extractElementLocalAssignments(sortedElementDecoratorInfo);
- if (protoInitLocal) {
- elementLocals.push(protoInitLocal);
- }
- if (staticInitLocal) {
- elementLocals.push(staticInitLocal);
- }
- const classLocals = [];
- let classInitInjected = false;
- const classInitCall = classInitLocal && _core.types.callExpression(_core.types.cloneNode(classInitLocal), []);
- let originalClassPath = path;
- const originalClass = path.node;
- const staticClosures = [];
- if (classDecorators) {
- classLocals.push(classIdLocal, classInitLocal);
- const statics = [];
- path.get("body.body").forEach(element => {
- if (element.isStaticBlock()) {
- if (hasInstancePrivateAccess(element, instancePrivateNames)) {
- const staticBlockClosureId = memoiseExpression(staticBlockToFunctionClosure(element.node), "staticBlock", staticClosures);
- staticFieldInitializerExpressions.push(_core.types.callExpression(_core.types.memberExpression(staticBlockClosureId, _core.types.identifier("call")), [_core.types.thisExpression()]));
- } else {
- staticFieldInitializerExpressions.push(staticBlockToIIFE(element.node));
- }
- element.remove();
- return;
- }
- if ((element.isClassProperty() || element.isClassPrivateProperty()) && element.node.static) {
- const valuePath = element.get("value");
- if (hasInstancePrivateAccess(valuePath, instancePrivateNames)) {
- const fieldValueClosureId = memoiseExpression(fieldInitializerToClosure(valuePath.node), "fieldValue", staticClosures);
- valuePath.replaceWith(_core.types.callExpression(_core.types.memberExpression(fieldValueClosureId, _core.types.identifier("call")), [_core.types.thisExpression()]));
- }
- if (staticFieldInitializerExpressions.length > 0) {
- prependExpressionsToFieldInitializer(staticFieldInitializerExpressions, element);
- staticFieldInitializerExpressions = [];
- }
- element.node.static = false;
- statics.push(element.node);
- element.remove();
- } else if (element.isClassPrivateMethod({
- static: true
- })) {
- if (hasInstancePrivateAccess(element, instancePrivateNames)) {
- const replaceSupers = new _helperReplaceSupers.default({
- constantSuper,
- methodPath: element,
- objectRef: classIdLocal,
- superRef: path.node.superClass,
- file: state.file,
- refToPreserve: classIdLocal
- });
- replaceSupers.replace();
- const privateMethodDelegateId = memoiseExpression(createFunctionExpressionFromPrivateMethod(element.node), element.get("key.id").node.name, staticClosures);
- if (ignoreFunctionLength) {
- element.node.params = [_core.types.restElement(_core.types.identifier("arg"))];
- element.node.body = _core.types.blockStatement([_core.types.returnStatement(_core.types.callExpression(_core.types.memberExpression(privateMethodDelegateId, _core.types.identifier("apply")), [_core.types.thisExpression(), _core.types.identifier("arg")]))]);
- } else {
- element.node.params = element.node.params.map((p, i) => {
- if (_core.types.isRestElement(p)) {
- return _core.types.restElement(_core.types.identifier("arg"));
- } else {
- return _core.types.identifier("_" + i);
- }
- });
- element.node.body = _core.types.blockStatement([_core.types.returnStatement(_core.types.callExpression(_core.types.memberExpression(privateMethodDelegateId, _core.types.identifier("apply")), [_core.types.thisExpression(), _core.types.identifier("arguments")]))]);
- }
- }
- element.node.static = false;
- statics.push(element.node);
- element.remove();
- }
- });
- if (statics.length > 0 || staticFieldInitializerExpressions.length > 0) {
- const staticsClass = _core.template.expression.ast`
- class extends ${state.addHelper("identity")} {}
- `;
- staticsClass.body.body = [_core.types.classProperty(_core.types.toExpression(originalClass), undefined, undefined, undefined, true, true), ...statics];
- const constructorBody = [];
- const newExpr = _core.types.newExpression(staticsClass, []);
- if (staticFieldInitializerExpressions.length > 0) {
- constructorBody.push(...staticFieldInitializerExpressions);
- }
- if (classInitCall) {
- classInitInjected = true;
- constructorBody.push(classInitCall);
- }
- if (constructorBody.length > 0) {
- constructorBody.unshift(_core.types.callExpression(_core.types.super(), [_core.types.cloneNode(classIdLocal)]));
- staticsClass.body.body.push(createConstructorFromExpressions(constructorBody, false));
- } else {
- newExpr.arguments.push(_core.types.cloneNode(classIdLocal));
- }
- const [newPath] = path.replaceWith(newExpr);
- originalClassPath = newPath.get("callee").get("body").get("body.0.key");
- }
- }
- if (!classInitInjected && classInitCall) {
- path.node.body.body.push(_core.types.staticBlock([_core.types.expressionStatement(classInitCall)]));
- }
- let {
- superClass
- } = originalClass;
- if (superClass && (version === "2023-11" || version === "2023-05")) {
- const id = path.scope.maybeGenerateMemoised(superClass);
- if (id) {
- originalClass.superClass = _core.types.assignmentExpression("=", id, superClass);
- superClass = id;
- }
- }
- const applyDecoratorWrapper = _core.types.staticBlock([]);
- originalClass.body.body.unshift(applyDecoratorWrapper);
- const applyDecsBody = applyDecoratorWrapper.body;
- if (computedKeyAssignments.length > 0) {
- const elements = originalClassPath.get("body.body");
- let firstPublicElement;
- for (const path of elements) {
- if ((path.isClassProperty() || path.isClassMethod()) && path.node.kind !== "constructor") {
- firstPublicElement = path;
- break;
- }
- }
- if (firstPublicElement != null) {
- convertToComputedKey(firstPublicElement);
- prependExpressionsToComputedKey(computedKeyAssignments, firstPublicElement);
- } else {
- originalClass.body.body.unshift(_core.types.classProperty(_core.types.sequenceExpression([...computedKeyAssignments, _core.types.stringLiteral("_")]), undefined, undefined, undefined, true, true));
- applyDecsBody.push(_core.types.expressionStatement(_core.types.unaryExpression("delete", _core.types.memberExpression(_core.types.thisExpression(), _core.types.identifier("_")))));
- }
- computedKeyAssignments = [];
- }
- applyDecsBody.push(_core.types.expressionStatement(createLocalsAssignment(elementLocals, classLocals, elementDecorations, classDecorationsId != null ? classDecorationsId : _core.types.arrayExpression(classDecorations), _core.types.numericLiteral(classDecorationsFlag), needsInstancePrivateBrandCheck ? lastInstancePrivateName : null, setClassName, _core.types.cloneNode(superClass), state, version)));
- if (staticInitLocal) {
- applyDecsBody.push(_core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(staticInitLocal), [_core.types.thisExpression()])));
- }
- if (staticClosures.length > 0) {
- applyDecsBody.push(...staticClosures.map(expr => _core.types.expressionStatement(expr)));
- }
- path.insertBefore(classAssignments.map(expr => _core.types.expressionStatement(expr)));
- if (needsDeclarationForClassBinding) {
- const classBindingInfo = scopeParent.getBinding(classIdLocal.name);
- if (!classBindingInfo.constantViolations.length) {
- path.insertBefore(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.cloneNode(classIdLocal))]));
- } else {
- const classOuterBindingDelegateLocal = scopeParent.generateUidIdentifier("t" + classIdLocal.name);
- const classOuterBindingLocal = classIdLocal;
- path.replaceWithMultiple([_core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.cloneNode(classOuterBindingLocal)), _core.types.variableDeclarator(classOuterBindingDelegateLocal)]), _core.types.blockStatement([_core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.cloneNode(classIdLocal))]), path.node, _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(classOuterBindingDelegateLocal), _core.types.cloneNode(classIdLocal)))]), _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(classOuterBindingLocal), _core.types.cloneNode(classOuterBindingDelegateLocal)))]);
- }
- }
- if (decoratedPrivateMethods.size > 0) {
- checkPrivateMethodUpdateError(path, decoratedPrivateMethods);
- }
- path.scope.crawl();
- return path;
- }
- function createLocalsAssignment(elementLocals, classLocals, elementDecorations, classDecorations, classDecorationsFlag, maybePrivateBrandName, setClassName, superClass, state, version) {
- let lhs, rhs;
- const args = [setClassName ? createSetFunctionNameCall(state, setClassName) : _core.types.thisExpression(), classDecorations, elementDecorations];
- {
- if (version !== "2023-11") {
- args.splice(1, 2, elementDecorations, classDecorations);
- }
- if (version === "2021-12" || version === "2022-03" && !state.availableHelper("applyDecs2203R")) {
- lhs = _core.types.arrayPattern([...elementLocals, ...classLocals]);
- rhs = _core.types.callExpression(state.addHelper(version === "2021-12" ? "applyDecs" : "applyDecs2203"), args);
- return _core.types.assignmentExpression("=", lhs, rhs);
- } else if (version === "2022-03") {
- rhs = _core.types.callExpression(state.addHelper("applyDecs2203R"), args);
- } else if (version === "2023-01") {
- if (maybePrivateBrandName) {
- args.push(createPrivateBrandCheckClosure(maybePrivateBrandName));
- }
- rhs = _core.types.callExpression(state.addHelper("applyDecs2301"), args);
- } else if (version === "2023-05") {
- if (maybePrivateBrandName || superClass || classDecorationsFlag.value !== 0) {
- args.push(classDecorationsFlag);
- }
- if (maybePrivateBrandName) {
- args.push(createPrivateBrandCheckClosure(maybePrivateBrandName));
- } else if (superClass) {
- args.push(_core.types.unaryExpression("void", _core.types.numericLiteral(0)));
- }
- if (superClass) args.push(superClass);
- rhs = _core.types.callExpression(state.addHelper("applyDecs2305"), args);
- }
- }
- if (version === "2023-11") {
- if (maybePrivateBrandName || superClass || classDecorationsFlag.value !== 0) {
- args.push(classDecorationsFlag);
- }
- if (maybePrivateBrandName) {
- args.push(createPrivateBrandCheckClosure(maybePrivateBrandName));
- } else if (superClass) {
- args.push(_core.types.unaryExpression("void", _core.types.numericLiteral(0)));
- }
- if (superClass) args.push(superClass);
- rhs = _core.types.callExpression(state.addHelper("applyDecs2311"), args);
- }
- if (elementLocals.length > 0) {
- if (classLocals.length > 0) {
- 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))]);
- } else {
- lhs = _core.types.arrayPattern(elementLocals);
- rhs = _core.types.memberExpression(rhs, _core.types.identifier("e"), false, false);
- }
- } else {
- lhs = _core.types.arrayPattern(classLocals);
- rhs = _core.types.memberExpression(rhs, _core.types.identifier("c"), false, false);
- }
- return _core.types.assignmentExpression("=", lhs, rhs);
- }
- function isProtoKey(node) {
- return node.type === "Identifier" ? node.name === "__proto__" : node.value === "__proto__";
- }
- function isDecorated(node) {
- return node.decorators && node.decorators.length > 0;
- }
- function shouldTransformElement(node) {
- switch (node.type) {
- case "ClassAccessorProperty":
- return true;
- case "ClassMethod":
- case "ClassProperty":
- case "ClassPrivateMethod":
- case "ClassPrivateProperty":
- return isDecorated(node);
- default:
- return false;
- }
- }
- function shouldTransformClass(node) {
- return isDecorated(node) || node.body.body.some(shouldTransformElement);
- }
- function buildNamedEvaluationVisitor(needsName, visitor) {
- function handleComputedProperty(propertyPath, key, state) {
- switch (key.type) {
- case "StringLiteral":
- return _core.types.stringLiteral(key.value);
- case "NumericLiteral":
- case "BigIntLiteral":
- {
- const keyValue = key.value + "";
- propertyPath.get("key").replaceWith(_core.types.stringLiteral(keyValue));
- return _core.types.stringLiteral(keyValue);
- }
- default:
- {
- const ref = propertyPath.scope.maybeGenerateMemoised(key);
- propertyPath.get("key").replaceWith(_core.types.assignmentExpression("=", ref, createToPropertyKeyCall(state, key)));
- return _core.types.cloneNode(ref);
- }
- }
- }
- return {
- VariableDeclarator(path, state) {
- const id = path.node.id;
- if (id.type === "Identifier") {
- const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("init"));
- if (needsName(initializer)) {
- const name = id.name;
- visitor(initializer, state, name);
- }
- }
- },
- AssignmentExpression(path, state) {
- const id = path.node.left;
- if (id.type === "Identifier") {
- const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("right"));
- if (needsName(initializer)) {
- switch (path.node.operator) {
- case "=":
- case "&&=":
- case "||=":
- case "??=":
- visitor(initializer, state, id.name);
- }
- }
- }
- },
- AssignmentPattern(path, state) {
- const id = path.node.left;
- if (id.type === "Identifier") {
- const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("right"));
- if (needsName(initializer)) {
- const name = id.name;
- visitor(initializer, state, name);
- }
- }
- },
- ObjectExpression(path, state) {
- for (const propertyPath of path.get("properties")) {
- if (!propertyPath.isObjectProperty()) continue;
- const {
- node
- } = propertyPath;
- const id = node.key;
- const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(propertyPath.get("value"));
- if (needsName(initializer)) {
- if (!node.computed) {
- if (!isProtoKey(id)) {
- if (id.type === "Identifier") {
- visitor(initializer, state, id.name);
- } else {
- const className = _core.types.stringLiteral(id.value + "");
- visitor(initializer, state, className);
- }
- }
- } else {
- const ref = handleComputedProperty(propertyPath, id, state);
- visitor(initializer, state, ref);
- }
- }
- }
- },
- ClassPrivateProperty(path, state) {
- const {
- node
- } = path;
- const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value"));
- if (needsName(initializer)) {
- const className = _core.types.stringLiteral("#" + node.key.id.name);
- visitor(initializer, state, className);
- }
- },
- ClassAccessorProperty(path, state) {
- const {
- node
- } = path;
- const id = node.key;
- const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value"));
- if (needsName(initializer)) {
- if (!node.computed) {
- if (id.type === "Identifier") {
- visitor(initializer, state, id.name);
- } else if (id.type === "PrivateName") {
- const className = _core.types.stringLiteral("#" + id.id.name);
- visitor(initializer, state, className);
- } else {
- const className = _core.types.stringLiteral(id.value + "");
- visitor(initializer, state, className);
- }
- } else {
- const ref = handleComputedProperty(path, id, state);
- visitor(initializer, state, ref);
- }
- }
- },
- ClassProperty(path, state) {
- const {
- node
- } = path;
- const id = node.key;
- const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value"));
- if (needsName(initializer)) {
- if (!node.computed) {
- if (id.type === "Identifier") {
- visitor(initializer, state, id.name);
- } else {
- const className = _core.types.stringLiteral(id.value + "");
- visitor(initializer, state, className);
- }
- } else {
- const ref = handleComputedProperty(path, id, state);
- visitor(initializer, state, ref);
- }
- }
- }
- };
- }
- function isDecoratedAnonymousClassExpression(path) {
- return path.isClassExpression({
- id: null
- }) && shouldTransformClass(path.node);
- }
- function generateLetUidIdentifier(scope, name) {
- const id = scope.generateUidIdentifier(name);
- scope.push({
- id,
- kind: "let"
- });
- return _core.types.cloneNode(id);
- }
- function _default({
- assertVersion,
- assumption
- }, {
- loose
- }, version, inherits) {
- var _assumption, _assumption2;
- {
- if (version === "2023-11" || version === "2023-05" || version === "2023-01") {
- assertVersion("^7.21.0");
- } else if (version === "2021-12") {
- assertVersion("^7.16.0");
- } else {
- assertVersion("^7.19.0");
- }
- }
- const VISITED = new WeakSet();
- const constantSuper = (_assumption = assumption("constantSuper")) != null ? _assumption : loose;
- const ignoreFunctionLength = (_assumption2 = assumption("ignoreFunctionLength")) != null ? _assumption2 : loose;
- const namedEvaluationVisitor = buildNamedEvaluationVisitor(isDecoratedAnonymousClassExpression, visitClass);
- function visitClass(path, state, className) {
- var _node$id;
- if (VISITED.has(path)) return;
- const {
- node
- } = path;
- className != null ? className : className = (_node$id = node.id) == null ? void 0 : _node$id.name;
- const newPath = transformClass(path, state, constantSuper, ignoreFunctionLength, className, namedEvaluationVisitor, version);
- if (newPath) {
- VISITED.add(newPath);
- return;
- }
- VISITED.add(path);
- }
- return {
- name: "proposal-decorators",
- inherits: inherits,
- visitor: Object.assign({
- ExportDefaultDeclaration(path, state) {
- const {
- declaration
- } = path.node;
- if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && isDecorated(declaration)) {
- const isAnonymous = !declaration.id;
- {
- var _path$splitExportDecl;
- (_path$splitExportDecl = path.splitExportDeclaration) != null ? _path$splitExportDecl : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
- }
- const updatedVarDeclarationPath = path.splitExportDeclaration();
- if (isAnonymous) {
- visitClass(updatedVarDeclarationPath, state, _core.types.stringLiteral("default"));
- }
- }
- },
- ExportNamedDeclaration(path) {
- const {
- declaration
- } = path.node;
- if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && isDecorated(declaration)) {
- {
- var _path$splitExportDecl2;
- (_path$splitExportDecl2 = path.splitExportDeclaration) != null ? _path$splitExportDecl2 : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
- }
- path.splitExportDeclaration();
- }
- },
- Class(path, state) {
- visitClass(path, state, undefined);
- }
- }, namedEvaluationVisitor)
- };
- }
- //# sourceMappingURL=decorators.js.map
|