Skip to content

Commit 3877356

Browse files
committed
fixed branch coverage issue for switch/case (#68)
1 parent e70894b commit 3877356

14 files changed

+75
-29
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Changelog
22

3+
- 2.10.3
4+
- fixed branch coverage issue for `switch/case` (#68)
5+
36
- 2.10.2
47
- fixed branch coverage issue for `static async` function (#67)
58

lib/converter/ast-visitor.js

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ const createBranchGroup = (type, node, parents, branchMap) => {
146146
};
147147

148148
const addBranch = (group, node, locationMap) => {
149-
const { start, end } = node;
149+
const {
150+
start, end, type
151+
} = node;
150152

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

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

164+
if (type === 'SwitchCase') {
165+
166+
// console.log(node);
167+
168+
// check break
169+
if (node.consequent) {
170+
const breakItem = node.consequent.find((it) => it.type === 'BreakStatement');
171+
if (breakItem) {
172+
branchInfo.hasBreak = true;
173+
}
174+
}
175+
176+
// check default
177+
if (!node.test) {
178+
branchInfo.isDefault = true;
179+
}
180+
}
181+
162182
group.locations.push(branchInfo);
163183

164184
// update group end
@@ -183,12 +203,14 @@ const addNoneBranch = (group) => {
183203
const updateBlockLocations = (locations) => {
184204
const noBlockList = [];
185205
let blockCount = 0;
186-
locations.forEach((item) => {
206+
locations.forEach((item, i) => {
187207
if (item.block) {
188208
item.count = item.block.count;
189209
blockCount += item.count;
190210
return;
191211
}
212+
// for calculate mo break branches
213+
item.index = i;
192214
noBlockList.push(item);
193215
});
194216

@@ -240,13 +262,34 @@ const LogicalExpression = (group, parentCount) => {
240262
};
241263

242264
const SwitchStatement = (group, parentCount) => {
243-
const { noBlockList, blockCount } = updateBlockLocations(group.locations);
265+
266+
const locations = group.locations;
267+
268+
const { noBlockList, blockCount } = updateBlockLocations(locations);
244269
if (!noBlockList.length) {
245270
return;
246271
}
247-
const count = parentCount - blockCount;
272+
273+
// calculate switch/case count
274+
const countLeft = parentCount - blockCount;
248275
noBlockList.forEach((item) => {
249-
item.count = count;
276+
277+
let hasCount = false;
278+
279+
// check no break branches
280+
for (let i = item.index - 1; i >= 0; i--) {
281+
const b = locations[i];
282+
if (b && !b.hasBreak && b.count > 0) {
283+
item.count += b.count;
284+
hasCount = true;
285+
continue;
286+
}
287+
break;
288+
}
289+
290+
if (!hasCount) {
291+
item.count = countLeft;
292+
}
250293
});
251294
};
252295

test/snapshot/cli.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "53.59 %",
55
"statements": "56.61 %",
6-
"branches": "52.04 %",
6+
"branches": "52.42 %",
77
"functions": "50.54 %",
88
"lines": "45.58 %"
99
},
@@ -100,7 +100,7 @@
100100
},
101101
"test/mock/src/branch/switch-no-break.js": {
102102
"functions": "100.00 %",
103-
"branches": "66.67 %",
103+
"branches": "100.00 %",
104104
"statements": "100.00 %",
105105
"lines": "100.00 %",
106106
"bytes": "100.00 %",

test/snapshot/esbuild.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "82.76 %",
55
"statements": "83.73 %",
6-
"branches": "59.68 %",
6+
"branches": "60.08 %",
77
"functions": "84.16 %",
88
"lines": "72.48 %"
99
},
@@ -64,7 +64,7 @@
6464
},
6565
"src/branch/switch-no-break.js": {
6666
"functions": "100.00 %",
67-
"branches": "66.67 %",
67+
"branches": "100.00 %",
6868
"statements": "100.00 %",
6969
"lines": "100.00 %",
7070
"bytes": "100.00 %",

test/snapshot/merge.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "75.97 %",
55
"statements": "78.39 %",
6-
"branches": "59.80 %",
6+
"branches": "60.13 %",
77
"functions": "72.90 %",
88
"lines": "65.58 %"
99
},
@@ -118,7 +118,7 @@
118118
},
119119
"test/mock/src/branch/switch-no-break.js": {
120120
"functions": "100.00 %",
121-
"branches": "66.67 %",
121+
"branches": "100.00 %",
122122
"statements": "100.00 %",
123123
"lines": "100.00 %",
124124
"bytes": "100.00 %",

test/snapshot/node-api.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "83.86 %",
55
"statements": "84.58 %",
6-
"branches": "60.52 %",
6+
"branches": "60.94 %",
77
"functions": "88.89 %",
88
"lines": "71.53 %"
99
},
@@ -64,7 +64,7 @@
6464
},
6565
"monocart-coverage-reports/test/mock/src/branch/switch-no-break.js": {
6666
"functions": "100.00 %",
67-
"branches": "66.67 %",
67+
"branches": "100.00 %",
6868
"statements": "100.00 %",
6969
"lines": "100.00 %",
7070
"bytes": "100.00 %",

test/snapshot/node-cdp.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "83.86 %",
55
"statements": "84.58 %",
6-
"branches": "60.52 %",
6+
"branches": "60.94 %",
77
"functions": "88.89 %",
88
"lines": "71.53 %"
99
},
@@ -64,7 +64,7 @@
6464
},
6565
"monocart-coverage-reports/test/mock/src/branch/switch-no-break.js": {
6666
"functions": "100.00 %",
67-
"branches": "66.67 %",
67+
"branches": "100.00 %",
6868
"statements": "100.00 %",
6969
"lines": "100.00 %",
7070
"bytes": "100.00 %",

test/snapshot/node-env.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "83.86 %",
55
"statements": "84.58 %",
6-
"branches": "60.52 %",
6+
"branches": "60.94 %",
77
"functions": "88.89 %",
88
"lines": "71.53 %"
99
},
@@ -64,7 +64,7 @@
6464
},
6565
"monocart-coverage-reports/test/mock/src/branch/switch-no-break.js": {
6666
"functions": "100.00 %",
67-
"branches": "66.67 %",
67+
"branches": "100.00 %",
6868
"statements": "100.00 %",
6969
"lines": "100.00 %",
7070
"bytes": "100.00 %",

test/snapshot/node-fgc.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "83.86 %",
55
"statements": "84.58 %",
6-
"branches": "60.52 %",
6+
"branches": "60.94 %",
77
"functions": "88.89 %",
88
"lines": "71.53 %"
99
},
@@ -64,7 +64,7 @@
6464
},
6565
"monocart-coverage-reports/test/mock/src/branch/switch-no-break.js": {
6666
"functions": "100.00 %",
67-
"branches": "66.67 %",
67+
"branches": "100.00 %",
6868
"statements": "100.00 %",
6969
"lines": "100.00 %",
7070
"bytes": "100.00 %",

test/snapshot/node-ins.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "83.86 %",
55
"statements": "84.58 %",
6-
"branches": "60.52 %",
6+
"branches": "60.94 %",
77
"functions": "88.89 %",
88
"lines": "71.53 %"
99
},
@@ -64,7 +64,7 @@
6464
},
6565
"monocart-coverage-reports/test/mock/src/branch/switch-no-break.js": {
6666
"functions": "100.00 %",
67-
"branches": "66.67 %",
67+
"branches": "100.00 %",
6868
"statements": "100.00 %",
6969
"lines": "100.00 %",
7070
"bytes": "100.00 %",

test/snapshot/puppeteer.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "76.46 %",
55
"statements": "82.44 %",
6-
"branches": "60.00 %",
6+
"branches": "60.39 %",
77
"functions": "79.52 %",
88
"lines": "65.92 %"
99
},
@@ -73,7 +73,7 @@
7373
},
7474
"test/mock/src/branch/switch-no-break.js": {
7575
"functions": "100.00 %",
76-
"branches": "66.67 %",
76+
"branches": "100.00 %",
7777
"statements": "100.00 %",
7878
"lines": "100.00 %",
7979
"bytes": "100.00 %",

test/snapshot/rollup.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "82.76 %",
55
"statements": "82.46 %",
6-
"branches": "59.68 %",
6+
"branches": "60.08 %",
77
"functions": "80.25 %",
88
"lines": "72.48 %"
99
},
@@ -64,7 +64,7 @@
6464
},
6565
"src/branch/switch-no-break.js": {
6666
"functions": "100.00 %",
67-
"branches": "66.67 %",
67+
"branches": "100.00 %",
6868
"statements": "100.00 %",
6969
"lines": "100.00 %",
7070
"bytes": "100.00 %",

test/snapshot/swc.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "82.76 %",
55
"statements": "83.05 %",
6-
"branches": "59.68 %",
6+
"branches": "60.08 %",
77
"functions": "80.49 %",
88
"lines": "72.48 %"
99
},
@@ -64,7 +64,7 @@
6464
},
6565
"src/branch/switch-no-break.js": {
6666
"functions": "100.00 %",
67-
"branches": "66.67 %",
67+
"branches": "100.00 %",
6868
"statements": "100.00 %",
6969
"lines": "100.00 %",
7070
"bytes": "100.00 %",

test/snapshot/v8.snapshot.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"summary": {
44
"bytes": "76.46 %",
55
"statements": "82.44 %",
6-
"branches": "60.00 %",
6+
"branches": "60.39 %",
77
"functions": "79.52 %",
88
"lines": "65.92 %"
99
},
@@ -73,7 +73,7 @@
7373
},
7474
"test/mock/src/branch/switch-no-break.js": {
7575
"functions": "100.00 %",
76-
"branches": "66.67 %",
76+
"branches": "100.00 %",
7777
"statements": "100.00 %",
7878
"lines": "100.00 %",
7979
"bytes": "100.00 %",

0 commit comments

Comments
 (0)