1- import { NodePath } from "./node-path" ;
1+ import type { ExpressionKind } from "./gen/kinds" ;
2+ import type { namedTypes } from "./main" ;
3+ import type { NodePath } from "./node-path" ;
24import { maybeSetModuleExports } from "./shared" ;
3- import typesPlugin , { Fork } from "./types" ;
5+ import type { Fork } from "./types" ;
6+ import typesPlugin from "./types" ;
47
58var hasOwn = Object . prototype . hasOwnProperty ;
69
10+ export type ScopeBinding = Record < string , ( NodePath | namedTypes . Identifier ) [ ] > ;
11+
712export interface Scope {
813 path : NodePath ;
9- node : any ;
14+ node : NodePath [ 'value' ] ;
1015 isGlobal : boolean ;
1116 depth : number ;
12- parent : any ;
13- bindings : any ;
17+ parent : Scope | null ;
18+ bindings : ScopeBinding ;
1419 types : any ;
1520 didScan : boolean ;
16- declares ( name : any ) : any
17- declaresType ( name : any ) : any
18- declareTemporary ( prefix ?: any ) : any ;
19- injectTemporary ( identifier : any , init : any ) : any ;
20- scan ( force ?: any ) : any ;
21- getBindings ( ) : any ;
21+ declares ( name : string ) : boolean
22+ declaresType ( name : string ) : boolean
23+ declareTemporary ( prefix ?: string ) : namedTypes . Identifier ;
24+ injectTemporary ( identifier ?: namedTypes . Identifier , init ?: ExpressionKind | null ) : namedTypes . Identifier ;
25+ scan ( force ?: boolean ) : void ;
26+ getBindings ( ) : ScopeBinding ;
2227 getTypes ( ) : any ;
23- lookup ( name : any ) : any ;
24- lookupType ( name : any ) : any ;
25- getGlobalScope ( ) : Scope ;
28+ lookup ( name : string ) : Scope ;
29+ lookupType ( name : string ) : Scope ;
30+ getGlobalScope ( ) : Scope | null ;
2631}
2732
2833export interface ScopeConstructor {
29- new ( path : NodePath , parentScope : any ) : Scope ;
30- isEstablishedBy ( node : any ) : any ;
34+ new ( path : NodePath , parentScope ?: Scope | null ) : Scope ;
35+ isEstablishedBy ( node : NodePath [ 'node' ] ) : boolean ;
3136}
3237
3338export default function scopePlugin ( fork : Fork ) {
@@ -39,7 +44,7 @@ export default function scopePlugin(fork: Fork) {
3944 var isArray = types . builtInTypes . array ;
4045 var b = types . builders ;
4146
42- const Scope = function Scope ( this : Scope , path : NodePath , parentScope : unknown ) {
47+ const Scope = function Scope ( this : Scope , path : NodePath , parentScope ?: Scope | null ) {
4348 if ( ! ( this instanceof Scope ) ) {
4449 throw new Error ( "Scope constructor cannot be invoked without 'new'" ) ;
4550 }
@@ -100,7 +105,7 @@ export default function scopePlugin(fork: Fork) {
100105 namedTypes . TSTypeParameter ,
101106 ) ;
102107
103- Scope . isEstablishedBy = function ( node ) {
108+ Scope . isEstablishedBy = function ( node : NodePath [ 'node' ] ) {
104109 return ScopeType . check ( node ) || TypeParameterScopeType . check ( node ) ;
105110 } ;
106111
@@ -140,10 +145,12 @@ export default function scopePlugin(fork: Fork) {
140145 }
141146
142147 var name = prefix + index ;
143- return this . bindings [ name ] = types . builders . identifier ( name ) ;
148+ var identifier = types . builders . identifier ( name ) ;
149+ this . bindings [ name ] = [ identifier ] ;
150+ return identifier ;
144151 } ;
145152
146- Sp . injectTemporary = function ( identifier , init ) {
153+ Sp . injectTemporary = function ( identifier ?: namedTypes . Identifier , init ?: ExpressionKind | null ) {
147154 identifier || ( identifier = this . declareTemporary ( ) ) ;
148155
149156 var bodyPath = this . path . get ( "body" ) ;
@@ -186,7 +193,7 @@ export default function scopePlugin(fork: Fork) {
186193 return this . types ;
187194 } ;
188195
189- function scanScope ( path : NodePath , bindings : any , scopeTypes : any ) {
196+ function scanScope ( path : NodePath , bindings : ScopeBinding , scopeTypes : any ) {
190197 var node = path . value ;
191198 if ( TypeParameterScopeType . check ( node ) ) {
192199 const params = path . get ( 'typeParameters' , 'params' ) ;
@@ -208,7 +215,7 @@ export default function scopePlugin(fork: Fork) {
208215 }
209216 }
210217
211- function recursiveScanScope ( path : NodePath , bindings : any , scopeTypes : any ) {
218+ function recursiveScanScope ( path : NodePath , bindings : ScopeBinding , scopeTypes : any ) {
212219 var node = path . value ;
213220
214221 if ( path . parent &&
@@ -286,7 +293,7 @@ export default function scopePlugin(fork: Fork) {
286293 return false ;
287294 }
288295
289- function recursiveScanChild ( path : NodePath , bindings : any , scopeTypes : any ) {
296+ function recursiveScanChild ( path : NodePath , bindings : ScopeBinding , scopeTypes : any ) {
290297 var node = path . value ;
291298
292299 if ( ! node || Expression . check ( node ) ) {
@@ -338,7 +345,7 @@ export default function scopePlugin(fork: Fork) {
338345 }
339346 }
340347
341- function addPattern ( patternPath : NodePath , bindings : any ) {
348+ function addPattern ( patternPath : NodePath , bindings : ScopeBinding ) {
342349 var pattern = patternPath . value ;
343350 namedTypes . Pattern . assert ( pattern ) ;
344351
@@ -357,7 +364,7 @@ export default function scopePlugin(fork: Fork) {
357364 namedTypes . ObjectPattern &&
358365 namedTypes . ObjectPattern . check ( pattern )
359366 ) {
360- patternPath . get ( 'properties' ) . each ( function ( propertyPath : any ) {
367+ patternPath . get ( 'properties' ) . each ( function ( propertyPath : NodePath ) {
361368 var property = propertyPath . value ;
362369 if ( namedTypes . Pattern . check ( property ) ) {
363370 addPattern ( propertyPath , bindings ) ;
@@ -379,7 +386,7 @@ export default function scopePlugin(fork: Fork) {
379386 namedTypes . ArrayPattern &&
380387 namedTypes . ArrayPattern . check ( pattern )
381388 ) {
382- patternPath . get ( 'elements' ) . each ( function ( elementPath : any ) {
389+ patternPath . get ( 'elements' ) . each ( function ( elementPath : NodePath ) {
383390 var element = elementPath . value ;
384391 if ( namedTypes . Pattern . check ( element ) ) {
385392 addPattern ( elementPath , bindings ) ;
@@ -434,22 +441,22 @@ export default function scopePlugin(fork: Fork) {
434441 }
435442
436443 Sp . lookup = function ( name ) {
437- for ( var scope = this ; scope ; scope = scope . parent )
444+ for ( var scope : Scope | null = this ; scope ; scope = scope . parent )
438445 if ( scope . declares ( name ) )
439446 break ;
440- return scope ;
447+ return scope ! ;
441448 } ;
442449
443450 Sp . lookupType = function ( name ) {
444- for ( var scope = this ; scope ; scope = scope . parent )
451+ for ( var scope : Scope | null = this ; scope ; scope = scope . parent )
445452 if ( scope . declaresType ( name ) )
446453 break ;
447- return scope ;
454+ return scope ! ;
448455 } ;
449456
450457 Sp . getGlobalScope = function ( ) {
451- var scope = this ;
452- while ( ! scope . isGlobal )
458+ var scope : Scope | null = this ;
459+ while ( scope && ! scope . isGlobal )
453460 scope = scope . parent ;
454461 return scope ;
455462 } ;
0 commit comments