index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _fs = require('fs');
  6. var _path = require('path');
  7. var _istanbulLibInstrument = require('istanbul-lib-instrument');
  8. var _babelPluginSyntaxObjectRestSpread = require('babel-plugin-syntax-object-rest-spread');
  9. var _babelPluginSyntaxObjectRestSpread2 = _interopRequireDefault(_babelPluginSyntaxObjectRestSpread);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. var testExclude = require('test-exclude');
  12. var findUp = require('find-up');
  13. function getRealpath(n) {
  14. try {
  15. return (0, _fs.realpathSync)(n) || n;
  16. } catch (e) {
  17. return n;
  18. }
  19. }
  20. function makeShouldSkip() {
  21. var exclude = void 0;
  22. return function shouldSkip(file, opts) {
  23. if (!exclude) {
  24. var cwd = getRealpath(process.env.NYC_CWD || process.cwd());
  25. var nycConfig = process.env.NYC_CONFIG ? JSON.parse(process.env.NYC_CONFIG) : {};
  26. var config = {};
  27. if (Object.keys(opts).length > 0) {
  28. // explicitly configuring options in babel
  29. // takes precedence.
  30. config = opts;
  31. } else if (nycConfig.include || nycConfig.exclude) {
  32. // nyc was configured in a parent process (keep these settings).
  33. config = {
  34. include: nycConfig.include,
  35. exclude: nycConfig.exclude
  36. };
  37. } else {
  38. // fallback to loading config from key in package.json.
  39. config = {
  40. configKey: 'nyc',
  41. configPath: (0, _path.dirname)(findUp.sync('package.json', { cwd: cwd }))
  42. };
  43. }
  44. exclude = testExclude(Object.assign({ cwd: cwd }, config));
  45. }
  46. return !exclude.shouldInstrument(file);
  47. };
  48. }
  49. function makeVisitor(_ref) {
  50. var t = _ref.types;
  51. var shouldSkip = makeShouldSkip();
  52. return {
  53. inherits: _babelPluginSyntaxObjectRestSpread2.default,
  54. visitor: {
  55. Program: {
  56. enter: function enter(path) {
  57. this.__dv__ = null;
  58. var realPath = getRealpath(this.file.opts.filename);
  59. if (shouldSkip(realPath, this.opts)) {
  60. return;
  61. }
  62. var inputSourceMap = this.opts.inputSourceMap;
  63. if (this.opts.useInlineSourceMaps !== false) {
  64. inputSourceMap = inputSourceMap || this.file.opts.inputSourceMap;
  65. }
  66. this.__dv__ = (0, _istanbulLibInstrument.programVisitor)(t, realPath, {
  67. coverageVariable: '__coverage__',
  68. inputSourceMap: inputSourceMap
  69. });
  70. this.__dv__.enter(path);
  71. },
  72. exit: function exit(path) {
  73. if (!this.__dv__) {
  74. return;
  75. }
  76. var result = this.__dv__.exit(path);
  77. if (this.opts.onCover) {
  78. this.opts.onCover(getRealpath(this.file.opts.filename), result.fileCoverage);
  79. }
  80. }
  81. }
  82. }
  83. };
  84. }
  85. exports.default = makeVisitor;