aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-tracing-utils/src/collect_coverage_entries.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2019-01-12 00:12:55 +0800
committerFabio Berger <me@fabioberger.com>2019-01-12 00:12:55 +0800
commit96ff5eef644b8c4d3f8fc4ec53270991fb51f121 (patch)
tree530f43b03d2b3aa09b5f6552d4285df92d45fed0 /packages/sol-tracing-utils/src/collect_coverage_entries.ts
parent7f5a2c972bb86df77c423fb7029e9a629ecc1ede (diff)
parent2cf57a48dd2857dd5cf2f31f4c60dd47ae4d34a5 (diff)
downloaddexon-sol-tools-96ff5eef644b8c4d3f8fc4ec53270991fb51f121.tar
dexon-sol-tools-96ff5eef644b8c4d3f8fc4ec53270991fb51f121.tar.gz
dexon-sol-tools-96ff5eef644b8c4d3f8fc4ec53270991fb51f121.tar.bz2
dexon-sol-tools-96ff5eef644b8c4d3f8fc4ec53270991fb51f121.tar.lz
dexon-sol-tools-96ff5eef644b8c4d3f8fc4ec53270991fb51f121.tar.xz
dexon-sol-tools-96ff5eef644b8c4d3f8fc4ec53270991fb51f121.tar.zst
dexon-sol-tools-96ff5eef644b8c4d3f8fc4ec53270991fb51f121.zip
merge development
Diffstat (limited to 'packages/sol-tracing-utils/src/collect_coverage_entries.ts')
-rw-r--r--packages/sol-tracing-utils/src/collect_coverage_entries.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/packages/sol-tracing-utils/src/collect_coverage_entries.ts b/packages/sol-tracing-utils/src/collect_coverage_entries.ts
new file mode 100644
index 000000000..bdbcd613e
--- /dev/null
+++ b/packages/sol-tracing-utils/src/collect_coverage_entries.ts
@@ -0,0 +1,41 @@
+import * as ethUtil from 'ethereumjs-util';
+import * as _ from 'lodash';
+import * as parser from 'solidity-parser-antlr';
+
+import { ASTVisitor, CoverageEntriesDescription } from './ast_visitor';
+import { getLocationByOffset } from './source_maps';
+
+const IGNORE_RE = /\/\*\s*solcov\s+ignore\s+next\s*\*\/\s*/gm;
+
+// Parsing source code for each transaction/code is slow and therefore we cache it
+const coverageEntriesBySourceHash: { [sourceHash: string]: CoverageEntriesDescription } = {};
+
+export const collectCoverageEntries = (contractSource: string) => {
+ const sourceHash = ethUtil.sha3(contractSource).toString('hex');
+ if (_.isUndefined(coverageEntriesBySourceHash[sourceHash]) && !_.isUndefined(contractSource)) {
+ const ast = parser.parse(contractSource, { range: true });
+ const locationByOffset = getLocationByOffset(contractSource);
+ const ignoreRangesBegingingAt = gatherRangesToIgnore(contractSource);
+ const visitor = new ASTVisitor(locationByOffset, ignoreRangesBegingingAt);
+ parser.visit(ast, visitor);
+ coverageEntriesBySourceHash[sourceHash] = visitor.getCollectedCoverageEntries();
+ }
+ const coverageEntriesDescription = coverageEntriesBySourceHash[sourceHash];
+ return coverageEntriesDescription;
+};
+
+// Gather the start index of all code blocks preceeded by "/* solcov ignore next */"
+function gatherRangesToIgnore(contractSource: string): number[] {
+ const ignoreRangesStart = [];
+
+ let match;
+ do {
+ match = IGNORE_RE.exec(contractSource);
+ if (match) {
+ const matchLen = match[0].length;
+ ignoreRangesStart.push(match.index + matchLen);
+ }
+ } while (match);
+
+ return ignoreRangesStart;
+}