| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
import com.google.javascript.rhino.Node; |
| 19 |
|
| 20 |
import javax.annotation.Nullable; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class JSError { |
| 27 |
|
| 28 |
private final DiagnosticType type; |
| 29 |
|
| 30 |
|
| 31 |
public final String description; |
| 32 |
|
| 33 |
|
| 34 |
public final String sourceName; |
| 35 |
|
| 36 |
|
| 37 |
final Node node; |
| 38 |
|
| 39 |
|
| 40 |
public final int lineNumber; |
| 41 |
|
| 42 |
|
| 43 |
@Deprecated |
| 44 |
public final CheckLevel level; |
| 45 |
|
| 46 |
private final CheckLevel defaultLevel; |
| 47 |
|
| 48 |
|
| 49 |
private final int charno; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 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 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 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 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 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 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 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 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 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 |
|
| 167 |
|
| 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 |
|
| 184 |
|
| 185 |
|
| 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 |
|
| 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 |
|
| 212 |
|
| 213 |
public int getCharno() { |
| 214 |
return charno; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public int getLineNumber() { |
| 221 |
return lineNumber; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public int getNodeSourceOffset() { |
| 229 |
return node != null ? node.getSourceOffset() : -1; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public int getNodeLength() { |
| 237 |
return node != null ? node.getLength() : 0; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
public CheckLevel getDefaultLevel() { |
| 242 |
return defaultLevel; |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public boolean equals(Object o) { |
| 247 |
|
| 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 |
|
| 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 |
} |