index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 = function (classPath) {
  6. classPath.assertClass();
  7. var memoisedExpressions = [];
  8. function maybeMemoise(path) {
  9. if (!path.node || path.isPure()) return;
  10. var uid = classPath.scope.generateDeclaredUidIdentifier();
  11. memoisedExpressions.push(t.assignmentExpression("=", uid, path.node));
  12. path.replaceWith(uid);
  13. }
  14. function memoiseDecorators(paths) {
  15. if (!Array.isArray(paths) || !paths.length) return;
  16. paths = paths.reverse();
  17. (0, _babelHelperBindifyDecorators2.default)(paths);
  18. for (var _iterator = paths, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  19. var _ref;
  20. if (_isArray) {
  21. if (_i >= _iterator.length) break;
  22. _ref = _iterator[_i++];
  23. } else {
  24. _i = _iterator.next();
  25. if (_i.done) break;
  26. _ref = _i.value;
  27. }
  28. var path = _ref;
  29. maybeMemoise(path);
  30. }
  31. }
  32. maybeMemoise(classPath.get("superClass"));
  33. memoiseDecorators(classPath.get("decorators"), true);
  34. var methods = classPath.get("body.body");
  35. for (var _iterator2 = methods, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  36. var _ref2;
  37. if (_isArray2) {
  38. if (_i2 >= _iterator2.length) break;
  39. _ref2 = _iterator2[_i2++];
  40. } else {
  41. _i2 = _iterator2.next();
  42. if (_i2.done) break;
  43. _ref2 = _i2.value;
  44. }
  45. var methodPath = _ref2;
  46. if (methodPath.is("computed")) {
  47. maybeMemoise(methodPath.get("key"));
  48. }
  49. if (methodPath.has("decorators")) {
  50. memoiseDecorators(classPath.get("decorators"));
  51. }
  52. }
  53. if (memoisedExpressions) {
  54. classPath.insertBefore(memoisedExpressions.map(function (expr) {
  55. return t.expressionStatement(expr);
  56. }));
  57. }
  58. };
  59. var _babelHelperBindifyDecorators = require("babel-helper-bindify-decorators");
  60. var _babelHelperBindifyDecorators2 = _interopRequireDefault(_babelHelperBindifyDecorators);
  61. var _babelTypes = require("babel-types");
  62. var t = _interopRequireWildcard(_babelTypes);
  63. 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; } }
  64. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  65. module.exports = exports["default"];