123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- const normalize_1 = require("./normalize");
- const range_tree_1 = require("./range-tree");
- function mergeProcessCovs(processCovs) {
- if (processCovs.length === 0) {
- return { result: [] };
- }
- const urlToScripts = new Map();
- for (const processCov of processCovs) {
- for (const scriptCov of processCov.result) {
- let scriptCovs = urlToScripts.get(scriptCov.url);
- if (scriptCovs === undefined) {
- scriptCovs = [];
- urlToScripts.set(scriptCov.url, scriptCovs);
- }
- scriptCovs.push(scriptCov);
- }
- }
- const result = [];
- for (const scripts of urlToScripts.values()) {
-
- result.push(mergeScriptCovs(scripts));
- }
- const merged = { result };
- normalize_1.normalizeProcessCov(merged);
- return merged;
- }
- exports.mergeProcessCovs = mergeProcessCovs;
- function mergeScriptCovs(scriptCovs) {
- if (scriptCovs.length === 0) {
- return undefined;
- }
- else if (scriptCovs.length === 1) {
- const merged = scriptCovs[0];
- normalize_1.deepNormalizeScriptCov(merged);
- return merged;
- }
- const first = scriptCovs[0];
- const scriptId = first.scriptId;
- const url = first.url;
- const rangeToFuncs = new Map();
- for (const scriptCov of scriptCovs) {
- for (const funcCov of scriptCov.functions) {
- const rootRange = stringifyFunctionRootRange(funcCov);
- let funcCovs = rangeToFuncs.get(rootRange);
- if (funcCovs === undefined ||
-
-
- (!funcCovs[0].isBlockCoverage && funcCov.isBlockCoverage)) {
- funcCovs = [];
- rangeToFuncs.set(rootRange, funcCovs);
- }
- else if (funcCovs[0].isBlockCoverage && !funcCov.isBlockCoverage) {
-
-
- continue;
- }
- funcCovs.push(funcCov);
- }
- }
- const functions = [];
- for (const funcCovs of rangeToFuncs.values()) {
-
- functions.push(mergeFunctionCovs(funcCovs));
- }
- const merged = { scriptId, url, functions };
- normalize_1.normalizeScriptCov(merged);
- return merged;
- }
- exports.mergeScriptCovs = mergeScriptCovs;
- function stringifyFunctionRootRange(funcCov) {
- const rootRange = funcCov.ranges[0];
- return `${rootRange.startOffset.toString(10)};${rootRange.endOffset.toString(10)}`;
- }
- function mergeFunctionCovs(funcCovs) {
- if (funcCovs.length === 0) {
- return undefined;
- }
- else if (funcCovs.length === 1) {
- const merged = funcCovs[0];
- normalize_1.normalizeFunctionCov(merged);
- return merged;
- }
- const functionName = funcCovs[0].functionName;
- const trees = [];
- for (const funcCov of funcCovs) {
-
-
- trees.push(range_tree_1.RangeTree.fromSortedRanges(funcCov.ranges));
- }
-
- const mergedTree = mergeRangeTrees(trees);
- normalize_1.normalizeRangeTree(mergedTree);
- const ranges = mergedTree.toRanges();
- const isBlockCoverage = !(ranges.length === 1 && ranges[0].count === 0);
- const merged = { functionName, ranges, isBlockCoverage };
-
- return merged;
- }
- exports.mergeFunctionCovs = mergeFunctionCovs;
- function mergeRangeTrees(trees) {
- if (trees.length <= 1) {
- return trees[0];
- }
- const first = trees[0];
- let delta = 0;
- for (const tree of trees) {
- delta += tree.delta;
- }
- const children = mergeRangeTreeChildren(trees);
- return new range_tree_1.RangeTree(first.start, first.end, delta, children);
- }
- class RangeTreeWithParent {
- constructor(parentIndex, tree) {
- this.parentIndex = parentIndex;
- this.tree = tree;
- }
- }
- class StartEvent {
- constructor(offset, trees) {
- this.offset = offset;
- this.trees = trees;
- }
- static compare(a, b) {
- return a.offset - b.offset;
- }
- }
- class StartEventQueue {
- constructor(queue) {
- this.queue = queue;
- this.nextIndex = 0;
- this.pendingOffset = 0;
- this.pendingTrees = undefined;
- }
- static fromParentTrees(parentTrees) {
- const startToTrees = new Map();
- for (const [parentIndex, parentTree] of parentTrees.entries()) {
- for (const child of parentTree.children) {
- let trees = startToTrees.get(child.start);
- if (trees === undefined) {
- trees = [];
- startToTrees.set(child.start, trees);
- }
- trees.push(new RangeTreeWithParent(parentIndex, child));
- }
- }
- const queue = [];
- for (const [startOffset, trees] of startToTrees) {
- queue.push(new StartEvent(startOffset, trees));
- }
- queue.sort(StartEvent.compare);
- return new StartEventQueue(queue);
- }
- setPendingOffset(offset) {
- this.pendingOffset = offset;
- }
- pushPendingTree(tree) {
- if (this.pendingTrees === undefined) {
- this.pendingTrees = [];
- }
- this.pendingTrees.push(tree);
- }
- next() {
- const pendingTrees = this.pendingTrees;
- const nextEvent = this.queue[this.nextIndex];
- if (pendingTrees === undefined) {
- this.nextIndex++;
- return nextEvent;
- }
- else if (nextEvent === undefined) {
- this.pendingTrees = undefined;
- return new StartEvent(this.pendingOffset, pendingTrees);
- }
- else {
- if (this.pendingOffset < nextEvent.offset) {
- this.pendingTrees = undefined;
- return new StartEvent(this.pendingOffset, pendingTrees);
- }
- else {
- if (this.pendingOffset === nextEvent.offset) {
- this.pendingTrees = undefined;
- for (const tree of pendingTrees) {
- nextEvent.trees.push(tree);
- }
- }
- this.nextIndex++;
- return nextEvent;
- }
- }
- }
- }
- function mergeRangeTreeChildren(parentTrees) {
- const result = [];
- const startEventQueue = StartEventQueue.fromParentTrees(parentTrees);
- const parentToNested = new Map();
- let openRange;
- while (true) {
- const event = startEventQueue.next();
- if (event === undefined) {
- break;
- }
- if (openRange !== undefined && openRange.end <= event.offset) {
- result.push(nextChild(openRange, parentToNested));
- openRange = undefined;
- }
- if (openRange === undefined) {
- let openRangeEnd = event.offset + 1;
- for (const { parentIndex, tree } of event.trees) {
- openRangeEnd = Math.max(openRangeEnd, tree.end);
- insertChild(parentToNested, parentIndex, tree);
- }
- startEventQueue.setPendingOffset(openRangeEnd);
- openRange = { start: event.offset, end: openRangeEnd };
- }
- else {
- for (const { parentIndex, tree } of event.trees) {
- if (tree.end > openRange.end) {
- const right = tree.split(openRange.end);
- startEventQueue.pushPendingTree(new RangeTreeWithParent(parentIndex, right));
- }
- insertChild(parentToNested, parentIndex, tree);
- }
- }
- }
- if (openRange !== undefined) {
- result.push(nextChild(openRange, parentToNested));
- }
- return result;
- }
- function insertChild(parentToNested, parentIndex, tree) {
- let nested = parentToNested.get(parentIndex);
- if (nested === undefined) {
- nested = [];
- parentToNested.set(parentIndex, nested);
- }
- nested.push(tree);
- }
- function nextChild(openRange, parentToNested) {
- const matchingTrees = [];
- for (const nested of parentToNested.values()) {
- if (nested.length === 1 && nested[0].start === openRange.start && nested[0].end === openRange.end) {
- matchingTrees.push(nested[0]);
- }
- else {
- matchingTrees.push(new range_tree_1.RangeTree(openRange.start, openRange.end, 0, nested));
- }
- }
- parentToNested.clear();
- return mergeRangeTrees(matchingTrees);
- }
|