normalize.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { compareFunctionCovs, compareRangeCovs, compareScriptCovs } from "./compare";
  2. import { RangeTree } from "./range-tree";
  3. import { FunctionCov, ProcessCov, ScriptCov } from "./types";
  4. /**
  5. * Normalizes a process coverage.
  6. *
  7. * Sorts the scripts alphabetically by `url`.
  8. * Reassigns script ids: the script at index `0` receives `"0"`, the script at
  9. * index `1` receives `"1"` etc.
  10. * This does not normalize the script coverages.
  11. *
  12. * @param processCov Process coverage to normalize.
  13. */
  14. export function normalizeProcessCov(processCov: ProcessCov): void {
  15. processCov.result.sort(compareScriptCovs);
  16. for (const [scriptId, scriptCov] of processCov.result.entries()) {
  17. scriptCov.scriptId = scriptId.toString(10);
  18. }
  19. }
  20. /**
  21. * Normalizes a process coverage deeply.
  22. *
  23. * Normalizes the script coverages deeply, then normalizes the process coverage
  24. * itself.
  25. *
  26. * @param processCov Process coverage to normalize.
  27. */
  28. export function deepNormalizeProcessCov(processCov: ProcessCov): void {
  29. for (const scriptCov of processCov.result) {
  30. deepNormalizeScriptCov(scriptCov);
  31. }
  32. normalizeProcessCov(processCov);
  33. }
  34. /**
  35. * Normalizes a script coverage.
  36. *
  37. * Sorts the function by root range (pre-order sort).
  38. * This does not normalize the function coverages.
  39. *
  40. * @param scriptCov Script coverage to normalize.
  41. */
  42. export function normalizeScriptCov(scriptCov: ScriptCov): void {
  43. scriptCov.functions.sort(compareFunctionCovs);
  44. }
  45. /**
  46. * Normalizes a script coverage deeply.
  47. *
  48. * Normalizes the function coverages deeply, then normalizes the script coverage
  49. * itself.
  50. *
  51. * @param scriptCov Script coverage to normalize.
  52. */
  53. export function deepNormalizeScriptCov(scriptCov: ScriptCov): void {
  54. for (const funcCov of scriptCov.functions) {
  55. normalizeFunctionCov(funcCov);
  56. }
  57. normalizeScriptCov(scriptCov);
  58. }
  59. /**
  60. * Normalizes a function coverage.
  61. *
  62. * Sorts the ranges (pre-order sort).
  63. * TODO: Tree-based normalization of the ranges.
  64. *
  65. * @param funcCov Function coverage to normalize.
  66. */
  67. export function normalizeFunctionCov(funcCov: FunctionCov): void {
  68. funcCov.ranges.sort(compareRangeCovs);
  69. const tree: RangeTree = RangeTree.fromSortedRanges(funcCov.ranges)!;
  70. normalizeRangeTree(tree);
  71. funcCov.ranges = tree.toRanges();
  72. }
  73. /**
  74. * @internal
  75. */
  76. export function normalizeRangeTree(tree: RangeTree): void {
  77. tree.normalize();
  78. }