Skip to content

Commit 1bf74fd

Browse files
authored
fix: introduced a bug that resulted in source-maps not being loaded approriately on second test run (#566)
1 parent 55e826d commit 1bf74fd

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,17 @@ function NYC (config) {
8383

8484
this.processInfo = new ProcessInfo(config && config._processInfo)
8585
this.rootId = this.processInfo.root || this.generateUniqueID()
86+
87+
this.hashCache = {}
8688
}
8789

8890
NYC.prototype._createTransform = function (ext) {
91+
var _this = this
8992
var opts = {
9093
salt: Hash.salt,
9194
hash: function (code, metadata, salt) {
9295
var hash = Hash(code, metadata.filename)
96+
_this.hashCache[metadata.filename] = hash
9397
return hash
9498
},
9599
cacheDir: this.cacheDirectory,
@@ -267,7 +271,7 @@ NYC.prototype._transformFactory = function (cacheDir) {
267271
var filename = metadata.filename
268272
var sourceMap = null
269273

270-
if (_this._sourceMap) sourceMap = _this.sourceMaps.extractAndRegister(code, filename)
274+
if (_this._sourceMap) sourceMap = _this.sourceMaps.extractAndRegister(code, filename, hash)
271275

272276
try {
273277
instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
@@ -365,8 +369,8 @@ NYC.prototype.writeCoverageFile = function () {
365369

366370
if (this.cache) {
367371
Object.keys(coverage).forEach(function (absFile) {
368-
if (this.sourceMaps.hashCache[absFile] && coverage[absFile]) {
369-
coverage[absFile].contentHash = this.sourceMaps.hashCache[absFile]
372+
if (this.hashCache[absFile] && coverage[absFile]) {
373+
coverage[absFile].contentHash = this.hashCache[absFile]
370374
}
371375
}, this)
372376
} else {

lib/source-maps.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const convertSourceMap = require('convert-source-map')
22
const libCoverage = require('istanbul-lib-coverage')
33
const libSourceMaps = require('istanbul-lib-source-maps')
44
const fs = require('fs')
5-
const Hash = require('./hash')
65
const path = require('path')
76

87
// TODO: write some unit tests for this class.
@@ -11,15 +10,12 @@ function SourceMaps (opts) {
1110
this.cacheDirectory = opts.cacheDirectory
1211
this.sourceMapCache = libSourceMaps.createSourceMapStore()
1312
this.loadedMaps = {}
14-
this.hashCache = {}
1513
}
1614

17-
SourceMaps.prototype.extractAndRegister = function (code, filename) {
15+
SourceMaps.prototype.extractAndRegister = function (code, filename, hash) {
1816
var sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))
1917
if (sourceMap) {
20-
var hash = Hash(code, filename)
21-
if (this.cache && !this.hashCache[filename]) {
22-
this.hashCache[filename] = hash
18+
if (this.cache && hash) {
2319
var mapPath = path.join(this.cacheDirectory, hash + '.map')
2420
fs.writeFileSync(mapPath, sourceMap.toJSON())
2521
} else {

0 commit comments

Comments
 (0)