All files / solidity-coverage/lib parse.js

97.06% Statements 132/136
90.52% Branches 105/116
93.33% Functions 28/30
97.01% Lines 130/134

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326          1x 1x 1x   1x 1x     1x 174x 174x       1x       1x 936x 1282x 1282x         1x   888x 32x     856x 4x     852x         548x     548x     548x 94x         1x 98x 102x         1x     180x     180x 150x     180x 92x 92x 108x     180x               1x 78x     78x 2x     78x 6x     78x     1x 360x     1x       360x 358x       358x 18x 18x 18x       340x     358x 358x   358x   358x         360x   360x 360x 836x       360x       1x 8x     1x 572x       1x 8x 8x       1x     630x 630x 4x     630x 630x     620x       620x   620x     630x     1x 170x 170x   170x     170x     170x 74x                     1x 630x 630x 64x         1x 56x 54x 54x         1x 2x       1x       352x 352x           352x 2x           350x 173x 173x 173x   173x           173x 173x       1x 346x 738x         1x 224x   224x                     1x 10x 10x     10x 14x         1x 28x       1x         1x 286x 2x   286x     1x 12x   12x     12x       1x  
/**
 * Methods in this file walk the AST and call the instrumenter
 * functions where appropriate, which determine where to inject events.
 * (Listed in alphabetical order)
 */
const semver = require('semver');
const Registrar = require('./registrar');
const register = new Registrar();
 
const FILE_SCOPED_ID = "fileScopedId";
const parse = {};
 
// Utilities
parse.configure = function(_enabled, _whitelist){
  register.enabled = Object.assign(register.enabled, _enabled);
  register.modifierWhitelist = _whitelist;
}
 
// Nodes
parse.AssignmentExpression = function(contract, expression) {
  register.statement(contract, expression);
};
 
parse.Block = function(contract, expression) {
  for (let x = 0; x < expression.statements.length; x++) {
    register.line(contract, expression.statements[x]);
    parse[expression.statements[x].type] &&
    parse[expression.statements[x].type](contract, expression.statements[x]);
  }
};
 
parse.BinaryOperation = function(contract, expression, skipStatementRegistry) {
  // Free-floating ternary conditional
  if (expression.left && expression.left.type === 'Conditional'){
    parse[expression.left.type](contract, expression.left, true);
 
  // Ternary conditional assignment
  } else if (expression.right && expression.right.type === 'Conditional'){
    parse[expression.right.type](contract, expression.right, true);
 
  // Regular binary operation
  } else if(!skipStatementRegistry){
    // noop
 
  // LogicalOR condition search...
  } else {
    parse[expression.left.type] &&
    parse[expression.left.type](contract, expression.left, true);
 
    parse[expression.right.type] &&
    parse[expression.right.type](contract, expression.right, true);
 
    if (expression.operator === '||'){
      register.logicalOR(contract, expression);
    }
  }
}
 
parse.TupleExpression = function(contract, expression, skipStatementRegistry) {
  expression.components.forEach(component => {
    parse[component.type] &&
    parse[component.type](contract, component, skipStatementRegistry);
  });
}
 
parse.FunctionCall = function(contract, expression, skipStatementRegistry) {
  // In any given chain of call expressions, only the last one will fail this check.
  // This makes sure we don't instrument a chain of expressions multiple times.
  Eif (expression.expression.type !== 'FunctionCall') {
 
    // Don't register sub-expressions (like intermediate method calls)
    if (!skipStatementRegistry){
      register.statement(contract, expression);
    }
 
    if (expression.expression.name === 'require') {
      register.requireBranch(contract, expression);
      expression.arguments.forEach(arg => {
        parse[arg.type] && parse[arg.type](contract, arg, true);
      });
    }
    parse[expression.expression.type] &&
    parse[expression.expression.type](contract, expression.expression);
  } else {
    parse[expression.expression.type] &&
    parse[expression.expression.type](contract, expression.expression);
  }
};
 
parse.Conditional = function(contract, expression, skipStatementRegistry) {
  parse[expression.condition.type] &&
  parse[expression.condition.type](contract, expression.condition, true);
 
  if (expression.trueExpression && expression.trueExpression.type === 'Conditional'){
    parse[expression.trueExpression.type](contract, expression.trueExpression, true);
  }
 
  if (expression.falseExpression && expression.falseExpression.type === 'Conditional'){
    parse[expression.falseExpression.type](contract, expression.falseExpression, true);
  }
 
  register.conditional(contract, expression);
};
 
parse.ContractDefinition = function(contract, expression) {
  parse.ContractOrLibraryStatement(contract, expression);
};
 
parse.ContractOrLibraryStatement = function(contract, expression) {
 
  // We need to define a method to pass coverage hashes into at top of each contract.
  // This lets us get a fresh stack for the hash and avoid stack-too-deep errors.
  if (expression.kind !== 'interface'){
    let start = 0;
 
    // It's possible a base contract will have constructor string arg
    // which contains an open curly brace. Skip ahead pass the bases...
    if (expression.baseContracts && expression.baseContracts.length){
      for (let base of expression.baseContracts ){
        Eif (base.range[1] > start){
          start = base.range[1];
        }
      }
    } else {
      start = expression.range[0];
    }
 
    const end = contract.instrumented.slice(start).indexOf('{') + 1;
    const loc = start + end;;
 
    contract.contractName = expression.name;
 
    (contract.injectionPoints[loc])
      ? contract.injectionPoints[loc].push({ type: 'injectHashMethod', contractName: expression.name})
      : contract.injectionPoints[loc] = [{ type: 'injectHashMethod', contractName: expression.name}];
  }
 
  Eif (expression.subNodes) {
    // Set flag to allow alternate cov id generation of file-level fn defs
    contract.isContractScoped = true;
    expression.subNodes.forEach(construct => {
      parse[construct.type] &&
      parse[construct.type](contract, construct);
    });
    // Unset flag...
    contract.isContractScoped = false;
  }
};
 
