Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no wrapperLength for empty coverage #210

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/v8-to-istanbul.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading