Skip to content

Commit

Permalink
Merge 89839db into 628af48
Browse files Browse the repository at this point in the history
  • Loading branch information
j03m committed Mar 27, 2020
2 parents 628af48 + 89839db commit 52e7e80
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
coverage
.nyc_output
node_modules
.idea
2 changes: 1 addition & 1 deletion lib/source.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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 52e7e80

Please sign in to comment.