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: v8 coverage ranges that fall on \n characters cause exceptions #96

Merged
merged 1 commit into from Mar 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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