UNPKG

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