Skip to content

Commit

Permalink
feat: exclude lines that are missing from source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Mar 23, 2024
1 parent 3616adf commit 0cff4f5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 471 deletions.
30 changes: 25 additions & 5 deletions lib/source.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
const CovLine = require('./line')
const { sliceRange } = require('./range')
const { originalPositionFor, generatedPositionFor, GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('@jridgewell/trace-mapping')
const { originalPositionFor, generatedPositionFor, eachMapping, GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('@jridgewell/trace-mapping')

module.exports = class CovSource {
constructor (sourceRaw, wrapperLength) {
constructor (sourceRaw, wrapperLength, traceMap) {
sourceRaw = sourceRaw ? sourceRaw.trimEnd() : ''
this.lines = []
this.eof = sourceRaw.length
this.shebangLength = getShebangLength(sourceRaw)
this.wrapperLength = wrapperLength - this.shebangLength
this._buildLines(sourceRaw)
this._buildLines(sourceRaw, traceMap)
}

_buildLines (source) {
_buildLines (source, traceMap) {
let position = 0
let ignoreCount = 0
let ignoreAll = false
const linesToCover = traceMap && this._parseLinesToCover(traceMap)

for (const [i, lineStr] of source.split(/(?<=\r?\n)/u).entries()) {
const line = new CovLine(i + 1, position, lineStr)
const lineNumber = i + 1
const line = new CovLine(lineNumber, position, lineStr)

if (linesToCover && !linesToCover.has(lineNumber)) {
line.ignore = true
}

if (ignoreCount > 0) {
line.ignore = true
ignoreCount--
Expand Down Expand Up @@ -125,6 +133,18 @@ module.exports = class CovSource {
if (this.lines[line - 1] === undefined) return this.eof
return Math.min(this.lines[line - 1].startCol + relCol, this.lines[line - 1].endCol)
}

_parseLinesToCover (traceMap) {
const linesToCover = new Set()

eachMapping(traceMap, (mapping) => {
if (mapping.originalLine !== null) {
linesToCover.add(mapping.originalLine)
}
})

return linesToCover
}
}

// this implementation is pulled over from istanbul-lib-sourcemap:
Expand Down
8 changes: 4 additions & 4 deletions lib/v8-to-istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ module.exports = class V8ToIstanbul {
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)
this.covSources = this.sourceMap.sourcesContent.map((rawSource, i) => ({ source: new CovSource(rawSource, this.wrapperLength, this.sourceMap), path: this.sourceMap.sources[i] }))
this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength, this.sourceMap)
} else {
const candidatePath = this.rawSourceMap.sourcemap.sources.length >= 1 ? this.rawSourceMap.sourcemap.sources[0] : this.rawSourceMap.sourcemap.file
this.path = this._resolveSource(this.rawSourceMap, candidatePath || this.path)
Expand All @@ -82,8 +82,8 @@ module.exports = class V8ToIstanbul {
// We fallback to reading the original source from disk.
originalRawSource = await readFile(this.path, 'utf8')
}
this.covSources = [{ source: new CovSource(originalRawSource, this.wrapperLength), path: this.path }]
this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength)
this.covSources = [{ source: new CovSource(originalRawSource, this.wrapperLength, this.sourceMap), path: this.path }]
this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength, this.sourceMap)
}
} else {
this.covSources = [{ source: new CovSource(rawSource, this.wrapperLength), path: this.path }]
Expand Down
Loading

0 comments on commit 0cff4f5

Please sign in to comment.