Skip to content

Commit

Permalink
chore: Modernize lib/source-maps.js (#1175)
Browse files Browse the repository at this point in the history
* Convert to class
* Prefer const over var
* Use Object.entries()
  • Loading branch information
coreyfarrell committed Sep 24, 2019
1 parent 03ba5b9 commit b59fc8f
Showing 1 changed file with 50 additions and 49 deletions.
99 changes: 50 additions & 49 deletions lib/source-maps.js
Expand Up @@ -4,64 +4,65 @@ const libSourceMaps = require('istanbul-lib-source-maps')
const fs = require('fs')
const path = require('path')

function SourceMaps (opts) {
this.cache = opts.cache
this.cacheDirectory = opts.cacheDirectory
this.loadedMaps = {}
this._sourceMapCache = libSourceMaps.createSourceMapStore()
}
class SourceMaps {
constructor (opts) {
this.cache = opts.cache
this.cacheDirectory = opts.cacheDirectory
this.loadedMaps = {}
this._sourceMapCache = libSourceMaps.createSourceMapStore()
}

SourceMaps.prototype.cachedPath = function (source, hash) {
return path.join(
this.cacheDirectory,
`${path.parse(source).name}-${hash}.map`
)
}
cachedPath (source, hash) {
return path.join(
this.cacheDirectory,
`${path.parse(source).name}-${hash}.map`
)
}

SourceMaps.prototype.purgeCache = function () {
this._sourceMapCache = libSourceMaps.createSourceMapStore()
this.loadedMaps = {}
}
purgeCache () {
this._sourceMapCache = libSourceMaps.createSourceMapStore()
this.loadedMaps = {}
}

SourceMaps.prototype.extractAndRegister = function (code, filename, hash) {
var 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)
extractAndRegister (code, filename, hash) {
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
}

SourceMaps.prototype.remapCoverage = function (obj) {
var transformed = this._sourceMapCache.transformCoverage(
libCoverage.createCoverageMap(obj)
)
return transformed.map.data
}
remapCoverage (obj) {
const transformed = this._sourceMapCache.transformCoverage(
libCoverage.createCoverageMap(obj)
)
return transformed.map.data
}

SourceMaps.prototype.reloadCachedSourceMaps = function (report) {
Object.keys(report).forEach((absFile) => {
var fileReport = report[absFile]
if (fileReport && fileReport.contentHash) {
var hash = fileReport.contentHash
if (!(hash in this.loadedMaps)) {
try {
const mapPath = this.cachedPath(absFile, hash)
this.loadedMaps[hash] = JSON.parse(fs.readFileSync(mapPath, 'utf8'))
} catch (e) {
// set to false to avoid repeatedly trying to load the map
this.loadedMaps[hash] = false
reloadCachedSourceMaps (report) {
Object.entries(report).forEach(([absFile, fileReport]) => {
if (fileReport && fileReport.contentHash) {
const hash = fileReport.contentHash
if (!(hash in this.loadedMaps)) {
try {
const mapPath = this.cachedPath(absFile, hash)
this.loadedMaps[hash] = JSON.parse(fs.readFileSync(mapPath, 'utf8'))
} catch (e) {
// set to false to avoid repeatedly trying to load the map
this.loadedMaps[hash] = false
}
}
if (this.loadedMaps[hash]) {
this._sourceMapCache.registerMap(absFile, this.loadedMaps[hash])
}
}
if (this.loadedMaps[hash]) {
this._sourceMapCache.registerMap(absFile, this.loadedMaps[hash])
}
}
})
})
}
}

module.exports = SourceMaps

0 comments on commit b59fc8f

Please sign in to comment.