@@ -2,36 +2,40 @@ import { ContractDefinition, SourceUnit } from "solidity-ast";
2
2
import { findAll , isNodeType } from "solidity-ast/utils" ;
3
3
import { DocItemWithContext } from "../site" ;
4
4
import { filterValues , mapValues } from './map-values' ;
5
+ import { mapKeys } from './map-keys' ;
6
+
7
+ type Definition = SourceUnit [ 'nodes' ] [ number ] & { name : string } ;
8
+ type Scope = { [ name in string ] : ( ) => { namespace : Scope } | { definition : Definition } } ;
5
9
6
10
export function getContractsInScope ( item : DocItemWithContext ) {
7
- const cache = new WeakMap < SourceUnit , Record < string , ( ) => Definition > > ( ) ;
11
+ const cache = new WeakMap < SourceUnit , Scope > ( ) ;
8
12
9
13
return filterValues (
10
- mapValues ( run ( item . __item_context . file ) , getDef => getDef ( ) ) ,
14
+ flattenScope ( run ( item . __item_context . file ) ) ,
11
15
isNodeType ( 'ContractDefinition' ) ,
12
16
) ;
13
17
14
- type Definition = SourceUnit [ 'nodes' ] [ number ] & { name : string } ;
15
-
16
- function run ( file : SourceUnit ) : Record < string , ( ) => Definition > {
18
+ function run ( file : SourceUnit ) : Scope {
17
19
if ( cache . has ( file ) ) {
18
20
return cache . get ( file ) ! ;
19
21
}
20
22
21
- const scope : Record < string , ( ) => Definition > = { } ;
23
+ const scope : Scope = { } ;
22
24
23
25
cache . set ( file , scope ) ;
24
26
25
27
for ( const c of file . nodes ) {
26
28
if ( 'name' in c ) {
27
- scope [ c . name ] = ( ) => c ;
29
+ scope [ c . name ] = ( ) => ( { definition : c } ) ;
28
30
}
29
31
}
30
32
31
33
for ( const i of findAll ( 'ImportDirective' , file ) ) {
32
34
const importedFile = item . __item_context . build . deref ( 'SourceUnit' , i . sourceUnit ) ;
33
35
const importedScope = run ( importedFile ) ;
34
- if ( i . symbolAliases . length === 0 ) {
36
+ if ( i . unitAlias ) {
37
+ scope [ i . unitAlias ] = ( ) => ( { namespace : importedScope } ) ;
38
+ } else if ( i . symbolAliases . length === 0 ) {
35
39
Object . assign ( scope , importedScope ) ;
36
40
} else {
37
41
for ( const a of i . symbolAliases ) {
@@ -45,3 +49,15 @@ export function getContractsInScope(item: DocItemWithContext) {
45
49
}
46
50
}
47
51
52
+ function flattenScope ( scope : Scope ) : Record < string , Definition > {
53
+ return Object . fromEntries (
54
+ Object . entries ( scope ) . flatMap ( ( [ k , fn ] ) => {
55
+ const v = fn ( ) ;
56
+ if ( 'definition' in v ) {
57
+ return [ [ k , v . definition ] as const ] ;
58
+ } else {
59
+ return Object . entries ( mapKeys ( flattenScope ( v . namespace ) , k2 => k + '.' + k2 ) ) ;
60
+ }
61
+ } ) ,
62
+ ) ;
63
+ }
0 commit comments