tree.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. var 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(function (k) {
  29. var 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(function (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(function (k) {
  49. var f = 'on' + k;
  50. CompositeVisitor.prototype[f] = function (node, state) {
  51. this.visitors.forEach(function (v) {
  52. v[f](node, state);
  53. });
  54. };
  55. });
  56. function Node() {
  57. }
  58. /* istanbul ignore next: abstract method */
  59. Node.prototype.getQualifiedName = function () {
  60. throw new Error('getQualifiedName must be overridden');
  61. };
  62. /* istanbul ignore next: abstract method */
  63. Node.prototype.getRelativeName = function () {
  64. throw new Error('getRelativeName must be overridden');
  65. };
  66. /* istanbul ignore next: abstract method */
  67. Node.prototype.isRoot = function () {
  68. return !this.getParent();
  69. };
  70. /* istanbul ignore next: abstract method */
  71. Node.prototype.getParent = function () {
  72. throw new Error('getParent must be overridden');
  73. };
  74. /* istanbul ignore next: abstract method */
  75. Node.prototype.getChildren = function () {
  76. throw new Error('getChildren must be overridden');
  77. };
  78. /* istanbul ignore next: abstract method */
  79. Node.prototype.isSummary = function () {
  80. throw new Error('isSummary must be overridden');
  81. };
  82. /* istanbul ignore next: abstract method */
  83. Node.prototype.getCoverageSummary = function (/* filesOnly */) {
  84. throw new Error('getCoverageSummary must be overridden');
  85. };
  86. /* istanbul ignore next: abstract method */
  87. Node.prototype.getFileCoverage = function () {
  88. throw new Error('getFileCoverage must be overridden');
  89. };
  90. /**
  91. * visit all nodes depth-first from this node down. Note that `onStart`
  92. * and `onEnd` are never called on the visitor even if the current
  93. * node is the root of the tree.
  94. * @param visitor a full visitor that is called during tree traversal
  95. * @param state optional state that is passed around
  96. */
  97. Node.prototype.visit = function (visitor, state) {
  98. var that = this,
  99. visitChildren = function () {
  100. that.getChildren().forEach(function (child) {
  101. child.visit(visitor, state);
  102. });
  103. };
  104. if (this.isSummary()) {
  105. visitor.onSummary(this, state);
  106. } else {
  107. visitor.onDetail(this, state);
  108. }
  109. visitChildren();
  110. if (this.isSummary()) {
  111. visitor.onSummaryEnd(this, state);
  112. }
  113. };
  114. /**
  115. * abstract base class for a coverage tree.
  116. * @constructor
  117. */
  118. function Tree() {
  119. }
  120. /**
  121. * returns the root node of the tree
  122. */
  123. /* istanbul ignore next: abstract method */
  124. Tree.prototype.getRoot = function () {
  125. throw new Error('getRoot must be overridden');
  126. };
  127. /**
  128. * visits the tree depth-first with the supplied partial visitor
  129. * @param visitor - a potentially partial visitor
  130. * @param state - the state to be passed around during tree traversal
  131. */
  132. Tree.prototype.visit = function (visitor, state) {
  133. if (!(visitor instanceof Visitor)) {
  134. visitor = new Visitor(visitor);
  135. }
  136. visitor.onStart(this.getRoot(), state);
  137. this.getRoot().visit(visitor, state);
  138. visitor.onEnd(this.getRoot(), state);
  139. };
  140. module.exports = {
  141. Tree: Tree,
  142. Node: Node,
  143. Visitor: Visitor,
  144. CompositeVisitor: CompositeVisitor
  145. };