Skip to content

Commit ee69f45

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: rework AnalysisErrorListener deprecation
Work towards #60635 In this change, we rework the AnalysisErrorListener deprecation to better support users who have their own class that implements AnalysisErrorListener. This change introduces a sealed supertype, DiagnosticOrErrorListener, with the old implementation, AnalysisErrorListener, and the new implementation, DiagnosticListener, as its sole direct subclasses. Users who have implemented AnalysisErrorListener should be able to instead implement DiagnosticListener, and their class is an acceptable instance of DiagnosticOrErrorListener, wherever that is needed. In a breaking change we can drop AnalysisErrorListener and deprecate DiagnosticOrErrorListener, and in the next breaking change, we can drop DiagnosticOrErrorListener. For reference, see the first API difference when deprecating AnalysisErrorListener and introducing DiagnosticListener: 903d77c#diff-dec15868961d7eadcd009f49d129bebcdb747aaa8dbfde5f5a08884e0cf11e32 Change-Id: I3ccf11d54b41fbca98d020d89978d250c16b4c04 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/436480 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent bf97e28 commit ee69f45

File tree

23 files changed

+117
-61
lines changed

23 files changed

+117
-61
lines changed

pkg/analysis_server/lib/src/lsp/source_edits.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ class _MinimalEditComputer {
678678
var scanner = Scanner(
679679
_SourceMock.instance,
680680
CharSequenceReader(s),
681-
DiagnosticListener.NULL_LISTENER,
681+
DiagnosticListener.nullListener,
682682
)..configureFeatures(
683683
featureSetForOverriding: featureSet,
684684
featureSet: featureSet,

pkg/analysis_server/lib/src/services/correction/fix/data_driven/transform_set_manager.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class TransformSetManager {
115115
try {
116116
var content = file.readAsStringSync();
117117
var parser = TransformSetParser(
118-
DiagnosticReporter(DiagnosticListener.NULL_LISTENER, FileSource(file)),
118+
DiagnosticReporter(DiagnosticListener.nullListener, FileSource(file)),
119119
packageName,
120120
);
121121
return parser.parse(content);

pkg/analysis_server/lib/src/services/correction/statement_analyzer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ List<Token> _getTokens(String text, FeatureSet featureSet) {
2626
var scanner = Scanner(
2727
_SourceMock.instance,
2828
CharSequenceReader(text),
29-
DiagnosticListener.NULL_LISTENER,
29+
DiagnosticListener.nullListener,
3030
)..configureFeatures(
3131
featureSetForOverriding: featureSet,
3232
featureSet: featureSet,

pkg/analysis_server/test/stress/replay/replay.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class Driver {
226226
var scanner = Scanner(
227227
_TestSource(),
228228
CharSequenceReader(text),
229-
error.DiagnosticListener.NULL_LISTENER,
229+
error.DiagnosticListener.nullListener,
230230
)..configureFeatures(
231231
featureSetForOverriding: featureSet,
232232
featureSet: featureSet,

pkg/analysis_server/test/utils/test_support.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class GatheringDiagnosticListener implements DiagnosticListener {
137137
/// Adds the given [diagnostics] to this listener.
138138
void addAll(List<Diagnostic> diagnostics) {
139139
for (var diagnostic in diagnostics) {
140-
onError(diagnostic);
140+
onDiagnostic(diagnostic);
141141
}
142142
}
143143

@@ -388,7 +388,7 @@ class GatheringDiagnosticListener implements DiagnosticListener {
388388
}
389389

390390
@override
391-
void onError(Diagnostic diagnostic) {
391+
void onDiagnostic(Diagnostic diagnostic) {
392392
_diagnostics.add(diagnostic);
393393
}
394394

pkg/analysis_server_plugin/lib/edit/correction_utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ class TokenUtils {
443443
var scanner = Scanner(
444444
_SourceMock(),
445445
CharSequenceReader(s),
446-
DiagnosticListener.NULL_LISTENER,
446+
DiagnosticListener.nullListener,
447447
)..configureFeatures(
448448
featureSetForOverriding: featureSet,
449449
featureSet: featureSet,

pkg/analyzer/CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
* Deprecate `ErrorSeverity`; use `DiagnosticSeverity` instead.
1818
* Deprecate `DiagnosticCode.errorSeverity`; use
1919
`DiagnosticCode.diagnosticSeverity` instead.
20-
* Deprecate `ErrorListener` and `RecordingErrorListener`; use
21-
`DiagnosticListener` and `RecordingDiagnosticListener` instead.
20+
* Deprecate `AnalysisErrorListener`, `BooleanErrorListener`, and
21+
`RecordingErrorListener`; use `DiagnosticListener`,
22+
`BooleanDiagnosticListener`, and `RecordingDiagnosticListener` respectively,
23+
instead. Instead of calling or overriding `AnalysisErrorListener.onError`,
24+
call or override `DiagnosticListener.onDiagnostic`. Instead of using
25+
`AnalysisErrorListener.NULL_LISTENER`, use `DiagnosticListener.nullListener`.
2226
* Deprecate `RecordingErrorListener.errors`; use
2327
`RecordingDiagnosticListener.diagnostics` instead.
2428
* Deprecate `RecordingErrorListener.getErrorsForSource`; no longer supported.

pkg/analyzer/api.txt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4770,16 +4770,22 @@ package:analyzer/error/error.dart:
47704770
ErrorSeverity (type alias for DiagnosticSeverity, deprecated)
47714771
ErrorType (type alias for DiagnosticType, deprecated)
47724772
package:analyzer/error/listener.dart:
4773-
BooleanDiagnosticListener (class extends Object implements DiagnosticListener):
4773+
AnalysisErrorListener (class extends Object implements DiagnosticOrErrorListener, deprecated):
4774+
NULL_LISTENER (static getter: AnalysisErrorListener)
4775+
new (constructor: AnalysisErrorListener Function())
4776+
onError (method: void Function(Diagnostic))
4777+
BooleanDiagnosticListener (class extends Object implements AnalysisErrorListener, DiagnosticListener):
47744778
new (constructor: BooleanDiagnosticListener Function())
47754779
errorReported (getter: bool)
4780+
onDiagnostic (method: void Function(Diagnostic))
47764781
onError (method: void Function(Diagnostic))
4777-
DiagnosticListener (class extends Object):
4778-
NULL_LISTENER (static getter: DiagnosticListener)
4782+
DiagnosticListener (class extends Object implements DiagnosticOrErrorListener):
4783+
nullListener (static getter: DiagnosticListener)
47794784
new (constructor: DiagnosticListener Function())
4780-
onError (method: void Function(Diagnostic))
4785+
onDiagnostic (method: void Function(Diagnostic))
4786+
DiagnosticOrErrorListener (class extends Object, sealed (immediate subtypes: AnalysisErrorListener, DiagnosticListener))
47814787
DiagnosticReporter (class extends Object):
4782-
new (constructor: DiagnosticReporter Function(DiagnosticListener, Source))
4788+
new (constructor: DiagnosticReporter Function(DiagnosticOrErrorListener, Source))
47834789
lockLevel (getter: int)
47844790
lockLevel= (setter: int)
47854791
source (getter: Source)
@@ -4791,13 +4797,15 @@ package:analyzer/error/listener.dart:
47914797
atSourceSpan (method: void Function(SourceSpan, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data}))
47924798
atToken (method: void Function(Token, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data}))
47934799
reportError (method: void Function(Diagnostic))
4794-
RecordingDiagnosticListener (class extends Object implements DiagnosticListener):
4800+
RecordingDiagnosticListener (class extends Object implements AnalysisErrorListener, DiagnosticListener):
47954801
new (constructor: RecordingDiagnosticListener Function())
47964802
diagnostics (getter: List<Diagnostic>)
47974803
errors (getter: List<Diagnostic>, deprecated)
47984804
getErrorsForSource (method: List<Diagnostic> Function(Source), deprecated)
4805+
onDiagnostic (method: void Function(Diagnostic))
47994806
onError (method: void Function(Diagnostic))
4800-
AnalysisErrorListener (type alias for DiagnosticListener, deprecated)
4807+
DiagnosticOrErrorListenerExtension (extension on DiagnosticOrErrorListener):
4808+
onDiagnostic (method: void Function(Diagnostic))
48014809
BooleanErrorListener (type alias for BooleanDiagnosticListener, deprecated)
48024810
ErrorReporter (type alias for DiagnosticReporter, deprecated)
48034811
RecorderingErrorListener (type alias for RecordingDiagnosticListener, deprecated)

pkg/analyzer/lib/error/listener.dart

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import 'package:analyzer/src/utilities/extensions/collection.dart';
1616
import 'package:meta/meta.dart';
1717
import 'package:source_span/source_span.dart';
1818

19-
@Deprecated("Use 'DiagnosticListener' instead")
20-
typedef AnalysisErrorListener = DiagnosticListener;
21-
2219
@Deprecated("Use 'BooleanDiagnosticListener' instead")
2320
typedef BooleanErrorListener = BooleanDiagnosticListener;
2421

@@ -28,38 +25,55 @@ typedef ErrorReporter = DiagnosticReporter;
2825
@Deprecated("Use 'RecordingDiagnosticListener' instead")
2926
typedef 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.
6074
class 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.
306327
class _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+
}

pkg/analyzer/lib/src/dart/analysis/file_state.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ class FileState {
591591

592592
/// Return a new parsed unresolved [CompilationUnit].
593593
CompilationUnitImpl parse({
594-
DiagnosticListener diagnosticListener = DiagnosticListener.NULL_LISTENER,
594+
DiagnosticOrErrorListener diagnosticListener =
595+
DiagnosticListener.nullListener,
595596
required OperationPerformanceImpl performance,
596597
}) {
597598
try {
@@ -608,7 +609,7 @@ class FileState {
608609
/// Parses given [code] with the same features as this file.
609610
CompilationUnitImpl parseCode({
610611
required String code,
611-
required DiagnosticListener diagnosticListener,
612+
required DiagnosticOrErrorListener diagnosticListener,
612613
required OperationPerformanceImpl performance,
613614
}) {
614615
return performance.run('parseCode', (performance) {

0 commit comments

Comments
 (0)