index.js 3.0 KB

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