From fa8f4b4e41d8c2afb71c8ae2b4ba242426b7e80f Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Wed, 26 Sep 2018 11:13:14 -0400 Subject: [PATCH] Support if statements without a list of statements --- lib/modules/coverage/contract_source.js | 18 +++++++++--------- test_apps/coverage_app/contracts/branches.sol | 5 +++++ test_apps/coverage_app/test/branches_spec.js | 4 ++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/modules/coverage/contract_source.js b/lib/modules/coverage/contract_source.js index a0c36821..c0ab2315 100644 --- a/lib/modules/coverage/contract_source.js +++ b/lib/modules/coverage/contract_source.js @@ -162,19 +162,19 @@ class ContractSource { line: location.start.line }; - children = [node.condition] - .concat(node.trueBody.statements); - - if(node.falseBody) children = children.concat(node.falseBody.statements); - markLocations = [declarationLocation]; + children = [node.condition]; - if(node.trueBody.statements[0]) { - node.trueBody.statements[0]._parent = {type: 'b', id: node.id, idx: 0}; + let trueExpression = (node.trueBody && node.trueBody.statements && node.trueBody.statements[0]) || node.trueBody; + if(trueExpression) { + children = children.concat([].concat(trueExpression)); + trueExpression._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}; + let falseExpression = (node.falseBody && node.falseBody.statements && node.falseBody.statements[0]) || node.falseBody; + if(falseExpression) { + children = children.concat([].concat(falseExpression)); + falseExpression._parent = {type: 'b', id: node.id, idx: 1}; } sourceMapToNodeType[node.src] = [{type: 'b', id: node.id, body: {loc: location}}]; diff --git a/test_apps/coverage_app/contracts/branches.sol b/test_apps/coverage_app/contracts/branches.sol index 4c3854ab..d9f4d215 100644 --- a/test_apps/coverage_app/contracts/branches.sol +++ b/test_apps/coverage_app/contracts/branches.sol @@ -32,4 +32,9 @@ contract Branches { function get() public view returns (uint retVal) { return storedData; } + + function smallFunctionWithoutStatements() public view returns (uint retVal) { + if(false) return storedData * 10; + if(true) return storedData * 20; + } } diff --git a/test_apps/coverage_app/test/branches_spec.js b/test_apps/coverage_app/test/branches_spec.js index 0fd8290f..593c15b6 100644 --- a/test_apps/coverage_app/test/branches_spec.js +++ b/test_apps/coverage_app/test/branches_spec.js @@ -28,4 +28,8 @@ contract("Branches", function() { it("should return the smaller number", function(done) { Branches.methods.smaller(10).send().then(() => { done(); }); }); + + it("should not crash code coverage on `if` without statements", function(done) { + Branches.methods.smallFunctionWithoutStatements().call().then(() => { done(); }); + }); });