Skip to content

Commit 18e04ba

Browse files
bcoecoreyfarrell
authored andcommitted
fix: make --all work for transpiled code (#1047)
* cache source maps globally
1 parent b7e16cd commit 18e04ba

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

lib/source-maps.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ const libSourceMaps = require('istanbul-lib-source-maps')
44
const fs = require('fs')
55
const path = require('path')
66

7-
// TODO: write some unit tests for this class.
7+
const sourceMapCache = libSourceMaps.createSourceMapStore()
88
function SourceMaps (opts) {
99
this.cache = opts.cache
1010
this.cacheDirectory = opts.cacheDirectory
11-
this.sourceMapCache = libSourceMaps.createSourceMapStore()
1211
this.loadedMaps = {}
12+
this._sourceMapCache = sourceMapCache
1313
}
1414

1515
SourceMaps.prototype.extractAndRegister = function (code, filename, hash) {
@@ -19,36 +19,35 @@ SourceMaps.prototype.extractAndRegister = function (code, filename, hash) {
1919
var mapPath = path.join(this.cacheDirectory, hash + '.map')
2020
fs.writeFileSync(mapPath, sourceMap.toJSON())
2121
} else {
22-
this.sourceMapCache.registerMap(filename, sourceMap.sourcemap)
22+
this._sourceMapCache.registerMap(filename, sourceMap.sourcemap)
2323
}
2424
}
2525
return sourceMap
2626
}
2727

2828
SourceMaps.prototype.remapCoverage = function (obj) {
29-
var transformed = this.sourceMapCache.transformCoverage(
29+
var transformed = this._sourceMapCache.transformCoverage(
3030
libCoverage.createCoverageMap(obj)
3131
)
3232
return transformed.map.data
3333
}
3434

3535
SourceMaps.prototype.reloadCachedSourceMaps = function (report) {
36-
var _this = this
37-
Object.keys(report).forEach(function (absFile) {
36+
Object.keys(report).forEach((absFile) => {
3837
var fileReport = report[absFile]
3938
if (fileReport && fileReport.contentHash) {
4039
var hash = fileReport.contentHash
41-
if (!(hash in _this.loadedMaps)) {
40+
if (!(hash in this.loadedMaps)) {
4241
try {
43-
var mapPath = path.join(_this.cacheDirectory, hash + '.map')
44-
_this.loadedMaps[hash] = JSON.parse(fs.readFileSync(mapPath, 'utf8'))
42+
var mapPath = path.join(this.cacheDirectory, hash + '.map')
43+
this.loadedMaps[hash] = JSON.parse(fs.readFileSync(mapPath, 'utf8'))
4544
} catch (e) {
4645
// set to false to avoid repeatedly trying to load the map
47-
_this.loadedMaps[hash] = false
46+
this.loadedMaps[hash] = false
4847
}
4948
}
50-
if (_this.loadedMaps[hash]) {
51-
_this.sourceMapCache.registerMap(absFile, _this.loadedMaps[hash])
49+
if (this.loadedMaps[hash]) {
50+
this._sourceMapCache.registerMap(absFile, this.loadedMaps[hash])
5251
}
5352
}
5453
})

test/source-maps.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* global describe, it */
2+
3+
require('chai').should()
4+
require('tap').mochaGlobals()
5+
6+
const { readFileSync } = require('fs')
7+
const SourceMaps = require('../self-coverage/lib/source-maps')
8+
9+
describe('source-maps', function () {
10+
it('caches source maps globally', function () {
11+
const sm = new SourceMaps({})
12+
// load a source map into cache.
13+
const sourceFile = require.resolve('./fixtures/source-maps/instrumented/s1.min.js')
14+
sm.extractAndRegister(readFileSync(sourceFile, 'utf8'), sourceFile, 'abc123')
15+
// create a new SourceMaps instance.
16+
const sm2 = new SourceMaps({})
17+
// the two instances of SourceMaps should share a cache.
18+
sm._sourceMapCache.should.deep.equal(sm2._sourceMapCache)
19+
})
20+
})

0 commit comments

Comments
 (0)