@@ -16,9 +16,6 @@ import 'package:analyzer/src/utilities/extensions/collection.dart';
1616import 'package:meta/meta.dart' ;
1717import 'package:source_span/source_span.dart' ;
1818
19- @Deprecated ("Use 'DiagnosticListener' instead" )
20- typedef AnalysisErrorListener = DiagnosticListener ;
21-
2219@Deprecated ("Use 'BooleanDiagnosticListener' instead" )
2320typedef BooleanErrorListener = BooleanDiagnosticListener ;
2421
@@ -28,38 +25,55 @@ typedef ErrorReporter = DiagnosticReporter;
2825@Deprecated ("Use 'RecordingDiagnosticListener' instead" )
2926typedef RecorderingErrorListener = RecordingDiagnosticListener ;
3027
31- /// An [DiagnosticListener] that keeps track of whether any diagnostic has been
28+ /// An object that listens for [Diagnostic] s being produced by the analysis
29+ /// engine.
30+ @Deprecated ("Use 'DiagnosticListener' instead" )
31+ abstract class AnalysisErrorListener implements DiagnosticOrErrorListener {
32+ /// A diagnostic listener that ignores diagnostics that are reported to it.
33+ @Deprecated ("Use 'DiagnosticListener.nullListener' instead" )
34+ static const AnalysisErrorListener NULL_LISTENER = _NullErrorListener ();
35+
36+ /// This method is invoked when a [diagnostic] has been found by the analysis
37+ /// engine.
38+ void onError (Diagnostic diagnostic);
39+ }
40+
41+ /// A [DiagnosticListener] that keeps track of whether any diagnostic has been
3242/// reported to it.
33- class BooleanDiagnosticListener implements DiagnosticListener {
43+ class BooleanDiagnosticListener
44+ implements
45+ // ignore: deprecated_member_use_from_same_package
46+ AnalysisErrorListener ,
47+ DiagnosticListener {
3448 /// A flag indicating whether a diagnostic has been reported to this listener.
3549 bool _diagnosticReported = false ;
3650
3751 /// Whether a diagnostic has been reported to this listener.
3852 bool get errorReported => _diagnosticReported;
3953
4054 @override
41- void onError (Diagnostic diagnostic) {
55+ void onDiagnostic (Diagnostic diagnostic) {
4256 _diagnosticReported = true ;
4357 }
58+
59+ @override
60+ void onError (Diagnostic diagnostic) => onDiagnostic (diagnostic);
4461}
4562
46- /// An object that listens for [Diagnostic] s being produced by the analysis
47- /// engine.
48- abstract class DiagnosticListener {
63+ abstract class DiagnosticListener implements DiagnosticOrErrorListener {
4964 /// A diagnostic listener that ignores diagnostics that are reported to it.
50- static const DiagnosticListener NULL_LISTENER = _NullDiagnosticListener ();
65+ static const DiagnosticListener nullListener = _NullDiagnosticListener ();
5166
52- /// This method is invoked when a [diagnostic] has been found by the analysis
53- /// engine.
54- // TODO(srawlins): Rename to 'onDiagnostic'.
55- void onError (Diagnostic diagnostic);
67+ void onDiagnostic (Diagnostic diagnostic);
5668}
5769
70+ sealed class DiagnosticOrErrorListener {}
71+
5872/// An object used to create diagnostics and report them to a diagnostic
5973/// listener.
6074class DiagnosticReporter {
6175 /// The diagnostic listener to which diagnostics are reported.
62- final DiagnosticListener _diagnosticListener;
76+ final DiagnosticOrErrorListener _diagnosticListener;
6377
6478 /// The source to be used when reporting diagnostics.
6579 final Source _source;
@@ -211,7 +225,7 @@ class DiagnosticReporter {
211225
212226 contextMessages ?? = [];
213227 contextMessages.addAll (convertTypeNames (arguments));
214- _diagnosticListener.onError (
228+ _diagnosticListener.onDiagnostic (
215229 Diagnostic .tmp (
216230 source: _source,
217231 offset: offset,
@@ -266,14 +280,18 @@ class DiagnosticReporter {
266280
267281 /// Report the given [diagnostic] .
268282 void reportError (Diagnostic diagnostic) {
269- _diagnosticListener.onError (diagnostic);
283+ _diagnosticListener.onDiagnostic (diagnostic);
270284 }
271285}
272286
273287/// A diagnostic listener that records the diagnostics that are reported to it
274288/// in a way that is appropriate for caching those diagnostic within an
275289/// analysis context.
276- class RecordingDiagnosticListener implements DiagnosticListener {
290+ class RecordingDiagnosticListener
291+ implements
292+ // ignore: deprecated_member_use_from_same_package
293+ AnalysisErrorListener ,
294+ DiagnosticListener {
277295 Set <Diagnostic >? _diagnostics;
278296
279297 /// The diagnostics collected by the listener.
@@ -297,17 +315,42 @@ class RecordingDiagnosticListener implements DiagnosticListener {
297315 }
298316
299317 @override
300- void onError (Diagnostic diagnostic) {
318+ void onDiagnostic (Diagnostic diagnostic) {
301319 (_diagnostics ?? = {}).add (diagnostic);
302320 }
321+
322+ @override
323+ void onError (Diagnostic diagnostic) => onDiagnostic (diagnostic);
303324}
304325
305- /// An [DiagnosticListener] that ignores everything.
326+ /// A [DiagnosticListener] that ignores everything.
306327class _NullDiagnosticListener implements DiagnosticListener {
307328 const _NullDiagnosticListener ();
308329
330+ @override
331+ void onDiagnostic (Diagnostic diagnostic) {
332+ // Ignore diagnostics.
333+ }
334+ }
335+
336+ // ignore: deprecated_member_use_from_same_package
337+ /// An [AnalysisErrorListener] that ignores everything.
338+ class _NullErrorListener
339+ implements
340+ // ignore: deprecated_member_use_from_same_package
341+ AnalysisErrorListener {
342+ const _NullErrorListener ();
343+
309344 @override
310345 void onError (Diagnostic diagnostic) {
311346 // Ignore diagnostics.
312347 }
313348}
349+
350+ extension DiagnosticOrErrorListenerExtension on DiagnosticOrErrorListener {
351+ void onDiagnostic (Diagnostic diagnostic) => switch (this ) {
352+ DiagnosticListener self => self.onDiagnostic (diagnostic),
353+ // ignore: deprecated_member_use_from_same_package
354+ AnalysisErrorListener self => self.onError (diagnostic),
355+ };
356+ }
0 commit comments