From 79d61e68ebbb2d7704d29d45bdf8eba08772c8c4 Mon Sep 17 00:00:00 2001 From: David Losert Date: Sat, 2 Nov 2024 20:35:47 +0100 Subject: [PATCH 1/2] fix: Uncovered lines column now correctly merges ranges when they are interrupted by empty rows --- .../getUncoveredLinesFromStatements.test.ts | 24 ++++++++++++ src/report/getUncoveredLinesFromStatements.ts | 37 +++++++++++++------ 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/report/getUncoveredLinesFromStatements.test.ts b/src/report/getUncoveredLinesFromStatements.test.ts index 05e738a..714742e 100644 --- a/src/report/getUncoveredLinesFromStatements.test.ts +++ b/src/report/getUncoveredLinesFromStatements.test.ts @@ -99,4 +99,28 @@ describe("getUncoveredLinesFromStatements()", () => { { start: 3, end: 4 }, ]); }); + + it("returns a single range if statement numbers are not sequential.", () => { + const statements: StatementCoverageReport = { + statementMap: { + "0": { + start: { line: 1, column: 0 }, + end: { line: 1, column: 0 }, + }, + "5": { + start: { line: 6, column: 0 }, + end: { line: 6, column: 0 }, + }, + "6": { + start: { line: 7, column: 0 }, + end: { line: 7, column: 0 }, + }, + }, + s: { "0": 0, "5": 0, "6": 0 }, + }; + + const uncoveredLines = getUncoveredLinesFromStatements(statements); + + expect(uncoveredLines).toEqual([{ start: 1, end: 7 }]); + }); }); diff --git a/src/report/getUncoveredLinesFromStatements.ts b/src/report/getUncoveredLinesFromStatements.ts index f845279..fc64d64 100644 --- a/src/report/getUncoveredLinesFromStatements.ts +++ b/src/report/getUncoveredLinesFromStatements.ts @@ -5,28 +5,43 @@ type LineRange = { end: number; }; +const IS_COVERED = 1; + const getUncoveredLinesFromStatements = ({ s, statementMap, }: StatementCoverageReport): LineRange[] => { const keys = Object.keys(statementMap); - const uncoveredLineRanges = keys.reduce((acc, key) => { - if (s[key] === 0) { - const lastRange = acc.at(-1); - - if (lastRange && lastRange.end === statementMap[key].start.line - 1) { - lastRange.end = statementMap[key].end.line; - return acc; + const uncoveredLineRanges: LineRange[] = []; + let currentRange: LineRange | undefined = undefined; + for (const key of keys) { + if (s[key] === IS_COVERED) { + // If the statement is covered, we need to close the current range. + if (currentRange) { + uncoveredLineRanges.push(currentRange); + currentRange = undefined; } + // Besides that, we can just ignore covered lines + continue; + } - acc.push({ + // Start a new range if we don't have one yet. + if (!currentRange) { + currentRange = { start: statementMap[key].start.line, end: statementMap[key].end.line, - }); + }; + continue; } - return acc; - }, []); + + currentRange.end = statementMap[key].end.line; + } + + // If we still have a current range, we need to add it to the uncovered line ranges. + if (currentRange) { + uncoveredLineRanges.push(currentRange); + } return uncoveredLineRanges; }; From cb83c1a100a4c675467310c4148f17d32becb8aa Mon Sep 17 00:00:00 2001 From: David Losert Date: Sat, 2 Nov 2024 20:41:59 +0100 Subject: [PATCH 2/2] fix: Fixes case where s property is greater than 1 --- .../getUncoveredLinesFromStatements.test.ts | 20 +++++++++++++++++++ src/report/getUncoveredLinesFromStatements.ts | 4 +--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/report/getUncoveredLinesFromStatements.test.ts b/src/report/getUncoveredLinesFromStatements.test.ts index 714742e..1ea6912 100644 --- a/src/report/getUncoveredLinesFromStatements.test.ts +++ b/src/report/getUncoveredLinesFromStatements.test.ts @@ -123,4 +123,24 @@ describe("getUncoveredLinesFromStatements()", () => { expect(uncoveredLines).toEqual([{ start: 1, end: 7 }]); }); + + it("handles the case where the property in 's' is greater than 1.", () => { + const statements: StatementCoverageReport = { + statementMap: { + "0": { + start: { line: 1, column: 0 }, + end: { line: 1, column: 0 }, + }, + "1": { + start: { line: 2, column: 0 }, + end: { line: 2, column: 0 }, + }, + }, + s: { "0": 2, "1": 8 }, + }; + + const uncoveredLines = getUncoveredLinesFromStatements(statements); + + expect(uncoveredLines).toEqual([]); + }); }); diff --git a/src/report/getUncoveredLinesFromStatements.ts b/src/report/getUncoveredLinesFromStatements.ts index fc64d64..c52dde6 100644 --- a/src/report/getUncoveredLinesFromStatements.ts +++ b/src/report/getUncoveredLinesFromStatements.ts @@ -5,8 +5,6 @@ type LineRange = { end: number; }; -const IS_COVERED = 1; - const getUncoveredLinesFromStatements = ({ s, statementMap, @@ -16,7 +14,7 @@ const getUncoveredLinesFromStatements = ({ const uncoveredLineRanges: LineRange[] = []; let currentRange: LineRange | undefined = undefined; for (const key of keys) { - if (s[key] === IS_COVERED) { + if (s[key] > 0) { // If the statement is covered, we need to close the current range. if (currentRange) { uncoveredLineRanges.push(currentRange);