[79a0317] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
| 4 |
|
---|
| 5 | var _t = require('@babel/types');
|
---|
| 6 |
|
---|
| 7 | function _interopNamespace(e) {
|
---|
| 8 | if (e && e.__esModule) return e;
|
---|
| 9 | var n = Object.create(null);
|
---|
| 10 | if (e) {
|
---|
| 11 | Object.keys(e).forEach(function (k) {
|
---|
| 12 | if (k !== 'default') {
|
---|
| 13 | var d = Object.getOwnPropertyDescriptor(e, k);
|
---|
| 14 | Object.defineProperty(n, k, d.get ? d : {
|
---|
| 15 | enumerable: true,
|
---|
| 16 | get: function () { return e[k]; }
|
---|
| 17 | });
|
---|
| 18 | }
|
---|
| 19 | });
|
---|
| 20 | }
|
---|
| 21 | n.default = e;
|
---|
| 22 | return Object.freeze(n);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | var _t__namespace = /*#__PURE__*/_interopNamespace(_t);
|
---|
| 26 |
|
---|
| 27 | function willPathCastToBoolean(path) {
|
---|
| 28 | const maybeWrapped = path;
|
---|
| 29 | const {
|
---|
| 30 | node,
|
---|
| 31 | parentPath
|
---|
| 32 | } = maybeWrapped;
|
---|
| 33 | if (parentPath.isLogicalExpression()) {
|
---|
| 34 | const {
|
---|
| 35 | operator,
|
---|
| 36 | right
|
---|
| 37 | } = parentPath.node;
|
---|
| 38 | if (operator === "&&" || operator === "||" || operator === "??" && node === right) {
|
---|
| 39 | return willPathCastToBoolean(parentPath);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | if (parentPath.isSequenceExpression()) {
|
---|
| 43 | const {
|
---|
| 44 | expressions
|
---|
| 45 | } = parentPath.node;
|
---|
| 46 | if (expressions[expressions.length - 1] === node) {
|
---|
| 47 | return willPathCastToBoolean(parentPath);
|
---|
| 48 | } else {
|
---|
| 49 | return true;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | return parentPath.isConditional({
|
---|
| 53 | test: node
|
---|
| 54 | }) || parentPath.isUnaryExpression({
|
---|
| 55 | operator: "!"
|
---|
| 56 | }) || parentPath.isLoop({
|
---|
| 57 | test: node
|
---|
| 58 | });
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | const {
|
---|
| 62 | LOGICAL_OPERATORS,
|
---|
| 63 | arrowFunctionExpression,
|
---|
| 64 | assignmentExpression,
|
---|
| 65 | binaryExpression,
|
---|
| 66 | booleanLiteral,
|
---|
| 67 | callExpression,
|
---|
| 68 | cloneNode,
|
---|
| 69 | conditionalExpression,
|
---|
| 70 | identifier,
|
---|
| 71 | isMemberExpression,
|
---|
| 72 | isOptionalCallExpression,
|
---|
| 73 | isOptionalMemberExpression,
|
---|
| 74 | isUpdateExpression,
|
---|
| 75 | logicalExpression,
|
---|
| 76 | memberExpression,
|
---|
| 77 | nullLiteral,
|
---|
| 78 | optionalCallExpression,
|
---|
| 79 | optionalMemberExpression,
|
---|
| 80 | sequenceExpression,
|
---|
| 81 | updateExpression
|
---|
| 82 | } = _t__namespace;
|
---|
| 83 | class AssignmentMemoiser {
|
---|
| 84 | constructor() {
|
---|
| 85 | this._map = void 0;
|
---|
| 86 | this._map = new WeakMap();
|
---|
| 87 | }
|
---|
| 88 | has(key) {
|
---|
| 89 | return this._map.has(key);
|
---|
| 90 | }
|
---|
| 91 | get(key) {
|
---|
| 92 | if (!this.has(key)) return;
|
---|
| 93 | const record = this._map.get(key);
|
---|
| 94 | const {
|
---|
| 95 | value
|
---|
| 96 | } = record;
|
---|
| 97 | record.count--;
|
---|
| 98 | if (record.count === 0) {
|
---|
| 99 | return assignmentExpression("=", value, key);
|
---|
| 100 | }
|
---|
| 101 | return value;
|
---|
| 102 | }
|
---|
| 103 | set(key, value, count) {
|
---|
| 104 | return this._map.set(key, {
|
---|
| 105 | count,
|
---|
| 106 | value
|
---|
| 107 | });
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | function toNonOptional(path, base) {
|
---|
| 111 | const {
|
---|
| 112 | node
|
---|
| 113 | } = path;
|
---|
| 114 | if (isOptionalMemberExpression(node)) {
|
---|
| 115 | return memberExpression(base, node.property, node.computed);
|
---|
| 116 | }
|
---|
| 117 | if (path.isOptionalCallExpression()) {
|
---|
| 118 | const callee = path.get("callee");
|
---|
| 119 | if (path.node.optional && callee.isOptionalMemberExpression()) {
|
---|
| 120 | const object = callee.node.object;
|
---|
| 121 | const context = path.scope.maybeGenerateMemoised(object);
|
---|
| 122 | callee.get("object").replaceWith(assignmentExpression("=", context, object));
|
---|
| 123 | return callExpression(memberExpression(base, identifier("call")), [context, ...path.node.arguments]);
|
---|
| 124 | }
|
---|
| 125 | return callExpression(base, path.node.arguments);
|
---|
| 126 | }
|
---|
| 127 | return path.node;
|
---|
| 128 | }
|
---|
| 129 | function isInDetachedTree(path) {
|
---|
| 130 | while (path) {
|
---|
| 131 | if (path.isProgram()) break;
|
---|
| 132 | const {
|
---|
| 133 | parentPath,
|
---|
| 134 | container,
|
---|
| 135 | listKey
|
---|
| 136 | } = path;
|
---|
| 137 | const parentNode = parentPath.node;
|
---|
| 138 | if (listKey) {
|
---|
| 139 | if (container !== parentNode[listKey]) {
|
---|
| 140 | return true;
|
---|
| 141 | }
|
---|
| 142 | } else {
|
---|
| 143 | if (container !== parentNode) return true;
|
---|
| 144 | }
|
---|
| 145 | path = parentPath;
|
---|
| 146 | }
|
---|
| 147 | return false;
|
---|
| 148 | }
|
---|
| 149 | const handle = {
|
---|
| 150 | memoise() {},
|
---|
| 151 | handle(member, noDocumentAll) {
|
---|
| 152 | const {
|
---|
| 153 | node,
|
---|
| 154 | parent,
|
---|
| 155 | parentPath,
|
---|
| 156 | scope
|
---|
| 157 | } = member;
|
---|
| 158 | if (member.isOptionalMemberExpression()) {
|
---|
| 159 | if (isInDetachedTree(member)) return;
|
---|
| 160 | const endPath = member.find(({
|
---|
| 161 | node,
|
---|
| 162 | parent
|
---|
| 163 | }) => {
|
---|
| 164 | if (isOptionalMemberExpression(parent)) {
|
---|
| 165 | return parent.optional || parent.object !== node;
|
---|
| 166 | }
|
---|
| 167 | if (isOptionalCallExpression(parent)) {
|
---|
| 168 | return node !== member.node && parent.optional || parent.callee !== node;
|
---|
| 169 | }
|
---|
| 170 | return true;
|
---|
| 171 | });
|
---|
| 172 | if (scope.path.isPattern()) {
|
---|
| 173 | endPath.replaceWith(callExpression(arrowFunctionExpression([], endPath.node), []));
|
---|
| 174 | return;
|
---|
| 175 | }
|
---|
| 176 | const willEndPathCastToBoolean = willPathCastToBoolean(endPath);
|
---|
| 177 | const rootParentPath = endPath.parentPath;
|
---|
| 178 | if (rootParentPath.isUpdateExpression({
|
---|
| 179 | argument: node
|
---|
| 180 | })) {
|
---|
| 181 | throw member.buildCodeFrameError(`can't handle update expression`);
|
---|
| 182 | }
|
---|
| 183 | const isAssignment = rootParentPath.isAssignmentExpression({
|
---|
| 184 | left: endPath.node
|
---|
| 185 | });
|
---|
| 186 | const isDeleteOperation = rootParentPath.isUnaryExpression({
|
---|
| 187 | operator: "delete"
|
---|
| 188 | });
|
---|
| 189 | if (isDeleteOperation && endPath.isOptionalMemberExpression() && endPath.get("property").isPrivateName()) {
|
---|
| 190 | throw member.buildCodeFrameError(`can't delete a private class element`);
|
---|
| 191 | }
|
---|
| 192 | let startingOptional = member;
|
---|
| 193 | for (;;) {
|
---|
| 194 | if (startingOptional.isOptionalMemberExpression()) {
|
---|
| 195 | if (startingOptional.node.optional) break;
|
---|
| 196 | startingOptional = startingOptional.get("object");
|
---|
| 197 | continue;
|
---|
| 198 | } else if (startingOptional.isOptionalCallExpression()) {
|
---|
| 199 | if (startingOptional.node.optional) break;
|
---|
| 200 | startingOptional = startingOptional.get("callee");
|
---|
| 201 | continue;
|
---|
| 202 | }
|
---|
| 203 | throw new Error(`Internal error: unexpected ${startingOptional.node.type}`);
|
---|
| 204 | }
|
---|
| 205 | const startingNode = startingOptional.isOptionalMemberExpression() ? startingOptional.node.object : startingOptional.node.callee;
|
---|
| 206 | const baseNeedsMemoised = scope.maybeGenerateMemoised(startingNode);
|
---|
| 207 | const baseRef = baseNeedsMemoised != null ? baseNeedsMemoised : startingNode;
|
---|
| 208 | const parentIsOptionalCall = parentPath.isOptionalCallExpression({
|
---|
| 209 | callee: node
|
---|
| 210 | });
|
---|
| 211 | const isOptionalCall = parent => parentIsOptionalCall;
|
---|
| 212 | const parentIsCall = parentPath.isCallExpression({
|
---|
| 213 | callee: node
|
---|
| 214 | });
|
---|
| 215 | startingOptional.replaceWith(toNonOptional(startingOptional, baseRef));
|
---|
| 216 | if (isOptionalCall()) {
|
---|
| 217 | if (parent.optional) {
|
---|
| 218 | parentPath.replaceWith(this.optionalCall(member, parent.arguments));
|
---|
| 219 | } else {
|
---|
| 220 | parentPath.replaceWith(this.call(member, parent.arguments));
|
---|
| 221 | }
|
---|
| 222 | } else if (parentIsCall) {
|
---|
| 223 | member.replaceWith(this.boundGet(member));
|
---|
| 224 | } else if (this.delete && parentPath.isUnaryExpression({
|
---|
| 225 | operator: "delete"
|
---|
| 226 | })) {
|
---|
| 227 | parentPath.replaceWith(this.delete(member));
|
---|
| 228 | } else if (parentPath.isAssignmentExpression()) {
|
---|
| 229 | handleAssignment(this, member, parentPath);
|
---|
| 230 | } else {
|
---|
| 231 | member.replaceWith(this.get(member));
|
---|
| 232 | }
|
---|
| 233 | let regular = member.node;
|
---|
| 234 | for (let current = member; current !== endPath;) {
|
---|
| 235 | const parentPath = current.parentPath;
|
---|
| 236 | if (parentPath === endPath && isOptionalCall() && parent.optional) {
|
---|
| 237 | regular = parentPath.node;
|
---|
| 238 | break;
|
---|
| 239 | }
|
---|
| 240 | regular = toNonOptional(parentPath, regular);
|
---|
| 241 | current = parentPath;
|
---|
| 242 | }
|
---|
| 243 | let context;
|
---|
| 244 | const endParentPath = endPath.parentPath;
|
---|
| 245 | if (isMemberExpression(regular) && endParentPath.isOptionalCallExpression({
|
---|
| 246 | callee: endPath.node,
|
---|
| 247 | optional: true
|
---|
| 248 | })) {
|
---|
| 249 | const {
|
---|
| 250 | object
|
---|
| 251 | } = regular;
|
---|
| 252 | context = member.scope.maybeGenerateMemoised(object);
|
---|
| 253 | if (context) {
|
---|
| 254 | regular.object = assignmentExpression("=", context, object);
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | let replacementPath = endPath;
|
---|
| 258 | if (isDeleteOperation || isAssignment) {
|
---|
| 259 | replacementPath = endParentPath;
|
---|
| 260 | regular = endParentPath.node;
|
---|
| 261 | }
|
---|
| 262 | const baseMemoised = baseNeedsMemoised ? assignmentExpression("=", cloneNode(baseRef), cloneNode(startingNode)) : cloneNode(baseRef);
|
---|
| 263 | if (willEndPathCastToBoolean) {
|
---|
| 264 | let nonNullishCheck;
|
---|
| 265 | if (noDocumentAll) {
|
---|
| 266 | nonNullishCheck = binaryExpression("!=", baseMemoised, nullLiteral());
|
---|
| 267 | } else {
|
---|
| 268 | nonNullishCheck = logicalExpression("&&", binaryExpression("!==", baseMemoised, nullLiteral()), binaryExpression("!==", cloneNode(baseRef), scope.buildUndefinedNode()));
|
---|
| 269 | }
|
---|
| 270 | replacementPath.replaceWith(logicalExpression("&&", nonNullishCheck, regular));
|
---|
| 271 | } else {
|
---|
| 272 | let nullishCheck;
|
---|
| 273 | if (noDocumentAll) {
|
---|
| 274 | nullishCheck = binaryExpression("==", baseMemoised, nullLiteral());
|
---|
| 275 | } else {
|
---|
| 276 | nullishCheck = logicalExpression("||", binaryExpression("===", baseMemoised, nullLiteral()), binaryExpression("===", cloneNode(baseRef), scope.buildUndefinedNode()));
|
---|
| 277 | }
|
---|
| 278 | replacementPath.replaceWith(conditionalExpression(nullishCheck, isDeleteOperation ? booleanLiteral(true) : scope.buildUndefinedNode(), regular));
|
---|
| 279 | }
|
---|
| 280 | if (context) {
|
---|
| 281 | const endParent = endParentPath.node;
|
---|
| 282 | endParentPath.replaceWith(optionalCallExpression(optionalMemberExpression(endParent.callee, identifier("call"), false, true), [cloneNode(context), ...endParent.arguments], false));
|
---|
| 283 | }
|
---|
| 284 | return;
|
---|
| 285 | }
|
---|
| 286 | if (isUpdateExpression(parent, {
|
---|
| 287 | argument: node
|
---|
| 288 | })) {
|
---|
| 289 | if (this.simpleSet) {
|
---|
| 290 | member.replaceWith(this.simpleSet(member));
|
---|
| 291 | return;
|
---|
| 292 | }
|
---|
| 293 | const {
|
---|
| 294 | operator,
|
---|
| 295 | prefix
|
---|
| 296 | } = parent;
|
---|
| 297 | this.memoise(member, 2);
|
---|
| 298 | const ref = scope.generateUidIdentifierBasedOnNode(node);
|
---|
| 299 | scope.push({
|
---|
| 300 | id: ref
|
---|
| 301 | });
|
---|
| 302 | const seq = [assignmentExpression("=", cloneNode(ref), this.get(member))];
|
---|
| 303 | if (prefix) {
|
---|
| 304 | seq.push(updateExpression(operator, cloneNode(ref), prefix));
|
---|
| 305 | const value = sequenceExpression(seq);
|
---|
| 306 | parentPath.replaceWith(this.set(member, value));
|
---|
| 307 | return;
|
---|
| 308 | } else {
|
---|
| 309 | const ref2 = scope.generateUidIdentifierBasedOnNode(node);
|
---|
| 310 | scope.push({
|
---|
| 311 | id: ref2
|
---|
| 312 | });
|
---|
| 313 | seq.push(assignmentExpression("=", cloneNode(ref2), updateExpression(operator, cloneNode(ref), prefix)), cloneNode(ref));
|
---|
| 314 | const value = sequenceExpression(seq);
|
---|
| 315 | parentPath.replaceWith(sequenceExpression([this.set(member, value), cloneNode(ref2)]));
|
---|
| 316 | return;
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 | if (parentPath.isAssignmentExpression({
|
---|
| 320 | left: node
|
---|
| 321 | })) {
|
---|
| 322 | handleAssignment(this, member, parentPath);
|
---|
| 323 | return;
|
---|
| 324 | }
|
---|
| 325 | if (parentPath.isCallExpression({
|
---|
| 326 | callee: node
|
---|
| 327 | })) {
|
---|
| 328 | parentPath.replaceWith(this.call(member, parentPath.node.arguments));
|
---|
| 329 | return;
|
---|
| 330 | }
|
---|
| 331 | if (parentPath.isOptionalCallExpression({
|
---|
| 332 | callee: node
|
---|
| 333 | })) {
|
---|
| 334 | if (scope.path.isPattern()) {
|
---|
| 335 | parentPath.replaceWith(callExpression(arrowFunctionExpression([], parentPath.node), []));
|
---|
| 336 | return;
|
---|
| 337 | }
|
---|
| 338 | parentPath.replaceWith(this.optionalCall(member, parentPath.node.arguments));
|
---|
| 339 | return;
|
---|
| 340 | }
|
---|
| 341 | if (this.delete && parentPath.isUnaryExpression({
|
---|
| 342 | operator: "delete"
|
---|
| 343 | })) {
|
---|
| 344 | parentPath.replaceWith(this.delete(member));
|
---|
| 345 | return;
|
---|
| 346 | }
|
---|
| 347 | if (parentPath.isForXStatement({
|
---|
| 348 | left: node
|
---|
| 349 | }) || parentPath.isObjectProperty({
|
---|
| 350 | value: node
|
---|
| 351 | }) && parentPath.parentPath.isObjectPattern() || parentPath.isAssignmentPattern({
|
---|
| 352 | left: node
|
---|
| 353 | }) && parentPath.parentPath.isObjectProperty({
|
---|
| 354 | value: parent
|
---|
| 355 | }) && parentPath.parentPath.parentPath.isObjectPattern() || parentPath.isArrayPattern() || parentPath.isAssignmentPattern({
|
---|
| 356 | left: node
|
---|
| 357 | }) && parentPath.parentPath.isArrayPattern() || parentPath.isRestElement()) {
|
---|
| 358 | member.replaceWith(this.destructureSet(member));
|
---|
| 359 | return;
|
---|
| 360 | }
|
---|
| 361 | if (parentPath.isTaggedTemplateExpression()) {
|
---|
| 362 | member.replaceWith(this.boundGet(member));
|
---|
| 363 | } else {
|
---|
| 364 | member.replaceWith(this.get(member));
|
---|
| 365 | }
|
---|
| 366 | }
|
---|
| 367 | };
|
---|
| 368 | function handleAssignment(state, member, parentPath) {
|
---|
| 369 | if (state.simpleSet) {
|
---|
| 370 | member.replaceWith(state.simpleSet(member));
|
---|
| 371 | return;
|
---|
| 372 | }
|
---|
| 373 | const {
|
---|
| 374 | operator,
|
---|
| 375 | right: value
|
---|
| 376 | } = parentPath.node;
|
---|
| 377 | if (operator === "=") {
|
---|
| 378 | parentPath.replaceWith(state.set(member, value));
|
---|
| 379 | } else {
|
---|
| 380 | const operatorTrunc = operator.slice(0, -1);
|
---|
| 381 | if (LOGICAL_OPERATORS.includes(operatorTrunc)) {
|
---|
| 382 | state.memoise(member, 1);
|
---|
| 383 | parentPath.replaceWith(logicalExpression(operatorTrunc, state.get(member), state.set(member, value)));
|
---|
| 384 | } else {
|
---|
| 385 | state.memoise(member, 2);
|
---|
| 386 | parentPath.replaceWith(state.set(member, binaryExpression(operatorTrunc, state.get(member), value)));
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 | }
|
---|
| 390 | function memberExpressionToFunctions(path, visitor, state) {
|
---|
| 391 | path.traverse(visitor, Object.assign({}, handle, state, {
|
---|
| 392 | memoiser: new AssignmentMemoiser()
|
---|
| 393 | }));
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | exports.default = memberExpressionToFunctions;
|
---|
| 397 | //# sourceMappingURL=index.js.map
|
---|