22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5+ import 'package:analyzer/dart/analysis/results.dart' ;
56import 'package:analyzer/error/error.dart' ;
67import 'package:analyzer/source/line_info.dart' ;
7- import 'package:analyzer/src/generated/engine .dart' ;
8+ import 'package:analyzer/src/dart/analysis/driver_based_analysis_context .dart' ;
89import 'package:analyzer/src/generated/source.dart' ;
910import 'package:analyzer_cli/src/ansi.dart' ;
1011import 'package:analyzer_cli/src/options.dart' ;
@@ -109,6 +110,7 @@ class CLIError implements Comparable<CLIError> {
109110 final int line;
110111 final int column;
111112 final String message;
113+ final List <ContextMessage > contextMessages;
112114 final String errorCode;
113115 final String correction;
114116 final String url;
@@ -120,6 +122,7 @@ class CLIError implements Comparable<CLIError> {
120122 this .line,
121123 this .column,
122124 this .message,
125+ this .contextMessages,
123126 this .errorCode,
124127 this .correction,
125128 this .url,
@@ -161,6 +164,14 @@ class CLIError implements Comparable<CLIError> {
161164 }
162165}
163166
167+ class ContextMessage {
168+ final String filePath;
169+ final String message;
170+ final int line;
171+ final int column;
172+ ContextMessage (this .filePath, this .message, this .line, this .column);
173+ }
174+
164175/// Helper for formatting [AnalysisError] s.
165176///
166177/// The two format options are a user consumable format and a machine consumable
@@ -181,19 +192,18 @@ abstract class ErrorFormatter {
181192 void flush ();
182193
183194 void formatError (
184- Map <AnalysisError , LineInfo > errorToLine, AnalysisError error);
195+ Map <AnalysisError , ErrorsResult > errorToLine, AnalysisError error);
185196
186- void formatErrors (List <AnalysisErrorInfo > errorInfos ) {
187- stats.unfilteredCount += errorInfos .length;
197+ void formatErrors (List <ErrorsResult > results ) {
198+ stats.unfilteredCount += results .length;
188199
189200 List <AnalysisError > errors = new List <AnalysisError >();
190- Map <AnalysisError , LineInfo > errorToLine =
191- new Map <AnalysisError , LineInfo >();
192- for (AnalysisErrorInfo errorInfo in errorInfos) {
193- for (AnalysisError error in errorInfo.errors) {
201+ Map <AnalysisError , ErrorsResult > errorToLine = {};
202+ for (ErrorsResult result in results) {
203+ for (AnalysisError error in result.errors) {
194204 if (_computeSeverity (error) != null ) {
195205 errors.add (error);
196- errorToLine[error] = errorInfo.lineInfo ;
206+ errorToLine[error] = result ;
197207 }
198208 }
199209 }
@@ -253,6 +263,11 @@ class HumanErrorFormatter extends ErrorFormatter {
253263 // If verbose, also print any associated correction and URL.
254264 if (options.verbose) {
255265 String padding = ' ' .padLeft (error.severity.length + 2 );
266+ for (var message in error.contextMessages) {
267+ out.write ('$padding ${message .message } ' );
268+ out.write ('at ${message .filePath }' );
269+ out.writeln (':${message .line }:${message .column }' );
270+ }
256271 if (error.correction != null ) {
257272 out.writeln ('$padding ${error .correction }' );
258273 }
@@ -267,9 +282,10 @@ class HumanErrorFormatter extends ErrorFormatter {
267282 }
268283
269284 void formatError (
270- Map <AnalysisError , LineInfo > errorToLine, AnalysisError error) {
285+ Map <AnalysisError , ErrorsResult > errorToLine, AnalysisError error) {
271286 Source source = error.source;
272- var location = errorToLine[error].getLocation (error.offset);
287+ var result = errorToLine[error];
288+ var location = result.lineInfo.getLocation (error.offset);
273289
274290 ErrorSeverity severity = _severityProcessor (error);
275291
@@ -300,6 +316,17 @@ class HumanErrorFormatter extends ErrorFormatter {
300316 } else {
301317 sourcePath = _relative (source.fullName);
302318 }
319+ List <ContextMessage > contextMessages = [];
320+ for (var message in error.contextMessages) {
321+ var session = result.session.analysisContext;
322+ if (session is DriverBasedAnalysisContext ) {
323+ LineInfo lineInfo =
324+ session.driver.getFileSync (message.filePath)? .lineInfo;
325+ var location = lineInfo.getLocation (message.offset);
326+ contextMessages.add (ContextMessage (message.filePath, message.message,
327+ location.lineNumber, location.columnNumber));
328+ }
329+ }
303330
304331 batchedErrors.add (new CLIError (
305332 severity: errorType,
@@ -308,6 +335,7 @@ class HumanErrorFormatter extends ErrorFormatter {
308335 line: location.lineNumber,
309336 column: location.columnNumber,
310337 message: message,
338+ contextMessages: contextMessages,
311339 errorCode: error.errorCode.name.toLowerCase (),
312340 correction: error.correction,
313341 url: error.errorCode.url,
@@ -330,13 +358,13 @@ class MachineErrorFormatter extends ErrorFormatter {
330358 void flush () {}
331359
332360 void formatError (
333- Map <AnalysisError , LineInfo > errorToLine, AnalysisError error) {
361+ Map <AnalysisError , ErrorsResult > errorToLine, AnalysisError error) {
334362 // Ensure we don't over-report (#36062).
335363 if (! _seenErrors.add (error)) {
336364 return ;
337365 }
338366 Source source = error.source;
339- var location = errorToLine[error].getLocation (error.offset);
367+ var location = errorToLine[error].lineInfo. getLocation (error.offset);
340368 int length = error.length;
341369
342370 ErrorSeverity severity = _severityProcessor (error);
0 commit comments