inline-callSuper-helpers.js 2.3 KB

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