@@ -27,9 +27,10 @@ import { CFG, Properties, TargetPool, Target } from "@syntest/core";
2727import { ImportVisitor } from "./dependency/ImportVisitor" ;
2828import * as fs from "fs" ;
2929import { LibraryVisitor } from "./dependency/LibraryVisitor" ;
30- const SolidityParser = require ( "@solidity-parser/parser" ) ;
30+ import SolidityParser = require( "@solidity-parser/parser" ) ;
31+ import { SourceUnit } from "@solidity-parser/parser/dist/src/ast-types" ;
32+ // eslint-disable-next-line
3133const { outputFileSync, copySync } = require ( "fs-extra" ) ;
32- const Instrumenter = require ( "../../../src/instrumentation/instrumenter" ) ; // Local version
3334
3435/**
3536 * Pool for retrieving and caching expensive processing calls.
@@ -48,7 +49,7 @@ export class SolidityTargetPool extends TargetPool {
4849 protected _sources : Map < string , string > ;
4950
5051 // Mapping: filepath -> AST
51- protected _abstractSyntaxTrees : Map < string , any > ;
52+ protected _abstractSyntaxTrees : Map < string , SourceUnit > ;
5253
5354 // Mapping: filepath -> target name -> target
5455 protected _targetMap : Map < string , Map < string , ContractMetadata > > ;
@@ -62,10 +63,13 @@ export class SolidityTargetPool extends TargetPool {
6263 // Mapping: filepath -> target name -> (function name -> CFG)
6364 protected _controlFlowGraphs : Map < string , Map < string , CFG > > ;
6465
65- // Mapping: filepath -> target name -> [ importsMap, dependencyMap]
66+ // Mapping: filepath -> target name -> { importsMap, dependencyMap}
6667 protected _dependencyMaps : Map <
6768 string ,
68- Map < string , [ Map < string , string > , Map < string , Target [ ] > ] >
69+ Map <
70+ string ,
71+ { importMap : Map < string , string > ; dependencyMap : Map < string , Target [ ] > }
72+ >
6973 > ;
7074
7175 constructor (
@@ -81,7 +85,7 @@ export class SolidityTargetPool extends TargetPool {
8185 this . _controlFlowGraphGenerator = controlFlowGraphGenerator ;
8286
8387 this . _sources = new Map < string , string > ( ) ;
84- this . _abstractSyntaxTrees = new Map < string , any > ( ) ;
88+ this . _abstractSyntaxTrees = new Map < string , SourceUnit > ( ) ;
8589 this . _targetMap = new Map < string , Map < string , ContractMetadata > > ( ) ;
8690 this . _functionMaps = new Map <
8791 string ,
@@ -104,7 +108,7 @@ export class SolidityTargetPool extends TargetPool {
104108 }
105109 }
106110
107- getAST ( targetPath : string ) : any {
111+ getAST ( targetPath : string ) : SourceUnit {
108112 const absoluteTargetPath = path . resolve ( targetPath ) ;
109113
110114 if ( this . _abstractSyntaxTrees . has ( absoluteTargetPath ) ) {
@@ -118,7 +122,7 @@ export class SolidityTargetPool extends TargetPool {
118122 }
119123 }
120124
121- getTargetMap ( targetPath : string ) : Map < string , any > {
125+ getTargetMap ( targetPath : string ) : Map < string , ContractMetadata > {
122126 const absoluteTargetPath = path . resolve ( targetPath ) ;
123127
124128 if ( this . _targetMap . has ( absoluteTargetPath ) ) {
@@ -133,7 +137,26 @@ export class SolidityTargetPool extends TargetPool {
133137 }
134138 }
135139
136- getFunctionMap ( targetPath : string , targetName : string ) : Map < string , any > {
140+ getFunctionMap (
141+ targetPath : string
142+ ) : Map < string , Map < string , ContractFunction > > {
143+ const absoluteTargetPath = path . resolve ( targetPath ) ;
144+
145+ if ( ! this . _functionMaps . has ( absoluteTargetPath ) ) {
146+ const targetAST = this . getAST ( absoluteTargetPath ) ;
147+ const { targetMap, functionMap } =
148+ this . _targetMapGenerator . generate ( targetAST ) ;
149+ this . _targetMap . set ( absoluteTargetPath , targetMap ) ;
150+ this . _functionMaps . set ( absoluteTargetPath , functionMap ) ;
151+ }
152+
153+ return this . _functionMaps . get ( absoluteTargetPath ) ;
154+ }
155+
156+ getFunctionMapSpecific (
157+ targetPath : string ,
158+ targetName : string
159+ ) : Map < string , ContractFunction > {
137160 const absoluteTargetPath = path . resolve ( targetPath ) ;
138161
139162 if ( ! this . _functionMaps . has ( absoluteTargetPath ) ) {
@@ -176,7 +199,7 @@ export class SolidityTargetPool extends TargetPool {
176199 getImportDependencies (
177200 targetPath : string ,
178201 targetName : string
179- ) : [ Map < string , string > , Map < string , Target [ ] > ] {
202+ ) : { importMap : Map < string , string > ; dependencyMap : Map < string , Target [ ] > } {
180203 const absoluteTargetPath = path . resolve ( targetPath ) ;
181204
182205 if ( ! this . _dependencyMaps . has ( absoluteTargetPath ) )
@@ -186,8 +209,8 @@ export class SolidityTargetPool extends TargetPool {
186209 return this . _dependencyMaps . get ( absoluteTargetPath ) . get ( targetName ) ;
187210 } else {
188211 // Import the contract under test
189- const importsMap = new Map < string , string > ( ) ;
190- importsMap . set ( targetName , targetName ) ;
212+ const importMap = new Map < string , string > ( ) ;
213+ importMap . set ( targetName , targetName ) ;
191214
192215 // Find all external imports in the contract under test
193216 const importVisitor = new ImportVisitor ( ) ;
@@ -214,7 +237,7 @@ export class SolidityTargetPool extends TargetPool {
214237 SolidityParser . visit ( astLib , libraryVisitor ) ;
215238
216239 // Import the external file in the test
217- importsMap . set (
240+ importMap . set (
218241 path . basename ( importPath ) . split ( "." ) [ 0 ] ,
219242 path . basename ( importPath ) . split ( "." ) [ 0 ]
220243 ) ;
@@ -237,11 +260,12 @@ export class SolidityTargetPool extends TargetPool {
237260
238261 this . _dependencyMaps
239262 . get ( targetPath )
240- . set ( targetName , [ importsMap , dependencyMap ] ) ;
241- return [ importsMap , dependencyMap ] ;
263+ . set ( targetName , { importMap , dependencyMap } ) ;
264+ return { importMap , dependencyMap } ;
242265 }
243266 }
244267
268+ // eslint-disable-next-line
245269 async prepareAndInstrument ( api : any ) : Promise < void > {
246270 const absoluteRootPath = path . resolve ( Properties . target_root_directory ) ;
247271
0 commit comments