index.js 2.9 KB

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