1
1
import { ContractDefinition } from 'solidity-ast' ;
2
- import { findAll } from 'solidity-ast/utils' ;
2
+ import { findAll , astDereferencer , ASTDereferencer } from 'solidity-ast/utils' ;
3
3
import { NodeType , NodeTypeMap } from 'solidity-ast/node' ;
4
4
5
5
import { SolcOutput } from './solc/input-output' ;
6
6
7
7
export class ASTResolver {
8
- constructor ( readonly output : SolcOutput , readonly exclude ?: ( source : string ) => boolean ) { }
8
+ private deref : ASTDereferencer ;
9
+
10
+ constructor ( readonly output : SolcOutput , readonly exclude ?: ( source : string ) => boolean ) {
11
+ this . deref = astDereferencer ( output ) ;
12
+ }
9
13
10
14
resolveContract ( id : number ) : ContractDefinition | undefined {
11
15
try {
@@ -28,16 +32,12 @@ export class ASTResolver {
28
32
}
29
33
30
34
tryResolveNode < T extends NodeType > ( nodeType : T , id : number ) : NodeTypeMap [ T ] | undefined {
31
- for ( const source in this . output . sources ) {
32
- for ( const c of findAll ( nodeType , this . output . sources [ source ] . ast ) ) {
33
- if ( c . id === id ) {
34
- if ( this . exclude ?.( source ) ) {
35
- throw new Error ( `Symbol #${ id } was imported from an excluded file (${ source } )` ) ;
36
- } else {
37
- return c ;
38
- }
39
- }
40
- }
35
+ const { node, sourceUnit } = this . deref . withSourceUnit ( nodeType , id ) ;
36
+ const source = sourceUnit . absolutePath ;
37
+ if ( this . exclude ?.( source ) ) {
38
+ throw new Error ( `Symbol #${ id } was imported from an excluded file (${ source } )` ) ;
39
+ } else {
40
+ return node ;
41
41
}
42
42
}
43
43
}
0 commit comments