1 |
|
2 |
|
3 | "use strict";
|
4 |
|
5 | var _getIterator = require("babel-runtime/core-js/get-iterator")["default"];
|
6 |
|
7 | exports.__esModule = true;
|
8 |
|
9 | exports["default"] = function (_ref2) {
|
10 | var t = _ref2.types;
|
11 |
|
12 | var findBareSupers = {
|
13 | Super: function Super(path) {
|
14 | if (path.parentPath.isCallExpression({ callee: path.node })) {
|
15 | this.push(path.parentPath);
|
16 | }
|
17 | }
|
18 | };
|
19 |
|
20 | var referenceVisitor = {
|
21 | ReferencedIdentifier: function ReferencedIdentifier(path) {
|
22 | if (this.scope.hasOwnBinding(path.node.name)) {
|
23 | this.collision = true;
|
24 | path.skip();
|
25 | }
|
26 | }
|
27 | };
|
28 |
|
29 | return {
|
30 | inherits: require("babel-plugin-syntax-class-properties"),
|
31 |
|
32 | visitor: {
|
33 | Class: function Class(path) {
|
34 | var isDerived = !!path.node.superClass;
|
35 | var constructor = undefined;
|
36 | var props = [];
|
37 | var body = path.get("body");
|
38 |
|
39 | for (var _iterator = body.get("body"), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _getIterator(_iterator);;) {
|
40 | var _ref;
|
41 |
|
42 | if (_isArray) {
|
43 | if (_i >= _iterator.length) break;
|
44 | _ref = _iterator[_i++];
|
45 | } else {
|
46 | _i = _iterator.next();
|
47 | if (_i.done) break;
|
48 | _ref = _i.value;
|
49 | }
|
50 |
|
51 | var _path = _ref;
|
52 |
|
53 | if (_path.isClassProperty()) {
|
54 | props.push(_path);
|
55 | } else if (_path.isClassMethod({ kind: "constructor" })) {
|
56 | constructor = _path;
|
57 | }
|
58 | }
|
59 |
|
60 | if (!props.length) return;
|
61 |
|
62 | var nodes = [];
|
63 | var ref = undefined;
|
64 |
|
65 | if (path.isClassExpression()) {
|
66 | ref = path.scope.generateUidIdentifier();
|
67 | } else {
|
68 |
|
69 | ref = path.node.id;
|
70 | }
|
71 |
|
72 | var instanceBody = [];
|
73 |
|
74 | for (var _i2 = 0; _i2 < props.length; _i2++) {
|
75 | var prop = props[_i2];
|
76 | var propNode = prop.node;
|
77 | if (propNode.decorators) continue;
|
78 | if (!propNode.value) continue;
|
79 |
|
80 | var isStatic = propNode["static"];
|
81 |
|
82 | if (isStatic) {
|
83 | nodes.push(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(ref, propNode.key), propNode.value)));
|
84 | } else {
|
85 | instanceBody.push(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(t.thisExpression(), propNode.key), propNode.value)));
|
86 | }
|
87 | }
|
88 |
|
89 | if (instanceBody.length) {
|
90 | if (!constructor) {
|
91 | var newConstructor = t.classMethod("constructor", t.identifier("constructor"), [], t.blockStatement([]));
|
92 | if (isDerived) {
|
93 | newConstructor.params = [t.restElement(t.identifier("args"))];
|
94 | newConstructor.body.body.push(t.returnStatement(t.callExpression(t["super"](), [t.spreadElement(t.identifier("args"))])));
|
95 | }
|
96 |
|
97 | var _body$unshiftContainer = body.unshiftContainer("body", newConstructor);
|
98 |
|
99 | constructor = _body$unshiftContainer[0];
|
100 | }
|
101 |
|
102 | var collisionState = {
|
103 | collision: false,
|
104 | scope: constructor.scope
|
105 | };
|
106 |
|
107 | for (var _i3 = 0; _i3 < props.length; _i3++) {
|
108 | var prop = props[_i3];
|
109 | prop.traverse(referenceVisitor, collisionState);
|
110 | if (collisionState.collision) break;
|
111 | }
|
112 |
|
113 | if (collisionState.collision) {
|
114 | var initialisePropsRef = path.scope.generateUidIdentifier("initialiseProps");
|
115 |
|
116 | nodes.push(t.variableDeclaration("var", [t.variableDeclarator(initialisePropsRef, t.functionExpression(null, [], t.blockStatement(instanceBody)))]));
|
117 |
|
118 | instanceBody = [t.expressionStatement(t.callExpression(t.memberExpression(initialisePropsRef, t.identifier("call")), [t.thisExpression()]))];
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 | if (isDerived) {
|
124 | var bareSupers = [];
|
125 | constructor.traverse(findBareSupers, bareSupers);
|
126 | for (var _i4 = 0; _i4 < bareSupers.length; _i4++) {
|
127 | var bareSuper = bareSupers[_i4];
|
128 | bareSuper.insertAfter(instanceBody);
|
129 | }
|
130 | } else {
|
131 | constructor.get("body").unshiftContainer("body", instanceBody);
|
132 | }
|
133 | }
|
134 |
|
135 | for (var _i5 = 0; _i5 < props.length; _i5++) {
|
136 | var prop = props[_i5];
|
137 | prop.remove();
|
138 | }
|
139 |
|
140 | if (!nodes.length) return;
|
141 |
|
142 | if (path.isClassExpression()) {
|
143 | nodes.push(t.expressionStatement(ref));
|
144 | }
|
145 |
|
146 | path.insertAfter(nodes);
|
147 | }
|
148 | }
|
149 | };
|
150 | };
|
151 |
|
152 | module.exports = exports["default"]; |
\ | No newline at end of file |