-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixclosure.js
304 lines (275 loc) · 8.33 KB
/
fixclosure.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
var Token = org.mozilla.javascript.Token;
function registerIdentifier(node, scope) {
var current = node.getTarget();
while (current) {
switch (current.type) {
case Token.GETPROP:
if (current.getProperty().type == Token.NAME) {
current = current.getTarget();
} else {
return null;
}
break;
case Token.NAME:
if (current.getIdentifier() in scope) {
return null;
}
return String(node.toSource());
default:
return null;
}
}
}
/**
* @param {string} name
* @return {?string}
*/
function getPackageName(name) {
var name = replaceMethod(name);
var names = name.split('.');
var lastname = names[names.length - 1];
// Remove calling with apply or call.
if ('apply' === lastname || 'call' == lastname) {
names.pop();
lastname = names[names.length - 1];
}
// Remove prototype or superClass_.
names = names.reduceRight(function(prev, cur) {
if (cur === 'prototype' || cur === 'superClass_') {
return [];
} else {
prev.unshift(cur);
return prev;
}
}, []);
if (!isPackageMethod(name)) {
lastname = names[names.length - 1];
if (/^[a-z]/.test(lastname)) {
// Remove the last method name.
names.pop();
} else if (/^[A-Z][_0-9A-Z]*$/.test(lastname)) {
// Remove the last constant name.
names.pop();
}
}
if (names.length > 1) {
return names.join('.');
} else {
// Ignore just one word namespace like 'goog'.
return null;
}
}
/**
* @param {string} method Method name.
* @return {string}
*/
function replaceMethod(method) {
var replaceMap = {
'goog.disposeAll': 'goog.dispose',
//'goog.ui.KeyboardShortcutHandler.Modifiers': 'goog.ui.KeyboardShortcutHandler'
};
return replaceMap[method] || method;
}
/**
* @param {string} method Method name.
* @return {boolean}
*/
function isPackageMethod(method) {
return method === 'goog.dispose' ||
method === 'goog.ui.decorate';
}
function addPackageName(packages, node, scope) {
if (node && node.type == Token.GETPROP) {
var use = registerIdentifier(node, scope);
if (use) {
var p = getPackageName(use);
if (p) {
packages.add(p);
}
}
}
}
function addWritedName(packages, node) {
var args = node.getArguments();
if (args.size() != 1) {
throw new Error('invalid called ' + node.getTarget() + ': ' + node.toSource());
}
packages.add(args.get(0).getValue());
}
/**
* @constructor
*/
function Parsed() {
this.provided = java.util.TreeSet();
this.required = java.util.TreeSet();
this.toProvide = java.util.TreeSet();
this.toRequire = java.util.TreeSet();
}
Parsed.prototype.toResult = function() {
return {
provided: [String(e) for (e in Iterator(this.provided))],
required: [String(e) for (e in Iterator(this.required))],
toProvide: [String(e) for (e in Iterator(this.toProvide))],
toRequire: [String(e) for (e in Iterator(this.toRequire)) if (!this.toProvide.contains(e)) ]
};
};
function buildVisitor(rootNode, scope, parsed) {
return function(node) {
switch (node.type) {
// Syntax.AssignmentExpression
case Token.ASSIGN:
case Token.ASSIGN_BITOR:
case Token.ASSIGN_BITXOR:
case Token.ASSIGN_BITAND:
case Token.ASSIGN_LSH:
case Token.ASSIGN_RSH:
case Token.ASSIGN_URSH:
case Token.ASSIGN_ADD:
case Token.ASSIGN_SUB:
case Token.ASSIGN_MUL:
case Token.ASSIGN_DIV:
case Token.ASSIGN_MOD:
addPackageName(parsed.toProvide, node.getLeft(), scope);
addPackageName(parsed.toRequire, node.getRight(), scope);
break;
// Syntax.ArrayExpression
case Token.ARRAYLIT:
for (var e in Iterator(node.getElements())) {
addPackageName(parsed.toRequire, e, scope);
}
break;
// Syntax.CallExpression
// Syntax.NewExpression
case Token.CALL:
case Token.NEW:
addPackageName(parsed.toRequire, node.getTarget(), scope);
for (var arg in Iterator(node.getArguments())) {
addPackageName(parsed.toRequire, arg, scope);
}
if (node.getTarget().type == Token.GETPROP) {
if (node.getTarget().toSource() == 'goog.provide') {
addWritedName(parsed.provided, node);
} else if (node.getTarget().toSource() == 'goog.require') {
addWritedName(parsed.required, node);
}
}
break;
// Syntax.ConditionalExpression
case Token.HOOK:
addPackageName(parsed.toRequire, node.getTestExpression(), scope);
addPackageName(parsed.toRequire, node.getTrueExpression(), scope);
addPackageName(parsed.toRequire, node.getFalseExpression(), scope);
break;
case Token.EXPR_RESULT:
addPackageName(parsed.toProvide, node.getExpression(), scope);
break;
// Syntax.ExpressionStatement
// Syntax.SwitchCase
// Syntax.SwitchStatement
// Syntax.ThrowStatement
case Token.CASE:
case Token.SWITCH:
case Token.THROW:
addPackageName(parsed.toRequire, node.getExpression(), scope);
break;
// Syntax.ForInStatement
// Syntax.ForStatement
case Token.FOR:
if (node instanceof org.mozilla.javascript.ast.ForInLoop) {
addPackageName(parsed.toRequire, node.getIterator(), scope);
addPackageName(parsed.toRequire, node.getIteratedObject(), scope);
} else {
addPackageName(parsed.toRequire, node.getInitializer(), scope);
addPackageName(parsed.toRequire, node.getCondition(), scope);
addPackageName(parsed.toRequire, node.getIncrement(), scope);
}
break;
// Syntax.DoWhileStatement
// Syntax.IfStatement
// Syntax.WhileStatement
case Token.IF:
case Token.WHILE:
case Token.DO:
addPackageName(parsed.toRequire, node.getCondition(), scope);
break;
// Syntax.LogicalExpression
// Syntax.Property
case Token.COLON:
addPackageName(parsed.toRequire, node.getRight(), scope);
break;
// Syntax.ReturnStatement
case Token.RETURN:
addPackageName(parsed.toRequire, node.getReturnValue(), scope);
break;
// Syntax.BinaryExpression
// Syntax.SequenceExpression
// Syntax.MultiplicativeExpression
// Syntax.AdditiveExpression
case Token.ADD:
case Token.AND:
case Token.COMMA:
case Token.DIV:
case Token.GE:
case Token.GT:
case Token.IN:
case Token.INSTANCEOF:
case Token.LE:
case Token.LT:
case Token.MOD:
case Token.MUL:
case Token.OR:
case Token.SUB:
addPackageName(parsed.toRequire, node.getLeft(), scope);
addPackageName(parsed.toRequire, node.getRight(), scope);
break;
// Syntax.UnaryExpression
// Syntax.UpdateExpression
case Token.BITNOT:
case Token.DEC:
case Token.DELPROP:
case Token.INC:
case Token.NEG:
case Token.NOT:
case Token.POS:
case Token.TYPEOF:
case Token.VOID:
addPackageName(parsed.toRequire, node.getOperand(), scope);
break;
// Syntax.VariableDeclarator
case Token.CONST:
case Token.VAR:
if (node instanceof org.mozilla.javascript.ast.VariableInitializer) {
addPackageName(parsed.toRequire, node.getInitializer(), scope);
scope[node.getTarget().getIdentifier()] = 1;
}
break;
case Token.FUNCTION:
if (rootNode !== node) {
var newScope = Object.create(scope);
for (var p in Iterator(node.getParams())) {
newScope[p.getIdentifier()] = 1;
}
node.visit(buildVisitor(node, newScope, parsed));
return false;
}
break;
case Token.CATCH:
if (rootNode !== node) {
var newScope = Object.create(scope);
newScope[node.getVarName().getIdentifier()] = 1;
node.visit(buildVisitor(node, newScope, parsed));
return false;
}
break;
}
return true;
};
}
function parse(source) {
var parsed = new Parsed();
var scope = Object.create(null);
var ast = new org.mozilla.javascript.Parser().parse(source, '', 0);
ast.visit(buildVisitor(ast, scope, parsed));
return parsed.toResult();
}
exports.parse = parse;