inline-createSuper-helpers.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = addCreateSuperHelper;
  6. var _core = require("@babel/core");
  7. const helper = _core.template.statement`
  8. function CREATE_SUPER(Derived) {
  9. function isNativeReflectConstruct() {
  10. if (typeof Reflect === "undefined" || !Reflect.construct) return false;
  11. // core-js@3
  12. if (Reflect.construct.sham) return false;
  13. // Proxy can't be polyfilled. Every browser implemented
  14. // proxies before or at the same time as Reflect.construct,
  15. // so if they support Proxy they also support Reflect.construct.
  16. if (typeof Proxy === "function") return true;
  17. // Since Reflect.construct can't be properly polyfilled, some
  18. // implementations (e.g. core-js@2) don't set the correct internal slots.
  19. // Those polyfills don't allow us to subclass built-ins, so we need to
  20. // use our fallback implementation.
  21. try {
  22. // If the internal slots aren't set, this throws an error similar to
  23. // TypeError: this is not a Date object.
  24. Date.prototype.toString.call(Reflect.construct(Date, [], function() {}));
  25. return true;
  26. } catch (e) {
  27. return false;
  28. }
  29. }
  30. return function () {
  31. var Super = GET_PROTOTYPE_OF(Derived), result;
  32. if (isNativeReflectConstruct()) {
  33. // NOTE: This doesn't work if this.__proto__.constructor has been modified.
  34. var NewTarget = GET_PROTOTYPE_OF(this).constructor;
  35. result = Reflect.construct(Super, arguments, NewTarget);
  36. } else {
  37. result = Super.apply(this, arguments);
  38. }
  39. return POSSIBLE_CONSTRUCTOR_RETURN(this, result);
  40. }
  41. }
  42. `;
  43. const helperIDs = new WeakMap();
  44. function addCreateSuperHelper(file) {
  45. if (helperIDs.has(file)) {
  46. return (_core.types.cloneNode || _core.types.clone)(helperIDs.get(file));
  47. }
  48. try {
  49. return file.addHelper("createSuper");
  50. } catch (_unused) {
  51. }
  52. const id = file.scope.generateUidIdentifier("createSuper");
  53. helperIDs.set(file, id);
  54. const fn = helper({
  55. CREATE_SUPER: id,
  56. GET_PROTOTYPE_OF: file.addHelper("getPrototypeOf"),
  57. POSSIBLE_CONSTRUCTOR_RETURN: file.addHelper("possibleConstructorReturn")
  58. });
  59. file.path.unshiftContainer("body", [fn]);
  60. file.scope.registerDeclaration(file.path.get("body.0"));
  61. return _core.types.cloneNode(id);
  62. }
  63. //# sourceMappingURL=inline-createSuper-helpers.js.map