Skip to content

Commit

Permalink
fix covered lines
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Dec 18, 2023
1 parent 78fe54c commit 7d0259c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 78 deletions.
116 changes: 55 additions & 61 deletions lib/converter/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ const updateLineComment = (positionMapping, range) => {

const lines = Util.getRangeLines(sLoc, eLoc, block);

lines.forEach((i) => {
const line = positionMapping.getLine(i);
lines.forEach((it) => {
if (!it.entire) {
return;
}
const line = positionMapping.getLine(it.line);
if (line) {
line.comment = true;
}
Expand Down Expand Up @@ -129,12 +132,11 @@ const calculateV8Lines = (lines, blankCount, commentCount) => {
};

lines.forEach((ln) => {

v8Lines.total += 1;
if (ln.count > 0) {
// full line covered
if (ln.covered) {
v8Lines.covered += 1;
}

});

return v8Lines;
Expand All @@ -149,9 +151,11 @@ const createEmptyCoverage = () => {

// functions ranges from ast
const functions = [];
// line 1 based
const functionMap = new Map();

const lines = [];
// line 1 based
const lineMap = new Map();

const blankCount = 0;
Expand Down Expand Up @@ -251,54 +255,6 @@ const getFileCoverage = (coverage, sourcePath) => {

// ========================================================================================================

const setLineCount = (lineMap, line, count) => {
const lineInfo = lineMap.get(line);
if (lineInfo) {
lineInfo.count = count;
}
};

const setSingleLineCount = (lineMap, sLoc, eLoc, count) => {
// nothing between
if (sLoc.column >= eLoc.column) {
return;
}

// sometimes column > length
if (sLoc.column <= sLoc.indent && eLoc.column >= eLoc.length) {
// console.log('single', sLoc.line);
setLineCount(lineMap, sLoc.line, count);
}

};

const updateLinesCount = (lineMap, sLoc, eLoc, count) => {

// single line
if (sLoc.line === eLoc.line) {
setSingleLineCount(lineMap, sLoc, eLoc, count);
return;
}

const firstELoc = {
... sLoc,
column: sLoc.length
};
setSingleLineCount(lineMap, sLoc, firstELoc, count);

for (let i = sLoc.line + 1; i < eLoc.line; i++) {
setLineCount(lineMap, i, count);
}

const lastSLoc = {
... eLoc,
column: eLoc.indent
};
setSingleLineCount(lineMap, lastSLoc, eLoc, count);

};


const updateFunctionsCount = (coverage, sLoc, eLoc, count, functionName) => {
if (!count) {
return;
Expand Down Expand Up @@ -401,8 +357,27 @@ const addJsCoverage = (coverage, block, range, index, positionMapping) => {
const eLoc = positionMapping.offsetToLocation(endOffset);

// line, column
updateLinesCount(lineMap, sLoc, eLoc, count);
const lines = Util.getRangeLines(sLoc, eLoc);
lines.forEach((it) => {
const line = lineMap.get(it.line);
if (!line) {
return;
}

// from outside into inside, uncovered is certain
// default is covered
if (line.covered) {
line.covered = count > 0;
}

line.count = count;

// if (!line.history) {
// line.history = [];
// }
// line.history.push(`${it.entire}-${count}`);

});

if (isBlockCoverage) {
// the ranges form a tree of blocks representing how many times each statement or expression inside was executed.
Expand Down Expand Up @@ -434,8 +409,20 @@ const addCssCoverage = (coverage, range, positionMapping) => {
const sLoc = positionMapping.offsetToLocation(start);
const eLoc = positionMapping.offsetToLocation(end);

// line, column
updateLinesCount(lineMap, sLoc, eLoc, 1);
// covered css lines
const lines = Util.getRangeLines(sLoc, eLoc);
lines.forEach((it) => {
if (!it.entire) {
return;
}
const line = lineMap.get(it.line);
if (line) {
// default is uncovered
// count always 1, covered is certain
line.covered = true;
}
});


};

Expand Down Expand Up @@ -486,11 +473,6 @@ const handleDistLinesCoverage = (item, state, coverage) => {
let blankCount = 0;
let commentCount = 0;

// baseLineCount:
// js: 1 (functions include all uncovered)
// css: 0 (ranges include all covered)
const baseLineCount = item.type === 'js' ? 1 : 0;

// handle comment before collect lines
const comments = astInfo.comments;

Expand All @@ -503,6 +485,11 @@ const handleDistLinesCoverage = (item, state, coverage) => {
updateLineComment(positionMapping, range);
});


// js: 1 (functions include all uncovered)
// css: 0 (ranges include all covered)
const baseLineCount = item.type === 'js' ? 1 : 0;

positionMapping.lines.forEach((it) => {
// exclude blank and comment
if (it.blank) {
Expand Down Expand Up @@ -539,6 +526,13 @@ const handleDistRangesCoverage = (item, state, coverage) => {
addCssCoverage(coverage, range, positionMapping);
});
}


// if (item.sourcePath.indexOf('demo.js') !== -1) {
// console.log(coverage.lines);
// }


};

const generateCoverageForDist = (item, state) => {
Expand Down
3 changes: 3 additions & 0 deletions lib/converter/info-line.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module.exports = class InfoLine {
constructor(line, column, count = 1) {
// 1 based
this.line = line;
this.column = column;
this.count = count;
// covered full line, could be false even count > 0
this.covered = count > 0;
}

generate() {
Expand Down
45 changes: 30 additions & 15 deletions lib/platform/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,44 +234,59 @@ const Util = {

getRangeLines: (sLoc, eLoc, block = true) => {

// line index, 0 based
const lines = [];

const addStart = () => {
if (sLoc.column === sLoc.indent) {
lines.push(sLoc.line);
}
const entire = sLoc.column === sLoc.indent;
lines.push({
line: sLoc.line,
entire
});
};

const addMiddle = () => {
const start = sLoc.column === sLoc.indent;
const end = Util.isBlank(eLoc.text.slice(eLoc.column));
const entire = start && end;
// same line
lines.push({
line: sLoc.line,
entire
});
};

const addEnd = () => {
const rightText = eLoc.text.slice(eLoc.column);
if (Util.isBlank(rightText)) {
lines.push(eLoc.line);
}
const entire = Util.isBlank(eLoc.text.slice(eLoc.column));
lines.push({
line: eLoc.line,
entire
});
};

// block
if (block) {

// same line, single line
if (sLoc.line === eLoc.line) {
// check start
if (sLoc.column === sLoc.indent) {
// check end is blank
addEnd();
}

addMiddle();

} else {
// multiple lines

addStart();

// mids
// always entire for middle lines
const lineStart = sLoc.line + 1;
const lineEnd = eLoc.line;
if (lineEnd > lineStart) {
for (let i = lineStart; i < lineEnd; i++) {
lines.push(i);
lines.push({
line: i,
// for js nested functions
middle: true,
entire: true
});
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/v8/src/utils/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ class CoverageParser {

const lines = Util.getRangeLines(sLoc, eLoc, block);

lines.forEach((i) => {
const line = formattedLines[i];
lines.forEach((it) => {
if (!it.entire) {
return;
}
const line = formattedLines[it.line];
if (line) {
line.comment = true;
}
Expand Down

0 comments on commit 7d0259c

Please sign in to comment.