@@ -160,33 +160,33 @@ module.exports = {
160
160
}
161
161
} ) ;
162
162
163
+ function _getPathFor ( node , parent , prop , index ) {
164
+ const parentPath = NodePath . getForNode ( parent ) ;
165
+ const nodePath = NodePath . getForNode (
166
+ node ,
167
+ parentPath ,
168
+ prop ,
169
+ index
170
+ ) ;
171
+
172
+ return nodePath ;
173
+ }
174
+
163
175
// Handle actual nodes.
164
176
astTraverse ( ast , {
165
-
166
177
/**
167
178
* Handler on node enter.
168
179
*/
169
180
pre ( node , parent , prop , index ) {
170
- let parentPath ;
171
181
let nodePath ;
172
-
173
182
if ( ! options . asNodes ) {
174
- parentPath = NodePath . getForNode ( parent ) ;
175
-
176
- nodePath = NodePath . getForNode (
177
- node ,
178
- parentPath ,
179
- prop ,
180
- index
181
- ) ;
183
+ nodePath = _getPathFor ( node , parent , prop , index ) ;
182
184
}
183
185
184
186
for ( const handler of handlers ) {
185
187
// "Catch-all" `*` handler.
186
188
if ( typeof handler [ '*' ] === 'function' ) {
187
- if ( options . asNodes ) {
188
- handler [ '*' ] ( node , parent , prop , index ) ;
189
- } else {
189
+ if ( nodePath ) {
190
190
// A path/node can be removed by some previous handler.
191
191
if ( ! nodePath . isRemoved ( ) ) {
192
192
const handlerResult = handler [ '*' ] ( nodePath ) ;
@@ -196,32 +196,34 @@ module.exports = {
196
196
}
197
197
}
198
198
}
199
+ else {
200
+ handler [ '*' ] ( node , parent , prop , index ) ;
201
+ }
199
202
}
200
203
201
204
// Per-node handler.
202
- let handlerFunc_pre = null ;
203
- if ( handler [ node . type ] &&
204
- typeof handler [ node . type ] === 'function' ) {
205
- handlerFunc_pre = handler [ node . type ] . bind ( handler ) ;
206
- }
207
- else if ( handler [ node . type ] &&
208
- typeof handler [ node . type ] === 'object' &&
209
- typeof handler [ node . type ] . pre === 'function' ) {
210
- handlerFunc_pre = handler [ node . type ] . pre . bind ( handler ) ;
205
+ let handlerFuncPre ;
206
+ if ( typeof handler [ node . type ] === 'function' ) {
207
+ handlerFuncPre = handler [ node . type ] ;
208
+ } else if (
209
+ typeof handler [ node . type ] === 'object' &&
210
+ typeof handler [ node . type ] . pre === 'function'
211
+ ) {
212
+ handlerFuncPre = handler [ node . type ] . pre ;
211
213
}
212
214
213
- if ( handlerFunc_pre ) {
214
- if ( options . asNodes ) {
215
- handlerFunc_pre ( node , parent , prop , index ) ;
216
- } else {
215
+ if ( handlerFuncPre ) {
216
+ if ( nodePath ) {
217
217
// A path/node can be removed by some previous handler.
218
218
if ( ! nodePath . isRemoved ( ) ) {
219
- const handlerResult = handlerFunc_pre ( nodePath ) ;
219
+ const handlerResult = handlerFuncPre . call ( handler , nodePath ) ;
220
220
// Explicitly stop traversal.
221
221
if ( handlerResult === false ) {
222
222
return false ;
223
223
}
224
224
}
225
+ } else {
226
+ handlerFuncPre . call ( handler , node , parent , prop , index ) ;
225
227
}
226
228
}
227
229
} // Loop over handlers
@@ -232,45 +234,37 @@ module.exports = {
232
234
* Handler on node exit.
233
235
*/
234
236
post ( node , parent , prop , index ) {
235
- if ( ! node ) { // No node? Not sure how this can happen, but without it many tests fail.
237
+ if ( ! node ) {
236
238
return ;
237
239
}
238
240
239
- let parentPath ;
240
241
let nodePath ;
241
-
242
242
if ( ! options . asNodes ) {
243
- parentPath = NodePath . getForNode ( parent ) ;
244
-
245
- nodePath = NodePath . getForNode (
246
- node ,
247
- parentPath ,
248
- prop ,
249
- index
250
- ) ;
243
+ nodePath = _getPathFor ( node , parent , prop , index ) ;
251
244
}
252
245
253
246
for ( const handler of handlers ) {
254
247
// Per-node handler.
255
- let handlerFunc_post = null ;
256
- if ( handler [ node . type ] &&
257
- typeof handler [ node . type ] === 'object' &&
258
- typeof handler [ node . type ] . post === 'function' ) {
259
- handlerFunc_post = handler [ node . type ] . post ;
248
+ let handlerFuncPost ;
249
+ if (
250
+ typeof handler [ node . type ] === 'object' &&
251
+ typeof handler [ node . type ] . post === 'function'
252
+ ) {
253
+ handlerFuncPost = handler [ node . type ] . post ;
260
254
}
261
255
262
- if ( handlerFunc_post ) {
263
- if ( options . asNodes ) {
264
- handlerFunc_post ( node , parent , prop , index ) ;
265
- } else {
256
+ if ( handlerFuncPost ) {
257
+ if ( nodePath ) {
266
258
// A path/node can be removed by some previous handler.
267
259
if ( ! nodePath . isRemoved ( ) ) {
268
- const handlerResult = handlerFunc_post ( nodePath ) ;
260
+ const handlerResult = handlerFuncPost . call ( handler , nodePath ) ;
269
261
// Explicitly stop traversal.
270
262
if ( handlerResult === false ) {
271
263
return false ;
272
264
}
273
265
}
266
+ } else {
267
+ handlerFuncPost . call ( handler , node , parent , prop , index ) ;
274
268
}
275
269
}
276
270
} // Loop over handlers
0 commit comments