Debian Codegraph @

src/com/google/javascript/jscomp/BasicErrorManager.java

raw symbols SCIP
1 /*
2 * Copyright 2007 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.collect.Sets;
20 import com.google.javascript.jscomp.CheckLevel;
21
22 import java.util.ArrayList;
23 import java.util.Comparator;
24 import java.util.List;
25 import java.util.SortedSet;
26
27 /**
28 * <p>A basic error manager that sorts all errors and warnings reported to it to
29 * generate a sorted report when the {@link #generateReport()} method
30 * is called.</p>
31 *
32 * <p>This error manager does not produce any output, but subclasses can
33 * override the {@link #println(CheckLevel, JSError)} method to generate custom
34 * output.</p>
35 *
36 */
37 public abstract class BasicErrorManager implements ErrorManager {
38 private final SortedSet<ErrorWithLevel> messages =
39 Sets.newTreeSet(new LeveledJSErrorComparator());
40 private int errorCount = 0;
41 private int warningCount = 0;
42 private double typedPercent = 0.0;
43
44 @Override
45 public void report(CheckLevel level, JSError error) {
46 if (messages.add(new ErrorWithLevel(error, level))) {
47 if (level == CheckLevel.ERROR) {
48 errorCount++;
49 } else if (level == CheckLevel.WARNING) {
50 warningCount++;
51 }
52 }
53 }
54
55 @Override
56 public void generateReport() {
57 for (ErrorWithLevel message : messages) {
58 println(message.level, message.error);
59 }
60 printSummary();
61 }
62
63 /**
64 * Print a message with a trailing new line. This method is called by the
65 * {@link #generateReport()} method when generating messages.
66 */
67 public abstract void println(CheckLevel level, JSError error);
68
69 /**
70 * Print the summary of the compilation - number of errors and warnings.
71 */
72 protected abstract void printSummary();
73
74 @Override
75 public int getErrorCount() {
76 return errorCount;
77 }
78
79 @Override
80 public int getWarningCount() {
81 return warningCount;
82 }
83
84 @Override
85 public JSError[] getErrors() {
86 return toArray(CheckLevel.ERROR);
87 }
88
89 @Override
90 public JSError[] getWarnings() {
91 return toArray(CheckLevel.WARNING);
92 }
93
94 @Override
95 public void setTypedPercent(double typedPercent) {
96 this.typedPercent = typedPercent;
97 }
98
99 @Override
100 public double getTypedPercent() {
101 return typedPercent;
102 }
103
104 private JSError[] toArray(CheckLevel level) {
105 List<JSError> errors = new ArrayList<JSError>(messages.size());
106 for (ErrorWithLevel p : messages) {
107 if (p.level == level) {
108 errors.add(p.error);
109 }
110 }
111 return errors.toArray(new JSError[errors.size()]);
112 }
113
114 /**
115 * <p>Comparator of {@link JSError} with an associated {@link CheckLevel}.
116 * The ordering is the standard lexical ordering on the quintuple
117 * (file name, line number, {@link CheckLevel},
118 * character number, description).</p>
119 *
120 * <p>Note: this comparator imposes orderings that are inconsistent with
121 * {@link JSError#equals(Object)}.</p>
122 */
123 static final class LeveledJSErrorComparator
124 implements Comparator<ErrorWithLevel> {
125 private static final int P1_LT_P2 = -1;
126 private static final int P1_GT_P2 = 1;
127
128 @Override
129 public int compare(ErrorWithLevel p1, ErrorWithLevel p2) {
130 // null is the smallest value
131 if (p2 == null) {
132 if (p1 == null) {
133 return 0;
134 } else {
135 return P1_GT_P2;
136 }
137 }
138
139 // check level
140 if (p1.level != p2.level) {
141 return p2.level.compareTo(p1.level);
142 }
143
144 // sourceName comparison
145 String source1 = p1.error.sourceName;
146 String source2 = p2.error.sourceName;
147 if (source1 != null && source2 != null) {
148 int sourceCompare = source1.compareTo(source2);
149 if (sourceCompare != 0) {
150 return sourceCompare;
151 }
152 } else if (source1 == null && source2 != null) {
153 return P1_LT_P2;
154 } else if (source1 != null && source2 == null) {
155 return P1_GT_P2;
156 }
157 // lineno comparison
158 int lineno1 = p1.error.lineNumber;
159 int lineno2 = p2.error.lineNumber;
160 if (lineno1 != lineno2) {
161 return lineno1 - lineno2;
162 } else if (lineno1 < 0 && 0 <= lineno2) {
163 return P1_LT_P2;
164 } else if (0 <= lineno1 && lineno2 < 0) {
165 return P1_GT_P2;
166 }
167 // charno comparison
168 int charno1 = p1.error.getCharno();
169 int charno2 = p2.error.getCharno();
170 if (charno1 != charno2) {
171 return charno1 - charno2;
172 } else if (charno1 < 0 && 0 <= charno2) {
173 return P1_LT_P2;
174 } else if (0 <= charno1 && charno2 < 0) {
175 return P1_GT_P2;
176 }
177 // description
178 return p1.error.description.compareTo(p2.error.description);
179 }
180 }
181
182 static class ErrorWithLevel {
183 final JSError error;
184 final CheckLevel level;
185
186 ErrorWithLevel(JSError error, CheckLevel level) {
187 this.error = error;
188 this.level = level;
189 }
190 }
191 }