Skip to content

Commit

Permalink
fixed branch coverage issue for switch/case (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Aug 13, 2024
1 parent e70894b commit 3877356
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 29 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

- 2.10.3
- fixed branch coverage issue for `switch/case` (#68)

- 2.10.2
- fixed branch coverage issue for `static async` function (#67)

Expand Down
53 changes: 48 additions & 5 deletions lib/converter/ast-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ const createBranchGroup = (type, node, parents, branchMap) => {
};

const addBranch = (group, node, locationMap) => {
const { start, end } = node;
const {
start, end, type
} = node;

const branchKey = `${group.start}_${group.end}`;

Expand All @@ -159,6 +161,24 @@ const addBranch = (group, node, locationMap) => {
count: 0
};

if (type === 'SwitchCase') {

// console.log(node);

// check break
if (node.consequent) {
const breakItem = node.consequent.find((it) => it.type === 'BreakStatement');
if (breakItem) {
branchInfo.hasBreak = true;
}
}

// check default
if (!node.test) {
branchInfo.isDefault = true;
}
}

group.locations.push(branchInfo);

// update group end
Expand All @@ -183,12 +203,14 @@ const addNoneBranch = (group) => {
const updateBlockLocations = (locations) => {
const noBlockList = [];
let blockCount = 0;
locations.forEach((item) => {
locations.forEach((item, i) => {
if (item.block) {
item.count = item.block.count;
blockCount += item.count;
return;
}
// for calculate mo break branches
item.index = i;
noBlockList.push(item);
});

Expand Down Expand Up @@ -240,13 +262,34 @@ const LogicalExpression = (group, parentCount) => {
};

const SwitchStatement = (group, parentCount) => {
const { noBlockList, blockCount } = updateBlockLocations(group.locations);

const locations = group.locations;

const { noBlockList, blockCount } = updateBlockLocations(locations);
if (!noBlockList.length) {
return;
}
const count = parentCount - blockCount;

// calculate switch/case count
const countLeft = parentCount - blockCount;
noBlockList.forEach((item) => {
item.count = count;

let hasCount = false;

// check no break branches
for (let i = item.index - 1; i >= 0; i--) {
const b = locations[i];
if (b && !b.hasBreak && b.count > 0) {
item.count += b.count;
hasCount = true;
continue;
}
break;
}

if (!hasCount) {
item.count = countLeft;
}
});
};

Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/cli.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "53.59 %",
"statements": "56.61 %",
"branches": "52.04 %",
"branches": "52.42 %",
"functions": "50.54 %",
"lines": "45.58 %"
},
Expand Down Expand Up @@ -100,7 +100,7 @@
},
"test/mock/src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/esbuild.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "82.76 %",
"statements": "83.73 %",
"branches": "59.68 %",
"branches": "60.08 %",
"functions": "84.16 %",
"lines": "72.48 %"
},
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/merge.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "75.97 %",
"statements": "78.39 %",
"branches": "59.80 %",
"branches": "60.13 %",
"functions": "72.90 %",
"lines": "65.58 %"
},
Expand Down Expand Up @@ -118,7 +118,7 @@
},
"test/mock/src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/node-api.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "83.86 %",
"statements": "84.58 %",
"branches": "60.52 %",
"branches": "60.94 %",
"functions": "88.89 %",
"lines": "71.53 %"
},
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"monocart-coverage-reports/test/mock/src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/node-cdp.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "83.86 %",
"statements": "84.58 %",
"branches": "60.52 %",
"branches": "60.94 %",
"functions": "88.89 %",
"lines": "71.53 %"
},
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"monocart-coverage-reports/test/mock/src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/node-env.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "83.86 %",
"statements": "84.58 %",
"branches": "60.52 %",
"branches": "60.94 %",
"functions": "88.89 %",
"lines": "71.53 %"
},
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"monocart-coverage-reports/test/mock/src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/node-fgc.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "83.86 %",
"statements": "84.58 %",
"branches": "60.52 %",
"branches": "60.94 %",
"functions": "88.89 %",
"lines": "71.53 %"
},
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"monocart-coverage-reports/test/mock/src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/node-ins.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "83.86 %",
"statements": "84.58 %",
"branches": "60.52 %",
"branches": "60.94 %",
"functions": "88.89 %",
"lines": "71.53 %"
},
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"monocart-coverage-reports/test/mock/src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/puppeteer.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "76.46 %",
"statements": "82.44 %",
"branches": "60.00 %",
"branches": "60.39 %",
"functions": "79.52 %",
"lines": "65.92 %"
},
Expand Down Expand Up @@ -73,7 +73,7 @@
},
"test/mock/src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/rollup.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "82.76 %",
"statements": "82.46 %",
"branches": "59.68 %",
"branches": "60.08 %",
"functions": "80.25 %",
"lines": "72.48 %"
},
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/swc.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "82.76 %",
"statements": "83.05 %",
"branches": "59.68 %",
"branches": "60.08 %",
"functions": "80.49 %",
"lines": "72.48 %"
},
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/v8.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"summary": {
"bytes": "76.46 %",
"statements": "82.44 %",
"branches": "60.00 %",
"branches": "60.39 %",
"functions": "79.52 %",
"lines": "65.92 %"
},
Expand Down Expand Up @@ -73,7 +73,7 @@
},
"test/mock/src/branch/switch-no-break.js": {
"functions": "100.00 %",
"branches": "66.67 %",
"branches": "100.00 %",
"statements": "100.00 %",
"lines": "100.00 %",
"bytes": "100.00 %",
Expand Down

0 comments on commit 3877356

Please sign in to comment.