-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
64 lines (46 loc) · 1.26 KB
/
utils.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
'use strict';
var uglify = require('uglify-js'),
jsParser = uglify.parser,
jsUglify = uglify.uglify;
function parseJS(code, stripFunc) {
/* jshint -W106 */
var ast = jsParser.parse(code);
ast = jsUglify.ast_lift_variables(ast);
// Strip f() call.
if (stripFunc) {
stripFunc(ast);
}
return ast;
/* jshint +W106 */
}
function parseJSExpression(code) {
return parseJS(
'f(\n' + code + '\n)',
function(ast) { ast[1] = ast[1][0][1][2]; /* Strip f() call. */ }
);
}
function parseJSFunction(code) {
return parseJS(
'function f() {\n' + code + '\n}',
function(ast) { ast[1] = ast[1][0][3]; /* Strip function f() {} wrapper. */ }
);
}
function adjustJS(ast, beautify) {
/* jshint -W106 */
return jsUglify.gen_code(ast, {beautify: beautify, indent_start: 0});
/* jshint +W106 */
}
function hasReturn() {
return true;
}
function evalString(val) {
/* jshint -W040 */
return eval(val);
/* jshint +W040 */
}
module.exports.parseJS = parseJS;
module.exports.parseJSExpression = parseJSExpression;
module.exports.parseJSFunction = parseJSFunction;
module.exports.adjustJS = adjustJS;
module.exports.evalString = evalString;
module.exports.hasReturn = hasReturn;