Skip to content

Commit

Permalink
add comments for v8
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Dec 17, 2023
1 parent ac45d5c commit b778eef
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
40 changes: 37 additions & 3 deletions lib/converter/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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
};
};
Expand Down Expand Up @@ -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);
Expand All @@ -97,6 +119,10 @@ const initFileCoverage = (positionMapping, type, sourcePath) => {
functions.push(functionInfo);
});

} else {
// css comments
// TODO

}

return {
Expand All @@ -113,7 +139,9 @@ const initFileCoverage = (positionMapping, type, sourcePath) => {
commentLines,

functions,
functionMap
functionMap,

comments
};
};

Expand Down Expand Up @@ -192,7 +220,9 @@ const getFileCoverage = (coverage, sourcePath) => {
functions,
branches,

ranges
ranges,

comments
} = coverage;

// istanbul
Expand Down Expand Up @@ -227,6 +257,7 @@ const getFileCoverage = (coverage, sourcePath) => {
// v8
const v8Coverage = {
ranges: dedupeCountRanges(ranges),
comments,
summary: {
functions: calculateV8Functions(functions),
lines: calculateV8Lines(lines, blankLines, commentLines)
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -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
Expand All @@ -679,6 +712,7 @@ const unpackSourceMap = async (item, state, options) => {
sourcePath,
distFile,
ranges,
comments,
summary,
source
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/v8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions packages/v8/src/utils/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
});
Expand Down

0 comments on commit b778eef

Please sign in to comment.