feat(diff-coverage): enhance coverage reporting for multi-line statements and branches (#33516)

Co-authored-by: CodingOnStar <hanxujiang@dify.com>
This commit is contained in:
Coding On Star
2026-03-16 15:09:46 +08:00
committed by GitHub
parent a01c384f5b
commit 7daec9717d
3 changed files with 66 additions and 4 deletions

View File

@ -100,7 +100,7 @@ export function getChangedStatementCoverage(entry, changedLines) {
continue
}
uncoveredLines.push(statement.start.line)
uncoveredLines.push(getFirstChangedLineInRange(statement, normalizedChangedLines))
}
return {
@ -111,6 +111,7 @@ export function getChangedStatementCoverage(entry, changedLines) {
}
export function getChangedBranchCoverage(entry, changedLines) {
const normalizedChangedLines = [...(changedLines ?? [])].sort((a, b) => a - b)
if (!entry) {
return {
covered: 0,
@ -141,7 +142,7 @@ export function getChangedBranchCoverage(entry, changedLines) {
const location = locations[armIndex] ?? branch.loc ?? branch
uncoveredBranches.push({
armIndex,
line: getLocationStartLine(location) ?? branch.line ?? 1,
line: getFirstChangedLineInRange(location, normalizedChangedLines, branch.line ?? 1),
})
}
}
@ -247,6 +248,20 @@ function rangeIntersectsChangedLines(location, changedLines) {
return false
}
function getFirstChangedLineInRange(location, changedLines, fallbackLine = 1) {
const startLine = getLocationStartLine(location)
const endLine = getLocationEndLine(location) ?? startLine
if (!startLine || !endLine)
return startLine ?? fallbackLine
for (const lineNumber of changedLines) {
if (lineNumber >= startLine && lineNumber <= endLine)
return lineNumber
}
return startLine ?? fallbackLine
}
function getLocationStartLine(location) {
return location?.start?.line ?? location?.line ?? null
}