index.js 2.7 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. var _symbol = require("babel-runtime/core-js/symbol");
  6. var _symbol2 = _interopRequireDefault(_symbol);
  7. exports.default = function (_ref) {
  8. var t = _ref.types;
  9. var ALREADY_VISITED = (0, _symbol2.default)();
  10. function findConstructorCall(path) {
  11. var methods = path.get("body.body");
  12. for (var _iterator = methods, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  13. var _ref2;
  14. if (_isArray) {
  15. if (_i >= _iterator.length) break;
  16. _ref2 = _iterator[_i++];
  17. } else {
  18. _i = _iterator.next();
  19. if (_i.done) break;
  20. _ref2 = _i.value;
  21. }
  22. var method = _ref2;
  23. if (method.node.kind === "constructorCall") {
  24. return method;
  25. }
  26. }
  27. return null;
  28. }
  29. function handleClassWithCall(constructorCall, classPath) {
  30. var _classPath = classPath,
  31. node = _classPath.node;
  32. var ref = node.id || classPath.scope.generateUidIdentifier("class");
  33. if (classPath.parentPath.isExportDefaultDeclaration()) {
  34. classPath = classPath.parentPath;
  35. classPath.insertAfter(t.exportDefaultDeclaration(ref));
  36. }
  37. classPath.replaceWithMultiple(buildWrapper({
  38. CLASS_REF: classPath.scope.generateUidIdentifier(ref.name),
  39. CALL_REF: classPath.scope.generateUidIdentifier(ref.name + "Call"),
  40. CALL: t.functionExpression(null, constructorCall.node.params, constructorCall.node.body),
  41. CLASS: t.toExpression(node),
  42. WRAPPER_REF: ref
  43. }));
  44. constructorCall.remove();
  45. }
  46. return {
  47. inherits: require("babel-plugin-syntax-class-constructor-call"),
  48. visitor: {
  49. Class: function Class(path) {
  50. if (path.node[ALREADY_VISITED]) return;
  51. path.node[ALREADY_VISITED] = true;
  52. var constructorCall = findConstructorCall(path);
  53. if (constructorCall) {
  54. handleClassWithCall(constructorCall, path);
  55. } else {
  56. return;
  57. }
  58. }
  59. }
  60. };
  61. };
  62. var _babelTemplate = require("babel-template");
  63. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  64. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  65. var buildWrapper = (0, _babelTemplate2.default)("\n let CLASS_REF = CLASS;\n var CALL_REF = CALL;\n var WRAPPER_REF = function (...args) {\n if (this instanceof WRAPPER_REF) {\n return Reflect.construct(CLASS_REF, args);\n } else {\n return CALL_REF.apply(this, args);\n }\n };\n WRAPPER_REF.__proto__ = CLASS_REF;\n WRAPPER_REF;\n");
  66. module.exports = exports["default"];