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