Skip to content

Commit

Permalink
fix: v8 coverage ranges that fall on \n characters cause exceptions (#96
Browse files Browse the repository at this point in the history
)

When a file has dos line endings \r\n we found a few instances in a large codebase
where v8 would report the position of the \n as startCol for a range. This would
cause the starting column supplied to sourceMap.originalPositionFor to be a -1
because the startColumns of the transpiled source lines were calculated as
endCol + length of line break sequence.

To replicate this condition, revert the fix but run the new unit test. The
call to `applyCoverage` will crash. While the unit test range was hand
crafted by me for this particular example, we saw this coming up in the
wild.

This change clamps the supplied value to
max 0 and adds a test fixture (force encoded as \r\n) to test for it.

Co-authored-by: Joe Mordetsky <jmordetsky@bloomberg.net>
  • Loading branch information
j03m and j03m committed Mar 27, 2020
1 parent 628af48 commit c5731a3
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitattributes
@@ -0,0 +1,3 @@
test/fixtures/scripts/needs-compile.js eol=crlf
test/fixtures/scripts/needs-compile.compiled.js eol=crlf
test/fixtures/scripts/needs-compile.compiled.js.map eol=crlf
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
coverage
.nyc_output
node_modules
.idea
2 changes: 1 addition & 1 deletion lib/source.js
Expand Up @@ -56,7 +56,7 @@ module.exports = class CovSource {
const start = originalPositionTryBoth(
sourceMap,
lines[0].line,
startCol - lines[0].startCol
Math.max(0, startCol - lines[0].startCol)
)
let end = originalEndPositionFor(
sourceMap,
Expand Down
51 changes: 51 additions & 0 deletions test/fixtures/scripts/needs-compile.compiled.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/scripts/needs-compile.compiled.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions test/fixtures/scripts/needs-compile.js
@@ -0,0 +1,32 @@
// compile me with babel 7+:
// > babel --plugins @babel/plugin-proposal-class-properties test/fixtures/scripts/needs-compile.js --out-file test/fixtures/scripts/needs-compile.compiled.js --source-maps
// in addition, a .gitattribute clause forces this file and its compilation assets to crlf

// class uses private class fields - must be compiled
// hello is called
class Foo {
a = 1
#b = 2
constructor () {
this.c = this.a + this.#b;
}
hello (i) {
console.info('hello:', this.c + i)
}
}

// class uses private class fields - must be compiled
// hello is never called
class Bar {
#test = 99
constructor () {
}
hello () {
console.info(`Hello ${this.#test}`)
}
}

const foo = new Foo();
foo.hello(1)
const bar = new Bar()

31 changes: 31 additions & 0 deletions test/v8-to-istanbul.js
Expand Up @@ -6,6 +6,7 @@ const V8ToIstanbul = require('../lib/v8-to-istanbul')
const crypto = require('crypto')
const os = require('os')
const sourcemap = require('source-map')
const assert = require('assert')

require('tap').mochaGlobals()
require('should')
Expand Down Expand Up @@ -88,6 +89,36 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap}
// that means it was bale to access the content via the provided sources object
v8ToIstanbul.sourceTranspiled.should.not.be.undefined()
})

it('should clamp line source column >= 0', async () => {
const v8ToIstanbul = new V8ToIstanbul(
`file://${require.resolve('./fixtures/scripts/needs-compile.compiled.js')}`,
0
)

// read the file and find the first end of line char
const fileBody = readFileSync(require.resolve('./fixtures/scripts/needs-compile.compiled.js')).toString()
const matchedNewLineChar = fileBody.match(/(?<=\r?\n)/u).index

// this isn't an assertion for the test so much as it is an assertion that the
// test fixture hasn't be reverted from \r\n to \n.
assert(fileBody.substring(matchedNewLineChar - 2, matchedNewLineChar) === '\r\n', 'The test fixture is misconfigured!')

await v8ToIstanbul.load()
// apply a fake range that starts with the matched new line
// (these ranges can occur on v8 running on windows) and verify
// coverage is applied correctly. CovLine will have a
// gap between the endCol of the previous line ending on \r and startCol of the
// next line. This would cause -1 to be sent for the source map lookup.
// This would cause source map translation to throw
v8ToIstanbul.applyCoverage([{
functionName: 'fake',
ranges: [{
startOffset: matchedNewLineChar - 1,
endOffset: matchedNewLineChar + 10
}]
}])
})
})
describe('source map format edge cases', () => {
let consoleWarn
Expand Down

0 comments on commit c5731a3

Please sign in to comment.