Skip to content

Commit

Permalink
fix: handle undefined sourcesContent and null sourcesContent entry
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Jan 10, 2022
1 parent 0a8a759 commit 6c2e2ec
Show file tree
Hide file tree
Showing 10 changed files with 11,030 additions and 1,623 deletions.
2 changes: 1 addition & 1 deletion lib/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('source-map').Source

module.exports = class CovSource {
constructor (sourceRaw, wrapperLength) {
sourceRaw = sourceRaw.trimEnd()
sourceRaw = sourceRaw ? sourceRaw.trimEnd() : ''
this.lines = []
this.eof = sourceRaw.length
this.shebangLength = getShebangLength(sourceRaw)
Expand Down
17 changes: 17 additions & 0 deletions lib/v8-to-istanbul.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const assert = require('assert')
const convertSourceMap = require('convert-source-map')
const util = require('util')
const debuglog = util.debuglog('c8')
const { dirname, isAbsolute, join, resolve } = require('path')
const { fileURLToPath } = require('url')
const CovBranch = require('./branch')
Expand Down Expand Up @@ -52,6 +54,9 @@ module.exports = class V8ToIstanbul {
if (this.rawSourceMap) {
if (this.rawSourceMap.sourcemap.sources.length > 1) {
this.sourceMap = await new SourceMapConsumer(this.rawSourceMap.sourcemap)
if (!this.sourceMap.sourcesContent) {
this.sourceMap.sourcesContent = await this.sourcesContentFromSources()
}
this.covSources = this.sourceMap.sourcesContent.map((rawSource, i) => ({ source: new CovSource(rawSource, this.wrapperLength), path: this.sourceMap.sources[i] }))
this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength)
} else {
Expand Down Expand Up @@ -84,6 +89,18 @@ module.exports = class V8ToIstanbul {
}
}

async sourcesContentFromSources () {
const fileList = this.sourceMap.sources.map(relativePath => {
const realPath = this._resolveSource(this.rawSourceMap, relativePath)
return readFile(realPath, 'utf-8')
.then(result => result)
.catch(err => {
debuglog(`failed to load ${realPath}: ${err.message}`)
})
})
return await Promise.all(fileList)
}

destroy () {
if (this.sourceMap) {
this.sourceMap.destroy()
Expand Down
Loading

0 comments on commit 6c2e2ec

Please sign in to comment.