Skip to content

Commit

Permalink
fix: introduced a bug that resulted in source-maps not being loaded a…
Browse files Browse the repository at this point in the history
…pproriately on second test run (#566)
  • Loading branch information
bcoe committed May 4, 2017
1 parent 55e826d commit 1bf74fd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 7 additions & 3 deletions index.js
Expand Up @@ -83,13 +83,17 @@ function NYC (config) {

this.processInfo = new ProcessInfo(config && config._processInfo)
this.rootId = this.processInfo.root || this.generateUniqueID()

this.hashCache = {}
}

NYC.prototype._createTransform = function (ext) {
var _this = this
var opts = {
salt: Hash.salt,
hash: function (code, metadata, salt) {
var hash = Hash(code, metadata.filename)
_this.hashCache[metadata.filename] = hash
return hash
},
cacheDir: this.cacheDirectory,
Expand Down Expand Up @@ -267,7 +271,7 @@ NYC.prototype._transformFactory = function (cacheDir) {
var filename = metadata.filename
var sourceMap = null

if (_this._sourceMap) sourceMap = _this.sourceMaps.extractAndRegister(code, filename)
if (_this._sourceMap) sourceMap = _this.sourceMaps.extractAndRegister(code, filename, hash)

try {
instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
Expand Down Expand Up @@ -365,8 +369,8 @@ NYC.prototype.writeCoverageFile = function () {

if (this.cache) {
Object.keys(coverage).forEach(function (absFile) {
if (this.sourceMaps.hashCache[absFile] && coverage[absFile]) {
coverage[absFile].contentHash = this.sourceMaps.hashCache[absFile]
if (this.hashCache[absFile] && coverage[absFile]) {
coverage[absFile].contentHash = this.hashCache[absFile]
}
}, this)
} else {
Expand Down
8 changes: 2 additions & 6 deletions lib/source-maps.js
Expand Up @@ -2,7 +2,6 @@ const convertSourceMap = require('convert-source-map')
const libCoverage = require('istanbul-lib-coverage')
const libSourceMaps = require('istanbul-lib-source-maps')
const fs = require('fs')
const Hash = require('./hash')
const path = require('path')

// TODO: write some unit tests for this class.
Expand All @@ -11,15 +10,12 @@ function SourceMaps (opts) {
this.cacheDirectory = opts.cacheDirectory
this.sourceMapCache = libSourceMaps.createSourceMapStore()
this.loadedMaps = {}
this.hashCache = {}
}

SourceMaps.prototype.extractAndRegister = function (code, filename) {
SourceMaps.prototype.extractAndRegister = function (code, filename, hash) {
var sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))
if (sourceMap) {
var hash = Hash(code, filename)
if (this.cache && !this.hashCache[filename]) {
this.hashCache[filename] = hash
if (this.cache && hash) {
var mapPath = path.join(this.cacheDirectory, hash + '.map')
fs.writeFileSync(mapPath, sourceMap.toJSON())
} else {
Expand Down

0 comments on commit 1bf74fd

Please sign in to comment.