index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. exports.__esModule = true;
  3. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  5. exports.default = bindifyDecorators;
  6. var _babelTypes = require("babel-types");
  7. var t = _interopRequireWildcard(_babelTypes);
  8. 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; } }
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function bindifyDecorators(decorators) {
  11. for (var _iterator = decorators, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  12. var _ref;
  13. if (_isArray) {
  14. if (_i >= _iterator.length) break;
  15. _ref = _iterator[_i++];
  16. } else {
  17. _i = _iterator.next();
  18. if (_i.done) break;
  19. _ref = _i.value;
  20. }
  21. var decoratorPath = _ref;
  22. var decorator = decoratorPath.node;
  23. var expression = decorator.expression;
  24. if (!t.isMemberExpression(expression)) continue;
  25. var temp = decoratorPath.scope.maybeGenerateMemoised(expression.object);
  26. var ref = void 0;
  27. var nodes = [];
  28. if (temp) {
  29. ref = temp;
  30. nodes.push(t.assignmentExpression("=", temp, expression.object));
  31. } else {
  32. ref = expression.object;
  33. }
  34. nodes.push(t.callExpression(t.memberExpression(t.memberExpression(ref, expression.property, expression.computed), t.identifier("bind")), [ref]));
  35. if (nodes.length === 1) {
  36. decorator.expression = nodes[0];
  37. } else {
  38. decorator.expression = t.sequenceExpression(nodes);
  39. }
  40. }
  41. }
  42. module.exports = exports["default"];