Skip to content

Commit

Permalink
fix: ignore all Types from TypeScript and not executable lines
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-redFox committed Mar 4, 2024
1 parent 521dee7 commit 2fccc75
Show file tree
Hide file tree
Showing 8 changed files with 648 additions and 6 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
9 changes: 7 additions & 2 deletions lib/v8-to-istanbul.js
Original file line number Diff line number Diff line change
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 @@ -282,7 +287,7 @@ module.exports = class V8ToIstanbul {
}
source.lines.forEach((line, index) => {
if (line.ignore) {
return;
return
}
statements.statementMap[`${index}`] = line.toIstanbul()
statements.s[`${index}`] = line.count
Expand Down
Loading

0 comments on commit 2fccc75

Please sign in to comment.