diff --git a/CHANGELOG.md b/CHANGELOG.md index 4be5941..6f0abaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/converter/ast-visitor.js b/lib/converter/ast-visitor.js index 3df8d04..b25fb5b 100644 --- a/lib/converter/ast-visitor.js +++ b/lib/converter/ast-visitor.js @@ -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}`; @@ -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 @@ -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); }); @@ -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; + } }); }; diff --git a/test/snapshot/cli.snapshot.json b/test/snapshot/cli.snapshot.json index e0397e2..23d1489 100644 --- a/test/snapshot/cli.snapshot.json +++ b/test/snapshot/cli.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "53.59 %", "statements": "56.61 %", - "branches": "52.04 %", + "branches": "52.42 %", "functions": "50.54 %", "lines": "45.58 %" }, @@ -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 %", diff --git a/test/snapshot/esbuild.snapshot.json b/test/snapshot/esbuild.snapshot.json index 0e003c9..880c8aa 100644 --- a/test/snapshot/esbuild.snapshot.json +++ b/test/snapshot/esbuild.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "82.76 %", "statements": "83.73 %", - "branches": "59.68 %", + "branches": "60.08 %", "functions": "84.16 %", "lines": "72.48 %" }, @@ -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 %", diff --git a/test/snapshot/merge.snapshot.json b/test/snapshot/merge.snapshot.json index a550739..0ac79b2 100644 --- a/test/snapshot/merge.snapshot.json +++ b/test/snapshot/merge.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "75.97 %", "statements": "78.39 %", - "branches": "59.80 %", + "branches": "60.13 %", "functions": "72.90 %", "lines": "65.58 %" }, @@ -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 %", diff --git a/test/snapshot/node-api.snapshot.json b/test/snapshot/node-api.snapshot.json index 833801e..d4bcbc1 100644 --- a/test/snapshot/node-api.snapshot.json +++ b/test/snapshot/node-api.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "83.86 %", "statements": "84.58 %", - "branches": "60.52 %", + "branches": "60.94 %", "functions": "88.89 %", "lines": "71.53 %" }, @@ -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 %", diff --git a/test/snapshot/node-cdp.snapshot.json b/test/snapshot/node-cdp.snapshot.json index 833801e..d4bcbc1 100644 --- a/test/snapshot/node-cdp.snapshot.json +++ b/test/snapshot/node-cdp.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "83.86 %", "statements": "84.58 %", - "branches": "60.52 %", + "branches": "60.94 %", "functions": "88.89 %", "lines": "71.53 %" }, @@ -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 %", diff --git a/test/snapshot/node-env.snapshot.json b/test/snapshot/node-env.snapshot.json index 833801e..d4bcbc1 100644 --- a/test/snapshot/node-env.snapshot.json +++ b/test/snapshot/node-env.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "83.86 %", "statements": "84.58 %", - "branches": "60.52 %", + "branches": "60.94 %", "functions": "88.89 %", "lines": "71.53 %" }, @@ -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 %", diff --git a/test/snapshot/node-fgc.snapshot.json b/test/snapshot/node-fgc.snapshot.json index 833801e..d4bcbc1 100644 --- a/test/snapshot/node-fgc.snapshot.json +++ b/test/snapshot/node-fgc.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "83.86 %", "statements": "84.58 %", - "branches": "60.52 %", + "branches": "60.94 %", "functions": "88.89 %", "lines": "71.53 %" }, @@ -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 %", diff --git a/test/snapshot/node-ins.snapshot.json b/test/snapshot/node-ins.snapshot.json index 833801e..d4bcbc1 100644 --- a/test/snapshot/node-ins.snapshot.json +++ b/test/snapshot/node-ins.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "83.86 %", "statements": "84.58 %", - "branches": "60.52 %", + "branches": "60.94 %", "functions": "88.89 %", "lines": "71.53 %" }, @@ -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 %", diff --git a/test/snapshot/puppeteer.snapshot.json b/test/snapshot/puppeteer.snapshot.json index 217d49b..68d6e53 100644 --- a/test/snapshot/puppeteer.snapshot.json +++ b/test/snapshot/puppeteer.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "76.46 %", "statements": "82.44 %", - "branches": "60.00 %", + "branches": "60.39 %", "functions": "79.52 %", "lines": "65.92 %" }, @@ -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 %", diff --git a/test/snapshot/rollup.snapshot.json b/test/snapshot/rollup.snapshot.json index b79c50f..a84269e 100644 --- a/test/snapshot/rollup.snapshot.json +++ b/test/snapshot/rollup.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "82.76 %", "statements": "82.46 %", - "branches": "59.68 %", + "branches": "60.08 %", "functions": "80.25 %", "lines": "72.48 %" }, @@ -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 %", diff --git a/test/snapshot/swc.snapshot.json b/test/snapshot/swc.snapshot.json index 7483b1c..96aa1cc 100644 --- a/test/snapshot/swc.snapshot.json +++ b/test/snapshot/swc.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "82.76 %", "statements": "83.05 %", - "branches": "59.68 %", + "branches": "60.08 %", "functions": "80.49 %", "lines": "72.48 %" }, @@ -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 %", diff --git a/test/snapshot/v8.snapshot.json b/test/snapshot/v8.snapshot.json index 217d49b..68d6e53 100644 --- a/test/snapshot/v8.snapshot.json +++ b/test/snapshot/v8.snapshot.json @@ -3,7 +3,7 @@ "summary": { "bytes": "76.46 %", "statements": "82.44 %", - "branches": "60.00 %", + "branches": "60.39 %", "functions": "79.52 %", "lines": "65.92 %" }, @@ -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 %",