|
1 | 1 | const CovLine = require('./line') |
2 | 2 | const { sliceRange } = require('./range') |
3 | | -const { originalPositionFor, generatedPositionFor, GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('@jridgewell/trace-mapping') |
| 3 | +const { originalPositionFor, generatedPositionFor, eachMapping, GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('@jridgewell/trace-mapping') |
4 | 4 |
|
5 | 5 | module.exports = class CovSource { |
6 | | - constructor (sourceRaw, wrapperLength) { |
| 6 | + constructor (sourceRaw, wrapperLength, traceMap) { |
7 | 7 | sourceRaw = sourceRaw ? sourceRaw.trimEnd() : '' |
8 | 8 | this.lines = [] |
9 | 9 | this.eof = sourceRaw.length |
10 | 10 | this.shebangLength = getShebangLength(sourceRaw) |
11 | 11 | this.wrapperLength = wrapperLength - this.shebangLength |
12 | | - this._buildLines(sourceRaw) |
| 12 | + this._buildLines(sourceRaw, traceMap) |
13 | 13 | } |
14 | 14 |
|
15 | | - _buildLines (source) { |
| 15 | + _buildLines (source, traceMap) { |
16 | 16 | let position = 0 |
17 | 17 | let ignoreCount = 0 |
18 | 18 | let ignoreAll = false |
| 19 | + const linesToCover = traceMap && this._parseLinesToCover(traceMap) |
| 20 | + |
19 | 21 | for (const [i, lineStr] of source.split(/(?<=\r?\n)/u).entries()) { |
20 | | - const line = new CovLine(i + 1, position, lineStr) |
| 22 | + const lineNumber = i + 1 |
| 23 | + const line = new CovLine(lineNumber, position, lineStr) |
| 24 | + |
| 25 | + if (linesToCover && !linesToCover.has(lineNumber)) { |
| 26 | + line.ignore = true |
| 27 | + } |
| 28 | + |
21 | 29 | if (ignoreCount > 0) { |
22 | 30 | line.ignore = true |
23 | 31 | ignoreCount-- |
@@ -125,6 +133,18 @@ module.exports = class CovSource { |
125 | 133 | if (this.lines[line - 1] === undefined) return this.eof |
126 | 134 | return Math.min(this.lines[line - 1].startCol + relCol, this.lines[line - 1].endCol) |
127 | 135 | } |
| 136 | + |
| 137 | + _parseLinesToCover (traceMap) { |
| 138 | + const linesToCover = new Set() |
| 139 | + |
| 140 | + eachMapping(traceMap, (mapping) => { |
| 141 | + if (mapping.originalLine !== null) { |
| 142 | + linesToCover.add(mapping.originalLine) |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + return linesToCover |
| 147 | + } |
128 | 148 | } |
129 | 149 |
|
130 | 150 | // this implementation is pulled over from istanbul-lib-sourcemap: |
|
0 commit comments