inline-createSuper-helpers.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. const id = file.scope.generateUidIdentifier("createSuper");
  52. helperIDs.set(file, id);
  53. const fn = helper({
  54. CREATE_SUPER: id,
  55. GET_PROTOTYPE_OF: file.addHelper("getPrototypeOf"),
  56. POSSIBLE_CONSTRUCTOR_RETURN: file.addHelper("possibleConstructorReturn")
  57. });
  58. file.path.unshiftContainer("body", [fn]);
  59. file.scope.registerDeclaration(file.path.get("body.0"));
  60. return _core.types.cloneNode(id);
  61. }
  62. //# sourceMappingURL=inline-createSuper-helpers.js.map