@@ -225,6 +225,23 @@ function walk(
225
225
const scope : Record < string , number > = Object . create ( null )
226
226
const scopeMap = new WeakMap < _Node , Set < string > > ( )
227
227
228
+ const setScope = ( node : FunctionNode , name : string ) => {
229
+ let scopeIds = scopeMap . get ( node )
230
+ if ( scopeIds && scopeIds . has ( name ) ) {
231
+ return
232
+ }
233
+ if ( name in scope ) {
234
+ scope [ name ] ++
235
+ } else {
236
+ scope [ name ] = 1
237
+ }
238
+ if ( ! scopeIds ) {
239
+ scopeIds = new Set ( )
240
+ scopeMap . set ( node , scopeIds )
241
+ }
242
+ scopeIds . add ( name )
243
+ }
244
+
228
245
; ( eswalk as any ) ( root , {
229
246
enter ( node : Node , parent : Node | null ) {
230
247
if ( node . type === 'ImportDeclaration' ) {
@@ -261,28 +278,29 @@ function walk(
261
278
parent . right === child
262
279
)
263
280
) {
264
- const { name } = child
265
- let scopeIds = scopeMap . get ( node )
266
- if ( scopeIds && scopeIds . has ( name ) ) {
267
- return
268
- }
269
- if ( name in scope ) {
270
- scope [ name ] ++
271
- } else {
272
- scope [ name ] = 1
273
- }
274
- if ( ! scopeIds ) {
275
- scopeIds = new Set ( )
276
- scopeMap . set ( node , scopeIds )
277
- }
278
- scopeIds . add ( name )
281
+ setScope ( node , child . name )
279
282
}
280
283
}
281
284
} )
282
285
)
283
286
} else if ( node . type === 'Property' && parent ! . type === 'ObjectPattern' ) {
284
287
// mark property in destructure pattern
285
288
; ( node as any ) . inPattern = true
289
+ } else if ( node . type === 'VariableDeclarator' ) {
290
+ const parentFunction = findParentFunction ( parentStack )
291
+ if ( parentFunction ) {
292
+ if ( node . id . type === 'ObjectPattern' ) {
293
+ node . id . properties . forEach ( ( property ) => {
294
+ if ( property . type === 'RestElement' ) {
295
+ setScope ( parentFunction , ( property . argument as Identifier ) . name )
296
+ } else {
297
+ setScope ( parentFunction , ( property . value as Identifier ) . name )
298
+ }
299
+ } )
300
+ } else {
301
+ setScope ( parentFunction , ( node . id as Identifier ) . name )
302
+ }
303
+ }
286
304
}
287
305
} ,
288
306
@@ -329,7 +347,7 @@ function isRefIdentifier(id: Identifier, parent: _Node, parentStack: _Node[]) {
329
347
330
348
// property key
331
349
// this also covers object destructure pattern
332
- if ( isStaticPropertyKey ( id , parent ) ) {
350
+ if ( isStaticPropertyKey ( id , parent ) || ( parent as any ) . inPattern ) {
333
351
return false
334
352
}
335
353
@@ -372,6 +390,14 @@ function isFunction(node: _Node): node is FunctionNode {
372
390
return / F u n c t i o n (?: E x p r e s s i o n | D e c l a r a t i o n ) $ | M e t h o d $ / . test ( node . type )
373
391
}
374
392
393
+ function findParentFunction ( parentStack : _Node [ ] ) : FunctionNode | undefined {
394
+ for ( const node of parentStack ) {
395
+ if ( isFunction ( node ) ) {
396
+ return node
397
+ }
398
+ }
399
+ }
400
+
375
401
function isInDestructureAssignment (
376
402
parent : _Node ,
377
403
parentStack : _Node [ ]
0 commit comments