Skip to content

Commit 5eee435

Browse files
fix: ignore all Types from TypeScript and not executable lines
1 parent 521dee7 commit 5eee435

File tree

6 files changed

+617
-5
lines changed

6 files changed

+617
-5
lines changed

index.js

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

3-
module.exports = function (path, wrapperLength, sources, excludePath) {
4-
return new V8ToIstanbul(path, wrapperLength, sources, excludePath)
3+
module.exports = function (path, wrapperLength, sources, excludePath, plugin) {
4+
return new V8ToIstanbul(path, wrapperLength, sources, excludePath, plugin)
55
}

lib/plugin.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const esbuild = require('esbuild');
2+
const vlq = require('vlq');
3+
4+
module.exports = function (source, resolvedPath) {
5+
let result
6+
try {
7+
const loader = path.endsWith('.tsx') || path.endsWith('.jsx') ? { loader: 'tsx', jsx: 'preserve' } : {}
8+
result = esbuild.transformSync(source.sourceRaw, {
9+
sourcemap: 'external',
10+
loader: 'ts',
11+
minify: false,
12+
minifyWhitespace: false,
13+
minifySyntax: false,
14+
legalComments: 'none',
15+
...loader
16+
})
17+
} catch (err) {
18+
console.error(`${resolvedPath} parsed with error ${err.message}`)
19+
return;
20+
}
21+
const mappings = JSON.parse(result.map).mappings
22+
if (mappings) {
23+
let start = 0
24+
const executableLines = mappings.split(';').reduce((set, str) => {
25+
start = str.split(',').reduce((number, seg) => {
26+
const [,,line] = vlq.decode(seg)
27+
set.add(number + line)
28+
return number + line
29+
}, start)
30+
return set
31+
}, new Set())
32+
source.lines.forEach((line, index) =>
33+
line.ignore = line.ignore || !executableLines.has(index)
34+
)
35+
}
36+
}

lib/source.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { originalPositionFor, generatedPositionFor, GREATEST_LOWER_BOUND, LEAST_U
44

55
module.exports = class CovSource {
66
constructor (sourceRaw, wrapperLength) {
7+
this.sourceRaw = sourceRaw;
78
sourceRaw = sourceRaw ? sourceRaw.trimEnd() : ''
89
this.lines = []
910
this.eof = sourceRaw.length

lib/v8-to-istanbul.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ const isNode8 = /^v8\./.test(process.version)
2525
const cjsWrapperLength = isOlderNode10 ? require('module').wrapper[0].length : 0
2626

2727
module.exports = class V8ToIstanbul {
28-
constructor (scriptPath, wrapperLength, sources, excludePath) {
28+
constructor (scriptPath, wrapperLength, sources, excludePath, plugin) {
2929
assert(typeof scriptPath === 'string', 'scriptPath must be a string')
3030
assert(!isNode8, 'This module does not support node 8 or lower, please upgrade to node 10')
3131
this.path = parsePath(scriptPath)
3232
this.wrapperLength = wrapperLength === undefined ? cjsWrapperLength : wrapperLength
3333
this.excludePath = excludePath || (() => false)
34+
this.plugin = plugin
3435
this.sources = sources || {}
3536
this.generatedLines = []
3637
this.branches = {}
@@ -260,6 +261,10 @@ module.exports = class V8ToIstanbul {
260261
return
261262
}
262263

264+
if (this.plugin) {
265+
this.plugin(source, resolvedPath)
266+
}
267+
263268
return {
264269
[resolvedPath]: {
265270
path: resolvedPath,

0 commit comments

Comments
 (0)