From 7059532b48906ade910b7e832ca7bb704fef1c33 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Mon, 20 Oct 2025 12:58:24 -0700 Subject: [PATCH] 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 Commit-Queue: Samuel Rawlins --- pkg/analyzer/CHANGELOG.md | 6 +++ pkg/analyzer/api.txt | 15 +++---- pkg/analyzer/lib/error/listener.dart | 43 +++---------------- .../lib/src/dart/analysis/file_state.dart | 5 +-- .../lib/src/dart/scanner/scanner.dart | 6 +-- pkg/analyzer/lib/src/error/listener.dart | 1 + pkg/analyzer/lib/src/generated/parser.dart | 2 +- .../test/error/error_listener_test.dart | 14 +++--- .../test/generated/parser_test_base.dart | 2 +- 9 files changed, 33 insertions(+), 61 deletions(-) diff --git a/pkg/analyzer/CHANGELOG.md b/pkg/analyzer/CHANGELOG.md index bdc7c9e1bc9..a7e8fc06a42 100644 --- a/pkg/analyzer/CHANGELOG.md +++ b/pkg/analyzer/CHANGELOG.md @@ -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 diff --git a/pkg/analyzer/api.txt b/pkg/analyzer/api.txt index f3dcdcbc029..a6231003921 100644 --- a/pkg/analyzer/api.txt +++ b/pkg/analyzer/api.txt @@ -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? arguments, List? contextMessages})) atToken (method: Diagnostic Function(Token, DiagnosticCode, {List? arguments, List? 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) - errors (getter: List, deprecated) - getErrorsForSource (method: List 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) diff --git a/pkg/analyzer/lib/error/listener.dart b/pkg/analyzer/lib/error/listener.dart index 6408da22afe..fa2cfaf4c5f 100644 --- a/pkg/analyzer/lib/error/listener.dart +++ b/pkg/analyzer/lib/error/listener.dart @@ -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? _diagnostics; /// The diagnostics collected by the listener. @@ -70,24 +54,12 @@ class RecordingDiagnosticListener return _diagnostics!.toList(); } - @Deprecated("Use 'diagnostics' instead") - List get errors => diagnostics; - - /// Return the errors collected by the listener for the given [source]. - @Deprecated('No longer supported') - List 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), }; } diff --git a/pkg/analyzer/lib/src/dart/analysis/file_state.dart b/pkg/analyzer/lib/src/dart/analysis/file_state.dart index e1b901a60f3..50d5ae36a0d 100644 --- a/pkg/analyzer/lib/src/dart/analysis/file_state.dart +++ b/pkg/analyzer/lib/src/dart/analysis/file_state.dart @@ -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) { diff --git a/pkg/analyzer/lib/src/dart/scanner/scanner.dart b/pkg/analyzer/lib/src/dart/scanner/scanner.dart index b349b3dd4c1..2ae5ee4dac2 100644 --- a/pkg/analyzer/lib/src/dart/scanner/scanner.dart +++ b/pkg/analyzer/lib/src/dart/scanner/scanner.dart @@ -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, }) { diff --git a/pkg/analyzer/lib/src/error/listener.dart b/pkg/analyzer/lib/src/error/listener.dart index fd441911d5f..8a17f0b35d2 100644 --- a/pkg/analyzer/lib/src/error/listener.dart +++ b/pkg/analyzer/lib/src/error/listener.dart @@ -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. diff --git a/pkg/analyzer/lib/src/generated/parser.dart b/pkg/analyzer/lib/src/generated/parser.dart index 7a4f1edd833..e3fdd42fd74 100644 --- a/pkg/analyzer/lib/src/generated/parser.dart +++ b/pkg/analyzer/lib/src/generated/parser.dart @@ -32,7 +32,7 @@ class Parser { Parser( Source source, - DiagnosticOrErrorListener diagnosticListener, { + DiagnosticListener diagnosticListener, { required FeatureSet featureSet, bool allowNativeClause = true, required LibraryLanguageVersion languageVersion, diff --git a/pkg/analyzer/test/error/error_listener_test.dart b/pkg/analyzer/test/error/error_listener_test.dart index b947e6fc058..54f0c1a0d80 100644 --- a/pkg/analyzer/test/error/error_listener_test.dart +++ b/pkg/analyzer/test/error/error_listener_test.dart @@ -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. diff --git a/pkg/analyzer/test/generated/parser_test_base.dart b/pkg/analyzer/test/generated/parser_test_base.dart index a0c093d0960..74b613d1aea 100644 --- a/pkg/analyzer/test/generated/parser_test_base.dart +++ b/pkg/analyzer/test/generated/parser_test_base.dart @@ -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;