1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/@babel/helper-replace-supers/lib/index.js

228 lines
7.5 KiB
JavaScript
Raw Normal View History

2023-12-18 13:12:25 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
2024-01-16 21:26:16 +08:00
exports.default = void 0;
var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
var _helperMemberExpressionToFunctions = require("@babel/helper-member-expression-to-functions");
var _helperOptimiseCallExpression = require("@babel/helper-optimise-call-expression");
var _core = require("@babel/core");
const {
assignmentExpression,
booleanLiteral,
callExpression,
cloneNode,
identifier,
memberExpression,
sequenceExpression,
stringLiteral,
thisExpression
} = _core.types;
{
const ns = require("@babel/helper-environment-visitor");
exports.environmentVisitor = ns.default;
exports.skipAllButComputedKey = ns.skipAllButComputedKey;
2023-12-18 13:12:25 +08:00
}
function getPrototypeOfExpression(objectRef, isStatic, file, isPrivateMethod) {
2024-01-16 21:26:16 +08:00
objectRef = cloneNode(objectRef);
const targetRef = isStatic || isPrivateMethod ? objectRef : memberExpression(objectRef, identifier("prototype"));
return callExpression(file.addHelper("getPrototypeOf"), [targetRef]);
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
const visitor = _core.traverse.visitors.merge([_helperEnvironmentVisitor.default, {
2023-12-18 13:12:25 +08:00
Super(path, state) {
const {
node,
parentPath
} = path;
if (!parentPath.isMemberExpression({
object: node
})) return;
state.handle(parentPath);
}
}]);
2024-01-16 21:26:16 +08:00
const unshadowSuperBindingVisitor = _core.traverse.visitors.merge([_helperEnvironmentVisitor.default, {
Scopable(path, {
refName
}) {
const binding = path.scope.getOwnBinding(refName);
if (binding && binding.identifier.name === refName) {
path.scope.rename(refName);
}
}
}]);
2023-12-18 13:12:25 +08:00
const specHandlers = {
memoise(superMember, count) {
const {
scope,
node
} = superMember;
const {
computed,
property
} = node;
if (!computed) {
return;
}
const memo = scope.maybeGenerateMemoised(property);
if (!memo) {
return;
}
this.memoiser.set(property, memo, count);
},
prop(superMember) {
const {
computed,
property
} = superMember.node;
if (this.memoiser.has(property)) {
2024-01-16 21:26:16 +08:00
return cloneNode(this.memoiser.get(property));
2023-12-18 13:12:25 +08:00
}
if (computed) {
2024-01-16 21:26:16 +08:00
return cloneNode(property);
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
return stringLiteral(property.name);
2023-12-18 13:12:25 +08:00
},
get(superMember) {
2024-01-16 21:26:16 +08:00
return this._get(superMember, this._getThisRefs());
},
_get(superMember, thisRefs) {
const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
return callExpression(this.file.addHelper("get"), [thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), thisRefs.this]);
},
_getThisRefs() {
if (!this.isDerivedConstructor) {
return {
this: thisExpression()
};
}
const thisRef = this.scope.generateDeclaredUidIdentifier("thisSuper");
return {
memo: assignmentExpression("=", thisRef, thisExpression()),
this: cloneNode(thisRef)
};
2023-12-18 13:12:25 +08:00
},
set(superMember, value) {
2024-01-16 21:26:16 +08:00
const thisRefs = this._getThisRefs();
const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
return callExpression(this.file.addHelper("set"), [thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), value, thisRefs.this, booleanLiteral(superMember.isInStrictMode())]);
2023-12-18 13:12:25 +08:00
},
destructureSet(superMember) {
throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`);
},
call(superMember, args) {
2024-01-16 21:26:16 +08:00
const thisRefs = this._getThisRefs();
return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, false);
},
optionalCall(superMember, args) {
const thisRefs = this._getThisRefs();
return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, true);
},
delete(superMember) {
if (superMember.node.computed) {
return sequenceExpression([callExpression(this.file.addHelper("toPropertyKey"), [cloneNode(superMember.node.property)]), _core.template.expression.ast`
function () { throw new ReferenceError("'delete super[expr]' is invalid"); }()
`]);
} else {
return _core.template.expression.ast`
function () { throw new ReferenceError("'delete super.prop' is invalid"); }()
`;
}
2023-12-18 13:12:25 +08:00
}
};
const looseHandlers = Object.assign({}, specHandlers, {
prop(superMember) {
const {
property
} = superMember.node;
if (this.memoiser.has(property)) {
2024-01-16 21:26:16 +08:00
return cloneNode(this.memoiser.get(property));
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
return cloneNode(property);
2023-12-18 13:12:25 +08:00
},
get(superMember) {
const {
isStatic,
2024-01-16 21:26:16 +08:00
getSuperRef
2023-12-18 13:12:25 +08:00
} = this;
const {
computed
} = superMember.node;
const prop = this.prop(superMember);
let object;
if (isStatic) {
2024-01-16 21:26:16 +08:00
var _getSuperRef;
object = (_getSuperRef = getSuperRef()) != null ? _getSuperRef : memberExpression(identifier("Function"), identifier("prototype"));
2023-12-18 13:12:25 +08:00
} else {
2024-01-16 21:26:16 +08:00
var _getSuperRef2;
object = memberExpression((_getSuperRef2 = getSuperRef()) != null ? _getSuperRef2 : identifier("Object"), identifier("prototype"));
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
return memberExpression(object, prop, computed);
2023-12-18 13:12:25 +08:00
},
set(superMember, value) {
const {
computed
} = superMember.node;
const prop = this.prop(superMember);
2024-01-16 21:26:16 +08:00
return assignmentExpression("=", memberExpression(thisExpression(), prop, computed), value);
2023-12-18 13:12:25 +08:00
},
destructureSet(superMember) {
const {
computed
} = superMember.node;
const prop = this.prop(superMember);
2024-01-16 21:26:16 +08:00
return memberExpression(thisExpression(), prop, computed);
},
call(superMember, args) {
return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, false);
},
optionalCall(superMember, args) {
return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, true);
2023-12-18 13:12:25 +08:00
}
});
class ReplaceSupers {
constructor(opts) {
2024-01-16 21:26:16 +08:00
var _opts$constantSuper;
2023-12-18 13:12:25 +08:00
const path = opts.methodPath;
this.methodPath = path;
2024-01-16 21:26:16 +08:00
this.isDerivedConstructor = path.isClassMethod({
kind: "constructor"
}) && !!opts.superRef;
this.isStatic = path.isObjectMethod() || path.node.static || (path.isStaticBlock == null ? void 0 : path.isStaticBlock());
2023-12-18 13:12:25 +08:00
this.isPrivateMethod = path.isPrivate() && path.isMethod();
this.file = opts.file;
2024-01-16 21:26:16 +08:00
this.constantSuper = (_opts$constantSuper = opts.constantSuper) != null ? _opts$constantSuper : opts.isLoose;
2023-12-18 13:12:25 +08:00
this.opts = opts;
}
getObjectRef() {
2024-01-16 21:26:16 +08:00
return cloneNode(this.opts.objectRef || this.opts.getObjectRef());
}
getSuperRef() {
if (this.opts.superRef) return cloneNode(this.opts.superRef);
if (this.opts.getSuperRef) {
return cloneNode(this.opts.getSuperRef());
}
2023-12-18 13:12:25 +08:00
}
replace() {
2024-01-16 21:26:16 +08:00
if (this.opts.refToPreserve) {
this.methodPath.traverse(unshadowSuperBindingVisitor, {
refName: this.opts.refToPreserve.name
});
}
const handler = this.constantSuper ? looseHandlers : specHandlers;
(0, _helperMemberExpressionToFunctions.default)(this.methodPath, visitor, Object.assign({
2023-12-18 13:12:25 +08:00
file: this.file,
2024-01-16 21:26:16 +08:00
scope: this.methodPath.scope,
isDerivedConstructor: this.isDerivedConstructor,
2023-12-18 13:12:25 +08:00
isStatic: this.isStatic,
isPrivateMethod: this.isPrivateMethod,
getObjectRef: this.getObjectRef.bind(this),
2024-01-16 21:26:16 +08:00
getSuperRef: this.getSuperRef.bind(this),
boundGet: handler.get
2023-12-18 13:12:25 +08:00
}, handler));
}
}
2024-01-16 21:26:16 +08:00
exports.default = ReplaceSupers;
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
//# sourceMappingURL=index.js.map