index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*istanbul ignore next*/"use strict";
  2. exports.__esModule = true;
  3. exports.default = function (opts) {
  4. var visitor = {};
  5. function isAssignment(node) {
  6. return node.operator === opts.operator + "=";
  7. }
  8. function buildAssignment(left, right) {
  9. return t.assignmentExpression("=", left, right);
  10. }
  11. visitor.ExpressionStatement = function (path, file) {
  12. // hit the `AssignmentExpression` one below
  13. if (path.isCompletionRecord()) return;
  14. var expr = path.node.expression;
  15. if (!isAssignment(expr)) return;
  16. var nodes = [];
  17. var exploded = /*istanbul ignore next*/(0, _babelHelperExplodeAssignableExpression2.default)(expr.left, nodes, file, path.scope, true);
  18. nodes.push(t.expressionStatement(buildAssignment(exploded.ref, opts.build(exploded.uid, expr.right))));
  19. path.replaceWithMultiple(nodes);
  20. };
  21. visitor.AssignmentExpression = function (path, file) {
  22. /*istanbul ignore next*/var node = path.node;
  23. /*istanbul ignore next*/var scope = path.scope;
  24. if (!isAssignment(node)) return;
  25. var nodes = [];
  26. var exploded = /*istanbul ignore next*/(0, _babelHelperExplodeAssignableExpression2.default)(node.left, nodes, file, scope);
  27. nodes.push(buildAssignment(exploded.ref, opts.build(exploded.uid, node.right)));
  28. path.replaceWithMultiple(nodes);
  29. };
  30. visitor.BinaryExpression = function (path) {
  31. /*istanbul ignore next*/var node = path.node;
  32. if (node.operator === opts.operator) {
  33. path.replaceWith(opts.build(node.left, node.right));
  34. }
  35. };
  36. return visitor;
  37. };
  38. var /*istanbul ignore next*/_babelHelperExplodeAssignableExpression = require("babel-helper-explode-assignable-expression");
  39. /*istanbul ignore next*/
  40. var _babelHelperExplodeAssignableExpression2 = _interopRequireDefault(_babelHelperExplodeAssignableExpression);
  41. var /*istanbul ignore next*/_babelTypes = require("babel-types");
  42. /*istanbul ignore next*/
  43. var t = _interopRequireWildcard(_babelTypes);
  44. /*istanbul ignore next*/
  45. 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; } }
  46. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  47. module.exports = exports["default"];