@@ -76,17 +76,38 @@ function(scope, node, ce) { // http.get
76
76
77
77
return true ;
78
78
79
+ } , function ( scope , node , ce ) { // (new require('express').Router()).route() && .post()
80
+ var ceName = scope . resolve ( ce . name ) ;
81
+ if ( typeof ceName != "string" || ceName . indexOf ( 'express' ) == - 1 )
82
+ return false ;
83
+ if ( [ 'post' , 'get' ] . indexOf ( ceName . split ( '.' ) . slice ( - 1 ) [ 0 ] != 'post' ) == - 1 )
84
+ return false ;
85
+
86
+ if ( ce . arguments && ce . arguments [ 1 ] ) {
87
+ var func = ce . arguments [ 1 ] ;
88
+
89
+ if ( func && func . scope ) {
90
+ func . scope . sources . push ( func . params [ 0 ] ) ;
91
+ func . scope . log ( 'SOURCE' , node , func . params [ 0 ] ) ;
92
+ traverse ( func . body , func . scope ) ;
93
+
94
+ }
95
+ }
96
+
97
+ return true ;
98
+
79
99
} , function ( scope , node , ce ) { // (new require('hapi').server()).route()
80
100
var ceName = scope . resolve ( ce . name ) ;
81
101
if ( ceName != 'require(\'fs\').readFile' ) {
82
102
return false ;
83
103
}
84
104
85
105
var func = ce . arguments [ 2 ] ; // the callback
86
-
87
- func . scope . sources . push ( func . params [ 1 ] ) ; // data
88
- func . scope . log ( 'SOURCE' , node , func . params [ 1 ] ) ;
89
- traverse ( func . body , func . scope ) ;
106
+ if ( func ) {
107
+ func . scope . sources . push ( func . params [ 1 ] ) ; // data
108
+ func . scope . log ( 'SOURCE' , node , func . params [ 1 ] ) ;
109
+ traverse ( func . body , func . scope ) ;
110
+ }
90
111
return true ;
91
112
} , function ( scope , node , ce ) { // require
92
113
if ( ce . name != 'require' )
@@ -105,8 +126,8 @@ function(scope, node, ce) { // http.get
105
126
return ;
106
127
}
107
128
108
- if ( file == 'hapi' || file . indexOf ( 'hapi' ) != - 1 ) // just ignore anything hapi
109
- return ;
129
+ if ( [ 'hapi' , 'express' , 'jade' ] . indexOf ( file ) != - 1 || file . indexOf ( 'hapi' ) != - 1 )
130
+ return ; // just ignore these things
110
131
111
132
scope . resolvePath ( file , function ( pkg ) {
112
133
if ( ! pkg )
@@ -239,7 +260,11 @@ Scope.prototype.resolveStatement = function(node) {
239
260
return false ;
240
261
var resolved = scope . resolve ( arg ) ;
241
262
242
- if ( scope . isSource ( arg . name || arg ) || scope . isSource ( resolved . name || resolved ) ) {
263
+ if ( scope . isSource ( arg . name || arg ) || scope . isSource ( resolved . name || resolved ) ||
264
+ arg . left ?_ . some ( climbBE ( arg ) , function ( a ) {
265
+ var r = scope . resolve ( a ) ;
266
+ return scope . isSource ( a . name || a ) || scope . isSource ( r . name || r ) ; } ) :false ) {
267
+
243
268
if ( scope . isSink ( ceName ) ) {
244
269
scope . log ( 'SINK' , node , ce . raw , ceName ) ;
245
270
return true ;
@@ -251,8 +276,8 @@ Scope.prototype.resolveStatement = function(node) {
251
276
return false ;
252
277
} ) ;
253
278
254
- if ( flags . verbose || t [ 0 ] == 'S' )
255
- this . log ( t , node , ce . raw , ceName ) ;
279
+ if ( ( flags . verbose || t [ 0 ] == 'S' ) && typeof ceName == 'string ')
280
+ this . log ( t , node , ce . raw , typeof ceName == 'string' ? ceName : { } ) ;
256
281
257
282
return ce ;
258
283
case 'AssignmentExpression' :
@@ -381,7 +406,10 @@ Scope.prototype.resolveExpression = function(right, isSourceCB) {
381
406
return false ;
382
407
var resolved = scope . resolve ( arg ) ;
383
408
384
- if ( scope . isSource ( arg . name || arg ) || scope . isSource ( resolved . name || resolved ) ) {
409
+ if ( scope . isSource ( arg . name || arg ) || scope . isSource ( resolved . name || resolved ) ||
410
+ arg . left ?_ . some ( climbBE ( arg ) , function ( a ) {
411
+ var r = scope . resolve ( a ) ;
412
+ return scope . isSource ( a . name || a ) || scope . isSource ( r . name || r ) ; } ) :false ) {
385
413
386
414
if ( scope . isSink ( ceName ) ) {
387
415
scope . log ( 'SINK' , right , ce . raw , ceName ) ;
@@ -400,7 +428,7 @@ Scope.prototype.resolveExpression = function(right, isSourceCB) {
400
428
}
401
429
402
430
if ( flags . verbose || t [ 0 ] == 'S' )
403
- this . log ( t , right , ce . raw , ceName ) ;
431
+ this . log ( t , right , ce . raw , typeof ceName == 'string' ? ceName : { } ) ;
404
432
405
433
return ce ;
406
434
case 'MemberExpression' : // a.b.c.d
@@ -478,7 +506,7 @@ Scope.prototype.resolveCallExpression = function(node) {
478
506
479
507
Scope . prototype . resolveForStatement = function ( node ) {
480
508
var fs = { } ;
481
- /* in ECMAScript 5 for statements do not create their own scope,
509
+ /* in ECMAScript 5, for statements do not create their own scope,
482
510
* so create a variable, then track it in current scope */
483
511
if ( node . init && node . init . declarations )
484
512
for ( var i = 0 ; i < node . init . declarations . length ; i ++ ) {
@@ -567,7 +595,6 @@ Scope.prototype.traverse = function(ast, returnCB) {
567
595
scope . log ( 'SOURCES' , ast , scope . sources ) ;
568
596
}
569
597
570
-
571
598
if ( ast . type == 'BlockStatement' ) {
572
599
( ast . body || [ ast ] ) . forEach ( function ( node ) {
573
600
if ( node . type == 'ExpressionStatement' )
@@ -592,10 +619,6 @@ Scope.prototype.resolvePath = function(file, cb) {
592
619
var pkg ;
593
620
if ( file . indexOf ( './' ) === 0 || file . indexOf ( '../' ) === 0 ) {
594
621
if ( path . extname ( file ) == '.json' ) {
595
- // input = JSON.parse(input);
596
- // if (Array.isArray(input)) {
597
- // input.forEach(cb);
598
- // }
599
622
return false ;
600
623
}
601
624
}
@@ -631,9 +654,8 @@ Scope.prototype.isSource = function(name) {
631
654
Scope . prototype . isSink = function ( name ) {
632
655
if ( typeof name != 'string' )
633
656
return false ;
634
- // console.log(name);
657
+
635
658
for ( var i in this . sinks ) {
636
- // console.log('\t', this.sinks[i], name.search(this.sinks[i]));
637
659
if ( name . search ( this . sinks [ i ] ) === 0 ) {
638
660
return true ;
639
661
}
@@ -694,16 +716,13 @@ climb = module.exports.climb = function(ast) {
694
716
}
695
717
} ;
696
718
719
+ climbBE = module . exports . climbBE = function ( be , func ) {
720
+ if ( ! be . left )
721
+ return be ;
722
+ return [ be . left . left ?climbBE ( be . left ) :be . left , be . right . left ?climbBE ( be . right ) :be . right ] ;
723
+ } ;
724
+
697
725
// Convience function to return the line of a node assuming a node has one.
698
726
module . exports . pos = pos = function ( node ) {
699
727
return node . loc ? String ( node . loc . start . line ) : "-1" ;
700
- } ;
701
-
702
- // function get(json, key) {
703
- // keys = key.split('.');
704
- // if (keys.length == 1)
705
- // return json[key];
706
- // else {
707
- // return get(json[keys[0]], keys.slice(1));
708
- // }
709
- // }
728
+ } ;
0 commit comments