index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. "use strict";
  2. exports.__esModule = true;
  3. var _create = require("babel-runtime/core-js/object/create");
  4. var _create2 = _interopRequireDefault(_create);
  5. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  6. var _getIterator3 = _interopRequireDefault(_getIterator2);
  7. exports.default = function (_ref) {
  8. var t = _ref.types;
  9. function cleanDecorators(decorators) {
  10. return decorators.reverse().map(function (dec) {
  11. return dec.expression;
  12. });
  13. }
  14. function transformClass(path, ref, state) {
  15. var nodes = [];
  16. state;
  17. var classDecorators = path.node.decorators;
  18. if (classDecorators) {
  19. path.node.decorators = null;
  20. classDecorators = cleanDecorators(classDecorators);
  21. for (var _iterator = classDecorators, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  22. var _ref2;
  23. if (_isArray) {
  24. if (_i >= _iterator.length) break;
  25. _ref2 = _iterator[_i++];
  26. } else {
  27. _i = _iterator.next();
  28. if (_i.done) break;
  29. _ref2 = _i.value;
  30. }
  31. var decorator = _ref2;
  32. nodes.push(buildClassDecorator({
  33. CLASS_REF: ref,
  34. DECORATOR: decorator
  35. }));
  36. }
  37. }
  38. var map = (0, _create2.default)(null);
  39. for (var _iterator2 = path.get("body.body"), _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  40. var _ref3;
  41. if (_isArray2) {
  42. if (_i2 >= _iterator2.length) break;
  43. _ref3 = _iterator2[_i2++];
  44. } else {
  45. _i2 = _iterator2.next();
  46. if (_i2.done) break;
  47. _ref3 = _i2.value;
  48. }
  49. var method = _ref3;
  50. var decorators = method.node.decorators;
  51. if (!decorators) continue;
  52. var _alias = t.toKeyAlias(method.node);
  53. map[_alias] = map[_alias] || [];
  54. map[_alias].push(method.node);
  55. method.remove();
  56. }
  57. for (var alias in map) {
  58. var items = map[alias];
  59. items;
  60. }
  61. return nodes;
  62. }
  63. function hasDecorators(path) {
  64. if (path.isClass()) {
  65. if (path.node.decorators) return true;
  66. for (var _iterator3 = path.node.body.body, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
  67. var _ref4;
  68. if (_isArray3) {
  69. if (_i3 >= _iterator3.length) break;
  70. _ref4 = _iterator3[_i3++];
  71. } else {
  72. _i3 = _iterator3.next();
  73. if (_i3.done) break;
  74. _ref4 = _i3.value;
  75. }
  76. var method = _ref4;
  77. if (method.decorators) {
  78. return true;
  79. }
  80. }
  81. } else if (path.isObjectExpression()) {
  82. for (var _iterator4 = path.node.properties, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) {
  83. var _ref5;
  84. if (_isArray4) {
  85. if (_i4 >= _iterator4.length) break;
  86. _ref5 = _iterator4[_i4++];
  87. } else {
  88. _i4 = _iterator4.next();
  89. if (_i4.done) break;
  90. _ref5 = _i4.value;
  91. }
  92. var prop = _ref5;
  93. if (prop.decorators) {
  94. return true;
  95. }
  96. }
  97. }
  98. return false;
  99. }
  100. function doError(path) {
  101. throw path.buildCodeFrameError("Decorators are not officially supported yet in 6.x pending a proposal update.\nHowever, if you need to use them you can install the legacy decorators transform with:\n\nnpm install babel-plugin-transform-decorators-legacy --save-dev\n\nand add the following line to your .babelrc file:\n\n{\n \"plugins\": [\"transform-decorators-legacy\"]\n}\n\nThe repo url is: https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy.\n ");
  102. }
  103. return {
  104. inherits: require("babel-plugin-syntax-decorators"),
  105. visitor: {
  106. ClassExpression: function ClassExpression(path) {
  107. if (!hasDecorators(path)) return;
  108. doError(path);
  109. (0, _babelHelperExplodeClass2.default)(path);
  110. var ref = path.scope.generateDeclaredUidIdentifier("ref");
  111. var nodes = [];
  112. nodes.push(t.assignmentExpression("=", ref, path.node));
  113. nodes = nodes.concat(transformClass(path, ref, this));
  114. nodes.push(ref);
  115. path.replaceWith(t.sequenceExpression(nodes));
  116. },
  117. ClassDeclaration: function ClassDeclaration(path) {
  118. if (!hasDecorators(path)) return;
  119. doError(path);
  120. (0, _babelHelperExplodeClass2.default)(path);
  121. var ref = path.node.id;
  122. var nodes = [];
  123. nodes = nodes.concat(transformClass(path, ref, this).map(function (expr) {
  124. return t.expressionStatement(expr);
  125. }));
  126. nodes.push(t.expressionStatement(ref));
  127. path.insertAfter(nodes);
  128. },
  129. ObjectExpression: function ObjectExpression(path) {
  130. if (!hasDecorators(path)) return;
  131. doError(path);
  132. }
  133. }
  134. };
  135. };
  136. var _babelTemplate = require("babel-template");
  137. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  138. var _babelHelperExplodeClass = require("babel-helper-explode-class");
  139. var _babelHelperExplodeClass2 = _interopRequireDefault(_babelHelperExplodeClass);
  140. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  141. var buildClassDecorator = (0, _babelTemplate2.default)("\n CLASS_REF = DECORATOR(CLASS_REF) || CLASS_REF;\n");
  142. module.exports = exports["default"];