From b4c1227def12c24707d7b3673fd235ce8ba445bb Mon Sep 17 00:00:00 2001 From: zhangtao25 Date: Wed, 13 Nov 2024 20:04:42 +0800 Subject: [PATCH] feat: istanbul if special logic --- packages/canyon-data/src/utils/line.ts | 10 +++++++++- .../src/components/CanyonReport/ShikiDetail.tsx | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/canyon-data/src/utils/line.ts b/packages/canyon-data/src/utils/line.ts index a7befd65..de4c7f88 100755 --- a/packages/canyon-data/src/utils/line.ts +++ b/packages/canyon-data/src/utils/line.ts @@ -33,6 +33,13 @@ function getLineCoverage(statementMap:{ [key: string]: Range },s:{ [key: string] return lineMap; } +function specialLogicByIf(branchRange) { + if (branchRange.type==='if' && branchRange.locations.length>1){ + return false; + } else { + return true; + } +} export function calculateNewLineCoverageForSingleFile(coverage:FileCoverageData, newLine:number[]) { @@ -63,7 +70,8 @@ export function calculateNewLineCoverageForSingleFile(coverage:FileCoverageData, Object.keys(coverage.branchMap).forEach((key) => { const branchRange = coverage.branchMap[key]; branchRange.locations.forEach((location,index) => { - if (coverage.b[key][index] === 0){ + // branch类型是if,并且有多个分支的,第一个if的location范围是整个分支的范围,所以如果它未覆盖,会导致标注的时候整个分支红色,先剔除。 + if (coverage.b[key][index] === 0 && specialLogicByIf(branchRange)){ noCovered.push({ startLine: location.start.line, endLine: location.end.line diff --git a/packages/canyon-platform/src/components/CanyonReport/ShikiDetail.tsx b/packages/canyon-platform/src/components/CanyonReport/ShikiDetail.tsx index 637095af..ba8d04b8 100644 --- a/packages/canyon-platform/src/components/CanyonReport/ShikiDetail.tsx +++ b/packages/canyon-platform/src/components/CanyonReport/ShikiDetail.tsx @@ -93,12 +93,20 @@ const ShikiDetail = ({ defaultValue, filecoverage, theme }) => { const branchDecorations = []; + function specialLogicByIf(branchRange) { + if (branchRange.type === "if" && branchRange.locations.length > 1) { + return false; + } else { + return true; + } + } + Object.entries(branchStats).forEach(([bName, counts]) => { const meta = branchMeta[bName]; if (meta) { Object.entries(meta.locations).forEach(([index, location]) => { const count = counts[index]; - if (count === 0) { + if (count === 0 && specialLogicByIf(meta)) { const startCol = location.start.column; const endCol = location.end.column + 1; const startLine = location.start.line;