From 71b57b9c8c7604660460032316d8192aeee660ef Mon Sep 17 00:00:00 2001 From: cenfun Date: Sun, 24 Dec 2023 11:43:37 +0800 Subject: [PATCH] fix locations sort --- lib/converter/ast.js | 66 +++++++++++++++++++++++++++-------------- test/mock/src/branch.js | 17 +++++------ 2 files changed, 52 insertions(+), 31 deletions(-) diff --git a/lib/converter/ast.js b/lib/converter/ast.js index 827d6131..2bbf7aca 100644 --- a/lib/converter/ast.js +++ b/lib/converter/ast.js @@ -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; @@ -155,32 +176,13 @@ 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; @@ -188,6 +190,8 @@ const handleBranches = (branches, cache) => { return; } + // support for tf2 || a || 1; + // "a" is in "|| a" if (isInUncoveredRanges(item, uncoveredBlockRanges)) { item.count = 0; return; @@ -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 @@ -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); @@ -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 @@ -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; }); diff --git a/test/mock/src/branch.js b/test/mock/src/branch.js index 5dd05e41..32690d12 100644 --- a/test/mock/src/branch.js +++ b/test/mock/src/branch.js @@ -1,4 +1,3 @@ - // 4 x 10 = 40 ( (count if) x 2 ) const IfStatement_11 = (tf1, tf2) => { @@ -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); };