Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b59fc8f

Browse files
authored
chore: Modernize lib/source-maps.js (istanbuljs#1175)
* Convert to class * Prefer const over var * Use Object.entries()
1 parent 03ba5b9 commit b59fc8f

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

lib/source-maps.js

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,65 @@ const libSourceMaps = require('istanbul-lib-source-maps')
44
const fs = require('fs')
55
const path = require('path')
66

7-
function SourceMaps (opts) {
8-
this.cache = opts.cache
9-
this.cacheDirectory = opts.cacheDirectory
10-
this.loadedMaps = {}
11-
this._sourceMapCache = libSourceMaps.createSourceMapStore()
12-
}
7+
class SourceMaps {
8+
constructor (opts) {
9+
this.cache = opts.cache
10+
this.cacheDirectory = opts.cacheDirectory
11+
this.loadedMaps = {}
12+
this._sourceMapCache = libSourceMaps.createSourceMapStore()
13+
}
1314

14-
SourceMaps.prototype.cachedPath = function (source, hash) {
15-
return path.join(
16-
this.cacheDirectory,
17-
`${path.parse(source).name}-${hash}.map`
18-
)
19-
}
15+
cachedPath (source, hash) {
16+
return path.join(
17+
this.cacheDirectory,
18+
`${path.parse(source).name}-${hash}.map`
19+
)
20+
}
2021

21-
SourceMaps.prototype.purgeCache = function () {
22-
this._sourceMapCache = libSourceMaps.createSourceMapStore()
23-
this.loadedMaps = {}
24-
}
22+
purgeCache () {
23+
this._sourceMapCache = libSourceMaps.createSourceMapStore()
24+
this.loadedMaps = {}
25+
}
2526

26-
SourceMaps.prototype.extractAndRegister = function (code, filename, hash) {
27-
var sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))
28-
if (sourceMap) {
29-
if (this.cache && hash) {
30-
const mapPath = this.cachedPath(filename, hash)
31-
fs.writeFileSync(mapPath, sourceMap.toJSON())
32-
} else {
33-
this._sourceMapCache.registerMap(filename, sourceMap.sourcemap)
27+
extractAndRegister (code, filename, hash) {
28+
const sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))
29+
if (sourceMap) {
30+
if (this.cache && hash) {
31+
const mapPath = this.cachedPath(filename, hash)
32+
fs.writeFileSync(mapPath, sourceMap.toJSON())
33+
} else {
34+
this._sourceMapCache.registerMap(filename, sourceMap.sourcemap)
35+
}
3436
}
37+
return sourceMap
3538
}
36-
return sourceMap
37-
}
3839

39-
SourceMaps.prototype.remapCoverage = function (obj) {
40-
var transformed = this._sourceMapCache.transformCoverage(
41-
libCoverage.createCoverageMap(obj)
42-
)
43-
return transformed.map.data
44-
}
40+
remapCoverage (obj) {
41+
const transformed = this._sourceMapCache.transformCoverage(
42+
libCoverage.createCoverageMap(obj)
43+
)
44+
return transformed.map.data
45+
}
4546

46-
SourceMaps.prototype.reloadCachedSourceMaps = function (report) {
47-
Object.keys(report).forEach((absFile) => {
48-
var fileReport = report[absFile]
49-
if (fileReport && fileReport.contentHash) {
50-
var hash = fileReport.contentHash
51-
if (!(hash in this.loadedMaps)) {
52-
try {
53-
const mapPath = this.cachedPath(absFile, hash)
54-
this.loadedMaps[hash] = JSON.parse(fs.readFileSync(mapPath, 'utf8'))
55-
} catch (e) {
56-
// set to false to avoid repeatedly trying to load the map
57-
this.loadedMaps[hash] = false
47+
reloadCachedSourceMaps (report) {
48+
Object.entries(report).forEach(([absFile, fileReport]) => {
49+
if (fileReport && fileReport.contentHash) {
50+
const hash = fileReport.contentHash
51+
if (!(hash in this.loadedMaps)) {
52+
try {
53+
const mapPath = this.cachedPath(absFile, hash)
54+
this.loadedMaps[hash] = JSON.parse(fs.readFileSync(mapPath, 'utf8'))
55+
} catch (e) {
56+
// set to false to avoid repeatedly trying to load the map
57+
this.loadedMaps[hash] = false
58+
}
59+
}
60+
if (this.loadedMaps[hash]) {
61+
this._sourceMapCache.registerMap(absFile, this.loadedMaps[hash])
5862
}
5963
}
60-
if (this.loadedMaps[hash]) {
61-
this._sourceMapCache.registerMap(absFile, this.loadedMaps[hash])
62-
}
63-
}
64-
})
64+
})
65+
}
6566
}
6667

6768
module.exports = SourceMaps

0 commit comments

Comments
 (0)