From b778eef540a72a1a4fb7259b1e9acc68e74144e3 Mon Sep 17 00:00:00 2001 From: cenfun Date: Sun, 17 Dec 2023 11:24:47 +0800 Subject: [PATCH] add comments for v8 --- lib/converter/converter.js | 40 ++++++++++++++++++++++++++++--- package.json | 2 +- packages/v8/package.json | 2 +- packages/v8/src/utils/coverage.js | 4 ++-- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/lib/converter/converter.js b/lib/converter/converter.js index 9e449ab7..20a0a834 100644 --- a/lib/converter/converter.js +++ b/lib/converter/converter.js @@ -19,8 +19,18 @@ const InfoFunction = require('./info-function.js'); // ======================================================================================================== const getSourceAstInfo = (positionMapping) => { + const comments = []; const ast = acornLoose.parse(positionMapping.source, { - ecmaVersion: 'latest' + ecmaVersion: 'latest', + onComment: (block, text, start, end) => { + // console.log(type, text, start, end); + comments.push({ + // block, + // text, + start, + end + }); + } }); const functions = []; @@ -32,11 +42,15 @@ const getSourceAstInfo = (positionMapping) => { }); // sort + comments.sort((a, b) => { + return a.start - b.start; + }); functions.sort((a, b) => { return a.start - b.start; }); return { + comments, functions }; }; @@ -76,9 +90,17 @@ const initFileCoverage = (positionMapping, type, sourcePath) => { // add all functions const functionMap = new Map(); const functions = []; + + let comments = []; + // only for js if (type === 'js') { const astInfo = getSourceAstInfo(positionMapping); + + // comments + comments = astInfo.comments; + + // functions astInfo.functions.forEach((node) => { const sLoc = positionMapping.offsetToLocation(node.start); const eLoc = positionMapping.offsetToLocation(node.end); @@ -97,6 +119,10 @@ const initFileCoverage = (positionMapping, type, sourcePath) => { functions.push(functionInfo); }); + } else { + // css comments + // TODO + } return { @@ -113,7 +139,9 @@ const initFileCoverage = (positionMapping, type, sourcePath) => { commentLines, functions, - functionMap + functionMap, + + comments }; }; @@ -192,7 +220,9 @@ const getFileCoverage = (coverage, sourcePath) => { functions, branches, - ranges + ranges, + + comments } = coverage; // istanbul @@ -227,6 +257,7 @@ const getFileCoverage = (coverage, sourcePath) => { // v8 const v8Coverage = { ranges: dedupeCountRanges(ranges), + comments, summary: { functions: calculateV8Functions(functions), lines: calculateV8Lines(lines, blankLines, commentLines) @@ -461,6 +492,7 @@ const unpackDistSource = (item, state) => { const { v8Coverage, istanbulCoverage } = getFileCoverage(coverage, sourcePath); state.coverageData[sourcePath] = istanbulCoverage; item.ranges = v8Coverage.ranges; + item.comments = v8Coverage.comments; item.summary = v8Coverage.summary; }; @@ -666,6 +698,7 @@ const unpackSourceMap = async (item, state, options) => { const { v8Coverage, istanbulCoverage } = getFileCoverage(coverage, sourcePath); state.coverageData[sourcePath] = istanbulCoverage; const ranges = v8Coverage.ranges; + const comments = v8Coverage.comments; const summary = v8Coverage.summary; // add file item @@ -679,6 +712,7 @@ const unpackSourceMap = async (item, state, options) => { sourcePath, distFile, ranges, + comments, summary, source }; diff --git a/package.json b/package.json index 0a5bc7b4..e5f27023 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "istanbul-reports": "^3.1.6", "lz-utils": "^2.0.1", "monocart-code-viewer": "^1.0.9", - "monocart-formatter": "^1.0.8", + "monocart-formatter": "^2.0.0", "turbogrid": "^3.0.12" }, "devDependencies": { diff --git a/packages/v8/package.json b/packages/v8/package.json index 27ade8eb..96195376 100644 --- a/packages/v8/package.json +++ b/packages/v8/package.json @@ -9,7 +9,7 @@ "license": "MIT", "dependencies": { "monocart-code-viewer": "^1.0.9", - "monocart-formatter": "^1.0.8", + "monocart-formatter": "^2.0.0", "turbogrid": "^3.0.12" }, "devDependencies": { diff --git a/packages/v8/src/utils/coverage.js b/packages/v8/src/utils/coverage.js index 746f02e5..02ed5c77 100644 --- a/packages/v8/src/utils/coverage.js +++ b/packages/v8/src/utils/coverage.js @@ -13,9 +13,9 @@ class CoverageParser { this.mapping = mapping; const formattedLines = mapping.formattedLines; - const commentLines = mapping.commentLines; - const blankLines = mapping.blankLines; + const blankLines = formattedLines.filter((it) => it.blank); + const commentLines = []; commentLines.forEach((lineIndex) => { this.uncoveredLines[lineIndex] = 'comment'; });