Skip to content

Commit

Permalink
sort functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Dec 15, 2023
1 parent 2e750ca commit 2a1ec50
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions lib/converter/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,28 @@ const updateLinesCount = (lineMap, sLoc, eLoc, count) => {

};

const getSourceAstInfo = (positionMapping) => {
const ast = acornLoose.parse(positionMapping.source, {
ecmaVersion: 'latest'
});

const functions = [];
acornWalk.simple(ast, {
// Function include FunctionDeclaration, ArrowFunctionExpression, FunctionExpression
Function(node) {
functions.push(node);
}
});

// sort
functions.sort((a, b) => {
return a.start - b.start;
});

return {
functions
};
};

const initFileCoverage = (positionMapping, type, sourcePath) => {

Expand Down Expand Up @@ -215,29 +237,25 @@ const initFileCoverage = (positionMapping, type, sourcePath) => {
const functions = [];
// only for js
if (type === 'js') {
const ast = acornLoose.parse(positionMapping.source, {
ecmaVersion: 'latest'
});
acornWalk.simple(ast, {
// Function include FunctionDeclaration, ArrowFunctionExpression, FunctionExpression
Function(node) {
const sLoc = positionMapping.offsetToLocation(node.start);
const eLoc = positionMapping.offsetToLocation(node.end);
const functionName = node.id && node.id.name;
const functionInfo = new InfoFunction(sLoc, eLoc, 0, functionName);

const { line, column } = sLoc;

let funLineMap = functionMap.get(line);
if (!funLineMap) {
funLineMap = new Map();
functionMap.set(line, funLineMap);
}
funLineMap.set(column, functionInfo);

functions.push(functionInfo);
const astInfo = getSourceAstInfo(positionMapping);
astInfo.functions.forEach((node) => {
const sLoc = positionMapping.offsetToLocation(node.start);
const eLoc = positionMapping.offsetToLocation(node.end);
const functionName = node.id && node.id.name;
const functionInfo = new InfoFunction(sLoc, eLoc, 0, functionName);

const { line, column } = sLoc;

let funLineMap = functionMap.get(line);
if (!funLineMap) {
funLineMap = new Map();
functionMap.set(line, funLineMap);
}
funLineMap.set(column, functionInfo);

functions.push(functionInfo);
});

}

return {
Expand Down

0 comments on commit 2a1ec50

Please sign in to comment.