This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
truncate.js
411 lines (390 loc) · 10.6 KB
/
truncate.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*******************************************************************************
* Copyright (c) 2013 Max Schaefer.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Max Schaefer - initial API and implementation
*******************************************************************************/
/*global require process console*/
var esprima = require('esprima'),
escodegen = require('escodegen'),
estraverse = require('estraverse'),
fs = require('fs'),
path = require('path'),
htmlparser = require('htmlparser'),
url = require('url');
var frameworks_regexp = /^(jquery|mootools|prototype)/i,
usercode_regexp = null;
function usage() {
console.log("Usage: node truncate.js [--framework REGEXP] [--usercode REGEXP] FILE OUTDIR");
process.exit(-1);
}
function isFramework(file) {
if(usercode_regexp) {
return !usercode_regexp.test(file);
} else {
return frameworks_regexp.test(file);
}
}
function parse(src) {
return esprima.parse(src, {
range: true,
loc: true
});
}
function visit(root, emit) {
function process(nd, parent, idx) {
if (Object(nd) != nd) {
return;
}
nd.parent = parent;
nd.child_idx = idx;
if (Array.isArray(nd)) {
for (var i = 0, n = nd.length; i < n; ++i) {
process(nd[i], nd, i);
}
return;
}
estraverse.VisitorKeys[nd.type].forEach(function(ch) {
if ((nd.type === 'AssignmentExpression' || nd.type === 'ForInStatement') && ch === 'left') {
nd[ch].lhs = true;
}
process(nd[ch], nd, ch);
});
if (nd.type === 'MemberExpression' && !nd.computed && !nd.lhs) {
var prop_name = nd.property.name;
nd.property.name = '$$' + nd.property.name;
var undo = truncate(parent, idx);
emit(root, nd.loc.start.line, nd.range[0], nd.range[1]);
undo();
nd.property.name = prop_name;
}
}
function truncate(nd, idx) {
var undo, body;
if (Array.isArray(nd)) {
var removed = nd.splice(idx + 1, nd.length - idx);
undo = truncate(nd.parent, nd.child_idx);
return function() {
undo();
for (var i = 0, n = removed.length; i < n; i++) {
nd[nd.length] = removed[i];
}
};
} else {
switch (nd.type) {
case 'FunctionDeclaration':
case 'FunctionExpression':
case 'Program':
return function() {};
case 'AssignmentExpression':
case 'BinaryExpression':
case 'LogicalExpression':
undo = truncate(nd.parent, nd.child_idx);
if (idx === 'left') {
nd.parent[nd.child_idx] = nd.left;
return function() {
undo();
nd.parent[nd.child_idx] = nd;
};
} else {
return undo;
}
break;
case 'ArrayExpression':
case 'BlockStatement':
case 'BreakStatement':
case 'CatchClause':
case 'ContinueStatement':
case 'DebuggerStatement':
case 'DirectiveStatement':
case 'EmptyStatement':
case 'ExpressionStatement':
case 'Identifier':
case 'Literal':
case 'LabeledStatement':
case 'ObjectExpression':
case 'Property':
case 'ReturnStatement':
case 'SequenceExpression':
case 'SwitchStatement':
case 'SwitchCase':
case 'ThisExpression':
case 'ThrowStatement':
case 'UnaryExpression':
case 'UpdateExpression':
case 'VariableDeclaration':
case 'VariableDeclarator':
case 'YieldExpression':
return truncate(nd.parent, nd.child_idx);
case 'CallExpression':
case 'NewExpression':
undo = truncate(nd.parent, nd.child_idx);
if (idx === 'callee') {
nd.parent[nd.child_idx] = nd.callee;
return function() {
undo();
nd.parent[nd.child_idx] = nd;
};
} else {
return undo;
}
break;
case 'ConditionalExpression':
case 'IfStatement':
undo = truncate(nd.parent, nd.child_idx);
if (idx === 'test') {
nd.parent[nd.child_idx] = (nd.type === 'ConditionalExpression' ? nd.test : {
type: 'ExpressionStatement',
expression: nd.test
});
return function() {
undo();
nd.parent[nd.child_idx] = nd;
};
} else if (idx === 'consequent' && nd.type === 'IfStatement') {
var alternate = nd.alternate;
nd.alternate = null;
return function() {
undo();
nd.alternate = alternate;
};
} else {
return undo;
}
break;
case 'DoWhileStatement':
undo = truncate(nd.parent, nd.child_idx);
if (idx === 'body') {
nd.parent[nd.child_idx] = nd.body;
return function() {
undo();
nd.parent[nd.child_idx] = nd;
};
} else {
return undo;
}
break;
case 'ForStatement':
undo = truncate(nd.parent, nd.child_idx);
if (idx === 'init') {
nd.parent[nd.child_idx] = (nd.init.type === 'VariableDeclaration' ? nd.init : {
type: 'ExpressionStatement',
expression: nd.init
});
return function() {
undo();
nd.parent[nd.child_idx] = nd;
};
} else if (idx === 'test') {
var update = nd.update;
body = nd.body;
nd.update = null;
nd.body = {
type: 'EmptyStatement'
};
return function() {
undo();
nd.update = update;
nd.body = body;
};
} else if (idx === 'update') {
body = nd.body;
nd.body = {
type: 'EmptyStatement'
};
return function() {
undo();
nd.body = body;
};
} else {
return undo;
}
break;
case 'ForInStatement':
undo = truncate(nd.parent, nd.child_idx);
if (idx === 'right') {
nd.parent[nd.child_idx] = {
type: 'ExpressionStatement',
expression: nd.right
};
return function() {
undo();
nd.parent[nd.child_idx] = nd;
};
} else {
return undo;
}
break;
case 'MemberExpression':
undo = truncate(nd.parent, nd.child_idx);
if (idx === 'object') {
nd.parent[nd.child_idx] = nd.object;
return function() {
undo();
nd.parent[nd.child_idx] = nd;
};
} else {
return undo;
}
break;
case 'TryStatement':
undo = truncate(nd.parent, nd.child_idx);
if (idx === 'block') {
nd.parent[nd.child_idx] = nd.block;
return function() {
undo();
nd.parent[nd.child_idx] = nd;
};
} else {
return undo;
}
break;
case 'WhileStatement':
undo = truncate(nd.parent, nd.child_idx);
if (idx === 'test') {
nd.parent[nd.child_idx] = {
type: 'ExpressionStatement',
expression: nd.test
};
return function() {
undo();
nd.parent[nd.child_idx] = nd;
};
} else {
return undo;
}
break;
case 'WithStatement':
undo = truncate(nd.parent, nd.child_idx);
if (idx === 'object') {
nd.parent[nd.child_idx] = {
type: 'ExpressionStatement',
expression: nd.object
};
return function() {
undo();
nd.parent[nd.child_idx] = nd;
};
} else {
return undo;
}
break;
default:
throw new Error(nd.type);
}
}
}
process(root, null, - 1);
}
var inlinecnt = 0;
function walkDOM(node, baseurl, scripts) {
if (node.type === 'script') {
if (!node.attribs || !node.attribs.type || node.attribs.type.match(/JavaScript/i)) {
var script_code, script_path;
if (node.attribs && node.attribs.src) {
script_path = url.parse(url.resolve(baseurl, node.attribs.src.trim())).pathname;
script_code = fs.readFileSync(script_path, 'utf-8');
} else {
script_path = baseurl + "inline_" + (++inlinecnt) + ".js";
script_code = node.children[0].raw;
}
scripts.push({
path: script_path,
source: script_code
});
}
} else if (node.type === 'tag') {
if (node.children) node.children.forEach(function(ch) {
walkDOM(ch, baseurl, scripts);
});
} else if (Array.isArray(node)) {
node.forEach(function(ch) {
walkDOM(ch, baseurl, scripts);
});
}
}
function extract_js(file) {
var handler = new htmlparser.DefaultHandler();
var HTMLparser = new htmlparser.Parser(handler);
var html = fs.readFileSync(file, 'utf-8');
var scripts = [];
HTMLparser.parseComplete(html);
walkDOM(handler.dom, path.dirname(file) + '/', scripts);
return scripts;
}
var i=2, nargs = process.argv.length;
while((/^--/).test(process.argv[i])) {
var opt = process.argv[i++];
if(opt.length == 2) {
break;
} else if(i+1<nargs && opt === '--frameworks') {
frameworks_regexp = new RegExp(process.argv[i++]);
} else if(i+1<nargs && opt === '--usercode') {
usercode_regexp = new RegExp(process.argv[i++]);
} else {
usage();
}
}
if(i+1>=process.argv.length) {
usage();
}
var files = process.argv.slice(i, nargs-1),
outdir = process.argv[nargs-1];
if ((/\.html?$/i).test(files[0])) {
var file = files[0],
scripts = extract_js(file),
outer_cnt = 0;
scripts.forEach(function(script, i) {
var basename = path.basename(script.path, ".js"),
source = script.source;
fs.writeFileSync(outdir + '/' + basename + ".js", source);
if (!isFramework(basename)) {
var ast = parse(source),
cnt = 0;
visit(ast, function(trunc_ast, start_line, start_offset, end_offset) {
var trunc_name = outdir + '/' + basename + '_truncated_' + (cnt++) + '.js';
fs.writeFileSync(trunc_name, escodegen.generate(trunc_ast));
var html = "<!-- " + basename + ".js@" + start_line + ":" + start_offset + "-" + end_offset + " -->\n" + "<html>\n<head>\n<title></title>\n";
scripts.forEach(function(script, j) {
if (i === j) {
html += "<script src='" + path.basename(trunc_name) + "'></script>\n";
} else {
html += "<script src='" + path.basename(script.path) + "'></script>\n";
}
});
html += "</head>\n<body></body>\n</html>\n";
fs.writeFileSync(outdir + '/' + path.basename(file, '.html') + '_truncated_' + (outer_cnt++) + '.html',
html);
});
}
});
} else {
files.forEach(function(file, i) {
if (!/\.js$/.test(file)) {
console.warn("Ignoring non-JavaScript file " + file);
} else {
var src = fs.readFileSync(file, 'utf-8'),
ast = parse(src),
cnt = 0,
basename = path.basename(file, '.js');
if(!isFramework(basename)) {
visit(ast, function(trunc_ast, start_line, start_offset, end_offset) {
var this_outdir = outdir + '/' + basename + '_truncated_' + (cnt++),
src = '// ' + file + '@' + start_line + ':' + start_offset + '-' + end_offset + '\n' + escodegen.generate(trunc_ast);
fs.mkdirSync(this_outdir);
files.forEach(function(file, j) {
if(i === j) {
fs.writeFileSync(this_outdir + '/' + path.basename(file), src);
} else {
fs.writeFileSync(this_outdir + '/' + path.basename(file), fs.readFileSync(file));
}
});
});
}
}
});
}