@@ -134,3 +134,116 @@ export function destructuredDynamicImportBindings(
134134 visit ( program ) ;
135135 return captures ;
136136}
137+
138+ function patternNames ( pattern : unknown , into : string [ ] ) : void {
139+ const node = asNode ( pattern ) ;
140+ if ( node === undefined ) return ;
141+ switch ( node . type ) {
142+ case 'Identifier' :
143+ into . push ( node . name as string ) ;
144+ break ;
145+ case 'ObjectPattern' :
146+ for ( const property of nodeList ( node . properties ) ) {
147+ const entry = asNode ( property ) ;
148+ patternNames ( entry ?. type === 'Property' ? entry . value : entry , into ) ;
149+ }
150+ break ;
151+ case 'ArrayPattern' :
152+ for ( const element of nodeList ( node . elements ) ) patternNames ( element , into ) ;
153+ break ;
154+ case 'AssignmentPattern' :
155+ patternNames ( node . left , into ) ;
156+ break ;
157+ case 'RestElement' :
158+ patternNames ( node . argument , into ) ;
159+ break ;
160+ case 'TSParameterProperty' :
161+ patternNames ( node . parameter , into ) ;
162+ break ;
163+ }
164+ }
165+
166+ function lexicalNames ( statements : unknown [ ] , into : string [ ] ) : void {
167+ for ( const statement of statements ) {
168+ const node = asNode ( statement ) ;
169+ if ( node ?. type === 'VariableDeclaration' && node . kind !== 'var' ) {
170+ for ( const declarator of nodeList ( node . declarations ) ) {
171+ patternNames ( asNode ( declarator ) ?. id , into ) ;
172+ }
173+ } else if ( node ?. type === 'FunctionDeclaration' || node ?. type === 'ClassDeclaration' ) {
174+ patternNames ( node . id , into ) ;
175+ }
176+ }
177+ }
178+
179+ function isFunctionNode ( node : AstNode ) : boolean {
180+ return (
181+ node . type === 'FunctionDeclaration' ||
182+ node . type === 'FunctionExpression' ||
183+ node . type === 'ArrowFunctionExpression'
184+ ) ;
185+ }
186+
187+ function hoistedVarNames ( node : unknown , into : string [ ] ) : void {
188+ for ( const child of Array . isArray ( node ) ? node : [ node ] ) {
189+ const record = asNode ( child ) ;
190+ if ( record === undefined || isFunctionNode ( record ) ) continue ;
191+ if ( record . type === 'VariableDeclaration' && record . kind === 'var' ) {
192+ for ( const declarator of nodeList ( record . declarations ) ) {
193+ patternNames ( asNode ( declarator ) ?. id , into ) ;
194+ }
195+ }
196+ for ( const value of Object . values ( record ) ) hoistedVarNames ( value , into ) ;
197+ }
198+ }
199+
200+ /** Names `node` itself declares for its own subtree: parameters, `var`s, block-level lexical declarations. */
201+ function scopeBindings ( node : AstNode ) : string [ ] {
202+ const names : string [ ] = [ ] ;
203+ if ( isFunctionNode ( node ) ) {
204+ for ( const param of nodeList ( node . params ) ) patternNames ( param , names ) ;
205+ if ( node . type === 'FunctionExpression' ) patternNames ( node . id , names ) ;
206+ hoistedVarNames ( node . body , names ) ;
207+ } else if (
208+ node . type === 'BlockStatement' ||
209+ node . type === 'StaticBlock' ||
210+ node . type === 'TSModuleBlock'
211+ ) {
212+ lexicalNames ( nodeList ( node . body ) , names ) ;
213+ } else if ( node . type === 'ClassExpression' ) {
214+ patternNames ( node . id , names ) ;
215+ } else if ( node . type === 'SwitchStatement' ) {
216+ for ( const switchCase of nodeList ( node . cases ) ) {
217+ lexicalNames ( nodeList ( asNode ( switchCase ) ?. consequent ) , names ) ;
218+ }
219+ } else if ( node . type === 'ForStatement' ) {
220+ lexicalNames ( [ node . init ] , names ) ;
221+ } else if ( node . type === 'ForInStatement' || node . type === 'ForOfStatement' ) {
222+ lexicalNames ( [ node . left ] , names ) ;
223+ } else if ( node . type === 'CatchClause' ) {
224+ patternNames ( node . param , names ) ;
225+ }
226+ return names ;
227+ }
228+
229+ /**
230+ * `visitAst` that also hands the visitor the names declared by the enclosing scopes: function
231+ * 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.
233+ */
234+ export function visitAstScoped (
235+ node : unknown ,
236+ shadowed : ReadonlySet < string > ,
237+ visitor : ( node : AstNode , shadowed : ReadonlySet < string > ) => void ,
238+ ) : void {
239+ if ( Array . isArray ( node ) ) {
240+ for ( const child of node ) visitAstScoped ( child , shadowed , visitor ) ;
241+ return ;
242+ }
243+ const record = asNode ( node ) ;
244+ if ( record === undefined ) return ;
245+ const declared = scopeBindings ( record ) ;
246+ const scope = declared . length > 0 ? new Set ( [ ...shadowed , ...declared ] ) : shadowed ;
247+ visitor ( record , scope ) ;
248+ for ( const child of Object . values ( record ) ) visitAstScoped ( child , scope , visitor ) ;
249+ }
0 commit comments