analyzer: Remove AnalysisErrorListener and friends

* Remove AnalysisErrorListener, `RecordingDiagnosticListener.errors`,
  and `RecordingDiagnosticListener.getErrorsForSource`.
* Deprecate `BooleanDiagnosticListener.onError` and
  `RecordingDiagnosticListener.onError` in favor of `.onDiagnostic`.
* Deprecate DiagnosticOrErrorListener. Where this class is used in
  private API, replace it with DiagnosticListener. Where this class is
  used in public API, keep it and ignore the deprecation lint.

Change-Id: Ie9c89008269db8f42e4ebd161df2764d27dfe0da
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/456100
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Sam Rawlins
2025-10-20 12:58:24 -07:00
committed by Commit Queue
parent 6077e86598
commit 7059532b48
9 changed files with 33 additions and 61 deletions
+6
View File
@@ -17,6 +17,12 @@
* Remove deprecated `ErrorCode`.
* Remove deprecated `ErrorSeverity`.
* Remove deprecated `ErrorType`.
* Remove deprecated `AnalysisErrorListener`.
* Remove deprecated `RecordingDiagnosticListener.errors`.
* Remove deprecated `RecordingDiagnosticListener.getErrorForSource`.
* Deprecate `BooleanDiagnosticListener.onError` in favor of `.onDiagnostic`.
* Deprecate `RecordingDiagnosticListener.onError` in favor of `.onDiagnostic`.
* Deprecate `DiagnosticOrErrorListener` in favor of `DiagnosticListener`.
## 8.4.0
* Add the `experimental_member_use` warning, which warns about any reference to
+5 -10
View File
@@ -4575,19 +4575,16 @@ package:analyzer/error/error.dart:
== (method: bool Function(Object))
AnalysisError (type alias for Diagnostic, deprecated)
package:analyzer/error/listener.dart:
AnalysisErrorListener (class extends Object implements DiagnosticOrErrorListener, deprecated):
new (constructor: AnalysisErrorListener Function())
onError (method: void Function(Diagnostic))
BooleanDiagnosticListener (class extends Object implements AnalysisErrorListener, DiagnosticListener):
BooleanDiagnosticListener (class extends Object implements DiagnosticListener):
new (constructor: BooleanDiagnosticListener Function())
errorReported (getter: bool)
onDiagnostic (method: void Function(Diagnostic))
onError (method: void Function(Diagnostic))
onError (method: void Function(Diagnostic), deprecated)
DiagnosticListener (class extends Object implements DiagnosticOrErrorListener):
nullListener (static getter: DiagnosticListener)
new (constructor: DiagnosticListener Function())
onDiagnostic (method: void Function(Diagnostic))
DiagnosticOrErrorListener (class extends Object, sealed (immediate subtypes: AnalysisErrorListener, DiagnosticListener))
DiagnosticOrErrorListener (class extends Object, sealed (immediate subtypes: DiagnosticListener), deprecated)
DiagnosticReporter (class extends Object):
new (constructor: DiagnosticReporter Function(DiagnosticOrErrorListener, Source))
lockLevel (getter: int)
@@ -4601,13 +4598,11 @@ package:analyzer/error/listener.dart:
atSourceSpan (method: Diagnostic Function(SourceSpan, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages}))
atToken (method: Diagnostic Function(Token, DiagnosticCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages}))
reportError (method: void Function(Diagnostic))
RecordingDiagnosticListener (class extends Object implements AnalysisErrorListener, DiagnosticListener):
RecordingDiagnosticListener (class extends Object implements DiagnosticListener):
new (constructor: RecordingDiagnosticListener Function())
diagnostics (getter: List<Diagnostic>)
errors (getter: List<Diagnostic>, deprecated)
getErrorsForSource (method: List<Diagnostic> Function(Source), deprecated)
onDiagnostic (method: void Function(Diagnostic))
onError (method: void Function(Diagnostic))
onError (method: void Function(Diagnostic), deprecated)
DiagnosticOrErrorListenerExtension (extension on DiagnosticOrErrorListener):
onDiagnostic (method: void Function(Diagnostic))
ErrorReporter (type alias for DiagnosticReporter, deprecated)
+7 -36
View File
@@ -4,7 +4,6 @@
import 'package:_fe_analyzer_shared/src/base/errors.dart';
import 'package:analyzer/diagnostic/diagnostic.dart';
import 'package:analyzer/source/source.dart';
import 'package:analyzer/src/error/listener.dart';
export 'package:analyzer/src/error/listener.dart' show DiagnosticReporter;
@@ -12,22 +11,9 @@ export 'package:analyzer/src/error/listener.dart' show DiagnosticReporter;
@Deprecated("Use 'DiagnosticReporter' instead")
typedef ErrorReporter = DiagnosticReporter;
/// An object that listens for [Diagnostic]s being produced by the analysis
/// engine.
@Deprecated("Use 'DiagnosticListener' instead")
abstract class AnalysisErrorListener implements DiagnosticOrErrorListener {
/// This method is invoked when a [diagnostic] has been found by the analysis
/// engine.
void onError(Diagnostic diagnostic);
}
/// A [DiagnosticListener] that keeps track of whether any diagnostic has been
/// reported to it.
class BooleanDiagnosticListener
implements
// ignore: deprecated_member_use_from_same_package
AnalysisErrorListener,
DiagnosticListener {
class BooleanDiagnosticListener implements DiagnosticListener {
/// A flag indicating whether a diagnostic has been reported to this listener.
bool _diagnosticReported = false;
@@ -39,10 +25,11 @@ class BooleanDiagnosticListener
_diagnosticReported = true;
}
@override
@Deprecated("Call 'onDiagnostic' instead")
void onError(Diagnostic diagnostic) => onDiagnostic(diagnostic);
}
// ignore: deprecated_member_use_from_same_package
abstract class DiagnosticListener implements DiagnosticOrErrorListener {
/// A diagnostic listener that ignores diagnostics that are reported to it.
static const DiagnosticListener nullListener = _NullDiagnosticListener();
@@ -50,16 +37,13 @@ abstract class DiagnosticListener implements DiagnosticOrErrorListener {
void onDiagnostic(Diagnostic diagnostic);
}
@Deprecated("Use 'DiagnosticListener' instead")
sealed class DiagnosticOrErrorListener {}
/// A diagnostic listener that records the diagnostics that are reported to it
/// in a way that is appropriate for caching those diagnostic within an
/// analysis context.
class RecordingDiagnosticListener
implements
// ignore: deprecated_member_use_from_same_package
AnalysisErrorListener,
DiagnosticListener {
class RecordingDiagnosticListener implements DiagnosticListener {
Set<Diagnostic>? _diagnostics;
/// The diagnostics collected by the listener.
@@ -70,24 +54,12 @@ class RecordingDiagnosticListener
return _diagnostics!.toList();
}
@Deprecated("Use 'diagnostics' instead")
List<Diagnostic> get errors => diagnostics;
/// Return the errors collected by the listener for the given [source].
@Deprecated('No longer supported')
List<Diagnostic> getErrorsForSource(Source source) {
if (_diagnostics == null) {
return const [];
}
return _diagnostics!.where((d) => d.source == source).toList();
}
@override
void onDiagnostic(Diagnostic diagnostic) {
(_diagnostics ??= {}).add(diagnostic);
}
@override
@Deprecated("Call 'onDiagnostic' instead")
void onError(Diagnostic diagnostic) => onDiagnostic(diagnostic);
}
@@ -101,10 +73,9 @@ class _NullDiagnosticListener implements DiagnosticListener {
}
}
// ignore: deprecated_member_use_from_same_package
extension DiagnosticOrErrorListenerExtension on DiagnosticOrErrorListener {
void onDiagnostic(Diagnostic diagnostic) => switch (this) {
DiagnosticListener self => self.onDiagnostic(diagnostic),
// ignore: deprecated_member_use_from_same_package
AnalysisErrorListener self => self.onError(diagnostic),
};
}
@@ -591,8 +591,7 @@ class FileState {
/// Return a new parsed unresolved [CompilationUnit].
CompilationUnitImpl parse({
DiagnosticOrErrorListener diagnosticListener =
DiagnosticListener.nullListener,
DiagnosticListener diagnosticListener = DiagnosticListener.nullListener,
required OperationPerformanceImpl performance,
}) {
try {
@@ -609,7 +608,7 @@ class FileState {
/// Parses given [code] with the same features as this file.
CompilationUnitImpl parseCode({
required String code,
required DiagnosticOrErrorListener diagnosticListener,
required DiagnosticListener diagnosticListener,
required OperationPerformanceImpl performance,
}) {
return performance.run('parseCode', (performance) {
@@ -48,7 +48,7 @@ class Scanner {
/// The diagnostic listener that will be informed of any diagnostics that are
/// found during the scan.
final DiagnosticOrErrorListener _diagnosticListener;
final DiagnosticListener _diagnosticListener;
/// If the file has [fasta.LanguageVersionToken], it is allowed to use the
/// language version greater than the one specified in the package config.
@@ -71,7 +71,7 @@ class Scanner {
factory Scanner(
Source source,
CharacterReader reader,
DiagnosticOrErrorListener diagnosticListener,
DiagnosticListener diagnosticListener,
) => Scanner.fasta(
source,
diagnosticListener,
@@ -81,7 +81,7 @@ class Scanner {
factory Scanner.fasta(
Source source,
DiagnosticOrErrorListener diagnosticListener, {
DiagnosticListener diagnosticListener, {
String? contents,
int offset = -1,
}) {
+1
View File
@@ -144,6 +144,7 @@ Expected types: $expectedTypes''');
@AnalyzerPublicApi(message: 'Exported by package:analyzer/error/listener.dart')
class DiagnosticReporter {
/// The diagnostic listener to which diagnostics are reported.
// ignore: deprecated_member_use_from_same_package
final DiagnosticOrErrorListener _diagnosticListener;
/// The source to be used when reporting diagnostics.
+1 -1
View File
@@ -32,7 +32,7 @@ class Parser {
Parser(
Source source,
DiagnosticOrErrorListener diagnosticListener, {
DiagnosticListener diagnosticListener, {
required FeatureSet featureSet,
bool allowNativeClause = true,
required LibraryLanguageVersion languageVersion,
@@ -9,19 +9,19 @@ import 'package:test_reflective_loader/test_reflective_loader.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(RecordingErrorListenerTest);
defineReflectiveTests(RecordingDiagnosticListenerTest);
});
}
@reflectiveTest
class RecordingErrorListenerTest {
class RecordingDiagnosticListenerTest {
test_orderedAsReported() {
var listener = RecordingDiagnosticListener();
listener.onError(_MockDiagnostic(expectedIndex: 0, hashCode: 1));
listener.onError(_MockDiagnostic(expectedIndex: 1, hashCode: 10));
listener.onError(_MockDiagnostic(expectedIndex: 2, hashCode: -50));
listener.onError(_MockDiagnostic(expectedIndex: 3, hashCode: 20));
listener.onError(_MockDiagnostic(expectedIndex: 4, hashCode: 1));
listener.onDiagnostic(_MockDiagnostic(expectedIndex: 0, hashCode: 1));
listener.onDiagnostic(_MockDiagnostic(expectedIndex: 1, hashCode: 10));
listener.onDiagnostic(_MockDiagnostic(expectedIndex: 2, hashCode: -50));
listener.onDiagnostic(_MockDiagnostic(expectedIndex: 3, hashCode: 20));
listener.onDiagnostic(_MockDiagnostic(expectedIndex: 4, hashCode: 1));
// Expect the errors are returned in the order they are reported, and not
// affected by their hashcodes.
@@ -1287,7 +1287,7 @@ class ParserTestCase with ParserTestHelpers implements AbstractParserTestCase {
/// Parse the given [content] as a compilation unit.
CompilationUnit parseCompilationUnit2(
String content, {
DiagnosticOrErrorListener listener = DiagnosticListener.nullListener,
DiagnosticListener listener = DiagnosticListener.nullListener,
}) {
Source source = NonExistingSource.unknown;