Skip to content

Commit 23b3450

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Make AnalysisError an alias for Diagnostic
Work towards #60635 Diagnostic has exactly one direct subclass, AnalysisError (AnalysisError has a few more for tests). Diagnostic was previously referenced only 7 times in all of analyzer. This change is essentially a no-op. Change-Id: I043970d96adcd7be4bb6a24d4a85b3f74309b7d6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/425682 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent fa48659 commit 23b3450

File tree

3 files changed

+183
-200
lines changed

3 files changed

+183
-200
lines changed

pkg/analyzer/api.txt

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ package:analyzer/dart/analysis/results.dart:
154154
session (getter: AnalysisSession)
155155
AnalysisResultWithErrors (class extends Object implements FileResult):
156156
new (constructor: AnalysisResultWithErrors Function())
157-
errors (getter: List<AnalysisError>)
157+
errors (getter: List<Diagnostic>)
158158
CannotResolveUriResult (class extends Object implements InvalidResult, SomeLibraryElementResult, SomeParsedLibraryResult, SomeResolvedLibraryResult):
159159
new (constructor: CannotResolveUriResult Function())
160160
DisposedAnalysisContextResult (class extends Object implements InvalidResult, SomeErrorsResult, SomeFileResult, SomeParsedLibraryResult, SomeParsedUnitResult, SomeResolvedLibraryResult, SomeResolvedUnitResult, SomeUnitElementResult):
@@ -199,7 +199,7 @@ package:analyzer/dart/analysis/results.dart:
199199
ParseStringResult (class extends Object):
200200
new (constructor: ParseStringResult Function())
201201
content (getter: String)
202-
errors (getter: List<AnalysisError>)
202+
errors (getter: List<Diagnostic>)
203203
lineInfo (getter: LineInfo)
204204
unit (getter: CompilationUnit)
205205
ParsedLibraryResult (class extends Object implements SomeParsedLibraryResult, AnalysisResult):
@@ -3080,7 +3080,7 @@ package:analyzer/dart/element/element.dart:
30803080
visitChildren2 (method: void Function<T>(ElementVisitor2<T>))
30813081
ElementAnnotation (class extends Object implements ConstantEvaluationTarget):
30823082
new (constructor: ElementAnnotation Function())
3083-
constantEvaluationErrors (getter: List<AnalysisError>?)
3083+
constantEvaluationErrors (getter: List<Diagnostic>?)
30843084
element2 (getter: Element?)
30853085
isAlwaysThrows (getter: bool)
30863086
isAwaitNotRequired (getter: bool)
@@ -4327,11 +4327,22 @@ package:analyzer/dart/sdk/build_sdk_summary.dart:
43274327
buildSdkSummary (function: Future<Uint8List> Function({String? embedderYamlPath, required ResourceProvider resourceProvider, required String sdkPath}))
43284328
package:analyzer/diagnostic/diagnostic.dart:
43294329
Diagnostic (class extends Object):
4330-
new (constructor: Diagnostic Function())
4330+
forValues (constructor: Diagnostic Function({List<DiagnosticMessage> contextMessages, String? correctionMessage, Object? data, required ErrorCode errorCode, required int length, required String message, required int offset, required Source source}))
4331+
tmp (constructor: Diagnostic Function({List<Object?> arguments, List<DiagnosticMessage> contextMessages, Object? data, required ErrorCode errorCode, required int length, required int offset, required Source source}))
43314332
contextMessages (getter: List<DiagnosticMessage>)
4333+
correction (getter: String?, deprecated)
43324334
correctionMessage (getter: String?)
4335+
data (getter: Object?)
4336+
errorCode (getter: ErrorCode)
4337+
hashCode (getter: int)
4338+
length (getter: int)
4339+
message (getter: String)
4340+
offset (getter: int)
43334341
problemMessage (getter: DiagnosticMessage)
43344342
severity (getter: Severity)
4343+
source (getter: Source)
4344+
== (method: bool Function(Object))
4345+
toString (method: String Function())
43354346
DiagnosticMessage (class extends Object):
43364347
new (constructor: DiagnosticMessage Function())
43374348
filePath (getter: String)
@@ -4347,23 +4358,6 @@ package:analyzer/diagnostic/diagnostic.dart:
43474358
package:analyzer/error/error.dart:
43484359
errorCodeValues (static getter: List<ErrorCode>)
43494360
errorCodeByUniqueName (function: ErrorCode? Function(String))
4350-
AnalysisError (class extends Object implements Diagnostic):
4351-
forValues (constructor: AnalysisError Function({List<DiagnosticMessage> contextMessages, String? correctionMessage, Object? data, required ErrorCode errorCode, required int length, required String message, required int offset, required Source source}))
4352-
tmp (constructor: AnalysisError Function({List<Object?> arguments, List<DiagnosticMessage> contextMessages, Object? data, required ErrorCode errorCode, required int length, required int offset, required Source source}))
4353-
contextMessages (getter: List<DiagnosticMessage>)
4354-
correction (getter: String?, deprecated)
4355-
correctionMessage (getter: String?)
4356-
data (getter: Object?)
4357-
errorCode (getter: ErrorCode)
4358-
hashCode (getter: int)
4359-
length (getter: int)
4360-
message (getter: String)
4361-
offset (getter: int)
4362-
problemMessage (getter: DiagnosticMessage)
4363-
severity (getter: Severity)
4364-
source (getter: Source)
4365-
== (method: bool Function(Object))
4366-
toString (method: String Function())
43674361
DiagnosticType (class extends Object implements Comparable<DiagnosticType>):
43684362
CHECKED_MODE_COMPILE_TIME_ERROR (static getter: DiagnosticType)
43694363
COMPILE_TIME_ERROR (static getter: DiagnosticType)
@@ -4417,18 +4411,19 @@ package:analyzer/error/error.dart:
44174411
type (getter: DiagnosticType)
44184412
url (getter: String?)
44194413
== (method: bool Function(Object))
4414+
AnalysisError (type alias for Diagnostic)
44204415
DiagnosticCode (type alias for ErrorCode)
44214416
DiagnosticSeverity (type alias for ErrorSeverity)
44224417
ErrorType (type alias for DiagnosticType, deprecated)
44234418
package:analyzer/error/listener.dart:
44244419
AnalysisErrorListener (class extends Object):
44254420
NULL_LISTENER (static getter: AnalysisErrorListener)
44264421
new (constructor: AnalysisErrorListener Function())
4427-
onError (method: void Function(AnalysisError))
4422+
onError (method: void Function(Diagnostic))
44284423
BooleanErrorListener (class extends Object implements AnalysisErrorListener):
44294424
new (constructor: BooleanErrorListener Function())
44304425
errorReported (getter: bool)
4431-
onError (method: void Function(AnalysisError))
4426+
onError (method: void Function(Diagnostic))
44324427
ErrorReporter (class extends Object):
44334428
new (constructor: ErrorReporter Function(AnalysisErrorListener, Source))
44344429
lockLevel (getter: int)
@@ -4441,12 +4436,12 @@ package:analyzer/error/listener.dart:
44414436
atOffset (method: void Function({List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data, required ErrorCode errorCode, required int length, required int offset}))
44424437
atSourceSpan (method: void Function(SourceSpan, ErrorCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data}))
44434438
atToken (method: void Function(Token, ErrorCode, {List<Object>? arguments, List<DiagnosticMessage>? contextMessages, Object? data}))
4444-
reportError (method: void Function(AnalysisError))
4439+
reportError (method: void Function(Diagnostic))
44454440
RecordingErrorListener (class extends Object implements AnalysisErrorListener):
44464441
new (constructor: RecordingErrorListener Function())
4447-
errors (getter: List<AnalysisError>)
4448-
getErrorsForSource (method: List<AnalysisError> Function(Source))
4449-
onError (method: void Function(AnalysisError))
4442+
errors (getter: List<Diagnostic>)
4443+
getErrorsForSource (method: List<Diagnostic> Function(Source))
4444+
onError (method: void Function(Diagnostic))
44504445
package:analyzer/exception/exception.dart:
44514446
AnalysisException (class extends Object implements Exception):
44524447
new (constructor: AnalysisException Function([String, CaughtException?]))
@@ -4701,13 +4696,13 @@ package:analyzer/source/error_processor.dart:
47014696
new (constructor: ErrorConfig Function(YamlNode?))
47024697
processors (getter: List<ErrorProcessor>)
47034698
ErrorProcessor (class extends Object):
4704-
getProcessor (static method: ErrorProcessor? Function(AnalysisOptions?, AnalysisError))
4699+
getProcessor (static method: ErrorProcessor? Function(AnalysisOptions?, Diagnostic))
47054700
ignore (constructor: ErrorProcessor Function(String))
47064701
new (constructor: ErrorProcessor Function(String, [ErrorSeverity?]))
47074702
code (getter: String)
47084703
description (getter: String)
47094704
severity (getter: ErrorSeverity?)
4710-
appliesTo (method: bool Function(AnalysisError))
4705+
appliesTo (method: bool Function(Diagnostic))
47114706
toString (method: String Function())
47124707
package:analyzer/source/file_source.dart:
47134708
FileSource (class extends Source):

