From ebcc62ea3e47ebf6f1a1035e1fe5b391e34f4602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Allen=20Zhang=20=28=E5=BC=A0=E6=B6=9B=29?= Date: Wed, 24 Jul 2024 10:43:17 +0800 Subject: [PATCH] feat: update --- .../components/CanyonReport/ShikiDetail.tsx | 108 +++++++++--------- .../src/components/CanyonReport/helper.tsx | 100 ---------------- 2 files changed, 56 insertions(+), 152 deletions(-) diff --git a/packages/canyon-platform/src/components/CanyonReport/ShikiDetail.tsx b/packages/canyon-platform/src/components/CanyonReport/ShikiDetail.tsx index 306e4ceb..8be443d0 100644 --- a/packages/canyon-platform/src/components/CanyonReport/ShikiDetail.tsx +++ b/packages/canyon-platform/src/components/CanyonReport/ShikiDetail.tsx @@ -19,31 +19,33 @@ const ShikiDetail = ({ defaultValue, filecoverage, theme }) => { Object.entries(statementStats).forEach(([stName, count]) => { const meta = statementMeta[stName]; - const type = count > 0 ? 'yes' : 'no'; - const startCol = meta.start.column; - let endCol = meta.end.column + 1; - const startLine = meta.start.line; - const endLine = meta.end.line; - - if (type === 'no' && structuredText[startLine]) { - if (endLine !== startLine) { - endCol = structuredText[startLine].length; + if (meta) { + const type = count > 0 ? 'yes' : 'no'; + const startCol = meta.start.column; + let endCol = meta.end.column + 1; + const startLine = meta.start.line; + const endLine = meta.end.line; + + if (type === 'no' && structuredText[startLine]) { + if (endLine !== startLine) { + endCol = structuredText[startLine].length; + } + // 转化为字符的起始 + + let start = 0; + let end = 0; + + for (let i = 0; i < startLine - 1; i++) { + start += structuredText[i].length + 1; + } + for (let i = 0; i < endLine - 1; i++) { + end += structuredText[i].length + 1; + } + + start += startCol; + end += endCol; + statementDecorations.push([start, end]); } - // 转化为字符的起始 - - let start = 0; - let end = 0; - - for (let i = 0; i < startLine - 1; i++) { - start += structuredText[i].length + 1; - } - for (let i = 0; i < endLine - 1; i++) { - end += structuredText[i].length + 1; - } - - start += startCol; - end += endCol; - statementDecorations.push([start, end]); } }); @@ -52,35 +54,37 @@ const ShikiDetail = ({ defaultValue, filecoverage, theme }) => { const fnMeta = filecoverage.fnMap; Object.entries(fnStats).forEach(([fName, count]) => { const meta = fnMeta[fName]; - const type = count > 0 ? 'yes' : 'no'; - // Some versions of the instrumenter in the wild populate 'func' - // but not 'decl': - const decl = meta.decl || meta.loc; - const startCol = decl.start.column; - let endCol = decl.end.column + 1; - const startLine = decl.start.line; - const endLine = decl.end.line; - - if (type === 'no' && structuredText[startLine]) { - if (endLine !== startLine) { - endCol = structuredText[startLine].length; + if (meta) { + const type = count > 0 ? 'yes' : 'no'; + // Some versions of the instrumenter in the wild populate 'func' + // but not 'decl': + const decl = meta.decl || meta.loc; + const startCol = decl.start.column; + let endCol = decl.end.column + 1; + const startLine = decl.start.line; + const endLine = decl.end.line; + + if (type === 'no' && structuredText[startLine]) { + if (endLine !== startLine) { + endCol = structuredText[startLine].length; + } + + // 转化为字符的起始 + + let start = 0; + let end = 0; + + for (let i = 0; i < startLine - 1; i++) { + start += structuredText[i].length + 1; + } + for (let i = 0; i < endLine - 1; i++) { + end += structuredText[i].length + 1; + } + + start += startCol; + end += endCol; + fnDecorations.push([start, end]); } - - // 转化为字符的起始 - - let start = 0; - let end = 0; - - for (let i = 0; i < startLine - 1; i++) { - start += structuredText[i].length + 1; - } - for (let i = 0; i < endLine - 1; i++) { - end += structuredText[i].length + 1; - } - - start += startCol; - end += endCol; - fnDecorations.push([start, end]); } }); diff --git a/packages/canyon-platform/src/components/CanyonReport/helper.tsx b/packages/canyon-platform/src/components/CanyonReport/helper.tsx index d13bdf34..c036589e 100644 --- a/packages/canyon-platform/src/components/CanyonReport/helper.tsx +++ b/packages/canyon-platform/src/components/CanyonReport/helper.tsx @@ -1,103 +1,3 @@ -// 批注语句 - -export function annotateStatements(fileCoverage: any) { - const annotateStatementsList: any[] = []; - const statementStats = fileCoverage.s; - const statementMeta = fileCoverage.statementMap; - Object.entries(statementStats).forEach(([stName, count]: any) => { - const meta = statementMeta[stName]; - // TODO可能是错位等问题导致的map和hit数据对不上 - if (meta) { - const type = count > 0 ? 'yes' : 'no'; - const startCol = meta.start.column; - const endCol = meta.end.column + 1; - const startLine = meta.start.line; - const endLine = meta.end.line; - if (type === 'no') { - annotateStatementsList.push({ - startLine, - endLine, - startCol, - endCol, - type, - }); - } - } - }); - return annotateStatementsList; -} - -export function annotateFunctions(fileCoverage, structuredText) { - const fnStats = fileCoverage.f; - const fnMeta = fileCoverage.fnMap; - if (!fnStats) { - return []; - } - const list = []; - Object.entries(fnStats).forEach(([fName, count]) => { - const meta = fnMeta[fName]; - const type = count > 0 ? 'yes' : 'no'; - // Some versions of the instrumenter in the wild populate 'func' - // but not 'decl': - const decl = meta.decl || meta.loc; - const startCol = decl.start.column; - let endCol = decl.end.column + 1; - const startLine = decl.start.line; - const endLine = decl.end.line; - if (type === 'no') { - if (endLine !== startLine) { - console.log('???????'); - endCol = structuredText[startLine - 1].length; - } - list.push({ - startLine, - endLine, - startCol, - endCol, - type, - }); - } - }); - return list; -} - -export function convertingProductionFunctions( - startEnds: { start: number; end: number }[], - sourcecode: string, -) { - // codeLines是源码的每一行 - const codeLines = sourcecode.split('\n'); - - const result = []; - - for (let g = 0; g < startEnds.length; g++) { - const xy = []; - let count = 0; - for (let i = 0; i < codeLines.length; i++) { - const line = codeLines[i]; - // 注意这块,需要加1,因为split('\n')的时候,每一行的末尾是没有\n的 - for (let j = 0; j < line.length + 1; j++) { - if (count === startEnds[g].start || count === startEnds[g].end) { - xy.push({ - l: i + 1, - c: j + 1, - }); - } - count++; - } - } - result.push(xy); - } - return result.map((i) => { - return { - startLine: i[0].l, - startCol: i[0].c, - endLine: i[1].l, - endCol: i[1].c, - }; - }); -} - export function coreFn( fileCoverage: any, fileDetail: string,