Skip to content

Commit 8411a26

Browse files
authored
fix: Correct handling of source-maps for pre-instrumented files (#1216)
Fixes #1208
1 parent f890360 commit 8411a26

File tree

7 files changed

+399
-321
lines changed

7 files changed

+399
-321
lines changed

index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,14 @@ class NYC {
269269

270270
return (code, metadata, hash) => {
271271
const filename = metadata.filename
272-
let sourceMap = null
272+
const sourceMap = {}
273273

274-
if (this._sourceMap) sourceMap = this.sourceMaps.extractAndRegister(code, filename, hash)
274+
if (this._sourceMap) {
275+
sourceMap.sourceMap = this.sourceMaps.extract(code, filename)
276+
sourceMap.registerMap = () => this.sourceMaps.registerMap(filename, hash, sourceMap.sourceMap)
277+
} else {
278+
sourceMap.registerMap = () => {}
279+
}
275280

276281
try {
277282
instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
@@ -378,8 +383,6 @@ class NYC {
378383
coverage[absFile].contentHash = this.hashCache[absFile]
379384
}
380385
}, this)
381-
} else {
382-
this.sourceMaps.addSourceMaps(coverage)
383386
}
384387

385388
var id = this.processInfo.uuid

lib/instrumenters/istanbul.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
function InstrumenterIstanbul (options) {
44
const { createInstrumenter } = require('istanbul-lib-instrument')
55
const convertSourceMap = require('convert-source-map')
6-
const mergeSourceMap = require('merge-source-map')
76

87
const instrumenter = createInstrumenter({
98
autoWrap: true,
@@ -18,24 +17,22 @@ function InstrumenterIstanbul (options) {
1817
})
1918

2019
return {
21-
instrumentSync (code, filename, sourceMap) {
22-
var instrumented = instrumenter.instrumentSync(code, filename)
20+
instrumentSync (code, filename, { sourceMap, registerMap }) {
21+
var instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
22+
if (instrumented !== code) {
23+
registerMap()
24+
}
25+
2326
// the instrumenter can optionally produce source maps,
2427
// this is useful for features like remapping stack-traces.
25-
// TODO: test source-map merging logic.
2628
if (options.produceSourceMap) {
2729
var lastSourceMap = instrumenter.lastSourceMap()
2830
/* istanbul ignore else */
2931
if (lastSourceMap) {
30-
if (sourceMap) {
31-
lastSourceMap = mergeSourceMap(
32-
sourceMap.toObject(),
33-
lastSourceMap
34-
)
35-
}
3632
instrumented += '\n' + convertSourceMap.fromObject(lastSourceMap).toComment()
3733
}
3834
}
35+
3936
return instrumented
4037
},
4138
lastFileCoverage () {

lib/source-maps.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@ class SourceMaps {
2828
this.loadedMaps = {}
2929
}
3030

31-
extractAndRegister (code, filename, hash) {
31+
extract (code, filename) {
3232
const sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))
33-
if (sourceMap) {
34-
if (this.cache && hash) {
35-
const mapPath = this.cachedPath(filename, hash)
36-
fs.writeFileSync(mapPath, sourceMap.toJSON())
37-
} else {
38-
this._sourceMapCache.registerMap(filename, sourceMap.sourcemap)
39-
}
40-
}
41-
return sourceMap
33+
return sourceMap ? sourceMap.toObject() : undefined
4234
}
4335

44-
addSourceMaps (coverage) {
45-
this._sourceMapCache.addInputSourceMapsSync(coverage)
36+
registerMap (filename, hash, sourceMap) {
37+
if (!sourceMap) {
38+
return
39+
}
40+
41+
if (this.cache && hash) {
42+
const mapPath = this.cachedPath(filename, hash)
43+
fs.writeFileSync(mapPath, JSON.stringify(sourceMap))
44+
} else {
45+
this._sourceMapCache.registerMap(filename, sourceMap)
46+
}
4647
}
4748

4849
async remapCoverage (obj) {

0 commit comments

Comments
 (0)