-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
276 lines (265 loc) · 9.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
export default function({types: t}) {
return {
visitor: {
Program(path) {
path.traverse({
Class(path) {
let insertPath = path.parentPath.type === 'ExportNamedDeclaration'||path.parentPath.type==='ExportDefaultDeclaration'?path.parentPath:path;
let isSubClass = !!path.node.superClass;
let constructor;
let props = [];
let body = path.get("body");
let privateDecorator=null;
let instanceBody = [];
let methods=[];
const refs = {};
for (let subPath of body.get("body")) {
if (subPath.isClassMethod({ kind: "constructor" })) {
constructor = subPath;
} else if (subPath.isClassMethod() && (privateDecorator=findPrivateDecorator(subPath.node))!==false) {
const id=refs[subPath.node.key.name]||path.scope.generateUidIdentifier(subPath.node.key.name);
insertPath.insertBefore(t.variableDeclaration('var',[t.variableDeclarator(
id,t.newExpression(t.Identifier('WeakMap'),[])
)]
));
subPath.node.decorators.splice(privateDecorator, 1);
// move into constructor
refs[subPath.node.key.name]=id;
methods.push(subPath);
} else if (subPath.isClassProperty() && subPath.node.decorators && subPath.node.decorators.length > 0) {
let propNode = subPath.node;
const privateDecorator=findPrivateDecorator(propNode);
const id=refs[propNode.key.name]||path.scope.generateUidIdentifier(propNode.key.name);
if (privateDecorator===false) continue;
insertPath.insertBefore(
t.variableDeclaration(
'var'
, [
t.variableDeclarator(
id
, t.newExpression(t.Identifier('WeakMap'),[])
)
]
)
);
refs[propNode.key.name]=id;
if (propNode.value) {
instanceBody.push(
t.expressionStatement(
t.callExpression(
t.memberExpression(
id
, t.Identifier('set')
)
, [
t.thisExpression()
, propNode.value
]
)
)
);
}
subPath.remove();
}
}
if (instanceBody.length) {
if (!constructor) {
let newConstructor = t.classMethod("constructor", t.identifier("constructor"), [], t.blockStatement([]));
if (isSubClass) {
newConstructor.params = [t.restElement(t.identifier("args"))];
newConstructor.body.body.push(
t.returnStatement(
t.callExpression(
t.super()
, [t.spreadElement(t.identifier("args"))]
)
)
);
}
[constructor] = body.unshiftContainer("body", newConstructor);
}
for(let node of methods) {
node.traverse({
ThisExpression(path) {
handleRefItems(t,node,path,refs,t.thisExpression(),[])
}
});
instanceBody.push(
t.expressionStatement(
t.callExpression(
t.memberExpression(
refs[node.node.key.name]
, t.Identifier('set')
)
, [
t.thisExpression()
, t.callExpression(
t.memberExpression(
t.functionExpression(
t.Identifier('')
, node.node.params
, node.node.body
)
, t.Identifier('bind')
)
, [t.thisExpression()]
)
]
)
)
);
node.remove();
}
methods=[];
for (let node of body.get("body")) {
node.traverse({
ThisExpression(path) {
handleRefItems(t,node,path,refs,t.thisExpression(),[])
}
});
}
if (isSubClass) {
let Supers = [];
constructor.traverse({Super(path) {
if (path.parentPath.isCallExpression({ callee: path.node })) {
this.push(path.parentPath);
}
}}, Supers);
for (let Super of Supers) {
Super.insertAfter(instanceBody);
}
} else {
constructor.get("body").unshiftContainer("body", instanceBody);
}
}
}
});
}
}
}
}
function cleanJSON (...fields) {
var objs=[];
var fields = [
'loc',
'parent',
'hub',
'contexts',
'opts',
'parentPath',
'container',
'parentKey',
'parentBlock',
].concat(fields);
return function(k,v) {
if (fields.indexOf(k)>=0) return "[Object]";
if (objs.indexOf(v)>=0) return "[Circular Reference]";
if (typeof v === typeof({}) || typeof v === typeof([])) objs.push(v);
return v;
}
}
function handleRefItems(t,node,path,refs,refExp,known_vars) {
if (path.parentPath.type==='MemberExpression') {
if (!refs[path.parent.property.name]) return;
if (path.parentPath.parentPath.type=="AssignmentExpression") {
if (path.parentPath.parent.left.type=="MemberExpression") {
// console.log("Writing setter ",path.parentPath.parent.left.property.name)
const id=refs[path.parentPath.parent.left.property.name]||path.scope.generateUidIdentifier(path.parentPath.parent.left.property.name);
path.parentPath.parentPath.parentPath.replaceWith(t.expressionStatement(
t.callExpression(
t.memberExpression(
id
, t.Identifier('set')
)
, [
path.node
, path.parentPath.parent.right
]
)
));
} else {
// console.log("Writing getter ",path.parentPath.parent.left.property.name)
const id=refs[path.parent.property.name]||path.scope.generateUidIdentifier(path.parent.property.name);
path.parentPath.replaceWith(t.callExpression(
t.memberExpression(
id
, t.Identifier('get')
)
, [
refExp
]
));
}
} else {
// console.log("Writing getter ",path.parent.property.name)
const id=refs[path.parent.property.name]||path.scope.generateUidIdentifier(path.parent.property.name);
path.parentPath.replaceWith(t.callExpression(
t.memberExpression(
id
, t.Identifier('get')
)
, [
refExp
]
));
}
} else {
// TODO: Handle non "var" assignment. if "x=this" and x is not defined in a "var" then following it fails.
if (path.parent.type=="AssignmentExpression") {
if (path.scope.hasOwnBinding(path.parent.left.name) ) {
node.traverse({
ReferencedIdentifier(path) {
if (this.scope.hasOwnBinding(path.node.name) && known_vars.indexOf(path.node.name)==-1) {
known_vars.push(path.node.name);
handleRefItems(t,node,path,refs,path.node,known_vars);
}
}}
, {
scope: path.parentPath.scope
}
);
} else {
const id=path.parent.left;
console.log("Traversing",id);
node.traverse({
ReferencedIdentifier(p) {
console.log(JSON.stringify(p.parent,cleanJSON(),4));
if (path.node.name!==this.left.name && path.node.name!==this.right.name) return;
handleRefItems(t,node,p,refs,p.node,known_vars);
}}
, {
scope: path.scope
, left: path.parent.left
, right: path.parent.right
}
);
}
} else
if (path.parent.type=="VariableDeclarator") {
node.traverse({
ReferencedIdentifier(path) {
if (this.scope.hasOwnBinding(path.node.name)) {
// known_vars.push(path.node.name);
handleRefItems(t,node,path,refs,path.node,known_vars);
}
}}
, {
scope: path.parentPath.scope
}
);
}
}
}
function findPrivateDecorator(node) {
var idx=0;
return (node.decorators || []).some((decorator) => {
return decorator.expression.name === 'Private'||(++idx&&false);
})?idx:false;
}
function deleteDecorators(klass, decorators) {
decorators.forEach((decorator) => {
var index = klass.decorators.indexOf(decorator);
if (index >= 0) {
}
});
}