1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
8 | var _core = require("@babel/core");
|
---|
9 | var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
|
---|
10 | api.assertVersion(7);
|
---|
11 | return {
|
---|
12 | name: "transform-new-target",
|
---|
13 | visitor: {
|
---|
14 | MetaProperty(path) {
|
---|
15 | const meta = path.get("meta");
|
---|
16 | const property = path.get("property");
|
---|
17 | const {
|
---|
18 | scope
|
---|
19 | } = path;
|
---|
20 | if (meta.isIdentifier({
|
---|
21 | name: "new"
|
---|
22 | }) && property.isIdentifier({
|
---|
23 | name: "target"
|
---|
24 | })) {
|
---|
25 | const func = path.findParent(path => {
|
---|
26 | if (path.isClass()) return true;
|
---|
27 | if (path.isFunction() && !path.isArrowFunctionExpression()) {
|
---|
28 | if (path.isClassMethod({
|
---|
29 | kind: "constructor"
|
---|
30 | })) {
|
---|
31 | return false;
|
---|
32 | }
|
---|
33 | return true;
|
---|
34 | }
|
---|
35 | return false;
|
---|
36 | });
|
---|
37 | if (!func) {
|
---|
38 | throw path.buildCodeFrameError("new.target must be under a (non-arrow) function or a class.");
|
---|
39 | }
|
---|
40 | const {
|
---|
41 | node
|
---|
42 | } = func;
|
---|
43 | if (_core.types.isMethod(node)) {
|
---|
44 | path.replaceWith(scope.buildUndefinedNode());
|
---|
45 | return;
|
---|
46 | }
|
---|
47 | const constructor = _core.types.memberExpression(_core.types.thisExpression(), _core.types.identifier("constructor"));
|
---|
48 | if (func.isClass()) {
|
---|
49 | path.replaceWith(constructor);
|
---|
50 | return;
|
---|
51 | }
|
---|
52 | if (!node.id) {
|
---|
53 | node.id = scope.generateUidIdentifier("target");
|
---|
54 | } else {
|
---|
55 | let scope = path.scope;
|
---|
56 | const name = node.id.name;
|
---|
57 | while (scope !== func.parentPath.scope) {
|
---|
58 | if (scope.hasOwnBinding(name) && !scope.bindingIdentifierEquals(name, node.id)) {
|
---|
59 | scope.rename(name);
|
---|
60 | }
|
---|
61 | scope = scope.parent;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | path.replaceWith(_core.types.conditionalExpression(_core.types.binaryExpression("instanceof", _core.types.thisExpression(), _core.types.cloneNode(node.id)), constructor, scope.buildUndefinedNode()));
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | };
|
---|
69 | });
|
---|
70 |
|
---|
71 | //# sourceMappingURL=index.js.map
|
---|