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

Ignore lines from code coverage which are not executable #242

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions index.js
@@ -1,5 +1,5 @@
const V8ToIstanbul = require('./lib/v8-to-istanbul')

module.exports = function (path, wrapperLength, sources, excludePath) {
return new V8ToIstanbul(path, wrapperLength, sources, excludePath)
module.exports = function (path, wrapperLength, sources, excludePath, plugin) {
return new V8ToIstanbul(path, wrapperLength, sources, excludePath, plugin)
}
36 changes: 36 additions & 0 deletions lib/plugin.js
@@ -0,0 +1,36 @@
const esbuild = require('esbuild')
const vlq = require('vlq')

module.exports = function (source, path) {
let result
try {
const loader = path.endsWith('.tsx') || path.endsWith('.jsx') ? { loader: 'tsx', jsx: 'preserve' } : {}
result = esbuild.transformSync(source.sourceRaw, {
sourcemap: 'external',
loader: 'ts',
minify: false,
minifyWhitespace: false,
minifySyntax: false,
legalComments: 'none',
...loader
})
} catch (err) {
console.error(`${path} parsed with error ${err.message}`)
return
}
const mappings = JSON.parse(result.map).mappings
if (mappings) {
let start = 0
const executableLines = mappings.split(';').reduce((set, str) => {
start = str.split(',').reduce((number, seg) => {
const [, , line] = vlq.decode(seg)
set.add(number + line)
return number + line
}, start)
return set
}, new Set())
source.lines.forEach((line, index) => {
line.ignore = line.ignore || !executableLines.has(index)
})
}
}
1 change: 1 addition & 0 deletions lib/source.js
Expand Up @@ -4,6 +4,7 @@ const { originalPositionFor, generatedPositionFor, GREATEST_LOWER_BOUND, LEAST_U

module.exports = class CovSource {
constructor (sourceRaw, wrapperLength) {
this.sourceRaw = sourceRaw
sourceRaw = sourceRaw ? sourceRaw.trimEnd() : ''
this.lines = []
this.eof = sourceRaw.length
Expand Down
12 changes: 10 additions & 2 deletions lib/v8-to-istanbul.js
Expand Up @@ -25,12 +25,13 @@ const isNode8 = /^v8\./.test(process.version)
const cjsWrapperLength = isOlderNode10 ? require('module').wrapper[0].length : 0

module.exports = class V8ToIstanbul {
constructor (scriptPath, wrapperLength, sources, excludePath) {
constructor (scriptPath, wrapperLength, sources, excludePath, plugin) {
assert(typeof scriptPath === 'string', 'scriptPath must be a string')
assert(!isNode8, 'This module does not support node 8 or lower, please upgrade to node 10')
this.path = parsePath(scriptPath)
this.wrapperLength = wrapperLength === undefined ? cjsWrapperLength : wrapperLength
this.excludePath = excludePath || (() => false)
this.plugin = plugin
this.sources = sources || {}
this.generatedLines = []
this.branches = {}
Expand Down Expand Up @@ -260,6 +261,10 @@ module.exports = class V8ToIstanbul {
return
}

if (this.plugin) {
this.plugin(source, resolvedPath)
}

return {
[resolvedPath]: {
path: resolvedPath,
Expand All @@ -281,8 +286,11 @@ module.exports = class V8ToIstanbul {
s: {}
}
source.lines.forEach((line, index) => {
if (line.ignore) {
return
}
statements.statementMap[`${index}`] = line.toIstanbul()
statements.s[`${index}`] = line.ignore ? 1 : line.count
statements.s[`${index}`] = line.count
})
return statements
}
Expand Down