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