1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | var util = require('util');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function 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 |
|
39 | function 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 |
|
51 | util.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 |
|
62 | function Node() {
|
63 | }
|
64 |
|
65 |
|
66 | Node.prototype.getQualifiedName = function () {
|
67 | throw new Error('getQualifiedName must be overridden');
|
68 | };
|
69 |
|
70 |
|
71 | Node.prototype.getRelativeName = function () {
|
72 | throw new Error('getRelativeName must be overridden');
|
73 | };
|
74 |
|
75 |
|
76 | Node.prototype.isRoot = function () {
|
77 | return !this.getParent();
|
78 | };
|
79 |
|
80 |
|
81 | Node.prototype.getParent = function () {
|
82 | throw new Error('getParent must be overridden');
|
83 | };
|
84 |
|
85 |
|
86 | Node.prototype.getChildren = function () {
|
87 | throw new Error('getChildren must be overridden');
|
88 | };
|
89 |
|
90 |
|
91 | Node.prototype.isSummary = function () {
|
92 | throw new Error('isSummary must be overridden');
|
93 | };
|
94 |
|
95 |
|
96 | Node.prototype.getCoverageSummary = function (/* filesOnly */) {
|
97 | throw new Error('getCoverageSummary must be overridden');
|
98 | };
|
99 |
|
100 |
|
101 | Node.prototype.getFileCoverage = function () {
|
102 | throw new Error('getFileCoverage must be overridden');
|
103 | };
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | Node.prototype.visit = function (visitor, state) {
|
112 |
|
113 | var that = this,
|
114 | visitChildren = function () {
|
115 | that.getChildren().forEach(function (child) {
|
116 | child.visit(visitor, state);
|
117 | });
|
118 | };
|
119 |
|
120 | if (this.isSummary()) {
|
121 | visitor.onSummary(this, state);
|
122 | } else {
|
123 | visitor.onDetail(this, state);
|
124 | }
|
125 |
|
126 | visitChildren();
|
127 |
|
128 | if (this.isSummary()) {
|
129 | visitor.onSummaryEnd(this, state);
|
130 | }
|
131 | };
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | function Tree() {
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | Tree.prototype.getRoot = function () {
|
145 | throw new Error('getRoot must be overridden');
|
146 | };
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | Tree.prototype.visit = function (visitor, state) {
|
154 | if (!(visitor instanceof Visitor)) {
|
155 | visitor = new Visitor(visitor);
|
156 | }
|
157 | visitor.onStart(this.getRoot(), state);
|
158 | this.getRoot().visit(visitor, state);
|
159 | visitor.onEnd(this.getRoot(), state);
|
160 | };
|
161 |
|
162 | module.exports = {
|
163 | Tree: Tree,
|
164 | Node: Node,
|
165 | Visitor: Visitor,
|
166 | CompositeVisitor: CompositeVisitor
|
167 | };
|