Skip to content

Commit

Permalink
feat: initial support for source-maps (#19)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: v8-to-istanbul is now async, making it possible to use the latest source-map library
  • Loading branch information
bcoe committed Apr 29, 2019
1 parent 8aa931e commit ab0fcdd
Show file tree
Hide file tree
Showing 17 changed files with 853 additions and 229 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ converts from v8 coverage format to [istanbul's coverage format](https://github.

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

## Testing
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Script = require('./lib/script')
const V8ToIstanbul = require('./lib/v8-to-istanbul')

module.exports = function (path, wrapperLength) {
return new Script(path, wrapperLength)
return new V8ToIstanbul(path, wrapperLength)
}
10 changes: 5 additions & 5 deletions lib/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ module.exports = class CovBranch {
toIstanbul () {
const location = {
start: {
line: this.startLine.line,
column: this.startCol - this.startLine.startCol
line: this.startLine,
column: this.startCol
},
end: {
line: this.endLine.line,
column: this.endCol - this.endLine.startCol
line: this.endLine,
column: this.endCol
}
}
return {
type: 'branch',
line: this.line,
line: this.startLine,
loc: location,
locations: [Object.assign({}, location)]
}
Expand Down
10 changes: 5 additions & 5 deletions lib/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ module.exports = class CovFunction {
toIstanbul () {
const loc = {
start: {
line: this.startLine.line,
column: this.startCol - this.startLine.startCol
line: this.startLine,
column: this.startCol
},
end: {
line: this.endLine.line,
column: this.endCol - this.endLine.startCol
line: this.endLine,
column: this.endCol
}
}
return {
name: this.name,
decl: loc,
loc: loc,
line: this.startLine.line
line: this.startLine
}
}
}
16 changes: 13 additions & 3 deletions lib/line.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
module.exports = class CovLine {
constructor (line, startCol, endCol) {
constructor (line, startCol, lineStr) {
this.line = line
// note that startCol and endCol are absolute positions
// within a file, not relative to the line.
this.startCol = startCol
this.endCol = endCol
this.count = 0

// the line length itself does not include the newline characters,
// these are however taken into account when enumerating absolute offset.
const matchedNewLineChar = lineStr.match(/\r?\n$/u)
const newLineLength = matchedNewLineChar ? matchedNewLineChar[0].length : 0
this.endCol = startCol + lineStr.length - newLineLength

// we start with all lines having been executed, and work
// backwards zeroing out lines based on V8 output.
this.count = 1
}
toIstanbul () {
return {
Expand Down
19 changes: 19 additions & 0 deletions lib/pathutils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/

// pulled from: https://github.com/istanbuljs/istanbuljs
'use strict'

const path = require('path')

module.exports = {
isAbsolute: path.isAbsolute,
relativeTo (file, origFile) {
const p = path.isAbsolute(file)
? file
: path.resolve(path.dirname(origFile), file)
return path.relative(process.cwd(), p)
}
}
154 changes: 0 additions & 154 deletions lib/script.js

This file was deleted.

Loading

0 comments on commit ab0fcdd

Please sign in to comment.