[79a0317] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = void 0;
|
---|
| 7 | var _helperMemberExpressionToFunctions = require("@babel/helper-member-expression-to-functions");
|
---|
| 8 | var _helperOptimiseCallExpression = require("@babel/helper-optimise-call-expression");
|
---|
| 9 | var _core = require("@babel/core");
|
---|
| 10 | var _traverse = require("@babel/traverse");
|
---|
| 11 | const {
|
---|
| 12 | assignmentExpression,
|
---|
| 13 | callExpression,
|
---|
| 14 | cloneNode,
|
---|
| 15 | identifier,
|
---|
| 16 | memberExpression,
|
---|
| 17 | sequenceExpression,
|
---|
| 18 | stringLiteral,
|
---|
| 19 | thisExpression
|
---|
| 20 | } = _core.types;
|
---|
| 21 | {
|
---|
| 22 | exports.environmentVisitor = _traverse.visitors.environmentVisitor({});
|
---|
| 23 | exports.skipAllButComputedKey = function skipAllButComputedKey(path) {
|
---|
| 24 | path.skip();
|
---|
| 25 | if (path.node.computed) {
|
---|
| 26 | path.context.maybeQueue(path.get("key"));
|
---|
| 27 | }
|
---|
| 28 | };
|
---|
| 29 | }
|
---|
| 30 | const visitor = _traverse.visitors.environmentVisitor({
|
---|
| 31 | Super(path, state) {
|
---|
| 32 | const {
|
---|
| 33 | node,
|
---|
| 34 | parentPath
|
---|
| 35 | } = path;
|
---|
| 36 | if (!parentPath.isMemberExpression({
|
---|
| 37 | object: node
|
---|
| 38 | })) return;
|
---|
| 39 | state.handle(parentPath);
|
---|
| 40 | }
|
---|
| 41 | });
|
---|
| 42 | const unshadowSuperBindingVisitor = _traverse.visitors.environmentVisitor({
|
---|
| 43 | Scopable(path, {
|
---|
| 44 | refName
|
---|
| 45 | }) {
|
---|
| 46 | const binding = path.scope.getOwnBinding(refName);
|
---|
| 47 | if (binding && binding.identifier.name === refName) {
|
---|
| 48 | path.scope.rename(refName);
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | });
|
---|
| 52 | const specHandlers = {
|
---|
| 53 | memoise(superMember, count) {
|
---|
| 54 | const {
|
---|
| 55 | scope,
|
---|
| 56 | node
|
---|
| 57 | } = superMember;
|
---|
| 58 | const {
|
---|
| 59 | computed,
|
---|
| 60 | property
|
---|
| 61 | } = node;
|
---|
| 62 | if (!computed) {
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 | const memo = scope.maybeGenerateMemoised(property);
|
---|
| 66 | if (!memo) {
|
---|
| 67 | return;
|
---|
| 68 | }
|
---|
| 69 | this.memoiser.set(property, memo, count);
|
---|
| 70 | },
|
---|
| 71 | prop(superMember) {
|
---|
| 72 | const {
|
---|
| 73 | computed,
|
---|
| 74 | property
|
---|
| 75 | } = superMember.node;
|
---|
| 76 | if (this.memoiser.has(property)) {
|
---|
| 77 | return cloneNode(this.memoiser.get(property));
|
---|
| 78 | }
|
---|
| 79 | if (computed) {
|
---|
| 80 | return cloneNode(property);
|
---|
| 81 | }
|
---|
| 82 | return stringLiteral(property.name);
|
---|
| 83 | },
|
---|
| 84 | _getPrototypeOfExpression() {
|
---|
| 85 | const objectRef = cloneNode(this.getObjectRef());
|
---|
| 86 | const targetRef = this.isStatic || this.isPrivateMethod ? objectRef : memberExpression(objectRef, identifier("prototype"));
|
---|
| 87 | return callExpression(this.file.addHelper("getPrototypeOf"), [targetRef]);
|
---|
| 88 | },
|
---|
| 89 | get(superMember) {
|
---|
| 90 | const objectRef = cloneNode(this.getObjectRef());
|
---|
| 91 | return callExpression(this.file.addHelper("superPropGet"), [this.isDerivedConstructor ? sequenceExpression([thisExpression(), objectRef]) : objectRef, this.prop(superMember), thisExpression(), ...(this.isStatic || this.isPrivateMethod ? [] : [_core.types.numericLiteral(1)])]);
|
---|
| 92 | },
|
---|
| 93 | _call(superMember, args, optional) {
|
---|
| 94 | const objectRef = cloneNode(this.getObjectRef());
|
---|
| 95 | let argsNode;
|
---|
| 96 | if (args.length === 1 && _core.types.isSpreadElement(args[0]) && (_core.types.isIdentifier(args[0].argument) || _core.types.isArrayExpression(args[0].argument))) {
|
---|
| 97 | argsNode = args[0].argument;
|
---|
| 98 | } else {
|
---|
| 99 | argsNode = _core.types.arrayExpression(args);
|
---|
| 100 | }
|
---|
| 101 | const call = _core.types.callExpression(this.file.addHelper("superPropGet"), [this.isDerivedConstructor ? sequenceExpression([thisExpression(), objectRef]) : objectRef, this.prop(superMember), thisExpression(), _core.types.numericLiteral(2 | (this.isStatic || this.isPrivateMethod ? 0 : 1))]);
|
---|
| 102 | if (optional) {
|
---|
| 103 | return _core.types.optionalCallExpression(call, [argsNode], true);
|
---|
| 104 | }
|
---|
| 105 | return callExpression(call, [argsNode]);
|
---|
| 106 | },
|
---|
| 107 | set(superMember, value) {
|
---|
| 108 | const objectRef = cloneNode(this.getObjectRef());
|
---|
| 109 | return callExpression(this.file.addHelper("superPropSet"), [this.isDerivedConstructor ? sequenceExpression([thisExpression(), objectRef]) : objectRef, this.prop(superMember), value, thisExpression(), _core.types.numericLiteral(superMember.isInStrictMode() ? 1 : 0), ...(this.isStatic || this.isPrivateMethod ? [] : [_core.types.numericLiteral(1)])]);
|
---|
| 110 | },
|
---|
| 111 | destructureSet(superMember) {
|
---|
| 112 | throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`);
|
---|
| 113 | },
|
---|
| 114 | call(superMember, args) {
|
---|
| 115 | return this._call(superMember, args, false);
|
---|
| 116 | },
|
---|
| 117 | optionalCall(superMember, args) {
|
---|
| 118 | return this._call(superMember, args, true);
|
---|
| 119 | },
|
---|
| 120 | delete(superMember) {
|
---|
| 121 | if (superMember.node.computed) {
|
---|
| 122 | return sequenceExpression([callExpression(this.file.addHelper("toPropertyKey"), [cloneNode(superMember.node.property)]), _core.template.expression.ast`
|
---|
| 123 | function () { throw new ReferenceError("'delete super[expr]' is invalid"); }()
|
---|
| 124 | `]);
|
---|
| 125 | } else {
|
---|
| 126 | return _core.template.expression.ast`
|
---|
| 127 | function () { throw new ReferenceError("'delete super.prop' is invalid"); }()
|
---|
| 128 | `;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | };
|
---|
| 132 | const specHandlers_old = {
|
---|
| 133 | memoise(superMember, count) {
|
---|
| 134 | const {
|
---|
| 135 | scope,
|
---|
| 136 | node
|
---|
| 137 | } = superMember;
|
---|
| 138 | const {
|
---|
| 139 | computed,
|
---|
| 140 | property
|
---|
| 141 | } = node;
|
---|
| 142 | if (!computed) {
|
---|
| 143 | return;
|
---|
| 144 | }
|
---|
| 145 | const memo = scope.maybeGenerateMemoised(property);
|
---|
| 146 | if (!memo) {
|
---|
| 147 | return;
|
---|
| 148 | }
|
---|
| 149 | this.memoiser.set(property, memo, count);
|
---|
| 150 | },
|
---|
| 151 | prop(superMember) {
|
---|
| 152 | const {
|
---|
| 153 | computed,
|
---|
| 154 | property
|
---|
| 155 | } = superMember.node;
|
---|
| 156 | if (this.memoiser.has(property)) {
|
---|
| 157 | return cloneNode(this.memoiser.get(property));
|
---|
| 158 | }
|
---|
| 159 | if (computed) {
|
---|
| 160 | return cloneNode(property);
|
---|
| 161 | }
|
---|
| 162 | return stringLiteral(property.name);
|
---|
| 163 | },
|
---|
| 164 | _getPrototypeOfExpression() {
|
---|
| 165 | const objectRef = cloneNode(this.getObjectRef());
|
---|
| 166 | const targetRef = this.isStatic || this.isPrivateMethod ? objectRef : memberExpression(objectRef, identifier("prototype"));
|
---|
| 167 | return callExpression(this.file.addHelper("getPrototypeOf"), [targetRef]);
|
---|
| 168 | },
|
---|
| 169 | get(superMember) {
|
---|
| 170 | return this._get(superMember);
|
---|
| 171 | },
|
---|
| 172 | _get(superMember) {
|
---|
| 173 | const proto = this._getPrototypeOfExpression();
|
---|
| 174 | return callExpression(this.file.addHelper("get"), [this.isDerivedConstructor ? sequenceExpression([thisExpression(), proto]) : proto, this.prop(superMember), thisExpression()]);
|
---|
| 175 | },
|
---|
| 176 | set(superMember, value) {
|
---|
| 177 | const proto = this._getPrototypeOfExpression();
|
---|
| 178 | return callExpression(this.file.addHelper("set"), [this.isDerivedConstructor ? sequenceExpression([thisExpression(), proto]) : proto, this.prop(superMember), value, thisExpression(), _core.types.booleanLiteral(superMember.isInStrictMode())]);
|
---|
| 179 | },
|
---|
| 180 | destructureSet(superMember) {
|
---|
| 181 | throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`);
|
---|
| 182 | },
|
---|
| 183 | call(superMember, args) {
|
---|
| 184 | return (0, _helperOptimiseCallExpression.default)(this._get(superMember), thisExpression(), args, false);
|
---|
| 185 | },
|
---|
| 186 | optionalCall(superMember, args) {
|
---|
| 187 | return (0, _helperOptimiseCallExpression.default)(this._get(superMember), cloneNode(thisExpression()), args, true);
|
---|
| 188 | },
|
---|
| 189 | delete(superMember) {
|
---|
| 190 | if (superMember.node.computed) {
|
---|
| 191 | return sequenceExpression([callExpression(this.file.addHelper("toPropertyKey"), [cloneNode(superMember.node.property)]), _core.template.expression.ast`
|
---|
| 192 | function () { throw new ReferenceError("'delete super[expr]' is invalid"); }()
|
---|
| 193 | `]);
|
---|
| 194 | } else {
|
---|
| 195 | return _core.template.expression.ast`
|
---|
| 196 | function () { throw new ReferenceError("'delete super.prop' is invalid"); }()
|
---|
| 197 | `;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | };
|
---|
| 201 | const looseHandlers = Object.assign({}, specHandlers, {
|
---|
| 202 | prop(superMember) {
|
---|
| 203 | const {
|
---|
| 204 | property
|
---|
| 205 | } = superMember.node;
|
---|
| 206 | if (this.memoiser.has(property)) {
|
---|
| 207 | return cloneNode(this.memoiser.get(property));
|
---|
| 208 | }
|
---|
| 209 | return cloneNode(property);
|
---|
| 210 | },
|
---|
| 211 | get(superMember) {
|
---|
| 212 | const {
|
---|
| 213 | isStatic,
|
---|
| 214 | getSuperRef
|
---|
| 215 | } = this;
|
---|
| 216 | const {
|
---|
| 217 | computed
|
---|
| 218 | } = superMember.node;
|
---|
| 219 | const prop = this.prop(superMember);
|
---|
| 220 | let object;
|
---|
| 221 | if (isStatic) {
|
---|
| 222 | var _getSuperRef;
|
---|
| 223 | object = (_getSuperRef = getSuperRef()) != null ? _getSuperRef : memberExpression(identifier("Function"), identifier("prototype"));
|
---|
| 224 | } else {
|
---|
| 225 | var _getSuperRef2;
|
---|
| 226 | object = memberExpression((_getSuperRef2 = getSuperRef()) != null ? _getSuperRef2 : identifier("Object"), identifier("prototype"));
|
---|
| 227 | }
|
---|
| 228 | return memberExpression(object, prop, computed);
|
---|
| 229 | },
|
---|
| 230 | set(superMember, value) {
|
---|
| 231 | const {
|
---|
| 232 | computed
|
---|
| 233 | } = superMember.node;
|
---|
| 234 | const prop = this.prop(superMember);
|
---|
| 235 | return assignmentExpression("=", memberExpression(thisExpression(), prop, computed), value);
|
---|
| 236 | },
|
---|
| 237 | destructureSet(superMember) {
|
---|
| 238 | const {
|
---|
| 239 | computed
|
---|
| 240 | } = superMember.node;
|
---|
| 241 | const prop = this.prop(superMember);
|
---|
| 242 | return memberExpression(thisExpression(), prop, computed);
|
---|
| 243 | },
|
---|
| 244 | call(superMember, args) {
|
---|
| 245 | return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, false);
|
---|
| 246 | },
|
---|
| 247 | optionalCall(superMember, args) {
|
---|
| 248 | return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, true);
|
---|
| 249 | }
|
---|
| 250 | });
|
---|
| 251 | class ReplaceSupers {
|
---|
| 252 | constructor(opts) {
|
---|
| 253 | var _opts$constantSuper;
|
---|
| 254 | const path = opts.methodPath;
|
---|
| 255 | this.methodPath = path;
|
---|
| 256 | this.isDerivedConstructor = path.isClassMethod({
|
---|
| 257 | kind: "constructor"
|
---|
| 258 | }) && !!opts.superRef;
|
---|
| 259 | this.isStatic = path.isObjectMethod() || path.node.static || (path.isStaticBlock == null ? void 0 : path.isStaticBlock());
|
---|
| 260 | this.isPrivateMethod = path.isPrivate() && path.isMethod();
|
---|
| 261 | this.file = opts.file;
|
---|
| 262 | this.constantSuper = (_opts$constantSuper = opts.constantSuper) != null ? _opts$constantSuper : opts.isLoose;
|
---|
| 263 | this.opts = opts;
|
---|
| 264 | }
|
---|
| 265 | getObjectRef() {
|
---|
| 266 | return cloneNode(this.opts.objectRef || this.opts.getObjectRef());
|
---|
| 267 | }
|
---|
| 268 | getSuperRef() {
|
---|
| 269 | if (this.opts.superRef) return cloneNode(this.opts.superRef);
|
---|
| 270 | if (this.opts.getSuperRef) {
|
---|
| 271 | return cloneNode(this.opts.getSuperRef());
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 | replace() {
|
---|
| 275 | const {
|
---|
| 276 | methodPath
|
---|
| 277 | } = this;
|
---|
| 278 | if (this.opts.refToPreserve) {
|
---|
| 279 | methodPath.traverse(unshadowSuperBindingVisitor, {
|
---|
| 280 | refName: this.opts.refToPreserve.name
|
---|
| 281 | });
|
---|
| 282 | }
|
---|
| 283 | const handler = this.constantSuper ? looseHandlers : this.file.availableHelper("superPropSet") ? specHandlers : specHandlers_old;
|
---|
| 284 | visitor.shouldSkip = path => {
|
---|
| 285 | if (path.parentPath === methodPath) {
|
---|
| 286 | if (path.parentKey === "decorators" || path.parentKey === "key") {
|
---|
| 287 | return true;
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 | };
|
---|
| 291 | (0, _helperMemberExpressionToFunctions.default)(methodPath, visitor, Object.assign({
|
---|
| 292 | file: this.file,
|
---|
| 293 | scope: this.methodPath.scope,
|
---|
| 294 | isDerivedConstructor: this.isDerivedConstructor,
|
---|
| 295 | isStatic: this.isStatic,
|
---|
| 296 | isPrivateMethod: this.isPrivateMethod,
|
---|
| 297 | getObjectRef: this.getObjectRef.bind(this),
|
---|
| 298 | getSuperRef: this.getSuperRef.bind(this),
|
---|
| 299 | boundGet: handler.get
|
---|
| 300 | }, handler));
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
| 303 | exports.default = ReplaceSupers;
|
---|
| 304 |
|
---|
| 305 | //# sourceMappingURL=index.js.map
|
---|