index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 (_ref) {
  6. var t = _ref.types;
  7. var findBareSupers = {
  8. Super: function Super(path) {
  9. if (path.parentPath.isCallExpression({ callee: path.node })) {
  10. this.push(path.parentPath);
  11. }
  12. }
  13. };
  14. var referenceVisitor = {
  15. ReferencedIdentifier: function ReferencedIdentifier(path) {
  16. if (this.scope.hasOwnBinding(path.node.name)) {
  17. this.collision = true;
  18. path.skip();
  19. }
  20. }
  21. };
  22. var buildObjectDefineProperty = (0, _babelTemplate2.default)("\n Object.defineProperty(REF, KEY, {\n // configurable is false by default\n enumerable: true,\n writable: true,\n value: VALUE\n });\n ");
  23. var buildClassPropertySpec = function buildClassPropertySpec(ref, _ref2) {
  24. var key = _ref2.key,
  25. value = _ref2.value,
  26. computed = _ref2.computed;
  27. return buildObjectDefineProperty({
  28. REF: ref,
  29. KEY: t.isIdentifier(key) && !computed ? t.stringLiteral(key.name) : key,
  30. VALUE: value ? value : t.identifier("undefined")
  31. });
  32. };
  33. var buildClassPropertyNonSpec = function buildClassPropertyNonSpec(ref, _ref3) {
  34. var key = _ref3.key,
  35. value = _ref3.value,
  36. computed = _ref3.computed;
  37. return t.expressionStatement(t.assignmentExpression("=", t.memberExpression(ref, key, computed || t.isLiteral(key)), value));
  38. };
  39. return {
  40. inherits: require("babel-plugin-syntax-class-properties"),
  41. visitor: {
  42. Class: function Class(path, state) {
  43. var buildClassProperty = state.opts.spec ? buildClassPropertySpec : buildClassPropertyNonSpec;
  44. var isDerived = !!path.node.superClass;
  45. var constructor = void 0;
  46. var props = [];
  47. var body = path.get("body");
  48. for (var _iterator = body.get("body"), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  49. var _ref4;
  50. if (_isArray) {
  51. if (_i >= _iterator.length) break;
  52. _ref4 = _iterator[_i++];
  53. } else {
  54. _i = _iterator.next();
  55. if (_i.done) break;
  56. _ref4 = _i.value;
  57. }
  58. var _path = _ref4;
  59. if (_path.isClassProperty()) {
  60. props.push(_path);
  61. } else if (_path.isClassMethod({ kind: "constructor" })) {
  62. constructor = _path;
  63. }
  64. }
  65. if (!props.length) return;
  66. var nodes = [];
  67. var ref = void 0;
  68. if (path.isClassExpression() || !path.node.id) {
  69. (0, _babelHelperFunctionName2.default)(path);
  70. ref = path.scope.generateUidIdentifier("class");
  71. } else {
  72. ref = path.node.id;
  73. }
  74. var instanceBody = [];
  75. for (var _iterator2 = props, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  76. var _ref5;
  77. if (_isArray2) {
  78. if (_i2 >= _iterator2.length) break;
  79. _ref5 = _iterator2[_i2++];
  80. } else {
  81. _i2 = _iterator2.next();
  82. if (_i2.done) break;
  83. _ref5 = _i2.value;
  84. }
  85. var _prop = _ref5;
  86. var propNode = _prop.node;
  87. if (propNode.decorators && propNode.decorators.length > 0) continue;
  88. if (!state.opts.spec && !propNode.value) continue;
  89. var isStatic = propNode.static;
  90. if (isStatic) {
  91. nodes.push(buildClassProperty(ref, propNode));
  92. } else {
  93. if (!propNode.value) continue;
  94. instanceBody.push(buildClassProperty(t.thisExpression(), propNode));
  95. }
  96. }
  97. if (instanceBody.length) {
  98. if (!constructor) {
  99. var newConstructor = t.classMethod("constructor", t.identifier("constructor"), [], t.blockStatement([]));
  100. if (isDerived) {
  101. newConstructor.params = [t.restElement(t.identifier("args"))];
  102. newConstructor.body.body.push(t.returnStatement(t.callExpression(t.super(), [t.spreadElement(t.identifier("args"))])));
  103. }
  104. var _body$unshiftContaine = body.unshiftContainer("body", newConstructor);
  105. constructor = _body$unshiftContaine[0];
  106. }
  107. var collisionState = {
  108. collision: false,
  109. scope: constructor.scope
  110. };
  111. for (var _iterator3 = props, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
  112. var _ref6;
  113. if (_isArray3) {
  114. if (_i3 >= _iterator3.length) break;
  115. _ref6 = _iterator3[_i3++];
  116. } else {
  117. _i3 = _iterator3.next();
  118. if (_i3.done) break;
  119. _ref6 = _i3.value;
  120. }
  121. var prop = _ref6;
  122. prop.traverse(referenceVisitor, collisionState);
  123. if (collisionState.collision) break;
  124. }
  125. if (collisionState.collision) {
  126. var initialisePropsRef = path.scope.generateUidIdentifier("initialiseProps");
  127. nodes.push(t.variableDeclaration("var", [t.variableDeclarator(initialisePropsRef, t.functionExpression(null, [], t.blockStatement(instanceBody)))]));
  128. instanceBody = [t.expressionStatement(t.callExpression(t.memberExpression(initialisePropsRef, t.identifier("call")), [t.thisExpression()]))];
  129. }
  130. if (isDerived) {
  131. var bareSupers = [];
  132. constructor.traverse(findBareSupers, bareSupers);
  133. for (var _iterator4 = bareSupers, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) {
  134. var _ref7;
  135. if (_isArray4) {
  136. if (_i4 >= _iterator4.length) break;
  137. _ref7 = _iterator4[_i4++];
  138. } else {
  139. _i4 = _iterator4.next();
  140. if (_i4.done) break;
  141. _ref7 = _i4.value;
  142. }
  143. var bareSuper = _ref7;
  144. bareSuper.insertAfter(instanceBody);
  145. }
  146. } else {
  147. constructor.get("body").unshiftContainer("body", instanceBody);
  148. }
  149. }
  150. for (var _iterator5 = props, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : (0, _getIterator3.default)(_iterator5);;) {
  151. var _ref8;
  152. if (_isArray5) {
  153. if (_i5 >= _iterator5.length) break;
  154. _ref8 = _iterator5[_i5++];
  155. } else {
  156. _i5 = _iterator5.next();
  157. if (_i5.done) break;
  158. _ref8 = _i5.value;
  159. }
  160. var _prop2 = _ref8;
  161. _prop2.remove();
  162. }
  163. if (!nodes.length) return;
  164. if (path.isClassExpression()) {
  165. path.scope.push({ id: ref });
  166. path.replaceWith(t.assignmentExpression("=", ref, path.node));
  167. } else {
  168. if (!path.node.id) {
  169. path.node.id = ref;
  170. }
  171. if (path.parentPath.isExportDeclaration()) {
  172. path = path.parentPath;
  173. }
  174. }
  175. path.insertAfter(nodes);
  176. },
  177. ArrowFunctionExpression: function ArrowFunctionExpression(path) {
  178. var classExp = path.get("body");
  179. if (!classExp.isClassExpression()) return;
  180. var body = classExp.get("body");
  181. var members = body.get("body");
  182. if (members.some(function (member) {
  183. return member.isClassProperty();
  184. })) {
  185. path.ensureBlock();
  186. }
  187. }
  188. }
  189. };
  190. };
  191. var _babelHelperFunctionName = require("babel-helper-function-name");
  192. var _babelHelperFunctionName2 = _interopRequireDefault(_babelHelperFunctionName);
  193. var _babelTemplate = require("babel-template");
  194. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  195. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  196. module.exports = exports["default"];