diff --git a/src/lib/modules/coverage/contractEnhanced.ts b/src/lib/modules/coverage/contractEnhanced.ts index 4d6afa78c..17cb4be25 100644 --- a/src/lib/modules/coverage/contractEnhanced.ts +++ b/src/lib/modules/coverage/contractEnhanced.ts @@ -30,7 +30,7 @@ export class ContractEnhanced { private coverageFilepath: string; private functionsBodyLocation: {[id: number]: Location} = {}; - constructor(public filepath: string) { + constructor(public filepath: string, public solcVersion: string) { this.id = nextId(); this.source = fs.readFileSync(filepath, "utf-8"); this.originalSource = this.source; diff --git a/src/lib/modules/coverage/index.ts b/src/lib/modules/coverage/index.ts index 6dd5423f1..afe718400 100644 --- a/src/lib/modules/coverage/index.ts +++ b/src/lib/modules/coverage/index.ts @@ -18,7 +18,6 @@ export default class Coverage { constructor(private embark: Embark, options: any) { fs.ensureDirSync(coverageContractsPath()); - const contractsDirConfig = this.embark.config.embarkConfig.contracts; this.contractsDir = Array.isArray(contractsDirConfig) ? contractsDirConfig : [contractsDirConfig]; @@ -33,12 +32,13 @@ export default class Coverage { } private getContracts() { + const solcVersion = this.embark.config.embarkConfig.versions.solc; const filepaths = this.contractsDir.reduce((acc: string[], pattern: string) => ( acc.concat(globule.find(pattern, { prefixBase: false, srcBase: fs.dappPath() })) ), []); return filepaths.filter((filepath) => fs.statSync(filepath).isFile()) - .map((filepath) => new ContractEnhanced(filepath)); + .map((filepath) => new ContractEnhanced(filepath, solcVersion)); } private instrumentContracts() { diff --git a/src/lib/modules/coverage/injector.ts b/src/lib/modules/coverage/injector.ts index 924a2ce27..c6fb66e3f 100644 --- a/src/lib/modules/coverage/injector.ts +++ b/src/lib/modules/coverage/injector.ts @@ -1,10 +1,15 @@ +import * as semver from "semver"; import { ContractEnhanced } from "./contractEnhanced"; import { encrypt } from "./eventId"; import { InjectionPoint } from "./types"; +const EMIT_VERSION = "0.4.21"; + export class Injector { + private isEmitSupported: boolean; constructor(private contract: ContractEnhanced) { + this.isEmitSupported = semver.gte(this.contract.solcVersion, EMIT_VERSION); } public process(injectionPoint: InjectionPoint) { @@ -17,7 +22,7 @@ export class Injector { } private statement(injectionPoint: InjectionPoint) { - const data = `emit __StatementCoverage(${encrypt(this.contract.id, injectionPoint.id)});`; + const data = `${this.isEmitSupported ? "emit" : ""} __StatementCoverage(${encrypt(this.contract.id, injectionPoint.id)});`; this.insertAt(injectionPoint.location.start.line - 1, data); } diff --git a/src/typings/embark.d.ts b/src/typings/embark.d.ts index b3777aee2..9526f9cd5 100644 --- a/src/typings/embark.d.ts +++ b/src/typings/embark.d.ts @@ -20,6 +20,9 @@ export interface Embark { config: { contracts: string; }; + versions: { + solc: string; + } }; reloadConfig(): void; };