diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-03-12 10:35:11 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-03-12 10:37:30 +0800 |
commit | 870995933a60b67af03b3f42a2aed169d33bd87f (patch) | |
tree | 2c8c583434080413dbb8aede8de515d5ed7e05ef /packages/sol-cov/src/collect_coverage_entries.ts | |
parent | d6c2e47bbd099f380c8fd4790d6d7be17d3532e5 (diff) | |
download | dexon-sol-tools-870995933a60b67af03b3f42a2aed169d33bd87f.tar.gz dexon-sol-tools-870995933a60b67af03b3f42a2aed169d33bd87f.tar.zst dexon-sol-tools-870995933a60b67af03b3f42a2aed169d33bd87f.zip |
Remove redundant Date.now()
Diffstat (limited to 'packages/sol-cov/src/collect_coverage_entries.ts')
-rw-r--r-- | packages/sol-cov/src/collect_coverage_entries.ts | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/packages/sol-cov/src/collect_coverage_entries.ts b/packages/sol-cov/src/collect_coverage_entries.ts new file mode 100644 index 000000000..d29fa2c37 --- /dev/null +++ b/packages/sol-cov/src/collect_coverage_entries.ts @@ -0,0 +1,24 @@ +import * as ethUtil from 'ethereumjs-util'; +import * as fs from 'fs'; +import * as _ from 'lodash'; +import * as path from 'path'; +import * as SolidityParser from 'solidity-parser-sc'; + +import { ASTVisitor, CoverageEntriesDescription } from './ast_visitor'; +import { getLocationByOffset } from './source_maps'; + +// Parsing source code for each transaction/code is slow and therefore we cache it +const coverageEntriesBySourceHash: { [sourceHash: string]: CoverageEntriesDescription } = {}; + +export const collectCoverageEntries = (contractSource: string, fileName: string) => { + const sourceHash = ethUtil.sha3(contractSource).toString('hex'); + if (_.isUndefined(coverageEntriesBySourceHash[sourceHash])) { + const ast = SolidityParser.parse(contractSource); + const locationByOffset = getLocationByOffset(contractSource); + const astVisitor = new ASTVisitor(locationByOffset); + astVisitor.walkAST(ast); + coverageEntriesBySourceHash[sourceHash] = astVisitor.getCollectedCoverageEntries(); + } + const coverageEntriesDescription = coverageEntriesBySourceHash[sourceHash]; + return coverageEntriesDescription; +}; |