From 7396cc51d38e18b81f44019cd404739cb1cfc933 Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Sat, 23 Mar 2024 10:11:24 +0200 Subject: [PATCH] feat: exclude lines that are missing from source maps --- lib/source.js | 30 +- lib/v8-to-istanbul.js | 8 +- tap-snapshots/test/v8-to-istanbul.js.test.cjs | 462 ------------------ 3 files changed, 29 insertions(+), 471 deletions(-) diff --git a/lib/source.js b/lib/source.js index d8ebc215..c9b0bf48 100644 --- a/lib/source.js +++ b/lib/source.js @@ -1,23 +1,31 @@ const CovLine = require('./line') const { sliceRange } = require('./range') -const { originalPositionFor, generatedPositionFor, GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('@jridgewell/trace-mapping') +const { originalPositionFor, generatedPositionFor, eachMapping, GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('@jridgewell/trace-mapping') module.exports = class CovSource { - constructor (sourceRaw, wrapperLength) { + constructor (sourceRaw, wrapperLength, traceMap) { sourceRaw = sourceRaw ? sourceRaw.trimEnd() : '' this.lines = [] this.eof = sourceRaw.length this.shebangLength = getShebangLength(sourceRaw) this.wrapperLength = wrapperLength - this.shebangLength - this._buildLines(sourceRaw) + this._buildLines(sourceRaw, traceMap) } - _buildLines (source) { + _buildLines (source, traceMap) { let position = 0 let ignoreCount = 0 let ignoreAll = false + const linesToCover = traceMap && this._parseLinesToCover(traceMap) + for (const [i, lineStr] of source.split(/(?<=\r?\n)/u).entries()) { - const line = new CovLine(i + 1, position, lineStr) + const lineNumber = i + 1 + const line = new CovLine(lineNumber, position, lineStr) + + if (linesToCover && !linesToCover.has(lineNumber)) { + line.ignore = true + } + if (ignoreCount > 0) { line.ignore = true ignoreCount-- @@ -125,6 +133,18 @@ module.exports = class CovSource { if (this.lines[line - 1] === undefined) return this.eof return Math.min(this.lines[line - 1].startCol + relCol, this.lines[line - 1].endCol) } + + _parseLinesToCover (traceMap) { + const linesToCover = new Set() + + eachMapping(traceMap, (mapping) => { + if (mapping.originalLine !== null) { + linesToCover.add(mapping.originalLine) + } + }) + + return linesToCover + } } // this implementation is pulled over from istanbul-lib-sourcemap: diff --git a/lib/v8-to-istanbul.js b/lib/v8-to-istanbul.js index 38e6a124..d77d11be 100644 --- a/lib/v8-to-istanbul.js +++ b/lib/v8-to-istanbul.js @@ -58,8 +58,8 @@ module.exports = class V8ToIstanbul { if (!this.sourceMap.sourcesContent) { this.sourceMap.sourcesContent = await this.sourcesContentFromSources() } - this.covSources = this.sourceMap.sourcesContent.map((rawSource, i) => ({ source: new CovSource(rawSource, this.wrapperLength), path: this.sourceMap.sources[i] })) - this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength) + this.covSources = this.sourceMap.sourcesContent.map((rawSource, i) => ({ source: new CovSource(rawSource, this.wrapperLength, this.sourceMap), path: this.sourceMap.sources[i] })) + this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength, this.sourceMap) } else { const candidatePath = this.rawSourceMap.sourcemap.sources.length >= 1 ? this.rawSourceMap.sourcemap.sources[0] : this.rawSourceMap.sourcemap.file this.path = this._resolveSource(this.rawSourceMap, candidatePath || this.path) @@ -82,8 +82,8 @@ module.exports = class V8ToIstanbul { // We fallback to reading the original source from disk. originalRawSource = await readFile(this.path, 'utf8') } - this.covSources = [{ source: new CovSource(originalRawSource, this.wrapperLength), path: this.path }] - this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength) + this.covSources = [{ source: new CovSource(originalRawSource, this.wrapperLength, this.sourceMap), path: this.path }] + this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength, this.sourceMap) } } else { this.covSources = [{ source: new CovSource(rawSource, this.wrapperLength), path: this.path }] diff --git a/tap-snapshots/test/v8-to-istanbul.js.test.cjs b/tap-snapshots/test/v8-to-istanbul.js.test.cjs index 640d6f4d..f50ebbec 100644 --- a/tap-snapshots/test/v8-to-istanbul.js.test.cjs +++ b/tap-snapshots/test/v8-to-istanbul.js.test.cjs @@ -2316,28 +2316,22 @@ Object { "13": 1, "14": 0, "15": 0, - "16": 0, "17": 0, "18": 1, "19": 1, - "2": 1, "20": 1, "21": 1, "22": 1, "23": 1, "24": 1, - "25": 1, "26": 1, - "27": 1, "28": 0, "29": 1, "3": 1, "30": 1, "4": 1, - "5": 1, "6": 1, "7": 1, - "8": 1, "9": 1, }, "statementMap": Object { @@ -2421,16 +2415,6 @@ Object { "line": 16, }, }, - "16": Object { - "end": Object { - "column": 0, - "line": 17, - }, - "start": Object { - "column": 0, - "line": 17, - }, - }, "17": Object { "end": Object { "column": 28, @@ -2461,16 +2445,6 @@ Object { "line": 20, }, }, - "2": Object { - "end": Object { - "column": 0, - "line": 3, - }, - "start": Object { - "column": 0, - "line": 3, - }, - }, "20": Object { "end": Object { "column": 23, @@ -2521,16 +2495,6 @@ Object { "line": 25, }, }, - "25": Object { - "end": Object { - "column": 0, - "line": 26, - }, - "start": Object { - "column": 0, - "line": 26, - }, - }, "26": Object { "end": Object { "column": 3, @@ -2541,16 +2505,6 @@ Object { "line": 27, }, }, - "27": Object { - "end": Object { - "column": 0, - "line": 28, - }, - "start": Object { - "column": 0, - "line": 28, - }, - }, "28": Object { "end": Object { "column": 46, @@ -2601,16 +2555,6 @@ Object { "line": 5, }, }, - "5": Object { - "end": Object { - "column": 0, - "line": 6, - }, - "start": Object { - "column": 0, - "line": 6, - }, - }, "6": Object { "end": Object { "column": 18, @@ -2631,16 +2575,6 @@ Object { "line": 8, }, }, - "8": Object { - "end": Object { - "column": 0, - "line": 9, - }, - "start": Object { - "column": 0, - "line": 9, - }, - }, "9": Object { "end": Object { "column": 16, @@ -2898,54 +2832,30 @@ Object { "s": Object { "0": 1, "1": 1, - "10": 1, "11": 1, "12": 1, "13": 1, "14": 1, - "15": 1, "16": 1, "17": 1, "18": 1, - "19": 1, "2": 1, "20": 1, "21": 1, - "22": 1, - "23": 1, "24": 1, "25": 0, - "26": 1, - "27": 1, - "28": 1, "29": 1, "3": 1, - "30": 1, - "31": 1, "32": 1, - "33": 1, - "34": 1, "35": 1, "36": 0, "37": 0, - "38": 1, - "39": 1, "4": 1, - "40": 1, - "41": 1, "42": 1, "43": 0, "44": 0, - "45": 0, "46": 0, - "47": 1, - "48": 1, - "49": 1, "5": 1, - "50": 1, - "51": 1, - "52": 1, - "53": 1, "54": 1, "55": 0, "56": 0, @@ -2956,9 +2866,6 @@ Object { "60": 0, "61": 0, "62": 0, - "63": 1, - "64": 1, - "65": 1, "66": 1, "67": 0, "68": 0, @@ -2966,19 +2873,10 @@ Object { "7": 1, "70": 0, "71": 0, - "72": 1, - "73": 1, - "74": 1, "75": 1, - "76": 1, - "77": 1, "78": 1, - "79": 1, "8": 1, - "80": 1, - "81": 1, "82": 1, - "9": 1, }, "statementMap": Object { "0": Object { @@ -3001,16 +2899,6 @@ Object { "line": 2, }, }, - "10": Object { - "end": Object { - "column": 53, - "line": 11, - }, - "start": Object { - "column": 0, - "line": 11, - }, - }, "11": Object { "end": Object { "column": 46, @@ -3051,16 +2939,6 @@ Object { "line": 15, }, }, - "15": Object { - "end": Object { - "column": 5, - "line": 16, - }, - "start": Object { - "column": 0, - "line": 16, - }, - }, "16": Object { "end": Object { "column": 0, @@ -3091,16 +2969,6 @@ Object { "line": 19, }, }, - "19": Object { - "end": Object { - "column": 0, - "line": 20, - }, - "start": Object { - "column": 0, - "line": 20, - }, - }, "2": Object { "end": Object { "column": 0, @@ -3131,26 +2999,6 @@ Object { "line": 22, }, }, - "22": Object { - "end": Object { - "column": 0, - "line": 23, - }, - "start": Object { - "column": 0, - "line": 23, - }, - }, - "23": Object { - "end": Object { - "column": 38, - "line": 24, - }, - "start": Object { - "column": 0, - "line": 24, - }, - }, "24": Object { "end": Object { "column": 25, @@ -3171,36 +3019,6 @@ Object { "line": 26, }, }, - "26": Object { - "end": Object { - "column": 0, - "line": 27, - }, - "start": Object { - "column": 0, - "line": 27, - }, - }, - "27": Object { - "end": Object { - "column": 0, - "line": 28, - }, - "start": Object { - "column": 0, - "line": 28, - }, - }, - "28": Object { - "end": Object { - "column": 52, - "line": 29, - }, - "start": Object { - "column": 0, - "line": 29, - }, - }, "29": Object { "end": Object { "column": 34, @@ -3221,26 +3039,6 @@ Object { "line": 4, }, }, - "30": Object { - "end": Object { - "column": 0, - "line": 31, - }, - "start": Object { - "column": 0, - "line": 31, - }, - }, - "31": Object { - "end": Object { - "column": 28, - "line": 32, - }, - "start": Object { - "column": 0, - "line": 32, - }, - }, "32": Object { "end": Object { "column": 43, @@ -3251,26 +3049,6 @@ Object { "line": 33, }, }, - "33": Object { - "end": Object { - "column": 0, - "line": 34, - }, - "start": Object { - "column": 0, - "line": 34, - }, - }, - "34": Object { - "end": Object { - "column": 47, - "line": 35, - }, - "start": Object { - "column": 0, - "line": 35, - }, - }, "35": Object { "end": Object { "column": 59, @@ -3301,26 +3079,6 @@ Object { "line": 38, }, }, - "38": Object { - "end": Object { - "column": 4, - "line": 39, - }, - "start": Object { - "column": 0, - "line": 39, - }, - }, - "39": Object { - "end": Object { - "column": 4, - "line": 40, - }, - "start": Object { - "column": 0, - "line": 40, - }, - }, "4": Object { "end": Object { "column": 42, @@ -3331,26 +3089,6 @@ Object { "line": 5, }, }, - "40": Object { - "end": Object { - "column": 0, - "line": 41, - }, - "start": Object { - "column": 0, - "line": 41, - }, - }, - "41": Object { - "end": Object { - "column": 33, - "line": 42, - }, - "start": Object { - "column": 0, - "line": 42, - }, - }, "42": Object { "end": Object { "column": 45, @@ -3381,16 +3119,6 @@ Object { "line": 45, }, }, - "45": Object { - "end": Object { - "column": 4, - "line": 46, - }, - "start": Object { - "column": 0, - "line": 46, - }, - }, "46": Object { "end": Object { "column": 65, @@ -3401,36 +3129,6 @@ Object { "line": 47, }, }, - "47": Object { - "end": Object { - "column": 4, - "line": 48, - }, - "start": Object { - "column": 0, - "line": 48, - }, - }, - "48": Object { - "end": Object { - "column": 0, - "line": 49, - }, - "start": Object { - "column": 0, - "line": 49, - }, - }, - "49": Object { - "end": Object { - "column": 35, - "line": 50, - }, - "start": Object { - "column": 0, - "line": 50, - }, - }, "5": Object { "end": Object { "column": 0, @@ -3441,46 +3139,6 @@ Object { "line": 6, }, }, - "50": Object { - "end": Object { - "column": 47, - "line": 51, - }, - "start": Object { - "column": 0, - "line": 51, - }, - }, - "51": Object { - "end": Object { - "column": 56, - "line": 52, - }, - "start": Object { - "column": 0, - "line": 52, - }, - }, - "52": Object { - "end": Object { - "column": 50, - "line": 53, - }, - "start": Object { - "column": 0, - "line": 53, - }, - }, - "53": Object { - "end": Object { - "column": 36, - "line": 54, - }, - "start": Object { - "column": 0, - "line": 54, - }, - }, "54": Object { "end": Object { "column": 49, @@ -3581,36 +3239,6 @@ Object { "line": 63, }, }, - "63": Object { - "end": Object { - "column": 4, - "line": 64, - }, - "start": Object { - "column": 0, - "line": 64, - }, - }, - "64": Object { - "end": Object { - "column": 0, - "line": 65, - }, - "start": Object { - "column": 0, - "line": 65, - }, - }, - "65": Object { - "end": Object { - "column": 73, - "line": 66, - }, - "start": Object { - "column": 0, - "line": 66, - }, - }, "66": Object { "end": Object { "column": 44, @@ -3681,36 +3309,6 @@ Object { "line": 72, }, }, - "72": Object { - "end": Object { - "column": 4, - "line": 73, - }, - "start": Object { - "column": 0, - "line": 73, - }, - }, - "73": Object { - "end": Object { - "column": 0, - "line": 74, - }, - "start": Object { - "column": 0, - "line": 74, - }, - }, - "74": Object { - "end": Object { - "column": 41, - "line": 75, - }, - "start": Object { - "column": 0, - "line": 75, - }, - }, "75": Object { "end": Object { "column": 120, @@ -3721,26 +3319,6 @@ Object { "line": 76, }, }, - "76": Object { - "end": Object { - "column": 0, - "line": 77, - }, - "start": Object { - "column": 0, - "line": 77, - }, - }, - "77": Object { - "end": Object { - "column": 28, - "line": 78, - }, - "start": Object { - "column": 0, - "line": 78, - }, - }, "78": Object { "end": Object { "column": 29, @@ -3751,16 +3329,6 @@ Object { "line": 79, }, }, - "79": Object { - "end": Object { - "column": 0, - "line": 80, - }, - "start": Object { - "column": 0, - "line": 80, - }, - }, "8": Object { "end": Object { "column": 46, @@ -3771,26 +3339,6 @@ Object { "line": 9, }, }, - "80": Object { - "end": Object { - "column": 0, - "line": 81, - }, - "start": Object { - "column": 0, - "line": 81, - }, - }, - "81": Object { - "end": Object { - "column": 41, - "line": 82, - }, - "start": Object { - "column": 0, - "line": 82, - }, - }, "82": Object { "end": Object { "column": 56, @@ -3801,16 +3349,6 @@ Object { "line": 83, }, }, - "9": Object { - "end": Object { - "column": 4, - "line": 10, - }, - "start": Object { - "column": 0, - "line": 10, - }, - }, }, } `