@@ -4,12 +4,12 @@ module.exports = function (ast, vars) {
4
4
if ( ! vars ) vars = { } ;
5
5
var FAIL = { } ;
6
6
7
- var result = ( function walk ( node , scopeVars ) {
7
+ var result = ( function walk ( node , noExecute ) {
8
8
if ( node . type === 'Literal' ) {
9
9
return node . value ;
10
10
}
11
11
else if ( node . type === 'UnaryExpression' ) {
12
- var val = walk ( node . argument )
12
+ var val = walk ( node . argument , noExecute )
13
13
if ( node . operator === '+' ) return + val
14
14
if ( node . operator === '-' ) return - val
15
15
if ( node . operator === '~' ) return ~ val
@@ -19,7 +19,7 @@ module.exports = function (ast, vars) {
19
19
else if ( node . type === 'ArrayExpression' ) {
20
20
var xs = [ ] ;
21
21
for ( var i = 0 , l = node . elements . length ; i < l ; i ++ ) {
22
- var x = walk ( node . elements [ i ] ) ;
22
+ var x = walk ( node . elements [ i ] , noExecute ) ;
23
23
if ( x === FAIL ) return FAIL ;
24
24
xs . push ( x ) ;
25
25
}
@@ -31,7 +31,7 @@ module.exports = function (ast, vars) {
31
31
var prop = node . properties [ i ] ;
32
32
var value = prop . value === null
33
33
? prop . value
34
- : walk ( prop . value )
34
+ : walk ( prop . value , noExecute )
35
35
;
36
36
if ( value === FAIL ) return FAIL ;
37
37
obj [ prop . key . value || prop . key . name ] = value ;
@@ -59,9 +59,9 @@ module.exports = function (ast, vars) {
59
59
return r ;
60
60
}
61
61
62
- var l = walk ( node . left ) ;
62
+ var l = walk ( node . left , noExecute ) ;
63
63
if ( l === FAIL ) return FAIL ;
64
- var r = walk ( node . right ) ;
64
+ var r = walk ( node . right , noExecute ) ;
65
65
if ( r === FAIL ) return FAIL ;
66
66
67
67
if ( op === '==' ) return l == r ;
@@ -96,23 +96,29 @@ module.exports = function (ast, vars) {
96
96
else return FAIL ;
97
97
}
98
98
else if ( node . type === 'CallExpression' ) {
99
- var callee = walk ( node . callee ) ;
99
+ var callee = walk ( node . callee , noExecute ) ;
100
100
if ( callee === FAIL ) return FAIL ;
101
101
if ( typeof callee !== 'function' ) return FAIL ;
102
+
102
103
103
- var ctx = node . callee . object ? walk ( node . callee . object ) : FAIL ;
104
+ var ctx = node . callee . object ? walk ( node . callee . object , noExecute ) : FAIL ;
104
105
if ( ctx === FAIL ) ctx = null ;
105
106
106
107
var args = [ ] ;
107
108
for ( var i = 0 , l = node . arguments . length ; i < l ; i ++ ) {
108
- var x = walk ( node . arguments [ i ] ) ;
109
+ var x = walk ( node . arguments [ i ] , noExecute ) ;
109
110
if ( x === FAIL ) return FAIL ;
110
111
args . push ( x ) ;
111
112
}
113
+
114
+ if ( noExecute ) {
115
+ return undefined ;
116
+ }
117
+
112
118
return callee . apply ( ctx , args ) ;
113
119
}
114
120
else if ( node . type === 'MemberExpression' ) {
115
- var obj = walk ( node . object ) ;
121
+ var obj = walk ( node . object , noExecute ) ;
116
122
// do not allow access to methods on Function
117
123
if ( ( obj === FAIL ) || ( typeof obj == 'function' ) ) {
118
124
return FAIL ;
@@ -121,26 +127,25 @@ module.exports = function (ast, vars) {
121
127
if ( isUnsafeProperty ( node . property . name ) ) return FAIL ;
122
128
return obj [ node . property . name ] ;
123
129
}
124
- var prop = walk ( node . property ) ;
130
+ var prop = walk ( node . property , noExecute ) ;
125
131
if ( prop === null || prop === FAIL ) return FAIL ;
126
132
if ( isUnsafeProperty ( prop ) ) return FAIL ;
127
133
return obj [ prop ] ;
128
134
}
129
135
else if ( node . type === 'ConditionalExpression' ) {
130
- var val = walk ( node . test )
136
+ var val = walk ( node . test , noExecute )
131
137
if ( val === FAIL ) return FAIL ;
132
- return val ? walk ( node . consequent ) : walk ( node . alternate )
138
+ return val ? walk ( node . consequent ) : walk ( node . alternate , noExecute )
133
139
}
134
140
else if ( node . type === 'ExpressionStatement' ) {
135
- var val = walk ( node . expression )
141
+ var val = walk ( node . expression , noExecute )
136
142
if ( val === FAIL ) return FAIL ;
137
143
return val ;
138
144
}
139
145
else if ( node . type === 'ReturnStatement' ) {
140
- return walk ( node . argument )
146
+ return walk ( node . argument , noExecute )
141
147
}
142
148
else if ( node . type === 'FunctionExpression' ) {
143
-
144
149
var bodies = node . body . body ;
145
150
146
151
// Create a "scope" for our arguments
@@ -157,7 +162,7 @@ module.exports = function (ast, vars) {
157
162
else return FAIL ;
158
163
}
159
164
for ( var i in bodies ) {
160
- if ( walk ( bodies [ i ] ) === FAIL ) {
165
+ if ( walk ( bodies [ i ] , true ) === FAIL ) {
161
166
return FAIL ;
162
167
}
163
168
}
@@ -173,14 +178,14 @@ module.exports = function (ast, vars) {
173
178
else if ( node . type === 'TemplateLiteral' ) {
174
179
var str = '' ;
175
180
for ( var i = 0 ; i < node . expressions . length ; i ++ ) {
176
- str += walk ( node . quasis [ i ] ) ;
177
- str += walk ( node . expressions [ i ] ) ;
181
+ str += walk ( node . quasis [ i ] , noExecute ) ;
182
+ str += walk ( node . expressions [ i ] , noExecute ) ;
178
183
}
179
- str += walk ( node . quasis [ i ] ) ;
184
+ str += walk ( node . quasis [ i ] , noExecute ) ;
180
185
return str ;
181
186
}
182
187
else if ( node . type === 'TaggedTemplateExpression' ) {
183
- var tag = walk ( node . tag ) ;
188
+ var tag = walk ( node . tag , noExecute ) ;
184
189
var quasi = node . quasi ;
185
190
var strings = quasi . quasis . map ( walk ) ;
186
191
var values = quasi . expressions . map ( walk ) ;
0 commit comments