@@ -184,10 +184,15 @@ function isFunctionNode(node: AstNode): boolean {
184184 ) ;
185185}
186186
187+ /** Bodies that open their own `var` scope: a `var` inside one never reaches the enclosing function. */
188+ function isVarScopeBoundary ( node : AstNode ) : boolean {
189+ return isFunctionNode ( node ) || node . type === 'StaticBlock' || node . type === 'TSModuleBlock' ;
190+ }
191+
187192function hoistedVarNames ( node : unknown , into : string [ ] ) : void {
188193 for ( const child of Array . isArray ( node ) ? node : [ node ] ) {
189194 const record = asNode ( child ) ;
190- if ( record === undefined || isFunctionNode ( record ) ) continue ;
195+ if ( record === undefined || isVarScopeBoundary ( record ) ) continue ;
191196 if ( record . type === 'VariableDeclaration' && record . kind === 'var' ) {
192197 for ( const declarator of nodeList ( record . declarations ) ) {
193198 patternNames ( asNode ( declarator ) ?. id , into ) ;
@@ -197,18 +202,20 @@ function hoistedVarNames(node: unknown, into: string[]): void {
197202 }
198203}
199204
200- /** Names `node` itself declares for its own subtree: parameters, `var`s, block-level lexical declarations. */
205+ /**
206+ * Names `node` declares for its own subtree: parameters, block-level lexical declarations, and the
207+ * `var`s of a static block or namespace body. A function's own `var`s join its body only
208+ * (`childScope`), since a parameter initializer runs before the body declarations exist.
209+ */
201210function scopeBindings ( node : AstNode ) : string [ ] {
202211 const names : string [ ] = [ ] ;
203212 if ( isFunctionNode ( node ) ) {
204213 for ( const param of nodeList ( node . params ) ) patternNames ( param , names ) ;
205214 if ( node . type === 'FunctionExpression' ) patternNames ( node . id , names ) ;
215+ } else if ( node . type === 'StaticBlock' || node . type === 'TSModuleBlock' ) {
216+ lexicalNames ( nodeList ( node . body ) , names ) ;
206217 hoistedVarNames ( node . body , names ) ;
207- } else if (
208- node . type === 'BlockStatement' ||
209- node . type === 'StaticBlock' ||
210- node . type === 'TSModuleBlock'
211- ) {
218+ } else if ( node . type === 'BlockStatement' ) {
212219 lexicalNames ( nodeList ( node . body ) , names ) ;
213220 } else if ( node . type === 'ClassExpression' ) {
214221 patternNames ( node . id , names ) ;
@@ -226,10 +233,36 @@ function scopeBindings(node: AstNode): string[] {
226233 return names ;
227234}
228235
236+ function withNames ( scope : ReadonlySet < string > , names : readonly string [ ] ) : ReadonlySet < string > {
237+ return names . length > 0 ? new Set ( [ ...scope , ...names ] ) : scope ;
238+ }
239+
240+ /**
241+ * The scope a child property sees. Most children see everything the node declares; a switch
242+ * discriminant runs before the case declarations exist, and only a function body sees the
243+ * function's `var`s.
244+ */
245+ function childScope (
246+ node : AstNode ,
247+ key : string ,
248+ outer : ReadonlySet < string > ,
249+ inner : ReadonlySet < string > ,
250+ ) : ReadonlySet < string > {
251+ if ( node . type === 'SwitchStatement' && key === 'discriminant' ) return outer ;
252+ if ( isFunctionNode ( node ) && key === 'body' ) {
253+ const vars : string [ ] = [ ] ;
254+ hoistedVarNames ( node . body , vars ) ;
255+ return withNames ( inner , vars ) ;
256+ }
257+ return inner ;
258+ }
259+
229260/**
230261 * `visitAst` that also hands the visitor the names declared by the enclosing scopes: function
231262 * parameters and `var`s, block-level `let`/`const`/`function`/`class`, loop heads and `catch`
232- * parameters. A `with` statement or `eval` can rebind names invisibly and is not modelled.
263+ * parameters. A parameter initializer, a switch discriminant, a static block and a namespace
264+ * body keep their own boundaries. A `with` statement or `eval` can rebind names invisibly and is
265+ * not modelled.
233266 */
234267export function visitAstScoped (
235268 node : unknown ,
@@ -242,8 +275,9 @@ export function visitAstScoped(
242275 }
243276 const record = asNode ( node ) ;
244277 if ( record === undefined ) return ;
245- const declared = scopeBindings ( record ) ;
246- const scope = declared . length > 0 ? new Set ( [ ...shadowed , ...declared ] ) : shadowed ;
278+ const scope = withNames ( shadowed , scopeBindings ( record ) ) ;
247279 visitor ( record , scope ) ;
248- for ( const child of Object . values ( record ) ) visitAstScoped ( child , scope , visitor ) ;
280+ for ( const [ key , child ] of Object . entries ( record ) ) {
281+ visitAstScoped ( child , childScope ( record , key , shadowed , scope ) , visitor ) ;
282+ }
249283}
0 commit comments