@@ -5,6 +5,7 @@ import type {
5
5
Identifier ,
6
6
Pattern ,
7
7
Property ,
8
+ VariableDeclaration ,
8
9
Node as _Node
9
10
} from 'estree'
10
11
import { extract_names as extractNames } from 'periscopic'
@@ -316,6 +317,7 @@ function walk(
316
317
{ onIdentifier, onImportMeta, onDynamicImport } : Visitors
317
318
) {
318
319
const parentStack : Node [ ] = [ ]
320
+ const varKindStack : VariableDeclaration [ 'kind' ] [ ] = [ ]
319
321
const scopeMap = new WeakMap < _Node , Set < string > > ( )
320
322
const identifiers : [ id : any , stack : Node [ ] ] [ ] = [ ]
321
323
@@ -375,6 +377,11 @@ function walk(
375
377
parentStack . unshift ( parent )
376
378
}
377
379
380
+ // track variable declaration kind stack used by VariableDeclarator
381
+ if ( node . type === 'VariableDeclaration' ) {
382
+ varKindStack . unshift ( node . kind )
383
+ }
384
+
378
385
if ( node . type === 'MetaProperty' && node . meta . name === 'import' ) {
379
386
onImportMeta ( node )
380
387
} else if ( node . type === 'ImportExpression' ) {
@@ -434,7 +441,10 @@ function walk(
434
441
// mark property in destructuring pattern
435
442
setIsNodeInPattern ( node )
436
443
} else if ( node . type === 'VariableDeclarator' ) {
437
- const parentFunction = findParentScope ( parentStack )
444
+ const parentFunction = findParentScope (
445
+ parentStack ,
446
+ varKindStack [ 0 ] === 'var'
447
+ )
438
448
if ( parentFunction ) {
439
449
handlePattern ( node . id , parentFunction )
440
450
}
@@ -449,6 +459,10 @@ function walk(
449
459
) {
450
460
parentStack . shift ( )
451
461
}
462
+
463
+ if ( node . type === 'VariableDeclaration' ) {
464
+ varKindStack . shift ( )
465
+ }
452
466
}
453
467
} )
454
468
@@ -538,8 +552,12 @@ function isFunction(node: _Node): node is FunctionNode {
538
552
539
553
const scopeNodeTypeRE =
540
554
/ (?: F u n c t i o n | C l a s s ) (?: 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 $ | ^ I f S t a t e m e n t $ /
541
- function findParentScope ( parentStack : _Node [ ] ) : _Node | undefined {
542
- return parentStack . find ( ( i ) => scopeNodeTypeRE . test ( i . type ) )
555
+ function findParentScope (
556
+ parentStack : _Node [ ] ,
557
+ isVar = false
558
+ ) : _Node | undefined {
559
+ const regex = isVar ? functionNodeTypeRE : scopeNodeTypeRE
560
+ return parentStack . find ( ( i ) => regex . test ( i . type ) )
543
561
}
544
562
545
563
function isInDestructuringAssignment (
0 commit comments