[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = simplifyAccess;
|
---|
| 7 |
|
---|
| 8 | var _t = require("@babel/types");
|
---|
| 9 |
|
---|
| 10 | const {
|
---|
| 11 | LOGICAL_OPERATORS,
|
---|
| 12 | assignmentExpression,
|
---|
| 13 | binaryExpression,
|
---|
| 14 | cloneNode,
|
---|
| 15 | identifier,
|
---|
| 16 | logicalExpression,
|
---|
| 17 | numericLiteral,
|
---|
| 18 | sequenceExpression,
|
---|
| 19 | unaryExpression
|
---|
| 20 | } = _t;
|
---|
| 21 |
|
---|
| 22 | function simplifyAccess(path, bindingNames) {
|
---|
| 23 | path.traverse(simpleAssignmentVisitor, {
|
---|
| 24 | scope: path.scope,
|
---|
| 25 | bindingNames,
|
---|
| 26 | seen: new WeakSet()
|
---|
| 27 | });
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | const simpleAssignmentVisitor = {
|
---|
| 31 | UpdateExpression: {
|
---|
| 32 | exit(path) {
|
---|
| 33 | const {
|
---|
| 34 | scope,
|
---|
| 35 | bindingNames
|
---|
| 36 | } = this;
|
---|
| 37 | const arg = path.get("argument");
|
---|
| 38 | if (!arg.isIdentifier()) return;
|
---|
| 39 | const localName = arg.node.name;
|
---|
| 40 | if (!bindingNames.has(localName)) return;
|
---|
| 41 |
|
---|
| 42 | if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
|
---|
| 43 | return;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | if (path.parentPath.isExpressionStatement() && !path.isCompletionRecord()) {
|
---|
| 47 | const operator = path.node.operator == "++" ? "+=" : "-=";
|
---|
| 48 | path.replaceWith(assignmentExpression(operator, arg.node, numericLiteral(1)));
|
---|
| 49 | } else if (path.node.prefix) {
|
---|
| 50 | path.replaceWith(assignmentExpression("=", identifier(localName), binaryExpression(path.node.operator[0], unaryExpression("+", arg.node), numericLiteral(1))));
|
---|
| 51 | } else {
|
---|
| 52 | const old = path.scope.generateUidIdentifierBasedOnNode(arg.node, "old");
|
---|
| 53 | const varName = old.name;
|
---|
| 54 | path.scope.push({
|
---|
| 55 | id: old
|
---|
| 56 | });
|
---|
| 57 | const binary = binaryExpression(path.node.operator[0], identifier(varName), numericLiteral(1));
|
---|
| 58 | path.replaceWith(sequenceExpression([assignmentExpression("=", identifier(varName), unaryExpression("+", arg.node)), assignmentExpression("=", cloneNode(arg.node), binary), identifier(varName)]));
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | },
|
---|
| 63 | AssignmentExpression: {
|
---|
| 64 | exit(path) {
|
---|
| 65 | const {
|
---|
| 66 | scope,
|
---|
| 67 | seen,
|
---|
| 68 | bindingNames
|
---|
| 69 | } = this;
|
---|
| 70 | if (path.node.operator === "=") return;
|
---|
| 71 | if (seen.has(path.node)) return;
|
---|
| 72 | seen.add(path.node);
|
---|
| 73 | const left = path.get("left");
|
---|
| 74 | if (!left.isIdentifier()) return;
|
---|
| 75 | const localName = left.node.name;
|
---|
| 76 | if (!bindingNames.has(localName)) return;
|
---|
| 77 |
|
---|
| 78 | if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
|
---|
| 79 | return;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | const operator = path.node.operator.slice(0, -1);
|
---|
| 83 |
|
---|
| 84 | if (LOGICAL_OPERATORS.includes(operator)) {
|
---|
| 85 | path.replaceWith(logicalExpression(operator, path.node.left, assignmentExpression("=", cloneNode(path.node.left), path.node.right)));
|
---|
| 86 | } else {
|
---|
| 87 | path.node.right = binaryExpression(operator, cloneNode(path.node.left), path.node.right);
|
---|
| 88 | path.node.operator = "=";
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | }
|
---|
| 93 | }; |
---|