Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2004 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 package com.google.javascript.jscomp;
17
18 import com.google.javascript.rhino.Node;
19
20 import javax.annotation.Nullable;
21
22 /**
23 * Compile error description
24 *
25 */
26 public class JSError {
27 /** A type of the error */
28 private final DiagnosticType type;
29
30 /** Description of the error */
31 public final String description;
32
33 /** Name of the source */
34 public final String sourceName;
35
36 /** Node where the warning occurred. */
37 final Node node;
38
39 /** Line number of the source */
40 public final int lineNumber;
41
42 /** @deprecated Use #getDefaultLevel */
43 @Deprecated
44 public final CheckLevel level;
45
46 private final CheckLevel defaultLevel;
47
48 // character number
49 private final int charno;
50
51 //
52 // JSError.make - static factory methods for creating JSError objects
53 //
54 // The general form of the arguments is
55 //
56 // [source location] [level] DiagnosticType [argument ...]
57 //
58 // This order echos a typical command line diagnostic. Source location
59 // arguments are arranged to be sources of information in the order
60 // file-line-column.
61 //
62 // If the level is not given, it is taken from the level of the
63 // DiagnosticType.
64
65
66 /**
67 * Creates a JSError with no source information
68 *
69 * @param type The DiagnosticType
70 * @param arguments Arguments to be incorporated into the message
71 */
72 public static JSError make(DiagnosticType type, String... arguments) {
73 return new JSError(null, null, -1, -1, type, null, arguments);
74 }
75
76 /**
77 * Creates a JSError at a given source location
78 *
79 * @param sourceName The source file name
80 * @param lineno Line number with source file, or -1 if unknown
81 * @param charno Column number within line, or -1 for whole line.
82 * @param type The DiagnosticType
83 * @param arguments Arguments to be incorporated into the message
84 */
85 public static JSError make(String sourceName, int lineno, int charno,
86 DiagnosticType type, String... arguments) {
87 return new JSError(sourceName, null, lineno, charno, type, null, arguments);
88 }
89
90 /**
91 * Creates a JSError at a given source location
92 *
93 * @param sourceName The source file name
94 * @param lineno Line number with source file, or -1 if unknown
95 * @param charno Column number within line, or -1 for whole line.
96 * @param type The DiagnosticType
97 * @param arguments Arguments to be incorporated into the message
98 */
99 public static JSError make(String sourceName, int lineno, int charno,
100 CheckLevel level, DiagnosticType type, String... arguments) {
101 return new JSError(
102 sourceName, null, lineno, charno, type, level, arguments);
103 }
104
105 /**
106 * Creates a JSError from a file and Node position.
107 *
108 * @param sourceName The source file name
109 * @param n Determines the line and char position within the source file name
110 * @param type The DiagnosticType
111 * @param arguments Arguments to be incorporated into the message
112 */
113 public static JSError make(String sourceName, Node n,
114 DiagnosticType type, String... arguments) {
115 return new JSError(sourceName, n, type, arguments);
116 }
117
118 /**
119 * Creates a JSError from a file and Node position.
120 *
121 * @param n Determines the line and char position and source file name
122 * @param type The DiagnosticType
123 * @param arguments Arguments to be incorporated into the message
124 */
125 public static JSError make(Node n, DiagnosticType type, String... arguments) {
126 return new JSError(n.getSourceFileName(), n, type, arguments);
127 }
128
129 /**
130 * Creates a JSError from a file and Node position.
131 *
132 * @param sourceName The source file name
133 * @param n Determines the line and char position within the source file name
134 * @param type The DiagnosticType
135 * @param arguments Arguments to be incorporated into the message
136 */
137 public static JSError make(String sourceName, Node n, CheckLevel level,
138 DiagnosticType type, String... arguments) {
139
140 return new JSError(sourceName, n, n.getLineno(), n.getCharno(), type, level,
141 arguments);
142 }
143
144 //
145 // JSError constructors
146 //
147
148 /**
149 * Creates a JSError at a CheckLevel for a source file location.
150 * Private to avoid any entanglement with code outside of the compiler.
151 */
152 private JSError(
153 String sourceName, @Nullable Node node, int lineno, int charno,
154 DiagnosticType type, CheckLevel level, String... arguments) {
155 this.type = type;
156 this.node = node;
157 this.description = type.format.format(arguments);
158 this.lineNumber = lineno;
159 this.charno = charno;
160 this.sourceName = sourceName;
161 this.defaultLevel = level == null ? type.level : level;
162 this.level = level == null ? type.level : level;
163 }
164
165 /**
166 * Creates a JSError for a source file location. Private to avoid
167 * any entanglement with code outside of the compiler.
168 */
169 private JSError(String sourceName, @Nullable Node node,
170 DiagnosticType type, String... arguments) {
171 this(sourceName,
172 node,
173 (node != null) ? node.getLineno() : -1,
174 (node != null) ? node.getCharno() : -1,
175 type, null, arguments);
176 }
177
178 public DiagnosticType getType() {
179 return type;
180 }
181
182 /**
183 * Format a message at the given level.
184 *
185 * @return the formatted message or {@code null}
186 */
187 public String format(CheckLevel level, MessageFormatter formatter) {
188 switch (level) {
189 case ERROR:
190 return formatter.formatError(this);
191
192 case WARNING:
193 return formatter.formatWarning(this);
194
195 default:
196 return null;
197 }
198 }
199
200 @Override
201 public String toString() {
202 // TODO(user): remove custom toString.
203 return type.key + ". " + description + " at " +
204 (sourceName != null && sourceName.length() > 0 ?
205 sourceName : "(unknown source)") + " line " +
206 (lineNumber != -1 ? String.valueOf(lineNumber) : "(unknown line)") +
207 " : " + (charno != -1 ? String.valueOf(charno) : "(unknown column)");
208 }
209
210 /**
211 * Get the character number.
212 */
213 public int getCharno() {
214 return charno;
215 }
216
217 /**
218 * Get the line number. One-based.
219 */
220 public int getLineNumber() {
221 return lineNumber;
222 }
223
224 /**
225 * @return the offset of the region the Error applies to, or -1 if the offset
226 * is unknown.
227 */
228 public int getNodeSourceOffset() {
229 return node != null ? node.getSourceOffset() : -1;
230 }
231
232 /**
233 * @return the length of the region the Error applies to, or 0 if the length
234 * is unknown.
235 */
236 public int getNodeLength() {
237 return node != null ? node.getLength() : 0;
238 }
239
240 /** The default level, before any of the WarningsGuards are applied. */
241 public CheckLevel getDefaultLevel() {
242 return defaultLevel;
243 }
244
245 @Override
246 public boolean equals(Object o) {
247 // Generated by Intellij IDEA
248 if (this == o) {
249 return true;
250 }
251 if (o == null || getClass() != o.getClass()) {
252 return false;
253 }
254
255 JSError jsError = (JSError) o;
256
257 if (charno != jsError.charno) {
258 return false;
259 }
260 if (lineNumber != jsError.lineNumber) {
261 return false;
262 }
263 if (!description.equals(jsError.description)) {
264 return false;
265 }
266 if (defaultLevel != jsError.defaultLevel) {
267 return false;
268 }
269 if (sourceName != null ? !sourceName.equals(jsError.sourceName)
270 : jsError.sourceName != null) {
271 return false;
272 }
273 if (!type.equals(jsError.type)) {
274 return false;
275 }
276
277 return true;
278 }
279
280 @Override
281 public int hashCode() {
282 // Generated by Intellij IDEA
283 int result = type.hashCode();
284 result = 31 * result + description.hashCode();
285 result = 31 * result + (sourceName != null ? sourceName.hashCode() : 0);
286 result = 31 * result + lineNumber;
287 result = 31 * result + defaultLevel.hashCode();
288 result = 31 * result + charno;
289 return result;
290 }
291 }