parse.EmitStatement = function(contract, expression){
  register.statement(contract, expression);
};
 
parse.ExpressionStatement = function(contract, content) {
  parse[content.expression.type] &&
  parse[content.expression.type](contract, content.expression);
};
 
parse.ForStatement = function(contract, expression) {
  register.statement(contract, expression);
  parse[expression.body.type] &&
  parse[expression.body.type](contract, expression.body);
};
 
parse.FunctionDefinition = function(contract, expression) {
  // Use generic name component to generate the hash for cov fn ids
  // if we're not inside a contract
  let tempContractName = contract.contractName;
  if (!contract.isContractScoped){
    contract.contractName = FILE_SCOPED_ID;
  }
 
  parse.Modifiers(contract, expression.modifiers);
  if (expression.body) {
    // Skip fn & statement instrumentation for `receive` methods to
    // minimize gas distortion
    (expression.name === null && expression.isReceiveEther)
      ? register.trackStatements = false
      : register.functionDeclaration(contract, expression);
 
    parse[expression.body.type] &&
    parse[expression.body.type](contract, expression.body);
    register.trackStatements = true;
  }
  // Reset contractName in case it was changed for file scoped methods
  contract.contractName = tempContractName;
};
 
parse.IfStatement = function(contract, expression) {
  register.statement(contract, expression);
  register.ifStatement(contract, expression);
 
  parse[expression.condition.type] &&
  parse[expression.condition.type](contract, expression.condition, true);
 
  parse[expression.trueBody.type] &&
  parse[expression.trueBody.type](contract, expression.trueBody);
 
  if (expression.falseBody) {
    parse[expression.falseBody.type] &&
    parse[expression.falseBody.type](contract, expression.falseBody);
  }
};
 
// TODO: Investigate Node structure
/*parse.MemberAccess = function(contract, expression) {
  parse[expression.object.type] &&
  parse[expression.object.type](contract, expression.object);
};*/
 
parse.Modifiers = function(contract, modifiers) {
  Eif (modifiers) {
    modifiers.forEach(modifier => {
      parse[modifier.type] && parse[modifier.type](contract, modifier);
    });
  }
};
 
parse.ModifierDefinition = function(contract, expression) {
  if (expression.body) {
    register.functionDeclaration(contract, expression);
    parse[expression.body.type] &&
    parse[expression.body.type](contract, expression.body);
  }
};
 
parse.NewExpression = function(contract, expression) {
  parse[expression.typeName.type] &&
  parse[expression.typeName.type](contract, expression.typeName);
};
 
parse.PragmaDirective = function(contract, expression){
  let minVersion;
 
  // Some solidity pragmas crash semver (ex: ABIEncoderV2)
  try {
    minVersion = semver.minVersion(expression.value);
  } catch(e){
    return;
  }
 
  // pragma abicoder v2 passes the semver test above but needs to be ignored
  if (expression.name === 'abicoder'){
    return
  }
 
  // From solc >=0.7.4, every file should have instrumentation methods
  // defined at the file level which file scoped fns can use. (Make sure we only do this
  // once - flattened contracts have multiple pragma statements)
  if (semver.lt("0.7.3", minVersion) && contract.finalParse && !contract.fileLevelFinished){
    const start = expression.range[0];
    const end = contract.instrumented.slice(start).indexOf(';') + 1;
    const loc = start + end;
 
    const injectionObject = {
      type: 'injectHashMethod',
      contractName: FILE_SCOPED_ID,
      isFileScoped: true
    };
 
    contract.injectionPoints[loc] = [injectionObject];
    contract.fileLevelFinished = true;
  }
}
 
parse.SourceUnit = function(contract, expression) {
  expression.children.forEach(construct => {
    parse[construct.type] &&
    parse[construct.type](contract, construct);
  });
};
 
parse.ReturnStatement = function(contract, expression) {
  register.statement(contract, expression);
 
  expression.expression &&
  parse[expression.expression.type] &&
  parse[expression.expression.type](contract, expression.expression, true);
};
 
// TODO:Investigate node structure
/*parse.UnaryOperation = function(contract, expression) {
  parse[subExpression.argument.type] &&
  parse[subExpression.argument.type](contract, expression.argument);
};*/
 
parse.TryStatement = function(contract, expression) {
  register.statement(contract, expression);
  parse[expression.body.type] &&
  parse[expression.body.type](contract, expression.body);
 
  for (let x = 0; x < expression.catchClauses.length; x++) {
    parse[expression.catchClauses[x].body.type] &&
    parse[expression.catchClauses[x].body.type](contract, expression.catchClauses[x].body);
  }
};
 
parse.UncheckedStatement = function(contract, expression) {
  parse[expression.block.type] &&
  parse[expression.block.type](contract, expression.block);
}
 
parse.UsingStatement = function (contract, expression) {
  parse[expression.for.type] &&
  parse[expression.for.type](contract, expression.for);
};
 
parse.VariableDeclarationStatement = function (contract, expression) {
  if (expression.initialValue && expression.initialValue.type === 'Conditional'){
    parse[expression.initialValue.type](contract, expression.initialValue, true)
  }
  register.statement(contract, expression);
};
 
parse.WhileStatement = function (contract, expression) {
  register.statement(contract, expression);
 
  parse[expression.condition.type] &&
  parse[expression.condition.type](contract, expression.condition, true);
 
  parse[expression.body.type] &&
  parse[expression.body.type](contract, expression.body);
};
 
module.exports = parse;