index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = function (opts) {
  4. var visitor = {};
  5. function isAssignment(node) {
  6. return node && node.operator === opts.operator + "=";
  7. }
  8. function buildAssignment(left, right) {
  9. return t.assignmentExpression("=", left, right);
  10. }
  11. visitor.ExpressionStatement = function (path, file) {
  12. if (path.isCompletionRecord()) return;
  13. var expr = path.node.expression;
  14. if (!isAssignment(expr)) return;
  15. var nodes = [];
  16. var exploded = (0, _babelHelperExplodeAssignableExpression2.default)(expr.left, nodes, file, path.scope, true);
  17. nodes.push(t.expressionStatement(buildAssignment(exploded.ref, opts.build(exploded.uid, expr.right))));
  18. path.replaceWithMultiple(nodes);
  19. };
  20. visitor.AssignmentExpression = function (path, file) {
  21. var node = path.node,
  22. scope = path.scope;
  23. if (!isAssignment(node)) return;
  24. var nodes = [];
  25. var exploded = (0, _babelHelperExplodeAssignableExpression2.default)(node.left, nodes, file, scope);
  26. nodes.push(buildAssignment(exploded.ref, opts.build(exploded.uid, node.right)));
  27. path.replaceWithMultiple(nodes);
  28. };
  29. visitor.BinaryExpression = function (path) {
  30. var node = path.node;
  31. if (node.operator === opts.operator) {
  32. path.replaceWith(opts.build(node.left, node.right));
  33. }
  34. };
  35. return visitor;
  36. };
  37. var _babelHelperExplodeAssignableExpression = require("babel-helper-explode-assignable-expression");
  38. var _babelHelperExplodeAssignableExpression2 = _interopRequireDefault(_babelHelperExplodeAssignableExpression);
  39. var _babelTypes = require("babel-types");
  40. var t = _interopRequireWildcard(_babelTypes);
  41. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  42. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  43. module.exports = exports["default"];