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