Skip to content

Commit 8a17ad9

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Deprecate AnalysisError in favor of Diagnostic
Work towards #60635 Change-Id: If9bbc96beed95129b099588c5bd9728afda8e392 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426902 Reviewed-by: Nate Bosch <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Bob Nystrom <[email protected]> Reviewed-by: Paul Berry <[email protected]>
1 parent 17cb638 commit 8a17ad9

File tree

17 files changed

+115
-103
lines changed

17 files changed

+115
-103
lines changed

pkg/analysis_server/test/src/services/correction/fix/merge_combinators_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/src/services/correction/fix.dart';
6+
import 'package:analyzer/diagnostic/diagnostic.dart';
67
import 'package:analyzer/error/error.dart';
78
import 'package:analyzer/src/error/codes.dart';
89
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -554,8 +555,8 @@ import 'other.dart' show FutureOr, Completer, Timer;
554555
}
555556

556557
abstract class _MergeCombinatorTest extends FixProcessorErrorCodeTest {
557-
bool diagnosticCodeFilter(AnalysisError e) {
558-
return e.errorCode == diagnosticCode;
558+
bool diagnosticCodeFilter(Diagnostic d) {
559+
return d.errorCode == diagnosticCode;
559560
}
560561

561562
@override

pkg/analyzer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Deprecate `XyzElement2` classes; use `XyzElement` instead.
99
* Deprecate `AnalysisError.correction` field; use
1010
`AnalysisError.correctionMessage` instead.
11+
* Deprecate `AnalysisError`; use `Diagnostic` instead.
1112
* Deprecate `ErrorCode`; use `DiagnosticCode` instead.
1213
* Deprecate `ErrorSeverity`; use `DiagnosticSeverity` instead.
1314
* Deprecate `ErrorType`; use `DiagnosticType` instead.

pkg/analyzer/api.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4415,7 +4415,7 @@ package:analyzer/error/error.dart:
44154415
type (getter: DiagnosticType)
44164416
url (getter: String?)
44174417
== (method: bool Function(Object))
4418-
AnalysisError (type alias for Diagnostic)
4418+
AnalysisError (type alias for Diagnostic, deprecated)
44194419
ErrorCode (type alias for DiagnosticCode, deprecated)
44204420
ErrorSeverity (type alias for DiagnosticSeverity, deprecated)
44214421
ErrorType (type alias for DiagnosticType, deprecated)

pkg/analyzer/lib/dart/analysis/results.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/element/element.dart';
99
import 'package:analyzer/dart/element/type_provider.dart';
1010
import 'package:analyzer/dart/element/type_system.dart';
11-
import 'package:analyzer/error/error.dart';
11+
import 'package:analyzer/diagnostic/diagnostic.dart';
1212
import 'package:analyzer/file_system/file_system.dart';
1313
import 'package:analyzer/source/line_info.dart';
1414
import 'package:meta/meta.dart';
@@ -27,7 +27,7 @@ abstract class AnalysisResult {
2727
/// Clients may not extend, implement or mix-in this class.
2828
abstract class AnalysisResultWithErrors implements FileResult {
2929
/// The analysis errors that were computed during analysis.
30-
List<AnalysisError> get errors;
30+
List<Diagnostic> get errors;
3131
}
3232

3333
/// The type of [InvalidResult] returned when the given URI cannot be resolved.
@@ -253,7 +253,7 @@ abstract class ParseStringResult {
253253
String get content;
254254

255255
/// The analysis errors that were computed during analysis.
256-
List<AnalysisError> get errors;
256+
List<Diagnostic> get errors;
257257

258258
/// Information about lines in the content.
259259
LineInfo get lineInfo;

pkg/analyzer/lib/dart/element/element.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import 'package:analyzer/dart/element/scope.dart';
5454
import 'package:analyzer/dart/element/type.dart';
5555
import 'package:analyzer/dart/element/type_provider.dart';
5656
import 'package:analyzer/dart/element/type_system.dart';
57-
import 'package:analyzer/error/error.dart';
57+
import 'package:analyzer/diagnostic/diagnostic.dart';
5858
import 'package:analyzer/source/line_info.dart';
5959
import 'package:analyzer/source/source.dart';
6060
import 'package:analyzer/src/dart/constant/evaluation.dart';
@@ -705,7 +705,7 @@ abstract class ElementAnnotation implements ConstantEvaluationTarget {
705705
///
706706
/// If a value has been produced but no errors were generated, then the
707707
/// list will be empty.
708-
List<AnalysisError>? get constantEvaluationErrors;
708+
List<Diagnostic>? get constantEvaluationErrors;
709709

710710
/// Returns the element referenced by this annotation.
711711
///

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ HashMap<String, DiagnosticCode> _computeUniqueNameToCodeMap() {
5757
}
5858

5959
/// A deprecated name for [Diagnostic]. Please use [Diagnostic].
60+
@Deprecated("Use 'Diagnostic' instead.")
6061
typedef AnalysisError = Diagnostic;

pkg/analyzer/lib/error/listener.dart

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

19-
/// An object that listens for [AnalysisError]s being produced by the analysis
19+
/// An object that listens for [Diagnostic]s being produced by the analysis
2020
/// engine.
2121
abstract class AnalysisErrorListener {
2222
/// An error listener that ignores errors that are reported to it.
2323
static final AnalysisErrorListener NULL_LISTENER = _NullErrorListener();
2424

2525
/// This method is invoked when an [error] has been found by the analysis
2626
/// engine.
27-
void onError(AnalysisError error);
27+
void onError(Diagnostic error);
2828
}
2929

3030
/// An [AnalysisErrorListener] that keeps track of whether any error has been
@@ -37,7 +37,7 @@ class BooleanErrorListener implements AnalysisErrorListener {
3737
bool get errorReported => _errorReported;
3838

3939
@override
40-
void onError(AnalysisError error) {
40+
void onError(Diagnostic error) {
4141
_errorReported = true;
4242
}
4343
}
@@ -182,7 +182,7 @@ class ErrorReporter {
182182
contextMessages ??= [];
183183
contextMessages.addAll(convertTypeNames(arguments));
184184
_errorListener.onError(
185-
AnalysisError.tmp(
185+
Diagnostic.tmp(
186186
source: _source,
187187
offset: offset,
188188
length: length,
@@ -233,42 +233,42 @@ class ErrorReporter {
233233
}
234234

235235
/// Report the given [error].
236-
void reportError(AnalysisError error) {
236+
void reportError(Diagnostic error) {
237237
_errorListener.onError(error);
238238
}
239239
}
240240

241241
/// An error listener that will record the errors that are reported to it in a
242242
/// way that is appropriate for caching those errors within an analysis context.
243243
class RecordingErrorListener implements AnalysisErrorListener {
244-
Set<AnalysisError>? _errors;
244+
Set<Diagnostic>? _errors;
245245

246246
/// Return the errors collected by the listener.
247-
List<AnalysisError> get errors {
247+
List<Diagnostic> get errors {
248248
if (_errors == null) {
249249
return const [];
250250
}
251251
return _errors!.toList();
252252
}
253253

254254
/// Return the errors collected by the listener for the given [source].
255-
List<AnalysisError> getErrorsForSource(Source source) {
255+
List<Diagnostic> getErrorsForSource(Source source) {
256256
if (_errors == null) {
257257
return const [];
258258
}
259259
return _errors!.where((error) => error.source == source).toList();
260260
}
261261

262262
@override
263-
void onError(AnalysisError error) {
263+
void onError(Diagnostic error) {
264264
(_errors ??= {}).add(error);
265265
}
266266
}
267267

268268
/// An [AnalysisErrorListener] that ignores error.
269269
class _NullErrorListener implements AnalysisErrorListener {
270270
@override
271-
void onError(AnalysisError event) {
271+
void onError(Diagnostic event) {
272272
// Ignore errors
273273
}
274274
}

pkg/analyzer_cli/lib/src/analyzer_impl.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:io';
66

77
import 'package:analyzer/dart/analysis/results.dart';
88
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/diagnostic/diagnostic.dart';
910
import 'package:analyzer/error/error.dart';
1011
import 'package:analyzer/instrumentation/instrumentation.dart';
1112
import 'package:analyzer/src/dart/analysis/driver.dart';
@@ -182,8 +183,8 @@ class AnalyzerImpl {
182183
return computeMaxErrorSeverity();
183184
}
184185

185-
DiagnosticSeverity? _defaultSeverityProcessor(AnalysisError error) =>
186-
determineProcessedSeverity(error, options, analysisOptions);
186+
DiagnosticSeverity? _defaultSeverityProcessor(Diagnostic diagnostic) =>
187+
determineProcessedSeverity(diagnostic, options, analysisOptions);
187188

188189
/// Returns true if we want to report diagnostics for this library.
189190
bool _isAnalyzedLibrary(LibraryElement library) {

pkg/analyzer_cli/lib/src/driver.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:io' as io;
66

77
import 'package:analyzer/dart/analysis/results.dart';
88
import 'package:analyzer/dart/sdk/build_sdk_summary.dart';
9+
import 'package:analyzer/diagnostic/diagnostic.dart';
910
import 'package:analyzer/error/error.dart';
1011
import 'package:analyzer/file_system/file_system.dart';
1112
import 'package:analyzer/file_system/physical_file_system.dart';
@@ -180,11 +181,11 @@ class Driver implements CommandLineStarter {
180181
// Note: This references analysisDriver via closure, so it will change over
181182
// time during the following analysis.
182183
SeverityProcessor defaultSeverityProcessor;
183-
defaultSeverityProcessor = (AnalysisError error) {
184-
var filePath = error.source.fullName;
184+
defaultSeverityProcessor = (Diagnostic diagnostic) {
185+
var filePath = diagnostic.source.fullName;
185186
var file = analysisDriver!.resourceProvider.getFile(filePath);
186187
return determineProcessedSeverity(
187-
error,
188+
diagnostic,
188189
options,
189190
analysisDriver!.getAnalysisOptionsForFile(file),
190191
);
@@ -307,7 +308,7 @@ class Driver implements CommandLineStarter {
307308
}
308309
}
309310
} else if (file_paths.isPubspecYaml(pathContext, path)) {
310-
var errors = <AnalysisError>[];
311+
var diagnostics = <Diagnostic>[];
311312
try {
312313
var file = resourceProvider.getFile(path);
313314
var analysisOptions = analysisDriver.currentSession.analysisContext
@@ -316,7 +317,7 @@ class Driver implements CommandLineStarter {
316317
var node = loadYamlNode(content, sourceUrl: file.toUri());
317318

318319
if (node is YamlMap) {
319-
errors.addAll(
320+
diagnostics.addAll(
320321
validatePubspec(
321322
contents: node,
322323
source: FileSource(file),
@@ -325,8 +326,8 @@ class Driver implements CommandLineStarter {
325326
),
326327
);
327328
}
328-
if (errors.isNotEmpty) {
329-
for (var error in errors) {
329+
if (diagnostics.isNotEmpty) {
330+
for (var error in diagnostics) {
330331
var severity =
331332
determineProcessedSeverity(
332333
error,
@@ -345,7 +346,7 @@ class Driver implements CommandLineStarter {
345346
lineInfo: lineInfo,
346347
isLibrary: true,
347348
isPart: false,
348-
errors: errors,
349+
errors: diagnostics,
349350
analysisOptions: analysisOptions,
350351
),
351352
]);

pkg/analyzer_cli/lib/src/error_formatter.dart

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:convert';
66

77
import 'package:analyzer/dart/analysis/results.dart';
8+
import 'package:analyzer/diagnostic/diagnostic.dart';
89
import 'package:analyzer/error/error.dart';
910
import 'package:analyzer/source/line_info.dart';
1011
import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
@@ -28,13 +29,13 @@ String _relative(String file) {
2829
return file.startsWith(path.current) ? path.relative(file) : file;
2930
}
3031

31-
/// Returns the given error's severity.
32-
DiagnosticSeverity _severityIdentity(AnalysisError error) =>
33-
error.errorCode.errorSeverity;
32+
/// Returns the given diagnostic's severity.
33+
DiagnosticSeverity _severityIdentity(Diagnostic diagnostic) =>
34+
diagnostic.errorCode.errorSeverity;
3435

35-
/// Returns desired severity for the given [error] (or `null` if it's to be
36+
/// Returns desired severity for the given [diagnostic] (or `null` if it's to be
3637
/// suppressed).
37-
typedef SeverityProcessor = DiagnosticSeverity? Function(AnalysisError error);
38+
typedef SeverityProcessor = DiagnosticSeverity? Function(Diagnostic diagnostic);
3839

3940
/// Analysis statistics counter.
4041
class AnalysisStats {
@@ -103,7 +104,7 @@ class AnalysisStats {
103104
}
104105
}
105106

106-
/// An [AnalysisError] with line and column information.
107+
/// A [Diagnostic] with line and column information.
107108
class CLIError implements Comparable<CLIError> {
108109
final String severity;
109110
final String sourcePath;
@@ -174,7 +175,7 @@ class ContextMessage {
174175
ContextMessage(this.filePath, this.message, this.line, this.column);
175176
}
176177

177-
/// Helper for formatting [AnalysisError]s.
178+
/// Helper for formatting [Diagnostic]s.
178179
///
179180
/// The two format options are a user consumable format and a machine consumable
180181
/// format.
@@ -194,34 +195,34 @@ abstract class ErrorFormatter {
194195
/// Call to write any batched up errors from [formatErrors].
195196
void flush();
196197

197-
Future<void> formatError(
198-
Map<AnalysisError, ErrorsResult> errorToLine,
199-
AnalysisError error,
198+
Future<void> formatDiagnostic(
199+
Map<Diagnostic, ErrorsResult> errorToLine,
200+
Diagnostic error,
200201
);
201202

202203
Future<void> formatErrors(List<ErrorsResult> results) async {
203204
stats.unfilteredCount += results.length;
204205

205-
var errors = <AnalysisError>[];
206-
var errorToLine = <AnalysisError, ErrorsResult>{};
206+
var diagnostics = <Diagnostic>[];
207+
var diagnosticToLine = <Diagnostic, ErrorsResult>{};
207208
for (var result in results) {
208209
for (var error in result.errors) {
209210
if (_computeSeverity(error) != null) {
210-
errors.add(error);
211-
errorToLine[error] = result;
211+
diagnostics.add(error);
212+
diagnosticToLine[error] = result;
212213
}
213214
}
214215
}
215216

216-
for (var error in errors) {
217-
await formatError(errorToLine, error);
217+
for (var error in diagnostics) {
218+
await formatDiagnostic(diagnosticToLine, error);
218219
}
219220
}
220221

221-
/// Compute the severity for this [error] or `null` if this error should be
222-
/// filtered.
223-
DiagnosticSeverity? _computeSeverity(AnalysisError error) =>
224-
_severityProcessor(error);
222+
/// Compute the severity for this [diagnostic] or `null` if this error should
223+
/// be filtered.
224+
DiagnosticSeverity? _computeSeverity(Diagnostic diagnostic) =>
225+
_severityProcessor(diagnostic);
225226
}
226227

227228
class HumanErrorFormatter extends ErrorFormatter {
@@ -287,9 +288,9 @@ class HumanErrorFormatter extends ErrorFormatter {
287288
}
288289

289290
@override
290-
Future<void> formatError(
291-
Map<AnalysisError, ErrorsResult> errorToLine,
292-
AnalysisError error,
291+
Future<void> formatDiagnostic(
292+
Map<Diagnostic, ErrorsResult> errorToLine,
293+
Diagnostic error,
293294
) async {
294295
var source = error.source;
295296
var result = errorToLine[error]!;
@@ -369,9 +370,9 @@ class JsonErrorFormatter extends ErrorFormatter {
369370
void flush() {}
370371

371372
@override
372-
Future<void> formatError(
373-
Map<AnalysisError, ErrorsResult> errorToLine,
374-
AnalysisError error,
373+
Future<void> formatDiagnostic(
374+
Map<Diagnostic, ErrorsResult> errorToLine,
375+
Diagnostic error,
375376
) async {
376377
throw UnsupportedError('Cannot format a single error');
377378
}
@@ -461,7 +462,7 @@ class MachineErrorFormatter extends ErrorFormatter {
461462
static final int _slashCodeUnit = '\\'.codeUnitAt(0);
462463
static final int _newline = '\n'.codeUnitAt(0);
463464
static final int _return = '\r'.codeUnitAt(0);
464-
final Set<AnalysisError> _seenErrors = <AnalysisError>{};
465+
final Set<Diagnostic> _seenDiagnostics = <Diagnostic>{};
465466

466467
MachineErrorFormatter(
467468
super.out,
@@ -474,12 +475,12 @@ class MachineErrorFormatter extends ErrorFormatter {
474475
void flush() {}
475476

476477
@override
477-
Future<void> formatError(
478-
Map<AnalysisError, ErrorsResult> errorToLine,
479-
AnalysisError error,
478+
Future<void> formatDiagnostic(
479+
Map<Diagnostic, ErrorsResult> errorToLine,
480+
Diagnostic error,
480481
) async {
481482
// Ensure we don't over-report (#36062).
482-
if (!_seenErrors.add(error)) {
483+
if (!_seenDiagnostics.add(error)) {
483484
return;
484485
}
485486
var source = error.source;

0 commit comments

Comments
 (0)