pkg/analyzer/lib/diagnostic/diagnostic.dart

Lines changed: 157 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/error/error.dart';
6+
import 'package:analyzer/source/source.dart';
7+
import 'package:analyzer/src/diagnostic/diagnostic.dart';
8+
import 'package:analyzer/src/generated/java_core.dart';
9+
510
/// A diagnostic, as defined by the [Diagnostic Design Guidelines][guidelines]:
611
///
712
/// > An indication of a specific problem at a specific location within the
@@ -10,20 +15,166 @@
1015
/// Clients may not extend, implement or mix-in this class.
1116
///
1217
/// [guidelines]: https://github.com/dart-lang/sdk/blob/main/pkg/analyzer/doc/implementation/diagnostics.md
13-
abstract class Diagnostic {
18+
class Diagnostic {
19+
/// The error code associated with the diagnostic.
20+
final ErrorCode errorCode;
21+
1422
/// A list of messages that provide context for understanding the problem
1523
/// being reported. The list will be empty if there are no such messages.
16-
List<DiagnosticMessage> get contextMessages;
24+
final List<DiagnosticMessage> contextMessages;
25+
26+
/// Data associated with this diagnostic, specific for [errorCode].
27+
final Object? data;
1728

1829
/// A description of how to fix the problem, or `null` if there is no such
1930
/// description.
20-
String? get correctionMessage;
31+
final String? correctionMessage;
2132

2233
/// A message describing what is wrong and why.
23-
DiagnosticMessage get problemMessage;
34+
final DiagnosticMessage problemMessage;
35+
36+
/// The source in which the diagnostic occurred, or `null` if unknown.
37+
final Source source;
38+
39+
Diagnostic.forValues({
40+
required this.source,
41+
required int offset,
42+
required int length,
43+
required this.errorCode,
44+
required String message,
45+
this.correctionMessage,
46+
this.contextMessages = const [],
47+
this.data,
48+
}) : problemMessage = DiagnosticMessageImpl(
49+
filePath: source.fullName,
50+
length: length,
51+
message: message,
52+
offset: offset,
53+
url: null,
54+
);
55+
56+
/// Initialize a newly created diagnostic.
57+
///
58+
/// The diagnostic is associated with the given [source] and is located at the
59+
/// given [offset] with the given [length]. The diagnostic will have the given
60+
/// [errorCode] and the list of [arguments] will be used to complete the
61+
/// message and correction. If any [contextMessages] are provided, they will
62+
/// be recorded with the diagnostic.
63+
factory Diagnostic.tmp({
64+
required Source source,
65+
required int offset,
66+
required int length,
67+
required ErrorCode errorCode,
68+
List<Object?> arguments = const [],
69+
List<DiagnosticMessage> contextMessages = const [],
70+
Object? data,
71+
}) {
72+
assert(
73+
arguments.length == errorCode.numParameters,
74+
'Message $errorCode requires ${errorCode.numParameters} '
75+
'argument${errorCode.numParameters == 1 ? '' : 's'}, but '
76+
'${arguments.length} '
77+
'argument${arguments.length == 1 ? ' was' : 's were'} '
78+
'provided',
79+
);
80+
String message = formatList(errorCode.problemMessage, arguments);
81+
String? correctionTemplate = errorCode.correctionMessage;
82+
String? correctionMessage;
83+
if (correctionTemplate != null) {
84+
correctionMessage = formatList(correctionTemplate, arguments);
85+
}
86+
87+
return Diagnostic.forValues(
88+
source: source,
89+
offset: offset,
90+
length: length,
91+
errorCode: errorCode,
92+
message: message,
93+
correctionMessage: correctionMessage,
94+
contextMessages: contextMessages,
95+
data: data,
96+
);
97+
}
98+
99+
/// The template used to create the correction to be displayed for this
100+
/// diagnostic, or `null` if there is no correction information for this
101+
/// error. The correction should indicate how the user can fix the error.
102+
@Deprecated("Use 'correctionMessage' instead.")
103+
String? get correction => correctionMessage;
104+
105+
@override
106+
int get hashCode {
107+
int hashCode = offset;
108+
hashCode ^= message.hashCode;
109+
hashCode ^= source.hashCode;
110+
return hashCode;
111+
}
112+
113+
/// The number of characters from the offset to the end of the source which
114+
/// encompasses the compilation error.
115+
int get length => problemMessage.length;
116+
117+
/// The message to be displayed for this diagnostic.
118+
///
119+
/// The message indicates what is wrong and why it is wrong.
120+
String get message => problemMessage.messageText(includeUrl: true);
121+
122+
/// The character offset from the beginning of the source (zero based) where
123+
/// the diagnostic occurred.
124+
int get offset => problemMessage.offset;
125+
126+
Severity get severity {
127+
switch (errorCode.errorSeverity) {
128+
case ErrorSeverity.ERROR:
129+
return Severity.error;
130+
case ErrorSeverity.WARNING:
131+
return Severity.warning;
132+
case ErrorSeverity.INFO:
133+
return Severity.info;
134+
default:
135+
throw StateError('Invalid error severity: ${errorCode.errorSeverity}');
136+
}
137+
}
138+
139+
@override
140+
bool operator ==(Object other) {
141+
if (identical(other, this)) {
142+
return true;
143+
}
144+
// prepare the other Diagnostic.
145+
if (other is Diagnostic) {
146+
// Quick checks.
147+
if (!identical(errorCode, other.errorCode)) {
148+
return false;
149+
}
150+
if (offset != other.offset || length != other.length) {
151+
return false;
152+
}
153+
// Deep checks.
154+
if (message != other.message) {
155+
return false;
156+
}
157+
if (source != other.source) {
158+
return false;
159+
}
160+
return true;
161+
}
162+
return false;
163+
}
24164

25-
/// The severity associated with the diagnostic.
26-
Severity get severity;
165+
@override
166+
String toString() {
167+
StringBuffer buffer = StringBuffer();
168+
buffer.write(source.fullName);
169+
buffer.write("(");
170+
buffer.write(offset);
171+
buffer.write("..");
172+
buffer.write(offset + length - 1);
173+
buffer.write("): ");
174+
//buffer.write("(" + lineNumber + ":" + columnNumber + "): ");
175+
buffer.write(message);
176+
return buffer.toString();
177+
}
27178
}
28179

29180
/// A single message associated with a [Diagnostic], consisting of the text of

0 commit comments

Comments
 (0)