index.js 2.0 KB

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