Skip to content
This repository was archived by the owner on Dec 4, 2021. It is now read-only.

Commit c598e8c

Browse files
Caleb HooverCaleb Hoover
Caleb Hoover
authored and
Caleb Hoover
committed
added prettyjson, fixed some bugs
1 parent c6d87f8 commit c598e8c

File tree

5 files changed

+53
-45
lines changed

5 files changed

+53
-45
lines changed

bin/check

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var sources = module.exports.sources = require('../danger.json').sources;
4242

4343
check.flags.verbose = argv.v;
4444
check.flags.recursive = argv.r;
45+
check.flags.json = argv.j;
4546

4647
if (!argv.j)
4748
console.log(' ---- '.yellow, file.white);
@@ -80,6 +81,8 @@ if (argv.j) {
8081
});
8182
}
8283
break;
84+
case 'SASSIGN':
85+
8386
case 'SINK':
8487
source = _.find(this.reports, function(i) {return value.indexOf(i.source.name) === 0;});
8588
if (source)
@@ -96,6 +99,8 @@ if (argv.j) {
9699
}
97100
};
98101

102+
scope.createNewScope = function () {};
103+
99104
scope.leaveScope = function() {
100105
var r = _.filter(this.reports, function(i) {return !!i.sink;});
101106
// if (r.length !== 0)
@@ -107,4 +112,4 @@ if (argv.j) {
107112
var ast = check.astFromFile(scope.file, argv.o);
108113
check.traverse(ast, scope);
109114

110-
console.log(JSON.stringify(reports));
115+
console.log(require('prettyjson').render(reports));

check.js

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
/*
22
;(function() {eval(String(require('fs').readFileSync(process.argv[1])));})()
3+
4+
Terms:
5+
BE - Binary Expression
6+
CE - Call Expression (functions)
7+
SCE - (source) Call Expression
8+
SCES - (source) Call Expression Statement
9+
SASSIGN - Assign as a sink
10+
311
*/
412

513
var fs = require('fs'),
@@ -23,6 +31,7 @@ var cs = {
2331
'SCE': colors.red,
2432
'SCES': colors.red,
2533
'SINK': colors.red,
34+
'SASSIGN': colors.red,
2635
'SOURCE': colors.red,
2736
'SOURCES': colors.yellow,
2837
'RETURN': colors.red
@@ -33,7 +42,7 @@ function log(type, node, name, value) {
3342
if (flags.recursive)
3443
p = path.relative(baseFile.split('/').reverse().slice(1).reverse().join('/'), this.file) + ':' + p;
3544

36-
console.log(cs[type]?cs[type]('[' + type + ']'):colors.blue('[' + type + ']'),
45+
console.log(' ', cs[type]?cs[type]('[' + type + ']'):colors.blue('[' + type + ']'),
3746
colors.grey(p), name, value ? value : '');
3847
}
3948

@@ -70,7 +79,6 @@ function(scope, node, ce) { // http.get
7079
func.scope.sources.push(func.params[0]);
7180
func.scope.log('SOURCE', node, false, func.params[0]);
7281
traverse(func.body, func.scope);
73-
7482
}
7583
}
7684

@@ -116,7 +124,6 @@ function(scope, node, ce) { // http.get
116124
if (!flags.recursive)
117125
return false;
118126

119-
120127
var r;
121128
if (ce.arguments[0]) {
122129
var file;
@@ -140,8 +147,9 @@ function(scope, node, ce) { // http.get
140147

141148
var ast = astFromFile(pkg);
142149
if (ast) {
143-
if (flags.verbose)
150+
if (flags.verbose && !flags.json)
144151
console.log(' ---- '.yellow, pkg);
152+
145153
var newScope = new Scope({
146154
sinks: sinks,
147155
sources: sources,
@@ -169,15 +177,14 @@ Scope = module.exports.Scope = function(scope) {
169177
this.vars = scope.vars || {};
170178
if (!this.vars.module) this.vars.module = {exports: {}};
171179
if (!this.vars.global) this.vars.global = {};
172-
if (!this.vars.process) this.vars.process = {};
173180
this.sources = scope.sources||sources;
174181
this.sinks = scope.sinks||sinks;
175182
this.file = scope.file;
176183
if (!baseFile) baseFile = scope.file;
177184
this.log = scope.log || log;
178185
this.leaveScope = scope.leaveScope;
179186
this.createNewScope = scope.createNewScope;
180-
this.reports = [];
187+
this.reports = scope.reports || [{source: {name: 'process.argv'}}];
181188
};
182189

183190
// handles creation of variables.
@@ -206,24 +213,18 @@ Scope.prototype.track = function(variable) {
206213

207214
// returns a value for a variable if one exists
208215
Scope.prototype.resolve = function(name) {
209-
if (!name)
210-
return false;
211-
else if (typeof name != 'string')
216+
if (!name || typeof name != 'string')
212217
return false;
218+
219+
if (get(this.vars, name)) {
220+
return eval('this.vars.' + name);
221+
}
222+
else if (name.indexOf('.') != -1) {
223+
var s = name.split('.');
224+
var r = this.resolve(s.slice(0,-1).join('.'));
225+
r = r.raw || r;
226+
return r + '.' + s.slice(-1);
213227

214-
if (name.indexOf('.') == -1) {
215-
if (get(this.vars, name)) {
216-
return eval('this.vars.' + name);
217-
}
218-
} else {
219-
if (get(this.vars, name)) {
220-
return eval('this.vars.' + name);
221-
} else {
222-
var s = name.split('.');
223-
var r = this.resolve(s.slice(0,-1).join('.'));
224-
r = r.raw || r;
225-
return r + '.' + s.slice(-1);
226-
}
227228
}
228229

229230
return name;
@@ -266,7 +267,6 @@ Scope.prototype.resolveStatement = function(node) {
266267
return false;
267268
var resolved = scope.resolve(arg);
268269
var source = resolved;
269-
270270
if (scope.isSource(arg.name || arg) || scope.isSource(resolved.name || resolved) ||
271271
(traverseJSON(arg, function (a) {
272272
if (!a) return false;
@@ -298,7 +298,10 @@ Scope.prototype.resolveStatement = function(node) {
298298
if (value) {
299299
var resolved = scope.resolve(value);
300300
if (resolved && typeof resolved == 'string') {
301-
if (scope.isSource(resolved.name || resolved) || scope.isSource(value.name || value) || isSource) {
301+
if (scope.isSink(value.name || value) || scope.isSink(resolved.name || resolved)) {
302+
scope.sinks.push(names);
303+
scope.log('SASSIGN', node, names.length==1?names[0]:names, value);
304+
} else if (scope.isSource(resolved.name || resolved) || scope.isSource(value.name || value)) {
302305
scope.sources.push(names);
303306
scope.log('SOURCE', node, names.length==1?names[0]:names, value);
304307
}
@@ -581,7 +584,8 @@ Scope.prototype.traverse = function(ast, returnCB) {
581584
var scope = this;
582585
if (flags.verbose) {
583586
(scope.createNewScope || function() {
584-
console.log('Creating new scope'.yellow);
587+
if (!flags.json)
588+
console.log('Creating new scope'.yellow);
585589
})();
586590
scope.log('SOURCES', ast, scope.sources);
587591
}
@@ -600,12 +604,12 @@ Scope.prototype.traverse = function(ast, returnCB) {
600604
this.resolveStatement(ast.expression || ast);
601605
}
602606

603-
// if (flags.verbose)
604-
// (scope.leaveScope || function () {
605-
// console.log('leaving scope'.yellow);
606-
// })();
607-
if (scope.leaveScope)
608-
scope.leaveScope();
607+
if (flags.verbose)
608+
(scope.leaveScope || function () {
609+
console.log('leaving scope'.yellow);
610+
})();
611+
// if (scope.leaveScope)
612+
// scope.leaveScope();
609613
};
610614

611615
Scope.prototype.resolvePath = function(file, cb) {
@@ -666,7 +670,8 @@ traverse = module.exports.traverse = function(ast, scope) {
666670
}
667671
if (flags.verbose) {
668672
(scope.createNewScope || function() {
669-
console.log('Creating new scope'.yellow);
673+
if (!flags.json)
674+
console.log('Creating new scope'.yellow);
670675
})();
671676
scope.log('SOURCES', ast, scope.sources);
672677
}
@@ -677,13 +682,13 @@ traverse = module.exports.traverse = function(ast, scope) {
677682
scope.resolveStatement(node);
678683
});
679684

680-
// if (flags.verbose) {
681-
// (scope.leaveScope || function () {
682-
// console.log('leaving scope'.yellow);
683-
// })();
684-
// }
685-
if (scope.leaveScope)
686-
scope.leaveScope();
685+
if (flags.verbose) {
686+
(scope.leaveScope || function () {
687+
console.log('leaving scope'.yellow);
688+
})();
689+
}
690+
// if (scope.leaveScope)
691+
// scope.leaveScope();
687692
};
688693

689694
astFromFile = module.exports.astFromFile = function(file, output) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"underscore": "~1.7.0",
1414
"resolve": "*",
1515
"yargs": "~1.3.3",
16-
"estraverse": "~1.9.1"
16+
"estraverse": "~1.9.1",
17+
"prettyjson": "~1.1.0"
1718
},
1819
"devDependencies": {},
1920
"repository": {

tests/lib/b.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
// var c = require('./c.js'),
2-
// d = require('./d.js');
3-
// module.exports.name = c.name + d.name;
41
module.exports.e = eval;

tests/require.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
var a = require('./lib/a.js');
22
console.log(a.b);
3-
a.b.e(process.argv[2]);
3+
a.b.e(process.argv[2]); //a.b.e == eval

0 commit comments

Comments
 (0)