Skip to content

Commit 0cff4f5

Browse files
committed
feat: exclude lines that are missing from source maps
1 parent 3616adf commit 0cff4f5

File tree

3 files changed

+29
-471
lines changed

3 files changed

+29
-471
lines changed

lib/source.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
const CovLine = require('./line')
22
const { sliceRange } = require('./range')
3-
const { originalPositionFor, generatedPositionFor, GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('@jridgewell/trace-mapping')
3+
const { originalPositionFor, generatedPositionFor, eachMapping, GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('@jridgewell/trace-mapping')
44

55
module.exports = class CovSource {
6-
constructor (sourceRaw, wrapperLength) {
6+
constructor (sourceRaw, wrapperLength, traceMap) {
77
sourceRaw = sourceRaw ? sourceRaw.trimEnd() : ''
88
this.lines = []
99
this.eof = sourceRaw.length
1010
this.shebangLength = getShebangLength(sourceRaw)
1111
this.wrapperLength = wrapperLength - this.shebangLength
12-
this._buildLines(sourceRaw)
12+
this._buildLines(sourceRaw, traceMap)
1313
}
1414

15-
_buildLines (source) {
15+
_buildLines (source, traceMap) {
1616
let position = 0
1717
let ignoreCount = 0
1818
let ignoreAll = false
19+
const linesToCover = traceMap && this._parseLinesToCover(traceMap)
20+
1921
for (const [i, lineStr] of source.split(/(?<=\r?\n)/u).entries()) {
20-
const line = new CovLine(i + 1, position, lineStr)
22+
const lineNumber = i + 1
23+
const line = new CovLine(lineNumber, position, lineStr)
24+
25+
if (linesToCover && !linesToCover.has(lineNumber)) {
26+
line.ignore = true
27+
}
28+
2129
if (ignoreCount > 0) {
2230
line.ignore = true
2331
ignoreCount--
@@ -125,6 +133,18 @@ module.exports = class CovSource {
125133
if (this.lines[line - 1] === undefined) return this.eof
126134
return Math.min(this.lines[line - 1].startCol + relCol, this.lines[line - 1].endCol)
127135
}
136+
137+
_parseLinesToCover (traceMap) {
138+
const linesToCover = new Set()
139+
140+
eachMapping(traceMap, (mapping) => {
141+
if (mapping.originalLine !== null) {
142+
linesToCover.add(mapping.originalLine)
143+
}
144+
})
145+
146+
return linesToCover
147+
}
128148
}
129149

130150
// this implementation is pulled over from istanbul-lib-sourcemap:

lib/v8-to-istanbul.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ module.exports = class V8ToIstanbul {
5858
if (!this.sourceMap.sourcesContent) {
5959
this.sourceMap.sourcesContent = await this.sourcesContentFromSources()
6060
}
61-
this.covSources = this.sourceMap.sourcesContent.map((rawSource, i) => ({ source: new CovSource(rawSource, this.wrapperLength), path: this.sourceMap.sources[i] }))
62-
this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength)
61+
this.covSources = this.sourceMap.sourcesContent.map((rawSource, i) => ({ source: new CovSource(rawSource, this.wrapperLength, this.sourceMap), path: this.sourceMap.sources[i] }))
62+
this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength, this.sourceMap)
6363
} else {
6464
const candidatePath = this.rawSourceMap.sourcemap.sources.length >= 1 ? this.rawSourceMap.sourcemap.sources[0] : this.rawSourceMap.sourcemap.file
6565
this.path = this._resolveSource(this.rawSourceMap, candidatePath || this.path)
@@ -82,8 +82,8 @@ module.exports = class V8ToIstanbul {
8282
// We fallback to reading the original source from disk.
8383
originalRawSource = await readFile(this.path, 'utf8')
8484
}
85-
this.covSources = [{ source: new CovSource(originalRawSource, this.wrapperLength), path: this.path }]
86-
this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength)
85+
this.covSources = [{ source: new CovSource(originalRawSource, this.wrapperLength, this.sourceMap), path: this.path }]
86+
this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength, this.sourceMap)
8787
}
8888
} else {
8989
this.covSources = [{ source: new CovSource(rawSource, this.wrapperLength), path: this.path }]

0 commit comments

Comments
 (0)