1076edfb744a08e6de1ef34630b8dc6077227e72.svn-base 19.7 KB
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 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _crypto = require("crypto");

var _template = _interopRequireDefault(require("@babel/template"));

var _sourceCoverage = require("./source-coverage");

var _constants = require("./constants");

var _instrumenter = require("./instrumenter");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// pattern for istanbul to ignore a section
const COMMENT_RE = /^\s*istanbul\s+ignore\s+(if|else|next)(?=\W|$)/; // pattern for istanbul to ignore the whole file

const COMMENT_FILE_RE = /^\s*istanbul\s+ignore\s+(file)(?=\W|$)/; // source map URL pattern

const SOURCE_MAP_RE = /[#@]\s*sourceMappingURL=(.*)\s*$/m; // generate a variable name from hashing the supplied file path

function genVar(filename) {
  const hash = (0, _crypto.createHash)(_constants.SHA);
  hash.update(filename);
  return 'cov_' + parseInt(hash.digest('hex').substr(0, 12), 16).toString(36);
} // VisitState holds the state of the visitor, provides helper functions
// and is the `this` for the individual coverage visitors.


class VisitState {
  constructor(types, sourceFilePath, inputSourceMap, ignoreClassMethods = []) {
    this.varName = genVar(sourceFilePath);
    this.attrs = {};
    this.nextIgnore = null;
    this.cov = new _sourceCoverage.SourceCoverage(sourceFilePath);

    if (typeof inputSourceMap !== 'undefined') {
      this.cov.inputSourceMap(inputSourceMap);
    }

    this.ignoreClassMethods = ignoreClassMethods;
    this.types = types;
    this.sourceMappingURL = null;
  } // should we ignore the node? Yes, if specifically ignoring
  // or if the node is generated.


  shouldIgnore(path) {
    return this.nextIgnore || !path.node.loc;
  } // extract the ignore comment hint (next|if|else) or null


  hintFor(node) {
    let hint = null;

    if (node.leadingComments) {
      node.leadingComments.forEach(c => {
        const v = (c.value ||
        /* istanbul ignore next: paranoid check */
        '').trim();
        const groups = v.match(COMMENT_RE);

        if (groups) {
          hint = groups[1];
        }
      });
    }

    return hint;
  } // extract a source map URL from comments and keep track of it


  maybeAssignSourceMapURL(node) {
    const extractURL = comments => {
      if (!comments) {
        return;
      }

      comments.forEach(c => {
        const v = (c.value ||
        /* istanbul ignore next: paranoid check */
        '').trim();
        const groups = v.match(SOURCE_MAP_RE);

        if (groups) {
          this.sourceMappingURL = groups[1];
        }
      });
    };

    extractURL(node.leadingComments);
    extractURL(node.trailingComments);
  } // for these expressions the statement counter needs to be hoisted, so
  // function name inference can be preserved


  counterNeedsHoisting(path) {
    return path.isFunctionExpression() || path.isArrowFunctionExpression() || path.isClassExpression();
  } // all the generic stuff that needs to be done on enter for every node


  onEnter(path) {
    const n = path.node;
    this.maybeAssignSourceMapURL(n); // if already ignoring, nothing more to do

    if (this.nextIgnore !== null) {
      return;
    } // check hint to see if ignore should be turned on


    const hint = this.hintFor(n);

    if (hint === 'next') {
      this.nextIgnore = n;
      return;
    } // else check custom node attribute set by a prior visitor


    if (this.getAttr(path.node, 'skip-all') !== null) {
      this.nextIgnore = n;
    } // else check for ignored class methods


    if (path.isFunctionExpression() && this.ignoreClassMethods.some(name => path.node.id && name === path.node.id.name)) {
      this.nextIgnore = n;
      return;
    }

    if (path.isClassMethod() && this.ignoreClassMethods.some(name => name === path.node.key.name)) {
      this.nextIgnore = n;
      return;
    }
  } // all the generic stuff on exit of a node,
  // including reseting ignores and custom node attrs


  onExit(path) {
    // restore ignore status, if needed
    if (path.node === this.nextIgnore) {
      this.nextIgnore = null;
    } // nuke all attributes for the node


    delete path.node.__cov__;
  } // set a node attribute for the supplied node


  setAttr(node, name, value) {
    node.__cov__ = node.__cov__ || {};
    node.__cov__[name] = value;
  } // retrieve a node attribute for the supplied node or null


  getAttr(node, name) {
    const c = node.__cov__;

    if (!c) {
      return null;
    }

    return c[name];
  } //


  increase(type, id, index) {
    const T = this.types;
    const wrap = index !== null ? // If `index` present, turn `x` into `x[index]`.
    x => T.memberExpression(x, T.numericLiteral(index), true) : x => x;
    return T.updateExpression('++', wrap(T.memberExpression(T.memberExpression(T.identifier(this.varName), T.identifier(type)), T.numericLiteral(id), true)));
  }

  insertCounter(path, increment) {
    const T = this.types;

    if (path.isBlockStatement()) {
      path.node.body.unshift(T.expressionStatement(increment));
    } else if (path.isStatement()) {
      path.insertBefore(T.expressionStatement(increment));
    } else if (this.counterNeedsHoisting(path) && T.isVariableDeclarator(path.parentPath)) {
      // make an attempt to hoist the statement counter, so that
      // function names are maintained.
      const parent = path.parentPath.parentPath;

      if (parent && T.isExportNamedDeclaration(parent.parentPath)) {
        parent.parentPath.insertBefore(T.expressionStatement(increment));
      } else if (parent && (T.isProgram(parent.parentPath) || T.isBlockStatement(parent.parentPath))) {
        parent.insertBefore(T.expressionStatement(increment));
      } else {
        path.replaceWith(T.sequenceExpression([increment, path.node]));
      }
    }
    /* istanbul ignore else: not expected */
    else if (path.isExpression()) {
        path.replaceWith(T.sequenceExpression([increment, path.node]));
      } else {
        console.error('Unable to insert counter for node type:', path.node.type);
      }
  }

  insertStatementCounter(path) {
    /* istanbul ignore if: paranoid check */
    if (!(path.node && path.node.loc)) {
      return;
    }

    const index = this.cov.newStatement(path.node.loc);
    const increment = this.increase('s', index, null);
    this.insertCounter(path, increment);
  }

  insertFunctionCounter(path) {
    const T = this.types;
    /* istanbul ignore if: paranoid check */

    if (!(path.node && path.node.loc)) {
      return;
    }

    const n = path.node;
    let dloc = null; // get location for declaration

    switch (n.type) {
      case 'FunctionDeclaration':
        /* istanbul ignore else: paranoid check */
        if (n.id) {
          dloc = n.id.loc;
        }

        break;

      case 'FunctionExpression':
        if (n.id) {
          dloc = n.id.loc;
        }

        break;
    }

    if (!dloc) {
      dloc = {
        start: n.loc.start,
        end: {
          line: n.loc.start.line,
          column: n.loc.start.column + 1
        }
      };
    }

    const name = path.node.id ? path.node.id.name : path.node.name;
    const index = this.cov.newFunction(name, dloc, path.node.body.loc);
    const increment = this.increase('f', index, null);
    const body = path.get('body');
    /* istanbul ignore else: not expected */

    if (body.isBlockStatement()) {
      body.node.body.unshift(T.expressionStatement(increment));
    } else {
      console.error('Unable to process function body node type:', path.node.type);
    }
  }

  getBranchIncrement(branchName, loc) {
    const index = this.cov.addBranchPath(branchName, loc);
    return this.increase('b', branchName, index);
  }

  insertBranchCounter(path, branchName, loc) {
    const increment = this.getBranchIncrement(branchName, loc || path.node.loc);
    this.insertCounter(path, increment);
  }

  findLeaves(node, accumulator, parent, property) {
    if (!node) {
      return;
    }

    if (node.type === 'LogicalExpression') {
      const hint = this.hintFor(node);

      if (hint !== 'next') {
        this.findLeaves(node.left, accumulator, node, 'left');
        this.findLeaves(node.right, accumulator, node, 'right');
      }
    } else {
      accumulator.push({
        node,
        parent,
        property
      });
    }
  }

} // generic function that takes a set of visitor methods and
// returns a visitor object with `enter` and `exit` properties,
// such that:
//
// * standard entry processing is done
// * the supplied visitors are called only when ignore is not in effect
//   This relieves them from worrying about ignore states and generated nodes.
// * standard exit processing is done
//


function entries(...enter) {
  // the enter function
  const wrappedEntry = function wrappedEntry(path, node) {
    this.onEnter(path);

    if (this.shouldIgnore(path)) {
      return;
    }

    enter.forEach(e => {
      e.call(this, path, node);
    });
  };

  const exit = function exit(path, node) {
    this.onExit(path, node);
  };

  return {
    enter: wrappedEntry,
    exit
  };
}

function coverStatement(path) {
  this.insertStatementCounter(path);
}
/* istanbul ignore next: no node.js support */


function coverAssignmentPattern(path) {
  const n = path.node;
  const b = this.cov.newBranch('default-arg', n.loc);
  this.insertBranchCounter(path.get('right'), b);
}

function coverFunction(path) {
  this.insertFunctionCounter(path);
}

function coverVariableDeclarator(path) {
  this.insertStatementCounter(path.get('init'));
}

function coverClassPropDeclarator(path) {
  this.insertStatementCounter(path.get('value'));
}

function makeBlock(path) {
  const T = this.types;

  if (!path.node) {
    path.replaceWith(T.blockStatement([]));
  }

  if (!path.isBlockStatement()) {
    path.replaceWith(T.blockStatement([path.node]));
    path.node.loc = path.node.body[0].loc;
  }
}

function blockProp(prop) {
  return function (path) {
    makeBlock.call(this, path.get(prop));
  };
}

function makeParenthesizedExpressionForNonIdentifier(path) {
  const T = this.types;

  if (path.node && !path.isIdentifier()) {
    path.replaceWith(T.parenthesizedExpression(path.node));
  }
}

function parenthesizedExpressionProp(prop) {
  return function (path) {
    makeParenthesizedExpressionForNonIdentifier.call(this, path.get(prop));
  };
}

function convertArrowExpression(path) {
  const n = path.node;
  const T = this.types;

  if (!T.isBlockStatement(n.body)) {
    const bloc = n.body.loc;

    if (n.expression === true) {
      n.expression = false;
    }

    n.body = T.blockStatement([T.returnStatement(n.body)]); // restore body location

    n.body.loc = bloc; // set up the location for the return statement so it gets
    // instrumented

    n.body.body[0].loc = bloc;
  }
}

function coverIfBranches(path) {
  const n = path.node;
  const hint = this.hintFor(n);
  const ignoreIf = hint === 'if';
  const ignoreElse = hint === 'else';
  const branch = this.cov.newBranch('if', n.loc);

  if (ignoreIf) {
    this.setAttr(n.consequent, 'skip-all', true);
  } else {
    this.insertBranchCounter(path.get('consequent'), branch, n.loc);
  }

  if (ignoreElse) {
    this.setAttr(n.alternate, 'skip-all', true);
  } else {
    this.insertBranchCounter(path.get('alternate'), branch, n.loc);
  }
}

function createSwitchBranch(path) {
  const b = this.cov.newBranch('switch', path.node.loc);
  this.setAttr(path.node, 'branchName', b);
}

function coverSwitchCase(path) {
  const T = this.types;
  const b = this.getAttr(path.parentPath.node, 'branchName');
  /* istanbul ignore if: paranoid check */

  if (b === null) {
    throw new Error('Unable to get switch branch name');
  }

  const increment = this.getBranchIncrement(b, path.node.loc);
  path.node.consequent.unshift(T.expressionStatement(increment));
}

function coverTernary(path) {
  const n = path.node;
  const branch = this.cov.newBranch('cond-expr', path.node.loc);
  const cHint = this.hintFor(n.consequent);
  const aHint = this.hintFor(n.alternate);

  if (cHint !== 'next') {
    this.insertBranchCounter(path.get('consequent'), branch);
  }

  if (aHint !== 'next') {
    this.insertBranchCounter(path.get('alternate'), branch);
  }
}

function coverLogicalExpression(path) {
  const T = this.types;

  if (path.parentPath.node.type === 'LogicalExpression') {
    return; // already processed
  }

  const leaves = [];
  this.findLeaves(path.node, leaves);
  const b = this.cov.newBranch('binary-expr', path.node.loc);

  for (let i = 0; i < leaves.length; i += 1) {
    const leaf = leaves[i];
    const hint = this.hintFor(leaf.node);

    if (hint === 'next') {
      continue;
    }

    const increment = this.getBranchIncrement(b, leaf.node.loc);

    if (!increment) {
      continue;
    }

    leaf.parent[leaf.property] = T.sequenceExpression([increment, leaf.node]);
  }
}

const codeVisitor = {
  ArrowFunctionExpression: entries(convertArrowExpression, coverFunction),
  AssignmentPattern: entries(coverAssignmentPattern),
  BlockStatement: entries(),
  // ignore processing only
  ExportDefaultDeclaration: entries(),
  // ignore processing only
  ExportNamedDeclaration: entries(),
  // ignore processing only
  ClassMethod: entries(coverFunction),
  ClassDeclaration: entries(parenthesizedExpressionProp('superClass')),
  ClassProperty: entries(coverClassPropDeclarator),
  ClassPrivateProperty: entries(coverClassPropDeclarator),
  ObjectMethod: entries(coverFunction),
  ExpressionStatement: entries(coverStatement),
  BreakStatement: entries(coverStatement),
  ContinueStatement: entries(coverStatement),
  DebuggerStatement: entries(coverStatement),
  ReturnStatement: entries(coverStatement),
  ThrowStatement: entries(coverStatement),
  TryStatement: entries(coverStatement),
  VariableDeclaration: entries(),
  // ignore processing only
  VariableDeclarator: entries(coverVariableDeclarator),
  IfStatement: entries(blockProp('consequent'), blockProp('alternate'), coverStatement, coverIfBranches),
  ForStatement: entries(blockProp('body'), coverStatement),
  ForInStatement: entries(blockProp('body'), coverStatement),
  ForOfStatement: entries(blockProp('body'), coverStatement),
  WhileStatement: entries(blockProp('body'), coverStatement),
  DoWhileStatement: entries(blockProp('body'), coverStatement),
  SwitchStatement: entries(createSwitchBranch, coverStatement),
  SwitchCase: entries(coverSwitchCase),
  WithStatement: entries(blockProp('body'), coverStatement),
  FunctionDeclaration: entries(coverFunction),
  FunctionExpression: entries(coverFunction),
  LabeledStatement: entries(coverStatement),
  ConditionalExpression: entries(coverTernary),
  LogicalExpression: entries(coverLogicalExpression)
};
const globalTemplateAlteredFunction = (0, _template.default)(`
        var Function = (function(){}).constructor;
        var global = (new Function(GLOBAL_COVERAGE_SCOPE))();
`);
const globalTemplateFunction = (0, _template.default)(`
        var global = (new Function(GLOBAL_COVERAGE_SCOPE))();
`);
const globalTemplateVariable = (0, _template.default)(`
        var global = GLOBAL_COVERAGE_SCOPE;
`); // the template to insert at the top of the program.

const coverageTemplate = (0, _template.default)(`
    var COVERAGE_VAR = (function () {
        var path = PATH;
        var hash = HASH;
        GLOBAL_COVERAGE_TEMPLATE
        var gcv = GLOBAL_COVERAGE_VAR;
        var coverageData = INITIAL;
        var coverage = global[gcv] || (global[gcv] = {});
        if (coverage[path] && coverage[path].hash === hash) {
            return coverage[path];
        }
        return coverage[path] = coverageData;
    })();
`); // the rewire plugin (and potentially other babel middleware)
// may cause files to be instrumented twice, see:
// https://github.com/istanbuljs/babel-plugin-istanbul/issues/94
// we should only instrument code for coverage the first time
// it's run through istanbul-lib-instrument.

function alreadyInstrumented(path, visitState) {
  return path.scope.hasBinding(visitState.varName);
}

function shouldIgnoreFile(programNode) {
  return programNode.parent && programNode.parent.comments.some(c => COMMENT_FILE_RE.test(c.value));
}

const defaultProgramVisitorOpts = {
  inputSourceMap: undefined
};
/**
 * programVisitor is a `babel` adaptor for instrumentation.
 * It returns an object with two methods `enter` and `exit`.
 * These should be assigned to or called from `Program` entry and exit functions
 * in a babel visitor.
 * These functions do not make assumptions about the state set by Babel and thus
 * can be used in a context other than a Babel plugin.
 *
 * The exit function returns an object that currently has the following keys:
 *
 * `fileCoverage` - the file coverage object created for the source file.
 * `sourceMappingURL` - any source mapping URL found when processing the file.
 *
 * @param {Object} types - an instance of babel-types
 * @param {string} sourceFilePath - the path to source file
 * @param {Object} opts - additional options
 * @param {string} [opts.coverageVariable=__coverage__] the global coverage variable name.
 * @param {string} [opts.coverageGlobalScope=this] the global coverage variable scope.
 * @param {boolean} [opts.coverageGlobalScopeFunc=true] use an evaluated function to find coverageGlobalScope.
 * @param {Array} [opts.ignoreClassMethods=[]] names of methods to ignore by default on classes.
 * @param {object} [opts.inputSourceMap=undefined] the input source map, that maps the uninstrumented code back to the
 * original code.
 */

function programVisitor(types, sourceFilePath = 'unknown.js', opts = defaultProgramVisitorOpts) {
  const T = types; // This sets some unused options but ensures all required options are initialized

  opts = Object.assign({}, (0, _instrumenter.defaultOpts)(), defaultProgramVisitorOpts, opts);
  const visitState = new VisitState(types, sourceFilePath, opts.inputSourceMap, opts.ignoreClassMethods);
  return {
    enter(path) {
      if (shouldIgnoreFile(path.find(p => p.isProgram()))) {
        return;
      }

      if (alreadyInstrumented(path, visitState)) {
        return;
      }

      path.traverse(codeVisitor, visitState);
    },

    exit(path) {
      if (alreadyInstrumented(path, visitState)) {
        return;
      }

      visitState.cov.freeze();
      const coverageData = visitState.cov.toJSON();

      if (shouldIgnoreFile(path.find(p => p.isProgram()))) {
        return {
          fileCoverage: coverageData,
          sourceMappingURL: visitState.sourceMappingURL
        };
      }

      coverageData[_constants.MAGIC_KEY] = _constants.MAGIC_VALUE;
      const hash = (0, _crypto.createHash)(_constants.SHA).update(JSON.stringify(coverageData)).digest('hex');
      coverageData.hash = hash;
      const coverageNode = T.valueToNode(coverageData);
      delete coverageData[_constants.MAGIC_KEY];
      delete coverageData.hash;
      let gvTemplate;

      if (opts.coverageGlobalScopeFunc) {
        if (path.scope.getBinding('Function')) {
          gvTemplate = globalTemplateAlteredFunction({
            GLOBAL_COVERAGE_SCOPE: T.stringLiteral('return ' + opts.coverageGlobalScope)
          });
        } else {
          gvTemplate = globalTemplateFunction({
            GLOBAL_COVERAGE_SCOPE: T.stringLiteral('return ' + opts.coverageGlobalScope)
          });
        }
      } else {
        gvTemplate = globalTemplateVariable({
          GLOBAL_COVERAGE_SCOPE: opts.coverageGlobalScope
        });
      }

      const cv = coverageTemplate({
        GLOBAL_COVERAGE_VAR: T.stringLiteral(opts.coverageVariable),
        GLOBAL_COVERAGE_TEMPLATE: gvTemplate,
        COVERAGE_VAR: T.identifier(visitState.varName),
        PATH: T.stringLiteral(sourceFilePath),
        INITIAL: coverageNode,
        HASH: T.stringLiteral(hash)
      });
      cv._blockHoist = 5;
      path.node.body.unshift(cv);
      return {
        fileCoverage: coverageData,
        sourceMappingURL: visitState.sourceMappingURL
      };
    }

  };
}

var _default = programVisitor;
exports.default = _default;