Last change
on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.9 KB
|
Line | |
---|
1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
9 |
|
---|
10 | var _core = require("@babel/core");
|
---|
11 |
|
---|
12 | var _default = (0, _helperPluginUtils.declare)(api => {
|
---|
13 | api.assertVersion(7);
|
---|
14 | return {
|
---|
15 | name: "transform-new-target",
|
---|
16 | visitor: {
|
---|
17 | MetaProperty(path) {
|
---|
18 | const meta = path.get("meta");
|
---|
19 | const property = path.get("property");
|
---|
20 | const {
|
---|
21 | scope
|
---|
22 | } = path;
|
---|
23 |
|
---|
24 | if (meta.isIdentifier({
|
---|
25 | name: "new"
|
---|
26 | }) && property.isIdentifier({
|
---|
27 | name: "target"
|
---|
28 | })) {
|
---|
29 | const func = path.findParent(path => {
|
---|
30 | if (path.isClass()) return true;
|
---|
31 |
|
---|
32 | if (path.isFunction() && !path.isArrowFunctionExpression()) {
|
---|
33 | if (path.isClassMethod({
|
---|
34 | kind: "constructor"
|
---|
35 | })) {
|
---|
36 | return false;
|
---|
37 | }
|
---|
38 |
|
---|
39 | return true;
|
---|
40 | }
|
---|
41 |
|
---|
42 | return false;
|
---|
43 | });
|
---|
44 |
|
---|
45 | if (!func) {
|
---|
46 | throw path.buildCodeFrameError("new.target must be under a (non-arrow) function or a class.");
|
---|
47 | }
|
---|
48 |
|
---|
49 | const {
|
---|
50 | node
|
---|
51 | } = func;
|
---|
52 |
|
---|
53 | if (!node.id) {
|
---|
54 | if (func.isMethod()) {
|
---|
55 | path.replaceWith(scope.buildUndefinedNode());
|
---|
56 | return;
|
---|
57 | }
|
---|
58 |
|
---|
59 | node.id = scope.generateUidIdentifier("target");
|
---|
60 | }
|
---|
61 |
|
---|
62 | const constructor = _core.types.memberExpression(_core.types.thisExpression(), _core.types.identifier("constructor"));
|
---|
63 |
|
---|
64 | if (func.isClass()) {
|
---|
65 | path.replaceWith(constructor);
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | path.replaceWith(_core.types.conditionalExpression(_core.types.binaryExpression("instanceof", _core.types.thisExpression(), _core.types.cloneNode(node.id)), constructor, scope.buildUndefinedNode()));
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|
74 | };
|
---|
75 | });
|
---|
76 |
|
---|
77 | exports.default = _default; |
---|
Note:
See
TracBrowser
for help on using the repository browser.