@@ -5,6 +5,7 @@ import type {
55 Identifier ,
66 Pattern ,
77 Property ,
8+ VariableDeclaration ,
89 Node as _Node
910} from 'estree'
1011import { extract_names as extractNames } from 'periscopic'
@@ -319,6 +320,7 @@ function walk(
319320 { onIdentifier, onImportMeta, onDynamicImport } : Visitors
320321) {
321322 const parentStack : Node [ ] = [ ]
323+ const varKindStack : VariableDeclaration [ 'kind' ] [ ] = [ ]
322324 const scopeMap = new WeakMap < _Node , Set < string > > ( )
323325 const identifiers : [ id : any , stack : Node [ ] ] [ ] = [ ]
324326
@@ -378,6 +380,11 @@ function walk(
378380 parentStack . unshift ( parent )
379381 }
380382
383+ // track variable declaration kind stack used by VariableDeclarator
384+ if ( node . type === 'VariableDeclaration' ) {
385+ varKindStack . unshift ( node . kind )
386+ }
387+
381388 if ( node . type === 'MetaProperty' && node . meta . name === 'import' ) {
382389 onImportMeta ( node )
383390 } else if ( node . type === 'ImportExpression' ) {
@@ -437,7 +444,10 @@ function walk(
437444 // mark property in destructuring pattern
438445 setIsNodeInPattern ( node )
439446 } else if ( node . type === 'VariableDeclarator' ) {
440- const parentFunction = findParentScope ( parentStack )
447+ const parentFunction = findParentScope (
448+ parentStack ,
449+ varKindStack [ 0 ] === 'var'
450+ )
441451 if ( parentFunction ) {
442452 handlePattern ( node . id , parentFunction )
443453 }
@@ -452,6 +462,10 @@ function walk(
452462 ) {
453463 parentStack . shift ( )
454464 }
465+
466+ if ( node . type === 'VariableDeclaration' ) {
467+ varKindStack . shift ( )
468+ }
455469 }
456470 } )
457471
@@ -541,8 +555,12 @@ function isFunction(node: _Node): node is FunctionNode {
541555
542556const scopeNodeTypeRE =
543557 / (?: 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 $ /
544- function findParentScope ( parentStack : _Node [ ] ) : _Node | undefined {
545- return parentStack . find ( ( i ) => scopeNodeTypeRE . test ( i . type ) )
558+ function findParentScope (
559+ parentStack : _Node [ ] ,
560+ isVar = false
561+ ) : _Node | undefined {
562+ const regex = isVar ? functionNodeTypeRE : scopeNodeTypeRE
563+ return parentStack . find ( ( i ) => regex . test ( i . type ) )
546564}
547565
548566function isInDestructuringAssignment (
0 commit comments