Skip to content

Commit ab0fcdd

Browse files
author
Benjamin E. Coe
committed
feat: initial support for source-maps (#19)
BREAKING CHANGE: v8-to-istanbul is now async, making it possible to use the latest source-map library
1 parent 8aa931e commit ab0fcdd

17 files changed

+853
-229
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ converts from v8 coverage format to [istanbul's coverage format](https://github.
1010

1111
```js
1212
const v8toIstanbul = require('v8-to-istanbul')
13-
// create a script object from a source-file. the source file
14-
// is loaded from disk and this is used to determine the original
15-
// line count.
16-
const script = v8toIstanbul('./path-to-instrumented-file.js')
13+
// the path to the original source-file is required, as its contents are
14+
// used during the conversion algorithm.
15+
const converter = v8toIstanbul('./path-to-instrumented-file.js')
16+
await converter.load() // this is required due to the async source-map dependency.
1717
// provide an array of coverage information in v8 format.
18-
script.applyCoverage([
18+
converter.applyCoverage([
1919
{
2020
"functionName": "",
2121
"ranges": [
@@ -31,7 +31,7 @@ script.applyCoverage([
3131
])
3232
// output coverage information in a form that can
3333
// be consumed by Istanbul.
34-
console.info(JSON.stringify(script.toIstanbul()))
34+
console.info(JSON.stringify(converter.toIstanbul()))
3535
```
3636

3737
## Testing

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const Script = require('./lib/script')
1+
const V8ToIstanbul = require('./lib/v8-to-istanbul')
22

33
module.exports = function (path, wrapperLength) {
4-
return new Script(path, wrapperLength)
4+
return new V8ToIstanbul(path, wrapperLength)
55
}

lib/branch.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ module.exports = class CovBranch {
99
toIstanbul () {
1010
const location = {
1111
start: {
12-
line: this.startLine.line,
13-
column: this.startCol - this.startLine.startCol
12+
line: this.startLine,
13+
column: this.startCol
1414
},
1515
end: {
16-
line: this.endLine.line,
17-
column: this.endCol - this.endLine.startCol
16+
line: this.endLine,
17+
column: this.endCol
1818
}
1919
}
2020
return {
2121
type: 'branch',
22-
line: this.line,
22+
line: this.startLine,
2323
loc: location,
2424
locations: [Object.assign({}, location)]
2525
}

lib/function.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ module.exports = class CovFunction {
1010
toIstanbul () {
1111
const loc = {
1212
start: {
13-
line: this.startLine.line,
14-
column: this.startCol - this.startLine.startCol
13+
line: this.startLine,
14+
column: this.startCol
1515
},
1616
end: {
17-
line: this.endLine.line,
18-
column: this.endCol - this.endLine.startCol
17+
line: this.endLine,
18+
column: this.endCol
1919
}
2020
}
2121
return {
2222
name: this.name,
2323
decl: loc,
2424
loc: loc,
25-
line: this.startLine.line
25+
line: this.startLine
2626
}
2727
}
2828
}

lib/line.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
module.exports = class CovLine {
2-
constructor (line, startCol, endCol) {
2+
constructor (line, startCol, lineStr) {
33
this.line = line
4+
// note that startCol and endCol are absolute positions
5+
// within a file, not relative to the line.
46
this.startCol = startCol
5-
this.endCol = endCol
6-
this.count = 0
7+
8+
// the line length itself does not include the newline characters,
9+
// these are however taken into account when enumerating absolute offset.
10+
const matchedNewLineChar = lineStr.match(/\r?\n$/u)
11+
const newLineLength = matchedNewLineChar ? matchedNewLineChar[0].length : 0
12+
this.endCol = startCol + lineStr.length - newLineLength
13+
14+
// we start with all lines having been executed, and work
15+
// backwards zeroing out lines based on V8 output.
16+
this.count = 1
717
}
818
toIstanbul () {
919
return {

lib/pathutils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright 2015, Yahoo Inc.
3+
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
4+
*/
5+
6+
// pulled from: https://github.com/istanbuljs/istanbuljs
7+
'use strict'
8+
9+
const path = require('path')
10+
11+
module.exports = {
12+
isAbsolute: path.isAbsolute,
13+
relativeTo (file, origFile) {
14+
const p = path.isAbsolute(file)
15+
? file
16+
: path.resolve(path.dirname(origFile), file)
17+
return path.relative(process.cwd(), p)
18+
}
19+
}

lib/script.js

Lines changed: 0 additions & 154 deletions
This file was deleted.

0 commit comments

Comments
 (0)