Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Nov 8, 2023
1 parent 22b650a commit 4c19a11
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 116 deletions.
44 changes: 17 additions & 27 deletions packages/canyon-report/src/Report/components/Code.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,39 @@
import hljs from 'highlight.js';
import { FC } from 'preact/compat';
import { useEffect, useState } from 'preact/hooks';
import { CoreFn } from './helpers.ts';

import { CoreFn } from '../../helper.ts';
// import { CoreFn } from './helpers.ts';
import { Line } from './Line.tsx';
import Mask from './Mask.tsx';

const Code: FC<{ fileCoverage: any; fileContent: string }> = ({
fileCoverage,
fileContent,
}) => {

console.log(fileCoverage,'fileCoverage',fileContent,'fileContent')

const data = {
new_lines: [],
};


// TODO new lines待实现

const Code: FC<{
fileCoverage: any;
fileContent: string;
fileCodeChange: number[];
}> = ({ fileCoverage, fileContent,fileCodeChange }) => {
const [renderMask, setRenderMask] = useState(false);
const { maxWidth, rows, times, zuizhong } = CoreFn({
fileCoverage,
fileContent,
});
const { maxWidth, rows, times, lines } = CoreFn(fileCoverage, fileContent);
useEffect(() => {
if (fileContent&&fileCoverage) {
hljs.highlightAll();
setRenderMask(true);
if (fileContent) {
setTimeout(() => {
hljs.highlightAll();
setRenderMask(true);
}, 0);
}
}, [fileContent,fileCoverage]);
}, [fileContent]);
return (
<div>
<div className={'box'}>
{/*{code}*/}
{renderMask && <Mask code={fileContent} coverage={fileCoverage} />}
<div style={{ display: 'flex' }}>
<Line
maxWidth={maxWidth}
rows={rows}
times={times}
lines={zuizhong}
newlines={data?.new_lines || []}
lines={lines}
newlines={fileCodeChange || []}
/>
{/*<div>{code}</div>*/}
<pre>
<code
style={{ fontSize: '14px', lineHeight: 1.5 }}
Expand Down
22 changes: 15 additions & 7 deletions packages/canyon-report/src/Report/components/IstanbulReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const IstanbulReport: FC<IstanbulReportProps> = ({
// @ts-ignore
const [fileCoverage, setFileCoverage] = useState({} as any);
const [fileContent, setFileContent] = useState('');
const [activePath, setActivePath] = useState('~');
const [fileCodeChange, setFileCodeChange] = useState<number[]>([]);
const [activePath, setActivePath] = useState('~/src/pages/Home.tsx');

const summaryTreeItem = useMemo(() => {
return genSummaryTreeItem(activePath, summary);
Expand All @@ -44,10 +45,13 @@ const IstanbulReport: FC<IstanbulReportProps> = ({

useEffect(() => {
if (activePath.includes('.')) {
onSelectFile(activePath).then(({ fileContent, fileCoverage }) => {
setFileContent(fileContent);
setFileCoverage(fileCoverage);
});
onSelectFile(activePath).then(
({ fileContent, fileCoverage, fileCodeChange }) => {
setFileContent(fileContent);
setFileCoverage(fileCoverage);
setFileCodeChange(fileCodeChange);
},
);
}
}, [activePath]);
useEffect(() => {
Expand Down Expand Up @@ -143,8 +147,12 @@ const IstanbulReport: FC<IstanbulReportProps> = ({
</div>
) : (
fileContent &&
fileCoverage.b && (
<Code fileCoverage={fileCoverage} fileContent={fileContent} />
fileCoverage && (
<Code
fileCoverage={fileCoverage}
fileContent={fileContent}
fileCodeChange={fileCodeChange}
/>
)
)}
</div>
Expand Down
4 changes: 3 additions & 1 deletion packages/canyon-report/src/Report/components/Line.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export function Line({ lines, newlines }) {
function getBgColor(num) {
// 0 粉色,1 绿色,-1没有颜色

function getBgColor(num: number) {
if (num === -1) {
return 'rgb(234, 234, 234)';
} else if (num === 0) {
Expand Down
77 changes: 0 additions & 77 deletions packages/canyon-report/src/Report/components/helpers.ts

This file was deleted.

10 changes: 6 additions & 4 deletions packages/canyon-report/src/Report/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ export enum Dims {
lines = 'lines',
}

export type OnSelectFile = (
path: string,
) => Promise<{ fileContent: string; fileCoverage: any }>;
export type OnSelectFile = (path: string) => Promise<{
fileContent: string;
fileCoverage: any;
fileCodeChange: number[];
}>;

export type Watermarks = {
[Dims.statements]: number[];
Expand Down Expand Up @@ -37,6 +39,6 @@ export type TrProps = {
};

export type IstanbulReportProps = {
onSelectFile: (path: string) => Promise<any>;
onSelectFile: OnSelectFile;
watermarks: Watermarks;
};
93 changes: 93 additions & 0 deletions packages/canyon-report/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,96 @@ export const classForPercent = (
}
return 'medium';
};

export function CoreFn(
fileCoverage: any,
fileDetail: string,
): {
times: {
lineNumber: number;
count: number;
}[];
rows: string[];
maxWidth: number;
lines: {
executionNumber: number;
}[];
} {
const nullData = {
times: [],
rows: [],
maxWidth: 0,
lines: [],
};
if (!fileCoverage.s) {
return nullData;
}

const content = fileDetail;
// 1.转换成数组
const rows = [''];
let index = 0;
for (let i = 0; i < content.length; i++) {
if (content[i] === '\n') {
index += 1;
rows.push('');
} else {
rows[index] += content[i];
}
}
const maxWidth = JSON.parse(JSON.stringify(rows)).sort(
(a: string, b: string) => -(a.length - b.length),
)[0].length;

// 获取numberOfRows
// 获取行覆盖率
function getLineCoverage(data: any) {
const statementMap = data.statementMap;
const statements = data.s;
const lineMap = Object.create(null);
Object.entries(statements).forEach(([st, count]: any) => {
if (!statementMap[st]) {
return;
}
const { line } = statementMap[st].start;
const prevVal = lineMap[line];
if (prevVal === undefined || prevVal < count) {
lineMap[line] = count;
}
});
return lineMap;
}

// 计算行
const lineStats = getLineCoverage(fileCoverage);
if (!lineStats) {
return nullData;
}
// numberOfRows
const numberOfRows: any[] = [];
Object.entries(lineStats).forEach(([lineNumber, count]) => {
numberOfRows.push({ lineNumber, count });
// 这边计算出了行的次数!!!!!!
});

const lines = [];
for (let i = 0; i < rows.length; i++) {
if (numberOfRows.find((n) => Number(n.lineNumber) === i + 1)) {
lines.push({
executionNumber: numberOfRows.find(
(n) => Number(n.lineNumber) === i + 1,
).count,
});
} else {
lines.push({
executionNumber: -1,
});
}
}
return {
times: numberOfRows,
rows,
lines,
maxWidth,
};
}
1 change: 1 addition & 0 deletions packages/canyon-report/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const report = init(document.querySelector('#root') as any, {
fileCoverage: __coverage__[path],
// @ts-ignore
fileContent: getDecode(__filecontent__[path]),
fileCodeChange:[1,2,3,4]
});
});
},
Expand Down

0 comments on commit 4c19a11

Please sign in to comment.