Skip to content

Commit

Permalink
Merge pull request #66 from novemberborn/improve-source-maps
Browse files Browse the repository at this point in the history
Improve source maps
  • Loading branch information
bcoe committed Dec 6, 2015
2 parents 361ad44 + 026859f commit 854be93
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ NYC.prototype.addFile = function (filename, returnImmediately) {
var content = stripBom(fs.readFileSync(filename, 'utf8'))

if (instrument) {
this.sourceMapCache.add(filename, content)
content = this.instrumenter.instrumentSync(content, './' + relFile)
} else if (returnImmediately) {
return {}
Expand Down
25 changes: 20 additions & 5 deletions lib/source-map-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function SourceMapCache (opts) {
}

SourceMapCache.prototype.add = function (filename, source) {
var sourceMap = convertSourceMap.fromSource(source)
var sourceMap = convertSourceMap.fromSource(source) || convertSourceMap.fromMapFileSource(source, path.dirname(filename))
if (sourceMap) this.cache['./' + path.relative(this.cwd, filename)] = new SourceMapConsumer(sourceMap.sourcemap)
}

Expand All @@ -20,16 +20,31 @@ SourceMapCache.prototype.applySourceMaps = function (coverage) {
var mappedCoverage = _.cloneDeep(coverage)

Object.keys(coverage).forEach(function (key) {
if (_this.cache[key]) {
_this._rewriteStatements(mappedCoverage[key], _this.cache[key])
_this._rewriteFunctions(mappedCoverage[key], _this.cache[key])
_this._rewriteBranches(mappedCoverage[key], _this.cache[key])
var sourceMap = _this.cache[key]
if (!sourceMap) {
return
}

var fileCoverage = mappedCoverage[key]
_this._rewritePath(mappedCoverage, fileCoverage, sourceMap)
_this._rewriteStatements(fileCoverage, sourceMap)
_this._rewriteFunctions(fileCoverage, sourceMap)
_this._rewriteBranches(fileCoverage, sourceMap)
})

return mappedCoverage
}

SourceMapCache.prototype._rewritePath = function (mappedCoverage, coverage, sourceMap) {
// only rewrite the path if the file comes from a single source
if (sourceMap.sources.length !== 1) return

var originalPath = './' + path.join(path.dirname(coverage.path), sourceMap.sources[0])
delete mappedCoverage[coverage.path]
coverage.path = originalPath
mappedCoverage[originalPath] = coverage
}

SourceMapCache.prototype._rewriteStatements = function (coverage, sourceMap) {
var start = null
var end = null
Expand Down

0 comments on commit 854be93

Please sign in to comment.