-
Notifications
You must be signed in to change notification settings - Fork 4
/
leaky.js
140 lines (114 loc) · 3.28 KB
/
leaky.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
var esprima = require('esprima');
var syntaxError = require('syntax-error');
// LeakError copied from https://github.com/substack/node-syntax-error
function LeakError (opts, src, file) {
Error.call(this);
this.message = 'global leak detected: ' + opts.variable;
this.line = opts.line - 1;
this.column = opts.column;
this.annotated = '\n'
+ (file || '(anonymous file)')
+ ':' + this.line
+ '\n'
+ src.split('\n')[this.line]
+ '\n'
+ Array(this.column + 1).join(' ') + '^'
+ '\n'
+ 'LeakError: ' + this.message
;
}
LeakError.prototype = new Error;
LeakError.prototype.toString = function () {
return this.annotated;
};
LeakError.prototype.inspect = function () {
return this.annotated;
};
function throwError(start, name, src, file) {
var opts = {
line: start.line
, column: start.column
, variable: name
};
throw new LeakError(opts, src, file);
}
function check(src, file) {
src = src.replace(/^#![^\n]*/, '');
src = '(function(){\n' + src + '\n})();';
var err = syntaxError(src, file);
if (err) {
return err;
}
var obj = esprima.parse(src, {loc: true});
var declared = [];
var globals = Object.keys(global).concat(['errno', 'exports', 'module']);
function getIds(node) {
return node.declarations.map(function(child) {
return child.id.name;
});
}
function collectDeclarations(node) {
var added = 0;
if (node.id && node.id.name) {
added++;
declared.push(node.id.name);
// deal with 'var a = function b() {}' where only 'a' should be defined as a variable
if (node.init && node.init.type === 'FunctionExpression') {
return added + collectDeclarations(node.init.body);
}
}
if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration') {
return 0;
}
Object.keys(node).forEach(function(key) {
var child = node[key];
if (Array.isArray(child) || child && child.type) {
added += collectDeclarations(child);
}
});
return added;
}
function collectParams(node) {
var params = node.params.map(function(param) {
return param.name;
})
declared = declared.concat(params);
return params.length;
}
function walk(node) {
var name;
if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'Program') {
var added = collectDeclarations(node.body);
if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration') {
added += collectParams(node);
}
walk(node.body);
declared.length = declared.length - added;
return;
}
if (node.type === 'AssignmentExpression' && node.left && node.left.name) {
name = node.left.name;
if (declared.indexOf(name) === -1 && globals.indexOf(name) === -1) {
throwError(node.loc.start, name, src, file);
}
}
Object.keys(node).forEach(function(key) {
var child = node[key];
if (Array.isArray(child)) {
child.forEach(walk);
} else if (child && typeof(child) === 'object' && child.type) {
walk(child);
}
});
}
try {
walk(obj);
} catch(e) {
if (e instanceof LeakError) {
return e;
}
throw e;
}
}
module.exports = check;
module.exports.LeakError = LeakError;