Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Side by Side Diff: pkg/analyzer_cli/lib/src/error_formatter.dart

Issue 1459683003: `analyzer_cli` move to SDK. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: master merge Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer_cli/lib/src/driver.dart ('k') | pkg/analyzer_cli/lib/src/options.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library analyzer_cli.src.error_formatter;
6
7 import 'package:analyzer/src/generated/engine.dart';
8 import 'package:analyzer/src/generated/error.dart';
9 import 'package:analyzer/src/generated/source.dart';
10 import 'package:analyzer_cli/src/analyzer_impl.dart';
11 import 'package:analyzer_cli/src/options.dart';
12
13 /// Allows any [AnalysisError].
14 bool _anyError(AnalysisError error) => true;
15
16 /// Returns `true` if [AnalysisError] should be printed.
17 typedef bool _ErrorFilter(AnalysisError error);
18
19 /// Helper for formatting [AnalysisError]s.
20 /// The two format options are a user consumable format and a machine consumable format.
21 class ErrorFormatter {
22 final StringSink out;
23 final CommandLineOptions options;
24 final _ErrorFilter errorFilter;
25
26 ErrorFormatter(this.out, this.options, [this.errorFilter = _anyError]);
27
28 void formatError(
29 Map<AnalysisError, LineInfo> errorToLine, AnalysisError error) {
30 Source source = error.source;
31 LineInfo_Location location = errorToLine[error].getLocation(error.offset);
32 int length = error.length;
33 ErrorSeverity severity =
34 AnalyzerImpl.computeSeverity(error, options);
35 if (options.machineFormat) {
36 if (severity == ErrorSeverity.WARNING && options.warningsAreFatal) {
37 severity = ErrorSeverity.ERROR;
38 }
39 out.write(severity);
40 out.write('|');
41 out.write(error.errorCode.type);
42 out.write('|');
43 out.write(error.errorCode.name);
44 out.write('|');
45 out.write(escapePipe(source.fullName));
46 out.write('|');
47 out.write(location.lineNumber);
48 out.write('|');
49 out.write(location.columnNumber);
50 out.write('|');
51 out.write(length);
52 out.write('|');
53 out.write(escapePipe(error.message));
54 } else {
55 String errorType = severity.displayName;
56 if (error.errorCode.type == ErrorType.HINT ||
57 error.errorCode.type == ErrorType.LINT) {
58 errorType = error.errorCode.type.displayName;
59 }
60 // [warning] 'foo' is not a... (/Users/.../tmp/foo.dart, line 1, col 2)
61 out.write('[$errorType] ${error.message} ');
62 out.write('(${source.fullName}');
63 out.write(', line ${location.lineNumber}, col ${location.columnNumber})');
64 }
65 out.writeln();
66 }
67
68 void formatErrors(List<AnalysisErrorInfo> errorInfos) {
69 var errors = new List<AnalysisError>();
70 var errorToLine = new Map<AnalysisError, LineInfo>();
71 for (AnalysisErrorInfo errorInfo in errorInfos) {
72 for (AnalysisError error in errorInfo.errors) {
73 if (errorFilter(error)) {
74 errors.add(error);
75 errorToLine[error] = errorInfo.lineInfo;
76 }
77 }
78 }
79 // Sort errors.
80 errors.sort((AnalysisError error1, AnalysisError error2) {
81 // Severity.
82 ErrorSeverity severity1 =
83 AnalyzerImpl.computeSeverity(error1, options);
84 ErrorSeverity severity2 =
85 AnalyzerImpl.computeSeverity(error2, options);
86 int compare = severity2.compareTo(severity1);
87 if (compare != 0) {
88 return compare;
89 }
90 // Path.
91 compare = Comparable.compare(error1.source.fullName.toLowerCase(),
92 error2.source.fullName.toLowerCase());
93 if (compare != 0) {
94 return compare;
95 }
96 // Offset.
97 return error1.offset - error2.offset;
98 });
99 // Format errors.
100 int errorCount = 0;
101 int warnCount = 0;
102 int hintCount = 0;
103 int lintCount = 0;
104 for (AnalysisError error in errors) {
105 ErrorSeverity severity =
106 AnalyzerImpl.computeSeverity(error, options);
107 if (severity == ErrorSeverity.ERROR) {
108 errorCount++;
109 } else if (severity == ErrorSeverity.WARNING) {
110 if (options.warningsAreFatal) {
111 errorCount++;
112 } else {
113 warnCount++;
114 }
115 } else if (error.errorCode.type == ErrorType.HINT) {
116 hintCount++;
117 } else if (error.errorCode.type == ErrorType.LINT) {
118 lintCount++;
119 }
120 formatError(errorToLine, error);
121 }
122 // Print statistics.
123 if (!options.machineFormat) {
124 var hasErrors = errorCount != 0;
125 var hasWarns = warnCount != 0;
126 var hasHints = hintCount != 0;
127 var hasLints = lintCount != 0;
128 bool hasContent = false;
129 if (hasErrors) {
130 out.write(errorCount);
131 out.write(' ');
132 out.write(pluralize("error", errorCount));
133 hasContent = true;
134 }
135 if (hasWarns) {
136 if (hasContent) {
137 if (!hasHints && !hasLints) {
138 out.write(' and ');
139 } else {
140 out.write(", ");
141 }
142 }
143 out.write(warnCount);
144 out.write(' ');
145 out.write(pluralize("warning", warnCount));
146 hasContent = true;
147 }
148 if (hasHints) {
149 if (hasContent) {
150 if (!hasLints) {
151 out.write(' and ');
152 } else {
153 out.write(", ");
154 }
155 }
156 out.write(hintCount);
157 out.write(' ');
158 out.write(pluralize("hint", hintCount));
159 hasContent = true;
160 }
161 if (hasLints) {
162 if (hasContent) {
163 out.write(" and ");
164 }
165 out.write(lintCount);
166 out.write(' ');
167 out.write(pluralize("lint", lintCount));
168 hasContent = true;
169 }
170 if (hasContent) {
171 out.writeln(" found.");
172 } else {
173 out.writeln("No issues found");
174 }
175 }
176 }
177
178 static String escapePipe(String input) {
179 var result = new StringBuffer();
180 for (var c in input.codeUnits) {
181 if (c == '\\' || c == '|') {
182 result.write('\\');
183 }
184 result.writeCharCode(c);
185 }
186 return result.toString();
187 }
188
189 static String pluralize(String word, int count) {
190 if (count == 1) {
191 return word;
192 } else {
193 return word + "s";
194 }
195 }
196 }
OLDNEW
« no previous file with comments | « pkg/analyzer_cli/lib/src/driver.dart ('k') | pkg/analyzer_cli/lib/src/options.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698