all files / lib/ MappingProvider.js

98.25% Statements 56/57
93.75% Branches 30/32
100% Functions 9/9
98.21% Lines 55/56
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                          15×   15× 15×   15×         15× 12×   11×     15×         14×   14×     14× 14× 14×     13×     14× 14× 14×   14×     12×         12× 12×     12×     12×         12× 606×            
'use strict';
 
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
 
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
 
var _createClass2 = require('babel-runtime/helpers/createClass');
 
var _createClass3 = _interopRequireDefault(_createClass2);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
var path = require('path');
var fs = require('fs');
 
var _require = require('source-map');
 
var SourceMapConsumer = _require.SourceMapConsumer;
 
 
var sourceMapRegEx = /(?:\/{2}[#@]{1,2}|\/\*)\s+sourceMappingURL\s*=\s*(data:(?:[^;]+;)+base64,)?(\S+)/;
 
var getMapping = require('./getMapping');
 
var MappingProvider = function () {
  function MappingProvider(options, sparceCoverageCollector) {
    (0, _classCallCheck3.default)(this, MappingProvider);
 
    this.sparceCoverageCollector = sparceCoverageCollector;
    this.warn = options.warn || console.warn;
 
    this.readJSON = options.readJSON || function readJSON(filePath) {
      Iif (!fs.existsSync(filePath)) {
        throw new Error('Could not find file: "' + filePath + '"');
      }
      return JSON.parse(fs.readFileSync(filePath));
    };
 
    this.readFile = options.readFile || function readFile(filePath) {
      if (!fs.existsSync(filePath)) {
        this.warn(new Error('Could not find file: "' + filePath + '"'));
        return '';
      }
      return fs.readFileSync(filePath);
    };
 
    this.sourceStore = options.sources;
  }
 
  (0, _createClass3.default)(MappingProvider, [{
    key: 'getMappingResolver',
    value: function getMappingResolver(filePath) {
      var _this = this;
 
      var code = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
 
      /* coverage.json can sometimes include the code inline */
      var codeIsArray = true;
      var jsText = code || this.readFile(filePath);
      if (Array.isArray(jsText)) {
        /* sometimes the source is an array */
        jsText = jsText.join('\n');
      } else {
        codeIsArray = false;
      }
 
      var match = sourceMapRegEx.exec(jsText);
      var sourceMapDir = path.dirname(filePath);
      var rawSourceMap = void 0;
 
      if (!match) {
        return false;
      }
 
      if (match[1]) {
        rawSourceMap = JSON.parse(new Buffer(match[2], 'base64').toString('utf8'));
      } else {
        var sourceMapPath = path.join(sourceMapDir, match[2]);
        rawSourceMap = this.readJSON(sourceMapPath);
        sourceMapDir = path.dirname(sourceMapPath);
      }
 
      // replace relative paths in source maps with absolute
      rawSourceMap.sources = rawSourceMap.sources.map(function (srcPath) {
        return path.resolve(sourceMapDir, srcPath);
      });
 
      var sourceMap = new SourceMapConsumer(rawSourceMap);
 
      /* if there are inline sources and a store to put them into, we will populate it */
      if (sourceMap.sourcesContent) {
        sourceMap.sourcesContent.forEach(function (source, idx) {
          _this.sparceCoverageCollector.setSourceCode(sourceMap.sources[idx], codeIsArray ? source.split('\n') : source);
          if (_this.sourceStore) {
            _this.sourceStore.set(sourceMap.sources[idx], source);
          }
        });
      }
 
      return function (location) {
        return getMapping(sourceMap, location);
      };
    }
  }]);
  return MappingProvider;
}();
 
module.exports.MappingProvider = MappingProvider;
//# sourceMappingURL=MappingProvider.js.map