index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. UpdateExpression: {
  20. exit(path) {
  21. const {
  22. scope,
  23. bindingNames,
  24. includeUpdateExpression
  25. } = this;
  26. if (!includeUpdateExpression) {
  27. return;
  28. }
  29. const arg = path.get("argument");
  30. if (!arg.isIdentifier()) return;
  31. const localName = arg.node.name;
  32. if (!bindingNames.has(localName)) return;
  33. if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
  34. return;
  35. }
  36. if (path.parentPath.isExpressionStatement() && !path.isCompletionRecord()) {
  37. const operator = path.node.operator == "++" ? "+=" : "-=";
  38. path.replaceWith(assignmentExpression(operator, arg.node, numericLiteral(1)));
  39. } else if (path.node.prefix) {
  40. path.replaceWith(assignmentExpression("=", identifier(localName), binaryExpression(path.node.operator[0], unaryExpression("+", arg.node), numericLiteral(1))));
  41. } else {
  42. const old = path.scope.generateUidIdentifierBasedOnNode(arg.node, "old");
  43. const varName = old.name;
  44. path.scope.push({
  45. id: old
  46. });
  47. const binary = binaryExpression(path.node.operator[0], identifier(varName),
  48. numericLiteral(1));
  49. path.replaceWith(sequenceExpression([assignmentExpression("=", identifier(varName), unaryExpression("+", arg.node)), assignmentExpression("=", cloneNode(arg.node), binary), identifier(varName)]));
  50. }
  51. }
  52. },
  53. AssignmentExpression: {
  54. exit(path) {
  55. const {
  56. scope,
  57. seen,
  58. bindingNames
  59. } = this;
  60. if (path.node.operator === "=") return;
  61. if (seen.has(path.node)) return;
  62. seen.add(path.node);
  63. const left = path.get("left");
  64. if (!left.isIdentifier()) return;
  65. const localName = left.node.name;
  66. if (!bindingNames.has(localName)) return;
  67. if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
  68. return;
  69. }
  70. const operator = path.node.operator.slice(0, -1);
  71. if (LOGICAL_OPERATORS.includes(operator)) {
  72. path.replaceWith(logicalExpression(
  73. operator, path.node.left, assignmentExpression("=", cloneNode(path.node.left), path.node.right)));
  74. } else {
  75. path.node.right = binaryExpression(
  76. operator, cloneNode(path.node.left), path.node.right);
  77. path.node.operator = "=";
  78. }
  79. }
  80. }
  81. };
  82. function simplifyAccess(path, bindingNames,
  83. includeUpdateExpression = true) {
  84. path.traverse(simpleAssignmentVisitor, {
  85. scope: path.scope,
  86. bindingNames,
  87. seen: new WeakSet(),
  88. includeUpdateExpression
  89. });
  90. }
  91. //# sourceMappingURL=index.js.map