tree.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. 'use strict';
  6. const util = require('util');
  7. /**
  8. * An object with methods that are called during the traversal of the coverage tree.
  9. * A visitor has the following methods that are called during tree traversal.
  10. *
  11. * * `onStart(root, state)` - called before traversal begins
  12. * * `onSummary(node, state)` - called for every summary node
  13. * * `onDetail(node, state)` - called for every detail node
  14. * * `onSummaryEnd(node, state)` - called after all children have been visited for
  15. * a summary node.
  16. * * `onEnd(root, state)` - called after traversal ends
  17. *
  18. * @param delegate - a partial visitor that only implements the methods of interest
  19. * The visitor object supplies the missing methods as noops. For example, reports
  20. * that only need the final coverage summary need implement `onStart` and nothing
  21. * else. Reports that use only detailed coverage information need implement `onDetail`
  22. * and nothing else.
  23. * @constructor
  24. */
  25. function Visitor(delegate) {
  26. this.delegate = delegate;
  27. }
  28. ['Start', 'End', 'Summary', 'SummaryEnd', 'Detail'].forEach(k => {
  29. const f = 'on' + k;
  30. Visitor.prototype[f] = function(node, state) {
  31. if (this.delegate[f] && typeof this.delegate[f] === 'function') {
  32. this.delegate[f].call(this.delegate, node, state);
  33. }
  34. };
  35. });
  36. function CompositeVisitor(visitors) {
  37. if (!Array.isArray(visitors)) {
  38. visitors = [visitors];
  39. }
  40. this.visitors = visitors.map(v => {
  41. if (v instanceof Visitor) {
  42. return v;
  43. }
  44. return new Visitor(v);
  45. });
  46. }
  47. util.inherits(CompositeVisitor, Visitor);
  48. ['Start', 'Summary', 'SummaryEnd', 'Detail', 'End'].forEach(k => {
  49. const f = 'on' + k;
  50. CompositeVisitor.prototype[f] = function(node, state) {
  51. this.visitors.forEach(v => {
  52. v[f](node, state);
  53. });
  54. };
  55. });
  56. function Node() {}
  57. /* istanbul ignore next: abstract method */
  58. Node.prototype.getQualifiedName = function() {
  59. throw new Error('getQualifiedName must be overridden');
  60. };
  61. /* istanbul ignore next: abstract method */
  62. Node.prototype.getRelativeName = function() {
  63. throw new Error('getRelativeName must be overridden');
  64. };
  65. /* istanbul ignore next: abstract method */
  66. Node.prototype.isRoot = function() {
  67. return !this.getParent();
  68. };
  69. /* istanbul ignore next: abstract method */
  70. Node.prototype.getParent = function() {
  71. throw new Error('getParent must be overridden');
  72. };
  73. /* istanbul ignore next: abstract method */
  74. Node.prototype.getChildren = function() {
  75. throw new Error('getChildren must be overridden');
  76. };
  77. /* istanbul ignore next: abstract method */
  78. Node.prototype.isSummary = function() {
  79. throw new Error('isSummary must be overridden');
  80. };
  81. /* istanbul ignore next: abstract method */
  82. Node.prototype.getCoverageSummary = function(/* filesOnly */) {
  83. throw new Error('getCoverageSummary must be overridden');
  84. };
  85. /* istanbul ignore next: abstract method */
  86. Node.prototype.getFileCoverage = function() {
  87. throw new Error('getFileCoverage must be overridden');
  88. };
  89. /**
  90. * visit all nodes depth-first from this node down. Note that `onStart`
  91. * and `onEnd` are never called on the visitor even if the current
  92. * node is the root of the tree.
  93. * @param visitor a full visitor that is called during tree traversal
  94. * @param state optional state that is passed around
  95. */
  96. Node.prototype.visit = function(visitor, state) {
  97. if (this.isSummary()) {
  98. visitor.onSummary(this, state);
  99. } else {
  100. visitor.onDetail(this, state);
  101. }
  102. this.getChildren().forEach(child => {
  103. child.visit(visitor, state);
  104. });
  105. if (this.isSummary()) {
  106. visitor.onSummaryEnd(this, state);
  107. }
  108. };
  109. /**
  110. * abstract base class for a coverage tree.
  111. * @constructor
  112. */
  113. function Tree() {}
  114. /**
  115. * returns the root node of the tree
  116. */
  117. /* istanbul ignore next: abstract method */
  118. Tree.prototype.getRoot = function() {
  119. throw new Error('getRoot must be overridden');
  120. };
  121. /**
  122. * visits the tree depth-first with the supplied partial visitor
  123. * @param visitor - a potentially partial visitor
  124. * @param state - the state to be passed around during tree traversal
  125. */
  126. Tree.prototype.visit = function(visitor, state) {
  127. if (!(visitor instanceof Visitor)) {
  128. visitor = new Visitor(visitor);
  129. }
  130. visitor.onStart(this.getRoot(), state);
  131. this.getRoot().visit(visitor, state);
  132. visitor.onEnd(this.getRoot(), state);
  133. };
  134. module.exports = {
  135. Tree,
  136. Node,
  137. Visitor,
  138. CompositeVisitor
  139. };