From 3b468a2a90d98393552598f34d6e6667fdb9c397 Mon Sep 17 00:00:00 2001 From: cenfun Date: Tue, 6 Aug 2024 21:48:47 +0800 Subject: [PATCH] fixed branch coverage issue for `static async` function (#67) --- CHANGELOG.md | 3 ++ lib/converter/ast-visitor.js | 40 ++++++++++++++++++++++++++- lib/converter/ast.js | 4 ++- test/mock/src/statics.js | 15 +++++++++- test/snapshot/cli.snapshot.json | 14 +++++----- test/snapshot/esbuild.snapshot.json | 24 ++++++++-------- test/snapshot/istanbul.snapshot.json | 18 ++++++------ test/snapshot/merge.snapshot.json | 24 ++++++++-------- test/snapshot/puppeteer.snapshot.json | 24 ++++++++-------- test/snapshot/rollup.snapshot.json | 24 ++++++++-------- test/snapshot/swc.snapshot.json | 24 ++++++++-------- test/snapshot/v8.snapshot.json | 24 ++++++++-------- 12 files changed, 147 insertions(+), 91 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12f0ccc7..4be5941a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/converter/ast-visitor.js b/lib/converter/ast-visitor.js index 1bde9a83..3df8d04f 100644 --- a/lib/converter/ast-visitor.js +++ b/lib/converter/ast-visitor.js @@ -632,6 +632,7 @@ const getRootFunctionState = (ast, coverageInfo) => { return rootState; }; +// eslint-disable-next-line complexity const findFunctionRange = (item, coverageInfo) => { const { node, reverseParents } = item; @@ -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) => { @@ -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; diff --git a/lib/converter/ast.js b/lib/converter/ast.js index b9240262..6da4b413 100644 --- a/lib/converter/ast.js +++ b/lib/converter/ast.js @@ -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 === '') { diff --git a/test/mock/src/statics.js b/test/mock/src/statics.js index d1ee0fbb..75a4423c 100644 --- a/test/mock/src/statics.js +++ b/test/mock/src/statics.js @@ -1,3 +1,4 @@ +// eslint-disable-next-line max-classes-per-file class ClassWithStaticInitializationBlock { static staticProperty1 = 'Property 1'; static staticProperty2; @@ -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); }; diff --git a/test/snapshot/cli.snapshot.json b/test/snapshot/cli.snapshot.json index 41124cf0..e51f01c9 100644 --- a/test/snapshot/cli.snapshot.json +++ b/test/snapshot/cli.snapshot.json @@ -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": { @@ -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 %", diff --git a/test/snapshot/esbuild.snapshot.json b/test/snapshot/esbuild.snapshot.json index 5ef89a3b..9d82d91f 100644 --- a/test/snapshot/esbuild.snapshot.json +++ b/test/snapshot/esbuild.snapshot.json @@ -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": { @@ -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 %", diff --git a/test/snapshot/istanbul.snapshot.json b/test/snapshot/istanbul.snapshot.json index 3df3c426..72adb20b 100644 --- a/test/snapshot/istanbul.snapshot.json +++ b/test/snapshot/istanbul.snapshot.json @@ -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": { @@ -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": { diff --git a/test/snapshot/merge.snapshot.json b/test/snapshot/merge.snapshot.json index 8aca76a4..c4052fb0 100644 --- a/test/snapshot/merge.snapshot.json +++ b/test/snapshot/merge.snapshot.json @@ -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": { @@ -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 %", diff --git a/test/snapshot/puppeteer.snapshot.json b/test/snapshot/puppeteer.snapshot.json index 62a8aeac..61589c13 100644 --- a/test/snapshot/puppeteer.snapshot.json +++ b/test/snapshot/puppeteer.snapshot.json @@ -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": { @@ -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 %", diff --git a/test/snapshot/rollup.snapshot.json b/test/snapshot/rollup.snapshot.json index abadb685..39fabb02 100644 --- a/test/snapshot/rollup.snapshot.json +++ b/test/snapshot/rollup.snapshot.json @@ -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": { @@ -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 %", diff --git a/test/snapshot/swc.snapshot.json b/test/snapshot/swc.snapshot.json index 27f73acd..577e255c 100644 --- a/test/snapshot/swc.snapshot.json +++ b/test/snapshot/swc.snapshot.json @@ -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": { @@ -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 %", diff --git a/test/snapshot/v8.snapshot.json b/test/snapshot/v8.snapshot.json index 62a8aeac..61589c13 100644 --- a/test/snapshot/v8.snapshot.json +++ b/test/snapshot/v8.snapshot.json @@ -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": { @@ -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 %",