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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | 1x 1x 1x 4005x 4005x 868x 860x 860x 860x 860x 860x 860x 192x 668x 860x 860x 860x 1282x 1276x 1276x 1276x 1276x 1276x 1276x 1276x 1212x 64x 30x 672x 668x 668x 668x 52x 62x 62x 62x 14x 48x 48x 48x 616x 668x 668x 668x 668x 668x 668x 668x 306x 306x 306x 306x 306x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 78x 76x 76x 76x 76x 76x 94x 92x 92x 92x 92x 92x 92x 92x 92x 92x 170x 166x 166x 136x 166x 147x 50x 97x 1x | /** * Registers injections points (e.g source location, type) and their associated data with * a contract / instrumentation target. Run during the `parse` step. This data is * consumed by the Injector as it modifies the source code in instrumentation's final step. */ class Registrar { constructor(){ // When, at *certain nodes* we don't want to inject statements // because they're an unnecessary expense. ex: `receive`, this // can be toggled on/off in the parser this.trackStatements = true; // These are set by user option and enable/disable the measurement completely this.enabled = { statements: true, functions: true, modifiers: true, branches: true, lines: true } this.modifierWhitelist = []; } /** * Adds injection point to injection points map * @param {Object} contract instrumentation target * @param {String} key injection point `type` * @param {Number} value injection point `id` */ _createInjectionPoint(contract, key, value) { value.contractName = contract.contractName; (contract.injectionPoints[key]) ? contract.injectionPoints[key].push(value) : contract.injectionPoints[key] = [value]; } /** * Registers injections for statement measurements * @param {Object} contract instrumentation target * @param {Object} expression AST node */ statement(contract, expression) { if (!this.trackStatements || !this.enabled.statements) return; const startContract = contract.instrumented.slice(0, expression.range[0]); const startline = ( startContract.match(/\n/g) || [] ).length + 1; const startcol = expression.range[0] - startContract.lastIndexOf('\n') - 1; const expressionContent = contract.instrumented.slice( expression.range[0], expression.range[1] + 1 ); const endline = startline + (contract, expressionContent.match('/\n/g') || []).length; let endcol; if (expressionContent.lastIndexOf('\n') >= 0) { endcol = contract.instrumented.slice( expressionContent.lastIndexOf('\n'), expression.range[1] ).length; } else endcol = startcol + (contract, expressionContent.length - 1); contract.statementId += 1; contract.statementMap[contract.statementId] = { start: { line: startline, column: startcol }, end: { line: endline, column: endcol }, }; this._createInjectionPoint(contract, expression.range[0], { type: 'injectStatement', statementId: contract.statementId, }); }; /** * Registers injections for line measurements * @param {Object} contract instrumentation target * @param {Object} expression AST node */ line(contract, expression) { if (!this.enabled.lines) return; const startchar = expression.range[0]; const endchar = expression.range[1] + 1; const lastNewLine = contract.instrumented.slice(0, startchar).lastIndexOf('\n'); const nextNewLine = startchar + contract.instrumented.slice(startchar).indexOf('\n'); const contractSnipped = contract.instrumented.slice(lastNewLine, nextNewLine); const restOfLine = contract.instrumented.slice(endchar, nextNewLine); if ( contract.instrumented.slice(lastNewLine, startchar).trim().length === 0 && ( restOfLine.replace(';', '').trim().length === 0 || restOfLine.replace(';', '').trim().substring(0, 2) === '//' ) ) { this._createInjectionPoint(contract, lastNewLine + 1, { type: 'injectLine' }); } else if ( contract.instrumented.slice(lastNewLine, startchar).replace('{', '').trim().length === 0 && contract.instrumented.slice(endchar, nextNewLine).replace(/[;}]/g, '').trim().length === 0) { this._createInjectionPoint(contract, expression.range[0], { type: 'injectLine' }); } }; /** * Registers injections for function measurements * @param {Object} contract instrumentation target * @param {Object} expression AST node */ functionDeclaration(contract, expression) { if (!this.enabled.functions) return; let start = 0; contract.fnId += 1; if (expression.modifiers && expression.modifiers.length){ for (let modifier of expression.modifiers ){ // It's possible functions will have modifiers that take string args // which contains an open curly brace. Skip ahead... Eif (modifier.range[1] > start){ start = modifier.range[1]; } // Add modifier branch coverage if ( !this.enabled.modifiers || expression.isConstructor || this.modifierWhitelist.includes(modifier.name) ) { continue; } this.addNewBranch(contract, modifier); this._createInjectionPoint( contract, modifier.range[0], { type: 'injectModifier', branchId: contract.branchId, modifierName: modifier.name, fnId: contract.fnId, condition: 'pre' } ); this._createInjectionPoint( contract, modifier.range[1] + 1, { type: 'injectModifier', branchId: contract.branchId, modifierName: modifier.name, fnId: contract.fnId, condition: 'post' } ); } } else { start = expression.range[0]; } const startContract = contract.instrumented.slice(0, start); const startline = ( startContract.match(/\n/g) || [] ).length + 1; const startcol = start - startContract.lastIndexOf('\n') - 1; const endlineDelta = contract.instrumented.slice(start).indexOf('{'); const functionDefinition = contract.instrumented.slice( start, start + endlineDelta ); contract.fnMap[contract.fnId] = { name: expression.isConstructor ? 'constructor' : expression.name, line: startline, loc: expression.loc }; this._createInjectionPoint( contract, start + endlineDelta + 1, { type: 'injectFunction', fnId: contract.fnId, } ); }; /** * Registers injections for branch measurements. This generic is consumed by * the `require` and `if` registration methods. * @param {Object} contract instrumentation target * @param {Object} expression AST node */ addNewBranch(contract, expression) { const startContract = contract.instrumented.slice(0, expression.range[0]); const startline = ( startContract.match(/\n/g) || [] ).length + 1; const startcol = expression.range[0] - startContract.lastIndexOf('\n') - 1; contract.branchId += 1; // NB locations for if branches in istanbul are zero // length and associated with the start of the if. contract.branchMap[contract.branchId] = { line: startline, type: 'if', locations: [{ start: { line: startline, column: startcol, }, end: { line: startline, column: startcol, }, }, { start: { line: startline, column: startcol, }, end: { line: startline, column: startcol, }, }], }; }; addNewConditionalBranch(contract, expression){ let start; // Instabul HTML highlighting location data... const trueZero = expression.trueExpression.range[0]; const trueOne = expression.trueExpression.range[1]; const falseZero = expression.falseExpression.range[0]; const falseOne = expression.falseExpression.range[1]; start = contract.instrumented.slice(0, trueZero); const trueStartLine = ( start.match(/\n/g) || [] ).length + 1; const trueStartCol = trueZero - start.lastIndexOf('\n') - 1; start = contract.instrumented.slice(0, trueOne); const trueEndLine = ( start.match(/\n/g) || [] ).length + 1; const trueEndCol = trueOne - start.lastIndexOf('\n') - 1; start = contract.instrumented.slice(0, falseZero); const falseStartLine = ( start.match(/\n/g) || [] ).length + 1; const falseStartCol = falseZero - start.lastIndexOf('\n') - 1; start = contract.instrumented.slice(0, falseOne); const falseEndLine = ( start.match(/\n/g) || [] ).length + 1; const falseEndCol = falseOne - start.lastIndexOf('\n') - 1; contract.branchId += 1; contract.branchMap[contract.branchId] = { line: trueStartLine, type: 'if', locations: [{ start: { line: trueStartLine, column: trueStartCol, }, end: { line: trueEndLine, column: trueEndCol, }, }, { start: { line: falseStartLine, column: falseStartCol, }, end: { line: falseEndLine, column: falseEndCol, }, }], }; } /** * Registers injections for branch measurements. Used by logicalOR registration method. * @param {Object} contract instrumentation target * @param {Object} expression AST node */ addNewLogicalORBranch(contract, expression) { let start; // Instabul HTML highlighting location data... const leftZero = expression.left.range[0]; const leftOne = expression.left.range[1]; const rightZero = expression.right.range[0]; const rightOne = expression.right.range[1]; start = contract.instrumented.slice(0, leftZero); const leftStartLine = ( start.match(/\n/g) || [] ).length + 1; const leftStartCol = leftZero - start.lastIndexOf('\n') - 1; start = contract.instrumented.slice(0, leftOne); const leftEndLine = ( start.match(/\n/g) || [] ).length + 1; const leftEndCol = leftOne - start.lastIndexOf('\n') - 1; start = contract.instrumented.slice(0, rightZero); const rightStartLine = ( start.match(/\n/g) || [] ).length + 1; const rightStartCol = rightZero - start.lastIndexOf('\n') - 1; start = contract.instrumented.slice(0, rightOne); const rightEndLine = ( start.match(/\n/g) || [] ).length + 1; const rightEndCol = rightOne - start.lastIndexOf('\n') - 1; contract.branchId += 1; // NB locations for if branches in istanbul are zero // length and associated with the start of the if. contract.branchMap[contract.branchId] = { line: leftStartLine, type: 'cond-expr', locations: [{ start: { line: leftStartLine, column: leftStartCol, }, end: { line: leftEndLine, column: leftEndCol, }, }, { start: { line: rightStartLine, column: rightStartCol, }, end: { line: rightEndLine, column: rightEndCol, }, }], }; }; conditional(contract, expression){ if (!this.enabled.branches) return; this.addNewConditionalBranch(contract, expression); // Double open parens this._createInjectionPoint( contract, expression.condition.range[0], { type: 'injectOpenParen', } ); this._createInjectionPoint( contract, expression.condition.range[0], { type: 'injectOpenParen', } ); // False condition: (these get sorted in reverse order, so create in reversed order) this._createInjectionPoint( contract, expression.condition.range[1] + 1, { type: 'injectOrFalse', branchId: contract.branchId, locationIdx: 1 } ); // True condition this._createInjectionPoint( contract, expression.condition.range[1] + 1, { type: 'injectAndTrue', branchId: contract.branchId, locationIdx: 0 } ); } /** * Registers injections for logicalOR clause measurements (branches) * @param {Object} contract instrumentation target * @param {Object} expression AST node * @param {Number} injectionIdx pre/post branch index (left=0, right=1) */ logicalOR(contract, expression) { if (!this.enabled.branches) return; this.addNewLogicalORBranch(contract, expression); // Left this._createInjectionPoint( contract, expression.left.range[0], { type: 'injectOpenParen', } ); this._createInjectionPoint( contract, expression.left.range[1] + 1, { type: 'injectAndTrue', branchId: contract.branchId, locationIdx: 0 } ); // Right this._createInjectionPoint( contract, expression.right.range[0], { type: 'injectOpenParen', } ); this._createInjectionPoint( contract, expression.right.range[1] + 1, { type: 'injectAndTrue', branchId: contract.branchId, locationIdx: 1 } ); } /** * Registers injections for require statement measurements (branches) * @param {Object} contract instrumentation target * @param {Object} expression AST node */ requireBranch(contract, expression) { Iif (!this.enabled.branches) return; this.addNewBranch(contract, expression); this._createInjectionPoint( contract, expression.range[0], { type: 'injectRequirePre', branchId: contract.branchId, } ); this._createInjectionPoint( contract, expression.range[1] + 2, { type: 'injectRequirePost', branchId: contract.branchId, } ); }; /** * Registers injections for if statement measurements (branches) * @param {Object} contract instrumentation target * @param {Object} expression AST node */ ifStatement(contract, expression) { if (!this.enabled.branches) return; this.addNewBranch(contract, expression); if (expression.trueBody.type === 'Block') { this._createInjectionPoint( contract, expression.trueBody.range[0] + 1, { type: 'injectBranch', branchId: contract.branchId, locationIdx: 0, } ); } if (expression.falseBody && expression.falseBody.type === 'IfStatement') { // Do nothing - we must be pre-preprocessing } else if (expression.falseBody && expression.falseBody.type === 'Block') { this._createInjectionPoint( contract, expression.falseBody.range[0] + 1, { type: 'injectBranch', branchId: contract.branchId, locationIdx: 1, } ); } else { this._createInjectionPoint( contract, expression.trueBody.range[1] + 1, { type: 'injectEmptyBranch', branchId: contract.branchId, locationIdx: 1, } ); } } } module.exports = Registrar; |