From 951e31ae6d1b33a1c5228ed22139f484d3694adc Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Thu, 9 Aug 2018 16:46:20 -0400 Subject: [PATCH] Linting things --- .eslintrc.json | 2 +- lib/modules/coverage/contract_source.js | 78 ++++++++++-------------- lib/modules/coverage/contract_sources.js | 9 +-- lib/modules/coverage/index.js | 16 +++-- lib/modules/coverage/source_map.js | 2 +- lib/modules/solidity/index.js | 1 - 6 files changed, 51 insertions(+), 57 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 4723ecbff..d666951e9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -186,7 +186,7 @@ "no-unmodified-loop-condition": "error", "no-unneeded-ternary": "error", "no-unused-expressions": "error", - "no-unused-vars": ["error", { "argsIgnorePattern": "^_" }], + "no-unused-vars": ["error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_"}], "no-use-before-define": "off", "no-useless-call": "off", "no-useless-computed-key": "error", diff --git a/lib/modules/coverage/contract_source.js b/lib/modules/coverage/contract_source.js index cd6a4db5f..5bd5995c3 100644 --- a/lib/modules/coverage/contract_source.js +++ b/lib/modules/coverage/contract_source.js @@ -23,23 +23,21 @@ class ContractSource { } sourceMapToLocations(sourceMap) { - var [offset, length, ..._rest] = sourceMap.split(":").map((val) => { + var [offset, length, ..._] = sourceMap.split(":").map((val) => { return parseInt(val, 10); }); var locations = {}; - for(var i = 0; i < this.lineCount; i++) { - if(this.lineOffsets[i+1] <= offset) - continue; + for(let i = 0; i < this.lineCount; i++) { + if(this.lineOffsets[i+1] <= offset) continue; locations.start = {line: i, column: offset - this.lineOffsets[i]}; break; } for(var i = locations.start.line; i < this.lineCount; i++) { - if(this.lineOffsets[i+1] <= offset + length) - continue; + if(this.lineOffsets[i+1] <= offset + length) continue; locations.end = {line: i, column: ((offset + length) - this.lineOffsets[i])}; break; @@ -81,7 +79,7 @@ class ContractSource { instruction: instruction, sourceMap: sourceMaps[instructions], seen: false - } + }; pc += length; instructions++; @@ -90,9 +88,9 @@ class ContractSource { } } + /*eslint complexity: ["error", 27]*/ generateCodeCoverage(trace) { - if(!this.ast || !this.contractBytecode) - throw new Error('Error generating coverage: solc output was not assigned'); + if(!this.ast || !this.contractBytecode) throw new Error('Error generating coverage: solc output was not assigned'); var coverage = { code: this.body.split("\n"), @@ -103,18 +101,15 @@ class ContractSource { f: {}, fnMap: {}, statementMap: {}, - branchMap: {}, + branchMap: {} }; var nodesRequiringVisiting = [this.ast]; - var nodeStack = []; var sourceMapToNodeType = {}; do { var node = nodesRequiringVisiting.pop(); - - if(!node) - continue; + if(!node) continue; var children = []; var markLocations = []; @@ -152,18 +147,15 @@ class ContractSource { markLocations = [location, trueBranchLocation, falseBranchLocation]; - if(node.trueBody.statements[0]) - node.trueBody.statements[0]._parent = {type: 'b', id: node.id, idx: 0} + if(node.trueBody.statements[0]) { + node.trueBody.statements[0]._parent = {type: 'b', id: node.id, idx: 0}; + } - if(node.falseBody && node.falseBody.statements[0]) - node.falseBody.statements[0]._parent = {type: 'b', id: node.id, idx: 1} - - sourceMapToNodeType[node.src] = [{ - type: 'b', - id: node.id, - body: {loc: location}, - }] + if(node.falseBody && node.falseBody.statements[0]) { + node.falseBody.statements[0]._parent = {type: 'b', id: node.id, idx: 1}; + } + sourceMapToNodeType[node.src] = [{type: 'b', id: node.id, body: {loc: location}}]; break; case 'BinaryOperation': @@ -175,12 +167,13 @@ class ContractSource { // return "body". We don't `break` here on purpose so it can share the // same logic below. node.src = node.expression.src; + // falls through case 'Assignment': case 'ExpressionStatement': coverage.s[node.id] = 0; - var location = this.sourceMapToLocations(node.src); + location = this.sourceMapToLocations(node.src); coverage.statementMap[node.id] = location; if(!sourceMapToNodeType[node.src]) sourceMapToNodeType[node.src] = []; @@ -188,7 +181,7 @@ class ContractSource { type: 's', id: node.id, body: {loc: coverage.statementMap[node.id]}, - parent: node._parent, + parent: node._parent }); @@ -217,21 +210,17 @@ class ContractSource { ).toString(); var fnName = node.isConstructor ? "(constructor)" : node.name; - var location = this.sourceMapToLocations(functionDefinitionSourceMap); + location = this.sourceMapToLocations(functionDefinitionSourceMap); coverage.f[node.id] = 0; coverage.fnMap[node.id] = { name: fnName, line: location.start.line, - loc: location, + loc: location }; // Record function positions. - sourceMapToNodeType[node.src] = [{ - type: 'f', - id: node.id, - body: coverage.fnMap[node.id], - }]; + sourceMapToNodeType[node.src] = [{type: 'f', id: node.id, body: coverage.fnMap[node.id]}]; if(node.body) children = node.body.statements; markLocations = [location]; @@ -266,17 +255,15 @@ class ContractSource { contractMatches = false; return; } - }) + }); if(!contractMatches) break; trace.structLogs.forEach((step) => { - var step = bytecode[step.pc]; + step = bytecode[step.pc]; + if(!step.sourceMap || step.sourceMap == '') return; - if(!step.sourceMap || step.sourceMap == '') - return; - - var nodes = sourceMapToNodeType[step.sourceMap] ; + var nodes = sourceMapToNodeType[step.sourceMap]; if(!nodes) return; @@ -291,16 +278,17 @@ class ContractSource { recordedLineHit = true; } - if(node.type != 'b') - coverage[node.type][node.id]++; + if(node.type != 'b') coverage[node.type][node.id]++; - if(!node.parent) - return; + if(!node.parent) return; switch(node.parent.type) { case 'b': coverage.b[node.parent.id][node.parent.idx]++; break; + + default: + // do nothing } }); }); @@ -310,9 +298,7 @@ class ContractSource { } _instructionLength(instruction) { - if(instruction.indexOf('PUSH') == -1) - return 1; - + if(instruction.indexOf('PUSH') == -1) return 1; return parseInt(instruction.match(/PUSH(\d+)/m)[1], 10) + 1; } } diff --git a/lib/modules/coverage/contract_sources.js b/lib/modules/coverage/contract_sources.js index 6d6b3a494..2b37a349b 100644 --- a/lib/modules/coverage/contract_sources.js +++ b/lib/modules/coverage/contract_sources.js @@ -19,6 +19,7 @@ class ContractSources { // No 'break' statement here on purpose, as it shares // the logic below. files = [files]; + // falls through case '[object Array]': files.forEach((file) => { @@ -31,6 +32,9 @@ class ContractSources { } }); break; + + default: + throw new Error(`Don't know how to initialize with ${Object.prototype.toString.call(files)}`); } } @@ -48,10 +52,7 @@ class ContractSources { for(var file in output.contracts) { var basename = path.basename(file); var contractSource = this.files[basename]; - if(!contractSource){ - continue; // TODO CHECK THIS LOGIC - throw new Error(`Can't attribute output to ${file}: file has not been read in.`); - } + if(!contractSource) continue; contractSource.parseSolcOutput(output.sources[file], output.contracts[file]); } diff --git a/lib/modules/coverage/index.js b/lib/modules/coverage/index.js index ea5367c3b..84d488e77 100644 --- a/lib/modules/coverage/index.js +++ b/lib/modules/coverage/index.js @@ -45,15 +45,23 @@ class CodeCoverage { } async runSolc(receipt) { - var block = await web3.eth.getBlock(receipt.number); - for(var i in block.transactions) { + let block = await web3.eth.getBlock(receipt.number); + if(block.transactions.length == 0) return; + + let requests = []; + for(let i in block.transactions) { var txHash = block.transactions[i]; if(this.seenTransactions[txHash]) return; this.seenTransactions[txHash] = true; - var trace = await web3.debug.traceTransaction(txHash, {}); - var cov = this.contractSources.generateCodeCoverage(trace); + requests.push(web3.debug.traceTransaction(txHash, {})); + } + + let traces = await Promise.all(requests); + + for(let i in traces) { + var cov = this.contractSources.generateCodeCoverage(traces[i]); this.coverageReport = cov; } } diff --git a/lib/modules/coverage/source_map.js b/lib/modules/coverage/source_map.js index 11de0498c..67ba6ebf3 100644 --- a/lib/modules/coverage/source_map.js +++ b/lib/modules/coverage/source_map.js @@ -1,7 +1,7 @@ class SourceMap { constructor(sourceMapStringOrOffset, length, id) { if(typeof sourceMapStringOrOffset == 'string') { - var [offset, length, id, ..._rest] = sourceMapStringOrOffset.split(":"); + let [offset, length, id, ..._rest] = sourceMapStringOrOffset.split(":"); this.offset = parseInt(offset, 10); this.length = parseInt(length, 10); diff --git a/lib/modules/solidity/index.js b/lib/modules/solidity/index.js index 60bd1a09f..89fe903b2 100644 --- a/lib/modules/solidity/index.js +++ b/lib/modules/solidity/index.js @@ -97,7 +97,6 @@ class Solidity { return callback(err); } - output.errors = []; // TODO REMOVE THIS if (output.errors) { for (let i=0; i