Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: introduced a bug that resulted in source-maps not being loaded properly on second run #566

Merged
merged 1 commit into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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