Skip to content

Commit

Permalink
fix locations sort
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Dec 24, 2023
1 parent a9d1095 commit 71b57b9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
66 changes: 44 additions & 22 deletions lib/converter/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ const getFunctionCount = (functionStart, cache) => {
return 1;
};

const updateNoneElseBranchCount = (item, functionStart, cache) => {
// check if any linked ifLocation is covered
let covered = false;
let current = item.ifLocation;
while (current) {
if (current.count > 0) {
covered = true;
break;
}
current = current.ifLocation;
}

// console.log('related item', item.relatedStart, prevItem.start, prevItem.count);

if (covered) {
item.count = 0;
} else {
item.count = getFunctionCount(functionStart, cache);
}
};

const handleBranches = (branches, cache) => {

const { blockRangeMap, uncoveredBlockRanges } = cache;
Expand All @@ -155,39 +176,22 @@ const handleBranches = (branches, cache) => {

// no need count for none else branch
if (typeof start !== 'number') {

// check if any linked ifLocation is covered
let covered = false;
let current = item.ifLocation;
while (current) {
if (current.count > 0) {
covered = true;
break;
}
current = current.ifLocation;
}

// console.log('related item', item.relatedStart, prevItem.start, prevItem.count);

if (covered) {
item.count = 0;
} else {
item.count = getFunctionCount(functionStart, cache);
}

updateNoneElseBranchCount(item, functionStart, cache);
return;
}

end = item.end;

// find from coverage info
// exact matched a block
const block = blockRangeMap.get(start);
if (block) {
item.count = block.count;
// console.log(block);
return;
}

// support for tf2 || a || 1;
// "a" is in "|| a"
if (isInUncoveredRanges(item, uncoveredBlockRanges)) {
item.count = 0;
return;
Expand All @@ -196,6 +200,11 @@ const handleBranches = (branches, cache) => {
// using parent function count
item.count = getFunctionCount(functionStart, cache);

// if (branchInfo.type === 'LogicalExpression' && start > 5490 && start < 5531) {
// console.log('===================================', branchInfo.type);
// console.log(item);
// }

});

// last location end
Expand Down Expand Up @@ -295,6 +304,9 @@ const handleFunctionBranches = (fNode, functionInfo, cache) => {

const { branchMap, locationMap } = cache;

// console.log(left.start, right.start);


let branchInfo;
// link to same branch start if LogicalExpression
const prevLocation = locationMap.get(start);
Expand All @@ -309,6 +321,16 @@ const handleFunctionBranches = (fNode, functionInfo, cache) => {

addBranchLocation(branchInfo, right, functionInfo, cache);

// console.log(branchInfo.locations.map((it) => it.start));

// sort branch locations
// a || b || c
// first, left a and right c
// then, left a and right b
branchInfo.locations.sort((a, b) => {
return a.start - b.start;
});

},

// Ternary
Expand Down Expand Up @@ -428,7 +450,7 @@ const initAstCoverage = (item, coverageList, ast, functions, branches) => {
cache.branchMap.forEach((it) => {
branches.push(it);
});

// sort branches
branches.sort((a, b) => {
return a.loc.start - b.loc.start;
});
Expand Down
17 changes: 8 additions & 9 deletions test/mock/src/branch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// 4 x 10 = 40 ( (count if) x 2 )
const IfStatement_11 = (tf1, tf2) => {

Expand Down Expand Up @@ -132,23 +131,23 @@ const ConditionalExpression_00 = (tf1, tf2) => {

// 4 x 5 = 20
const LogicalExpression_11 = (tf1, tf2) => {
const a = tf1 || 0;
const b = tf2 || a || 1;
const a = tf1 || tf2;
const b = tf2 || tf1 || a;
console.log(b);
};
const LogicalExpression_10 = (tf1, tf2) => {
const a = tf1 || 0;
const b = tf2 || a || 1;
const a = tf1 || tf2;
const b = tf2 || tf1 || a;
console.log(b);
};
const LogicalExpression_01 = (tf1, tf2) => {
const a = tf1 || 0;
const b = tf2 || a || 1;
const a = tf1 || tf2;
const b = tf2 || tf1 || a;
console.log(b);
};
const LogicalExpression_00 = (tf1, tf2) => {
const a = tf1 || 0;
const b = tf2 || a || 1;
const a = tf1 || tf2;
const b = tf2 || tf1 || a;
console.log(b);
};

Expand Down

0 comments on commit 71b57b9

Please sign in to comment.