diff --git a/lib/converter/converter.js b/lib/converter/converter.js index b94525ef..4c7eb073 100644 --- a/lib/converter/converter.js +++ b/lib/converter/converter.js @@ -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) => { @@ -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 {