Skip to content

Commit

Permalink
feat: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin committed Nov 25, 2017
0 parents commit 6140c6c
Show file tree
Hide file tree
Showing 14 changed files with 568 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
coverage
.nyc_output
node_modules
package-lock.json
14 changes: 14 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (c) 2017, Contributors

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice
appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE
LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# v8-to-istanbul

converts from v8 coverage format to [istanbul's coverage format](https://github.com/gotwarlost/istanbul/blob/master/coverage.json.md).
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Script = require('./lib/script')

module.exports = function (path) {
return new Script(path)
}
27 changes: 27 additions & 0 deletions lib/branch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = class CovBranch {
constructor (startLine, startCol, endLine, endCol, count) {
this.startLine = startLine
this.startCol = startCol
this.endLine = endLine
this.endCol = endCol
this.count = count
}
toIstanbul () {
const location = {
start: {
line: this.startLine.line,
column: this.startCol - this.startLine.startCol
},
end: {
line: this.endLine.line,
column: this.endCol - this.endLine.startCol
}
}
return {
type: 'branch',
line: this.line,
loc: location,
locations: [Object.assign({}, location)]
}
}
}
28 changes: 28 additions & 0 deletions lib/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = class CovFunction {
constructor (name, startLine, startCol, endLine, endCol, count) {
this.name = name
this.startLine = startLine
this.startCol = startCol
this.endLine = endLine
this.endCol = endCol
this.count = count
}
toIstanbul () {
const loc = {
start: {
line: this.startLine.line,
column: this.startCol - this.startLine.startCol
},
end: {
line: this.endLine.line,
column: this.endCol - this.endLine.startCol
}
}
return {
name: this.name,
decl: loc,
loc: loc,
line: this.startLine.line
}
}
}
20 changes: 20 additions & 0 deletions lib/line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = class CovLine {
constructor (line, startCol, endCol) {
this.line = line
this.startCol = startCol
this.endCol = endCol
this.count = 0
}
toIstanbul () {
return {
start: {
line: this.line,
column: 0
},
end: {
line: this.line,
column: this.endCol - this.startCol
}
}
}
}
114 changes: 114 additions & 0 deletions lib/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const fs = require('fs')
const CovBranch = require('./branch')
const CovLine = require('./line')
const CovFunction = require('./function')

// Node.js injects a header when executing a script.
const header = '(function (exports, require, module, __filename, __dirname) { '

module.exports = class CovScript {
constructor (scriptPath) {
const source = fs.readFileSync(scriptPath, 'utf8')
this.path = scriptPath
this.lines = []
this.branches = []
this.functions = []
this.eof = -1
this._buildLines(source, this.lines)
}
_buildLines (source, lines) {
let position = 0
source.split('\n').forEach((lineStr, i) => {
this.eof = position + lineStr.length
lines.push(new CovLine(i + 1, position, this.eof))
position += lineStr.length + 1 // also add the \n.
})
}
applyCoverage (blocks) {
blocks.forEach(block => {
block.ranges.forEach(range => {
const startCol = Math.max(0, range.startOffset - header.length)
const endCol = Math.min(this.eof, range.endOffset - header.length)
const lines = this.lines.filter(line => {
return startCol <= line.endCol && endCol >= line.startCol
})

if (block.isBlockCoverage && lines.length) {
// record branches.
this.branches.push(new CovBranch(
lines[0],
startCol,
lines[lines.length - 1],
endCol,
range.count
))
} else if (block.functionName && lines.length) {
// record functions.
this.functions.push(new CovFunction(
block.functionName,
lines[0],
startCol,
lines[lines.length - 1],
endCol,
range.count
))
}

// record the lines (we record these as statements, such that we're
// compatible with Istanbul 2.0).
lines.forEach(line => {
// make sure branch spans entire line; don't record 'goodbye'
// branch in `const foo = true ? 'hello' : 'goodbye'` as a
// 0 for line coverage.
if (startCol <= line.startCol && endCol >= line.endCol) {
line.count = range.count
}
})
})
})
}
toIstanbul () {
const istanbulInner = Object.assign(
{path: this.path},
this._statementsToIstanbul(),
this._branchesToIstanbul(),
this._functionsToIstanbul()
)
const istanbulOuter = {}
istanbulOuter[this.path] = istanbulInner
return istanbulOuter
}
_statementsToIstanbul () {
const statements = {
statementMap: {},
s: {}
}
this.lines.forEach((line, index) => {
statements.statementMap[`${index}`] = line.toIstanbul()
statements.s[`${index}`] = line.count
})
return statements
}
_branchesToIstanbul () {
const branches = {
branchMap: {},
b: {}
}
this.branches.forEach((branch, index) => {
branches.branchMap[`${index}`] = branch.toIstanbul()
branches.b[`${index}`] = [branch.count]
})
return branches
}
_functionsToIstanbul () {
const functions = {
fnMap: {},
f: {}
}
this.functions.forEach((fn, index) => {
functions.fnMap[`${index}`] = fn.toIstanbul()
functions.f[`${index}`] = fn.count
})
return functions
}
}
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "v8-to-istanbul",
"version": "1.0.0",
"description": "convert from v8 coverage format to istanbul's format",
"main": "index.js",
"scripts": {
"test": "nyc mocha test/*.js",
"posttest": "standard"
},
"repository": {
"url": "git@github.com:bcoe/v8-to-istanbul.git"
},
"keywords": [
"istanbul",
"v8",
"coverage"
],
"standard": {
"ignore": [
"**/test/fixtures"
]
},
"author": "Ben Coe <ben@npmjs.com>",
"license": "ISC",
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^4.0.1",
"nyc": "^11.3.0",
"standard": "^10.0.3"
}
}

0 comments on commit 6140c6c

Please sign in to comment.