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: Correct handling of source-maps for pre-instrumented files #1216

Merged
merged 2 commits into from Nov 1, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions index.js
Expand Up @@ -269,9 +269,14 @@ class NYC {

return (code, metadata, hash) => {
const filename = metadata.filename
let sourceMap = null
const sourceMap = {}

if (this._sourceMap) sourceMap = this.sourceMaps.extractAndRegister(code, filename, hash)
if (this._sourceMap) {
sourceMap.sourceMap = this.sourceMaps.extract(code, filename)
sourceMap.registerMap = () => this.sourceMaps.registerMap(filename, hash, sourceMap.sourceMap)
} else {
sourceMap.registerMap = () => {}
}

try {
instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
Expand Down Expand Up @@ -378,8 +383,6 @@ class NYC {
coverage[absFile].contentHash = this.hashCache[absFile]
}
}, this)
} else {
this.sourceMaps.addSourceMaps(coverage)
}

var id = this.processInfo.uuid
Expand Down
17 changes: 7 additions & 10 deletions lib/instrumenters/istanbul.js
Expand Up @@ -3,7 +3,6 @@
function InstrumenterIstanbul (options) {
const { createInstrumenter } = require('istanbul-lib-instrument')
const convertSourceMap = require('convert-source-map')
const mergeSourceMap = require('merge-source-map')

const instrumenter = createInstrumenter({
autoWrap: true,
Expand All @@ -18,24 +17,22 @@ function InstrumenterIstanbul (options) {
})

return {
instrumentSync (code, filename, sourceMap) {
var instrumented = instrumenter.instrumentSync(code, filename)
instrumentSync (code, filename, { sourceMap, registerMap }) {
var instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
if (instrumented !== code) {
registerMap()
}

// the instrumenter can optionally produce source maps,
// this is useful for features like remapping stack-traces.
// TODO: test source-map merging logic.
if (options.produceSourceMap) {
var lastSourceMap = instrumenter.lastSourceMap()
/* istanbul ignore else */
if (lastSourceMap) {
if (sourceMap) {
lastSourceMap = mergeSourceMap(
sourceMap.toObject(),
lastSourceMap
)
}
instrumented += '\n' + convertSourceMap.fromObject(lastSourceMap).toComment()
}
}

return instrumented
},
lastFileCoverage () {
Expand Down
25 changes: 13 additions & 12 deletions lib/source-maps.js
Expand Up @@ -28,21 +28,22 @@ class SourceMaps {
this.loadedMaps = {}
}

extractAndRegister (code, filename, hash) {
extract (code, filename) {
const sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))
if (sourceMap) {
if (this.cache && hash) {
const mapPath = this.cachedPath(filename, hash)
fs.writeFileSync(mapPath, sourceMap.toJSON())
} else {
this._sourceMapCache.registerMap(filename, sourceMap.sourcemap)
}
}
return sourceMap
return sourceMap ? sourceMap.toObject() : undefined
}

addSourceMaps (coverage) {
this._sourceMapCache.addInputSourceMapsSync(coverage)
registerMap (filename, hash, sourceMap) {
if (!sourceMap) {
return
}

if (this.cache && hash) {
const mapPath = this.cachedPath(filename, hash)
fs.writeFileSync(mapPath, JSON.stringify(sourceMap))
} else {
this._sourceMapCache.registerMap(filename, sourceMap)
}
}

async remapCoverage (obj) {
Expand Down