Skip to content

Commit

Permalink
fixed coverage for function defined after return (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Jun 14, 2024
1 parent 5418ac4 commit a4ca6ec
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- 2.8.4
- updated UI: highlight keywords for searching results
- fixed coverage for static initialization blocks (#37)
- fixed coverage for function defined after return (#38)
- fixed ignored ranges
- fixed `CoverageReportOptions#reports` type

Expand Down
13 changes: 2 additions & 11 deletions lib/converter/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ const Util = require('../utils/util.js');

const { collectAstInfo } = require('./ast-visitor.js');

const sortRanges = (ranges) => {
ranges.sort((a, b) => {
if (a.startOffset === b.startOffset) {
return a.endOffset - b.endOffset;
}
return a.startOffset - b.startOffset;
});
};

const getCoverageInfo = (coverageList) => {
const functionMap = new Map();
const functionNameMap = new Map();
Expand Down Expand Up @@ -49,7 +40,7 @@ const getCoverageInfo = (coverageList) => {
functionRange.blockMap = blockMap;
// uncovered is unique
const blockUncoveredRanges = blockRanges.filter((it) => it.count === 0);
sortRanges(blockUncoveredRanges);
Util.sortOffsetRanges(blockUncoveredRanges);
functionRange.blockUncoveredRanges = blockUncoveredRanges;
functionRange.blockCoveredRanges = blockRanges.filter((it) => it.count > 0);
}
Expand Down Expand Up @@ -82,7 +73,7 @@ const getCoverageInfo = (coverageList) => {
});

// sort ranges
sortRanges(functionUncoveredRanges);
Util.sortOffsetRanges(functionUncoveredRanges);

return {
functionMap,
Expand Down
81 changes: 80 additions & 1 deletion lib/converter/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,85 @@ const filterCoverageList = (item) => {
return coverageList;
};

const initJsCoverageList = (item) => {
const coverageList = filterCoverageList(item);

// function could be covered even it is defined after an uncovered return, see case closures.js
// fix uncovered range if there are covered ranges in uncovered range

const uncoveredBlocks = [];
const uncoveredList = [];
coverageList.forEach((block) => {
block.ranges.forEach((range, i) => {
const {
count, startOffset, endOffset
} = range;

if (i === 0) {
// check only first level
if (count > 0) {
const inUncoveredRange = Util.findInRanges(startOffset, endOffset, uncoveredBlocks, 'startOffset', 'endOffset');
if (inUncoveredRange) {
if (!inUncoveredRange.coveredList) {
inUncoveredRange.coveredList = [];
uncoveredList.push(inUncoveredRange);
}
inUncoveredRange.coveredList.push(range);
}
}
} else {
if (count === 0) {
uncoveredBlocks.push({
startOffset,
endOffset,

ranges: block.ranges,
index: i,
count
});
}
}
});
});

if (uncoveredList.length) {
uncoveredList.forEach((it) => {
const {
ranges, index, count, coveredList
} = it;

// remove previous range first
const args = [index, 1];

Util.sortOffsetRanges(coveredList);
let startOffset = it.startOffset;
coveredList.forEach((cov) => {
if (cov.startOffset > startOffset) {
args.push({
startOffset,
endOffset: cov.startOffset,
count
});
}
startOffset = cov.endOffset;
});

if (it.endOffset > startOffset) {
args.push({
startOffset,
endOffset: it.endOffset,
count
});
}

ranges.splice.apply(ranges, args);

});
}

return coverageList;
};

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

const isCoveredInRanges = (range, uncoveredBytes) => {
Expand Down Expand Up @@ -1439,7 +1518,7 @@ const convertV8List = (v8list, options) => {
let coverageList = [];
let astInfo;
if (js) {
coverageList = filterCoverageList(item);
coverageList = initJsCoverageList(item);
// remove original functions
if (!Util.isDebug()) {
delete item.functions;
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ const Util = {
};
},

// offset key
sortOffsetRanges: (ranges) => {
ranges.sort((a, b) => {
if (a.startOffset === b.startOffset) {
return a.endOffset - b.endOffset;
}
return a.startOffset - b.startOffset;
});
},

forEachFile: function(dir, extList, callback) {
if (!fs.existsSync(dir)) {
return;
Expand Down
10 changes: 5 additions & 5 deletions test/snapshot/esbuild.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "79.40 %",
"bytes": "80.30 %",
"statements": "82.57 %",
"branches": "59.27 %",
"functions": "82.61 %",
"lines": "69.34 %"
"lines": "70.36 %"
},
"files": {
"src/comments.js": {
Expand Down Expand Up @@ -140,9 +140,9 @@
"functions": "71.43 %",
"branches": "50.00 %",
"statements": "61.90 %",
"lines": "32.14 %",
"bytes": "33.11 %",
"uncoveredLines": "8-32"
"lines": "57.14 %",
"bytes": "59.90 %",
"uncoveredLines": "8-16,19-21,26-28"
},
"src/index.js": {
"functions": "50.00 %",
Expand Down
10 changes: 5 additions & 5 deletions test/snapshot/merge.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "72.76 %",
"bytes": "73.44 %",
"statements": "77.07 %",
"branches": "59.46 %",
"functions": "71.00 %",
"lines": "62.68 %"
"lines": "63.47 %"
},
"files": {
"test/mock/src/async.js": {
Expand All @@ -20,9 +20,9 @@
"functions": "66.67 %",
"branches": "50.00 %",
"statements": "60.00 %",
"lines": "32.14 %",
"bytes": "33.11 %",
"uncoveredLines": "8-32"
"lines": "57.14 %",
"bytes": "59.90 %",
"uncoveredLines": "8-16,19-21,26-28"
},
"test/mock/src/comments.js": {
"functions": "",
Expand Down
16 changes: 8 additions & 8 deletions test/snapshot/minify.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@
"bytes": "100.00 %",
"uncoveredLines": ""
},
"localhost-8130/with-map/bootstrap.min.css": {
"functions": "",
"branches": "",
"statements": "",
"lines": "50.00 %",
"bytes": "1.55 %",
"uncoveredLines": "6"
},
"localhost-8130/css/style.css": {
"functions": "",
"branches": "",
Expand All @@ -72,6 +64,14 @@
"bytes": "",
"uncoveredLines": ""
},
"localhost-8130/with-map/bootstrap.min.css": {
"functions": "",
"branches": "",
"statements": "",
"lines": "50.00 %",
"bytes": "1.55 %",
"uncoveredLines": "6"
},
"monocart-v8/webpack/universalModuleDefinition": {
"functions": "100.00 %",
"branches": "50.00 %",
Expand Down
10 changes: 5 additions & 5 deletions test/snapshot/puppeteer.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "72.80 %",
"bytes": "73.59 %",
"statements": "81.20 %",
"branches": "59.60 %",
"functions": "77.63 %",
"lines": "62.68 %"
"lines": "63.57 %"
},
"files": {
"test/mock/src/async.js": {
Expand All @@ -20,9 +20,9 @@
"functions": "66.67 %",
"branches": "50.00 %",
"statements": "60.00 %",
"lines": "32.14 %",
"bytes": "33.11 %",
"uncoveredLines": "8-32"
"lines": "57.14 %",
"bytes": "59.90 %",
"uncoveredLines": "8-16,19-21,26-28"
},
"test/mock/src/comments.js": {
"functions": "",
Expand Down
10 changes: 5 additions & 5 deletions test/snapshot/rollup.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "79.40 %",
"bytes": "80.30 %",
"statements": "81.22 %",
"branches": "59.27 %",
"functions": "78.38 %",
"lines": "69.34 %"
"lines": "70.36 %"
},
"files": {
"src/comments.js": {
Expand Down Expand Up @@ -140,9 +140,9 @@
"functions": "66.67 %",
"branches": "50.00 %",
"statements": "60.00 %",
"lines": "32.14 %",
"bytes": "33.11 %",
"uncoveredLines": "8-32"
"lines": "57.14 %",
"bytes": "59.90 %",
"uncoveredLines": "8-16,19-21,26-28"
},
"src/index.js": {
"functions": "46.15 %",
Expand Down
10 changes: 5 additions & 5 deletions test/snapshot/swc.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "79.40 %",
"bytes": "80.30 %",
"statements": "81.72 %",
"branches": "59.27 %",
"functions": "78.38 %",
"lines": "69.34 %"
"lines": "70.36 %"
},
"files": {
"src/async.js": {
Expand Down Expand Up @@ -68,9 +68,9 @@
"functions": "66.67 %",
"branches": "50.00 %",
"statements": "60.00 %",
"lines": "32.14 %",
"bytes": "33.11 %",
"uncoveredLines": "8-32"
"lines": "57.14 %",
"bytes": "59.90 %",
"uncoveredLines": "8-16,19-21,26-28"
},
"src/comments.js": {
"functions": "",
Expand Down
10 changes: 5 additions & 5 deletions test/snapshot/v8.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "72.80 %",
"bytes": "73.59 %",
"statements": "81.20 %",
"branches": "59.60 %",
"functions": "77.63 %",
"lines": "62.68 %"
"lines": "63.57 %"
},
"files": {
"test/mock/src/async.js": {
Expand All @@ -20,9 +20,9 @@
"functions": "66.67 %",
"branches": "50.00 %",
"statements": "60.00 %",
"lines": "32.14 %",
"bytes": "33.11 %",
"uncoveredLines": "8-32"
"lines": "57.14 %",
"bytes": "59.90 %",
"uncoveredLines": "8-16,19-21,26-28"
},
"test/mock/src/comments.js": {
"functions": "",
Expand Down

0 comments on commit a4ca6ec

Please sign in to comment.