Skip to content

Commit

Permalink
fixed branch coverage issue for static async function (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Aug 6, 2024
1 parent a5c0e65 commit 3b468a2
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 91 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.2
- fixed branch coverage issue for `static async` function (#67)

- 2.10.1
- fixed hits UI issue (skip indent)
- fixed ranges for original bytes
Expand Down
40 changes: 39 additions & 1 deletion lib/converter/ast-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ const getRootFunctionState = (ast, coverageInfo) => {
return rootState;
};

// eslint-disable-next-line complexity
const findFunctionRange = (item, coverageInfo) => {

const { node, reverseParents } = item;
Expand All @@ -645,13 +646,49 @@ const findFunctionRange = (item, coverageInfo) => {
return functionRange;
}

// `static async` case:
// [1]static [2]async [3]covered(active) {
// v8 start: 2
// ast start: 1,3

// fixed by async or static

// handle for static async
if (node.async) {
// 'async '.length
const asyncLen = 6;
const asyncStart = start - asyncLen;
// console.log(asyncStart, 'asyncStart ===============================================');
const asyncRange = getFunctionRange(asyncStart, end, type, coverageInfo);
if (asyncRange) {
return asyncRange;
}
}

// try if class MethodDefinition
// 0 is function self
const parent = reverseParents[1];
if (parent && parent.type === 'MethodDefinition') {
return getFunctionRange(parent.start, parent.end, parent.type, coverageInfo);

const parentRange = getFunctionRange(parent.start, parent.end, parent.type, coverageInfo);
if (parentRange) {
return parentRange;
}

if (parent.static) {
// 'static '.length
const staticLen = 7;
const staticStart = parent.start + staticLen;
// console.log(staticStart, ' staticStart ============================================');
const staticRange = getFunctionRange(staticStart, parent.end, parent.type, coverageInfo);
if (staticRange) {
return staticRange;
}
}

}


};

const collectAstInfo = (ast, coverageInfo) => {
Expand All @@ -669,6 +706,7 @@ const collectAstInfo = (ast, coverageInfo) => {
const {
start, end, id
} = node;

const bodyStart = node.body.start;
const bodyEnd = node.body.end;
const functionName = id && id.name;
Expand Down
4 changes: 3 additions & 1 deletion lib/converter/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ const getCoverageInfo = (coverageList) => {

functionMap.set(functionRange.startOffset, functionRange);
if (functionName) {
functionNameMap.set(functionRange.startOffset + functionName.length, functionRange);
// can not handle `async functionName`
const possibleNameOffset = functionRange.startOffset + functionName.length;
functionNameMap.set(possibleNameOffset, functionRange);
}

if (functionName === '<static_initializer>') {
Expand Down
15 changes: 14 additions & 1 deletion test/mock/src/statics.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line max-classes-per-file
class ClassWithStaticInitializationBlock {
static staticProperty1 = 'Property 1';
static staticProperty2;
Expand Down Expand Up @@ -27,6 +28,18 @@ class ClassWithStaticInitializationBlock {
}
}

module.exports = () => {
class StaticAsync {
static async covered(active) {
// should be covered
if (active) {
return 1;
}
return 2;
}
}

module.exports = async () => {
new ClassWithStaticInitializationBlock();
await StaticAsync.covered(false);
await StaticAsync.covered(true);
};
14 changes: 7 additions & 7 deletions test/snapshot/cli.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "53.32 %",
"statements": "56.20 %",
"branches": "52.27 %",
"functions": "50.00 %",
"lines": "44.89 %"
"bytes": "52.61 %",
"statements": "55.60 %",
"branches": "51.88 %",
"functions": "49.45 %",
"lines": "44.35 %"
},
"files": {
"test/mock/node/lib/app.js": {
Expand Down Expand Up @@ -203,8 +203,8 @@
"statements": "0.00 %",
"lines": "0.00 %",
"bytes": "0.00 %",
"uncoveredLines": "1-32",
"extras": "18b,20b,24b,29b,33b"
"uncoveredLines": "2-45",
"extras": "1c,19b,21b,25b,30b,33c,40b,46b"
},
"test/mock/src/typescript.ts": {
"functions": "100.00 %",
Expand Down
24 changes: 12 additions & 12 deletions test/snapshot/esbuild.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "82.06 %",
"statements": "83.11 %",
"branches": "59.27 %",
"functions": "83.51 %",
"lines": "71.37 %"
"bytes": "82.34 %",
"statements": "83.30 %",
"branches": "59.60 %",
"functions": "83.67 %",
"lines": "71.76 %"
},
"files": {
"src/async.js": {
Expand Down Expand Up @@ -162,13 +162,13 @@
"extras": "2b,7b,8c,12b,17b,22b,29b,33b"
},
"src/statics.js": {
"functions": "83.33 %",
"branches": "50.00 %",
"statements": "84.21 %",
"lines": "75.00 %",
"bytes": "81.41 %",
"uncoveredLines": "9,11-13,25-27",
"extras": "18b,20b,24b,29b,33b"
"functions": "85.71 %",
"branches": "66.67 %",
"statements": "88.00 %",
"lines": "81.58 %",
"bytes": "86.96 %",
"uncoveredLines": "10,12-14,26-28",
"extras": "1c,19b,21b,25b,30b,33c,40b,46b"
},
"src/typescript.ts": {
"functions": "100.00 %",
Expand Down
18 changes: 9 additions & 9 deletions test/snapshot/istanbul.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"type": "istanbul",
"summary": {
"lines": "81.94 %",
"statements": "81.87 %",
"functions": "76.54 %",
"branches": "58.01 %"
"lines": "82.12 %",
"statements": "82.05 %",
"functions": "76.82 %",
"branches": "58.33 %"
},
"files": {
"async.js": {
Expand Down Expand Up @@ -144,11 +144,11 @@
"extras": ""
},
"statics.js": {
"lines": "76.92 %",
"functions": "75.00 %",
"statements": "76.92 %",
"branches": "50.00 %",
"uncoveredLines": "9,12,26",
"lines": "83.33 %",
"functions": "80.00 %",
"statements": "83.33 %",
"branches": "66.66 %",
"uncoveredLines": "10,13,27",
"extras": ""
},
"typescript.ts": {
Expand Down
24 changes: 12 additions & 12 deletions test/snapshot/merge.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "75.22 %",
"statements": "77.70 %",
"branches": "59.46 %",
"functions": "72.12 %",
"lines": "64.49 %"
"bytes": "75.52 %",
"statements": "77.92 %",
"branches": "59.73 %",
"functions": "72.38 %",
"lines": "64.87 %"
},
"files": {
"my-vm-filename.js": {
Expand Down Expand Up @@ -216,13 +216,13 @@
"extras": "2b,7b,8c,12b,17b,22b,29b,33b"
},
"test/mock/src/statics.js": {
"functions": "80.00 %",
"branches": "50.00 %",
"statements": "76.92 %",
"lines": "75.00 %",
"bytes": "81.41 %",
"uncoveredLines": "9,11-13,25-27",
"extras": "18b,20b,24b,29b,33b"
"functions": "83.33 %",
"branches": "66.67 %",
"statements": "84.21 %",
"lines": "81.58 %",
"bytes": "86.96 %",
"uncoveredLines": "10,12-14,26-28",
"extras": "1c,19b,21b,25b,30b,33c,40b,46b"
},
"test/mock/src/typescript.ts": {
"functions": "100.00 %",
Expand Down
24 changes: 12 additions & 12 deletions test/snapshot/puppeteer.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "75.63 %",
"statements": "81.78 %",
"branches": "59.60 %",
"functions": "78.75 %",
"lines": "64.71 %"
"bytes": "75.96 %",
"statements": "82.00 %",
"branches": "59.92 %",
"functions": "79.01 %",
"lines": "65.13 %"
},
"files": {
"test/mock/css/style.css": {
Expand Down Expand Up @@ -171,13 +171,13 @@
"extras": "2b,7b,8c,12b,17b,22b,29b,33b"
},
"test/mock/src/statics.js": {
"functions": "80.00 %",
"branches": "50.00 %",
"statements": "76.92 %",
"lines": "75.00 %",
"bytes": "81.41 %",
"uncoveredLines": "9,11-13,25-27",
"extras": "18b,20b,24b,29b,33b"
"functions": "83.33 %",
"branches": "66.67 %",
"statements": "84.21 %",
"lines": "81.58 %",
"bytes": "86.96 %",
"uncoveredLines": "10,12-14,26-28",
"extras": "1c,19b,21b,25b,30b,33c,40b,46b"
},
"test/mock/src/typescript.ts": {
"functions": "100.00 %",
Expand Down
24 changes: 12 additions & 12 deletions test/snapshot/rollup.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "82.06 %",
"statements": "81.78 %",
"branches": "59.27 %",
"functions": "79.49 %",
"lines": "71.37 %"
"bytes": "82.34 %",
"statements": "82.00 %",
"branches": "59.60 %",
"functions": "79.75 %",
"lines": "71.76 %"
},
"files": {
"src/async.js": {
Expand Down Expand Up @@ -162,13 +162,13 @@
"extras": "2b,7b,8c,12b,17b,22b,29b,33b"
},
"src/statics.js": {
"functions": "80.00 %",
"branches": "50.00 %",
"statements": "76.92 %",
"lines": "75.00 %",
"bytes": "81.41 %",
"uncoveredLines": "9,11-13,25-27",
"extras": "18b,20b,24b,29b,33b"
"functions": "83.33 %",
"branches": "66.67 %",
"statements": "84.21 %",
"lines": "81.58 %",
"bytes": "86.96 %",
"uncoveredLines": "10,12-14,26-28",
"extras": "1c,19b,21b,25b,30b,33c,40b,46b"
},
"src/typescript.ts": {
"functions": "100.00 %",
Expand Down
24 changes: 12 additions & 12 deletions test/snapshot/swc.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "82.06 %",
"statements": "82.39 %",
"branches": "59.27 %",
"functions": "79.75 %",
"lines": "71.37 %"
"bytes": "82.34 %",
"statements": "82.63 %",
"branches": "59.60 %",
"functions": "80.00 %",
"lines": "71.76 %"
},
"files": {
"src/async.js": {
Expand Down Expand Up @@ -162,13 +162,13 @@
"extras": "2b,7b,8c,12b,17b,22b,29b,33b"
},
"src/statics.js": {
"functions": "80.00 %",
"branches": "50.00 %",
"statements": "82.35 %",
"lines": "75.00 %",
"bytes": "81.41 %",
"uncoveredLines": "9,11-13,25-27",
"extras": "18b,20b,24b,29b,33b"
"functions": "83.33 %",
"branches": "66.67 %",
"statements": "87.50 %",
"lines": "81.58 %",
"bytes": "86.96 %",
"uncoveredLines": "10,12-14,26-28",
"extras": "1c,19b,21b,25b,30b,33c,40b,46b"
},
"src/typescript.ts": {
"functions": "100.00 %",
Expand Down
24 changes: 12 additions & 12 deletions test/snapshot/v8.snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "v8",
"summary": {
"bytes": "75.63 %",
"statements": "81.78 %",
"branches": "59.60 %",
"functions": "78.75 %",
"lines": "64.71 %"
"bytes": "75.96 %",
"statements": "82.00 %",
"branches": "59.92 %",
"functions": "79.01 %",
"lines": "65.13 %"
},
"files": {
"test/mock/css/style.css": {
Expand Down Expand Up @@ -171,13 +171,13 @@
"extras": "2b,7b,8c,12b,17b,22b,29b,33b"
},
"test/mock/src/statics.js": {
"functions": "80.00 %",
"branches": "50.00 %",
"statements": "76.92 %",
"lines": "75.00 %",
"bytes": "81.41 %",
"uncoveredLines": "9,11-13,25-27",
"extras": "18b,20b,24b,29b,33b"
"functions": "83.33 %",
"branches": "66.67 %",
"statements": "84.21 %",
"lines": "81.58 %",
"bytes": "86.96 %",
"uncoveredLines": "10,12-14,26-28",
"extras": "1c,19b,21b,25b,30b,33c,40b,46b"
},
"test/mock/src/typescript.ts": {
"functions": "100.00 %",
Expand Down

0 comments on commit 3b468a2

Please sign in to comment.