Skip to content

Commit

Permalink
fix: no wrapperLength for empty coverage (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Oct 5, 2023
1 parent abad43f commit bde6de2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/v8-to-istanbul.js
Expand Up @@ -128,12 +128,13 @@ module.exports = class V8ToIstanbul {
applyCoverage (blocks) {
blocks.forEach(block => {
block.ranges.forEach((range, i) => {
const { startCol, endCol, path, covSource } = this._maybeRemapStartColEndCol(range)
const isEmptyCoverage = block.functionName === '(empty-report)'
const { startCol, endCol, path, covSource } = this._maybeRemapStartColEndCol(range, isEmptyCoverage)
if (this.excludePath(path)) {
return
}
let lines
if (block.functionName === '(empty-report)') {
if (isEmptyCoverage) {
// (empty-report), this will result in a report that has all lines zeroed out.
lines = covSource.lines.filter((line) => {
line.count = 0
Expand Down Expand Up @@ -207,15 +208,17 @@ module.exports = class V8ToIstanbul {
})
}

_maybeRemapStartColEndCol (range) {
_maybeRemapStartColEndCol (range, isEmptyCoverage) {
let covSource = this.covSources[0].source
let startCol = Math.max(0, range.startOffset - covSource.wrapperLength)
let endCol = Math.min(covSource.eof, range.endOffset - covSource.wrapperLength)
const covSourceWrapperLength = isEmptyCoverage ? 0 : covSource.wrapperLength
let startCol = Math.max(0, range.startOffset - covSourceWrapperLength)
let endCol = Math.min(covSource.eof, range.endOffset - covSourceWrapperLength)
let path = this.path

if (this.sourceMap) {
startCol = Math.max(0, range.startOffset - this.sourceTranspiled.wrapperLength)
endCol = Math.min(this.sourceTranspiled.eof, range.endOffset - this.sourceTranspiled.wrapperLength)
const sourceTranspiledWrapperLength = isEmptyCoverage ? 0 : this.sourceTranspiled.wrapperLength
startCol = Math.max(0, range.startOffset - sourceTranspiledWrapperLength)
endCol = Math.min(this.sourceTranspiled.eof, range.endOffset - sourceTranspiledWrapperLength)

const { startLine, relStartCol, endLine, relEndCol, source } = this.sourceTranspiled.offsetToOriginalRelative(
this.sourceMap,
Expand Down
36 changes: 36 additions & 0 deletions test/v8-to-istanbul.js
Expand Up @@ -233,6 +233,42 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap}
assert(v8ToIstanbul.sourceMap !== undefined)
})

/*
* Empty coverages should not cause negative end columns when
* wrapperLength is used.
*/
it('empty coverage with custom wrapperLength', async () => {
const filename = require.resolve('./fixtures/scripts/uncovered.js')
const fileSize = lstatSync(filename).size
const v8ToIstanbul = new V8ToIstanbul(filename, fileSize * 2)

await v8ToIstanbul.load()
v8ToIstanbul.applyCoverage([{
functionName: '(empty-report)',
ranges: [{
startOffset: 0,
endOffset: fileSize,
count: 0
}],
isBlockCoverage: true
}])

const { fnMap, branchMap } = v8ToIstanbul.toIstanbul()[filename]

Object.values(fnMap).forEach(fn => {
fn.decl.end.column.should.above(0)
fn.loc.end.column.should.above(0)
})

Object.values(branchMap).forEach(branch => {
branch.loc.end.column.should.above(0)

branch.locations.forEach(location => {
location.end.column.should.above(0)
})
})
})

it('empty coverage marks all lines uncovered', async () => {
const filename = require.resolve('./fixtures/scripts/uncovered.js')
const v8ToIstanbul = new V8ToIstanbul(filename)
Expand Down

0 comments on commit bde6de2

Please sign in to comment.