@@ -120,7 +120,7 @@ function extractNodesFromRoot(rootNode, opts) {
120120 const node = stack . shift ( ) ;
121121 if ( node . nodeId ) continue ;
122122 node . childNodes = node . childNodes || [ ] ;
123- const childrenLoc = { } ; // Store the location of child nodes to sort them by order
123+ const childrenLoc = { } ; // Store the location of child nodes to sort them by order
124124 node . parentKey = node . parentKey || '' ; // Make sure parentKey exists
125125 // Iterate over all keys of the node to find child nodes
126126 const keys = Object . keys ( node ) ;
@@ -134,6 +134,7 @@ function extractNodesFromRoot(rootNode, opts) {
134134 if ( Array . isArray ( content ) ) {
135135 for ( let j = 0 ; j < content . length ; j ++ ) {
136136 const childNode = content [ j ] ;
137+ if ( ! childNode ) continue ;
137138 childNode . parentNode = node ;
138139 childNode . parentKey = key ;
139140 childrenLoc [ childNode . start ] = childNode ;
@@ -154,7 +155,7 @@ function extractNodesFromRoot(rootNode, opts) {
154155 typeMap [ node . type ] = typeMap [ node . type ] || [ ] ;
155156 typeMap [ node . type ] . push ( node ) ;
156157 if ( opts . detailed ) {
157- node . scope = matchScopeToNode ( node , scopes ) ;
158+ node . scope = scopes [ node . scopeId ] || node . parentNode ?. scope ;
158159 node . lineage = [ ...node . parentNode ?. lineage || [ ] ] ;
159160 if ( ! node . lineage . includes ( node . scope . scopeId ) ) {
160161 node . lineage . push ( node . scope . scopeId ) ;
@@ -255,7 +256,7 @@ function getAllScopes(rootNode) {
255256 const stack = [ globalScope ] ;
256257 while ( stack . length ) {
257258 const scope = stack . shift ( ) ;
258- if ( scope . type !== 'module' ) {
259+ if ( scope . type !== 'module' && ! scope . type . includes ( '-name' ) ) {
259260 scope . scopeId = scopeId ++ ;
260261 scope . block . scopeId = scope . scopeId ;
261262 allScopes [ scope . scopeId ] = allScopes [ scope . scopeId ] || scope ;
@@ -279,30 +280,6 @@ function getAllScopes(rootNode) {
279280 return rootNode . allScopes = allScopes ;
280281}
281282
282- /**
283- * @param {ASTNode } node
284- * @param {{number: ASTScope} } allScopes
285- * @return {ASTScope }
286- */
287- function matchScopeToNode ( node , allScopes ) {
288- let scope = node . scope ;
289- if ( ! scope ) {
290- let scopeBlock = node ;
291- while ( scopeBlock && scopeBlock . scopeId === undefined ) {
292- scopeBlock = scopeBlock . parentNode ;
293- }
294- if ( scopeBlock ) {
295- scope = allScopes [ scopeBlock . scopeId ] ;
296- }
297- }
298- if ( scope ) {
299- if ( scope . type . includes ( '-name' ) && scope ?. childScopes ?. length === 1 ) scope = scope . childScopes [ 0 ] ;
300- if ( node === scope . block && scope . upper ) scope = scope . upper ;
301- if ( scope . type === 'module' ) scope = scope . upper ;
302- } else scope = allScopes [ 0 ] ; // Global scope - this should never be reached
303- return scope ;
304- }
305-
306283export {
307284 extractNodesFromRoot ,
308285 generateCode ,
0 commit comments