analyzer: Deprecate ErrorSeverity in favor of DiagnosticSeverity

Work towards https://github.com/dart-lang/sdk/issues/60635

Change-Id: Ic7f84584d4185e1ab7e8741052ec6694487c8c08
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426600
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Sam Rawlins
2025-05-05 13:58:32 -07:00
committed by Commit Queue
parent c6658d01c7
commit 9ab2139df5
9 changed files with 75 additions and 63 deletions
@@ -129,7 +129,8 @@ abstract class ErrorCode {
/// The severity of a [DiagnosticCode].
@AnalyzerPublicApi(message: 'exported by package:analyzer/error/error.dart')
typedef DiagnosticSeverity = ErrorSeverity;
@Deprecated("Use 'DiagnosticSeverity' instead.")
typedef ErrorSeverity = DiagnosticSeverity;
/**
* The severity of an [ErrorCode].
@@ -138,32 +139,39 @@ typedef DiagnosticSeverity = ErrorSeverity;
* the type alias, [DiagnosticSeverity].
*/
@AnalyzerPublicApi(message: 'exported by package:analyzer/error/error.dart')
class ErrorSeverity implements Comparable<ErrorSeverity> {
class DiagnosticSeverity implements Comparable<DiagnosticSeverity> {
/**
* The severity representing a non-error. This is never used for any error
* code, but is useful for clients.
*/
static const ErrorSeverity NONE = const ErrorSeverity('NONE', 0, " ", "none");
static const DiagnosticSeverity NONE =
const DiagnosticSeverity('NONE', 0, " ", "none");
/**
* The severity representing an informational level analysis issue.
*/
static const ErrorSeverity INFO = const ErrorSeverity('INFO', 1, "I", "info");
static const DiagnosticSeverity INFO =
const DiagnosticSeverity('INFO', 1, "I", "info");
/**
* The severity representing a warning. Warnings can become errors if the
* `-Werror` command line flag is specified.
*/
static const ErrorSeverity WARNING =
const ErrorSeverity('WARNING', 2, "W", "warning");
static const DiagnosticSeverity WARNING =
const DiagnosticSeverity('WARNING', 2, "W", "warning");
/**
* The severity representing an error.
*/
static const ErrorSeverity ERROR =
const ErrorSeverity('ERROR', 3, "E", "error");
static const DiagnosticSeverity ERROR =
const DiagnosticSeverity('ERROR', 3, "E", "error");
static const List<ErrorSeverity> values = const [NONE, INFO, WARNING, ERROR];
static const List<DiagnosticSeverity> values = const [
NONE,
INFO,
WARNING,
ERROR
];
final String name;
@@ -179,19 +187,19 @@ class ErrorSeverity implements Comparable<ErrorSeverity> {
*/
final String displayName;
const ErrorSeverity(
const DiagnosticSeverity(
this.name, this.ordinal, this.machineCode, this.displayName);
@override
int get hashCode => ordinal;
@override
int compareTo(ErrorSeverity other) => ordinal - other.ordinal;
int compareTo(DiagnosticSeverity other) => ordinal - other.ordinal;
/**
* Return the severity constant that represents the greatest severity.
*/
ErrorSeverity max(ErrorSeverity severity) =>
DiagnosticSeverity max(DiagnosticSeverity severity) =>
this.ordinal >= severity.ordinal ? this : severity;
@override
@@ -33,7 +33,7 @@ import 'package:analysis_server/src/utilities/process.dart';
import 'package:analysis_server_plugin/src/correction/performance.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/session.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/error.dart' as engine;
import 'package:analyzer/exception/exception.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/instrumentation/instrumentation.dart';
@@ -1394,12 +1394,12 @@ class LspServerContextManagerCallbacks
bool _shouldSendError(protocol.AnalysisError error) {
// Non-TODOs are always shown.
if (error.type.name != DiagnosticType.TODO.name) {
if (error.type.name != engine.DiagnosticType.TODO.name) {
return true;
}
// TODOs that are upgraded from INFO are always shown.
if (error.severity.name != ErrorSeverity.INFO.name) {
if (error.severity.name != engine.DiagnosticSeverity.INFO.name) {
return true;
}
@@ -112,7 +112,7 @@ List<T> mapEngineErrors<T>(
T Function(
engine.AnalysisResultWithErrors result,
engine.Diagnostic diagnostic, [
engine.ErrorSeverity errorSeverity,
engine.DiagnosticSeverity errorSeverity,
])
constructor,
) {
@@ -137,19 +137,20 @@ List<T> mapEngineErrors<T>(
/// Construct based on error information from the analyzer engine.
///
/// If an [errorSeverity] is specified, it will override the one in [error].
/// If an [diagnosticSeverity] is specified, it will override the one in
/// [diagnostic].
AnalysisError newAnalysisError_fromEngine(
engine.AnalysisResultWithErrors result,
engine.Diagnostic error, [
engine.ErrorSeverity? errorSeverity,
engine.Diagnostic diagnostic, [
engine.DiagnosticSeverity? diagnosticSeverity,
]) {
var errorCode = error.errorCode;
var errorCode = diagnostic.errorCode;
// prepare location
Location location;
{
var file = error.source.fullName;
var offset = error.offset;
var length = error.length;
var file = diagnostic.source.fullName;
var offset = diagnostic.offset;
var length = diagnostic.length;
var lineInfo = result.lineInfo;
var startLocation = lineInfo.getLocation(offset);
@@ -172,21 +173,21 @@ AnalysisError newAnalysisError_fromEngine(
}
// Default to the error's severity if none is specified.
errorSeverity ??= errorCode.errorSeverity;
diagnosticSeverity ??= errorCode.errorSeverity;
// done
var severity = AnalysisErrorSeverity.values.byName(errorSeverity.name);
var severity = AnalysisErrorSeverity.values.byName(diagnosticSeverity.name);
var type = AnalysisErrorType.values.byName(errorCode.type.name);
var message = error.message;
var message = diagnostic.message;
var code = errorCode.name.toLowerCase();
List<DiagnosticMessage>? contextMessages;
if (error.contextMessages.isNotEmpty) {
if (diagnostic.contextMessages.isNotEmpty) {
contextMessages =
error.contextMessages
diagnostic.contextMessages
.map((message) => newDiagnosticMessage(result, message))
.toList();
}
var correction = error.correctionMessage;
var correction = diagnostic.correctionMessage;
var url = errorCode.url;
return AnalysisError(
severity,
@@ -188,7 +188,7 @@ class TransformSetErrorCode extends DiagnosticCode {
);
@override
ErrorSeverity get errorSeverity => ErrorSeverity.ERROR;
DiagnosticSeverity get errorSeverity => DiagnosticSeverity.ERROR;
@override
DiagnosticType get type => DiagnosticType.COMPILE_TIME_ERROR;
@@ -229,16 +229,16 @@ class AnalysisErrorTest {
@reflectiveTest
class EnumTest {
void test_AnalysisErrorSeverity() {
EnumTester<engine.ErrorSeverity, AnalysisErrorSeverity>().run(
(engine.ErrorSeverity engineErrorSeverity) =>
AnalysisErrorSeverity.values.byName(engineErrorSeverity.name),
exceptions: {engine.ErrorSeverity.NONE: null},
EnumTester<engine.DiagnosticSeverity, AnalysisErrorSeverity>().run(
(engineSeverity) =>
AnalysisErrorSeverity.values.byName(engineSeverity.name),
exceptions: {engine.DiagnosticSeverity.NONE: null},
);
}
void test_AnalysisErrorType() {
EnumTester<engine.DiagnosticType, AnalysisErrorType>().run(
(engine.DiagnosticType engineErrorType) =>
(engineErrorType) =>
AnalysisErrorType.values.byName(engineErrorType.name),
);
}
@@ -391,7 +391,7 @@ class MockDiagnosticCode implements engine.DiagnosticCode {
engine.DiagnosticType type;
@override
engine.ErrorSeverity errorSeverity;
engine.DiagnosticSeverity errorSeverity;
@override
String name;
@@ -401,7 +401,7 @@ class MockDiagnosticCode implements engine.DiagnosticCode {
MockDiagnosticCode({
this.type = engine.DiagnosticType.COMPILE_TIME_ERROR,
this.errorSeverity = engine.ErrorSeverity.ERROR,
this.errorSeverity = engine.DiagnosticSeverity.ERROR,
this.name = 'TEST_ERROR',
this.url,
});
@@ -333,11 +333,11 @@ class GatheringErrorListener implements AnalysisErrorListener {
/// number of [expectedSeverities] and that there are the same number of
/// errors and warnings as specified by the argument. The order in which the
/// errors were gathered is ignored.
void assertErrorsWithSeverities(List<ErrorSeverity> expectedSeverities) {
void assertErrorsWithSeverities(List<DiagnosticSeverity> expectedSeverities) {
var expectedErrorCount = 0;
var expectedWarningCount = 0;
for (var severity in expectedSeverities) {
if (severity == ErrorSeverity.ERROR) {
if (severity == DiagnosticSeverity.ERROR) {
expectedErrorCount++;
} else {
expectedWarningCount++;
@@ -346,7 +346,7 @@ class GatheringErrorListener implements AnalysisErrorListener {
var actualErrorCount = 0;
var actualWarningCount = 0;
for (var error in _errors) {
if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) {
if (error.errorCode.errorSeverity == DiagnosticSeverity.ERROR) {
actualErrorCount++;
} else {
actualWarningCount++;
+1
View File
@@ -9,6 +9,7 @@
* Deprecate `AnalysisError.correction` field; use
`AnalysisError.correctionMessage` instead.
* Deprecate `ErrorType`; use `DiagnosticType` instead.
* Deprecate `ErrorSeverity`; use `DiagnosticSeverity` instead.
* Change `ElementDirective` from `sealed` to `abstract`.
This allows the analyzer to have an internal implementation
class corresponding to `ElementDirective`.
+23 -23
View File
@@ -4361,6 +4361,21 @@ package:analyzer/diagnostic/diagnostic.dart:
package:analyzer/error/error.dart:
errorCodeValues (static getter: List<ErrorCode>)
errorCodeByUniqueName (function: ErrorCode? Function(String))
DiagnosticSeverity (class extends Object implements Comparable<DiagnosticSeverity>):
ERROR (static getter: DiagnosticSeverity)
INFO (static getter: DiagnosticSeverity)
NONE (static getter: DiagnosticSeverity)
WARNING (static getter: DiagnosticSeverity)
values (static getter: List<DiagnosticSeverity>)
new (constructor: DiagnosticSeverity Function(String, int, String, String))
displayName (getter: String)
hashCode (getter: int)
machineCode (getter: String)
name (getter: String)
ordinal (getter: int)
compareTo (method: int Function(DiagnosticSeverity))
max (method: DiagnosticSeverity Function(DiagnosticSeverity))
toString (method: String Function())
DiagnosticType (class extends Object implements Comparable<DiagnosticType>):
CHECKED_MODE_COMPILE_TIME_ERROR (static getter: DiagnosticType)
COMPILE_TIME_ERROR (static getter: DiagnosticType)
@@ -4370,18 +4385,18 @@ package:analyzer/error/error.dart:
SYNTACTIC_ERROR (static getter: DiagnosticType)
TODO (static getter: DiagnosticType)
values (static getter: List<DiagnosticType>)
new (constructor: DiagnosticType Function(String, int, ErrorSeverity))
new (constructor: DiagnosticType Function(String, int, DiagnosticSeverity))
displayName (getter: String)
hashCode (getter: int)
name (getter: String)
ordinal (getter: int)
severity (getter: ErrorSeverity)
severity (getter: DiagnosticSeverity)
compareTo (method: int Function(DiagnosticType))
toString (method: String Function())
ErrorCode (class extends Object):
new (constructor: ErrorCode Function({String? correctionMessage, bool hasPublishedDocs, bool isUnresolvedIdentifier, required String name, required String problemMessage, required String uniqueName}))
correctionMessage (getter: String?)
errorSeverity (getter: ErrorSeverity)
errorSeverity (getter: DiagnosticSeverity)
hasPublishedDocs (getter: bool)
isIgnorable (getter: bool)
isUnresolvedIdentifier (getter: bool)
@@ -4392,31 +4407,16 @@ package:analyzer/error/error.dart:
uniqueName (getter: String)
url (getter: String?)
toString (method: String Function())
ErrorSeverity (class extends Object implements Comparable<ErrorSeverity>):
ERROR (static getter: ErrorSeverity)
INFO (static getter: ErrorSeverity)
NONE (static getter: ErrorSeverity)
WARNING (static getter: ErrorSeverity)
values (static getter: List<ErrorSeverity>)
new (constructor: ErrorSeverity Function(String, int, String, String))
displayName (getter: String)
hashCode (getter: int)
machineCode (getter: String)
name (getter: String)
ordinal (getter: int)
compareTo (method: int Function(ErrorSeverity))
max (method: ErrorSeverity Function(ErrorSeverity))
toString (method: String Function())
LintCode (class extends ErrorCode):
new (constructor: LintCode Function(String, String, {String? correctionMessage, bool hasPublishedDocs, String? uniqueName}))
errorSeverity (getter: ErrorSeverity)
errorSeverity (getter: DiagnosticSeverity)
hashCode (getter: int)
type (getter: DiagnosticType)
url (getter: String?)
== (method: bool Function(Object))
AnalysisError (type alias for Diagnostic)
DiagnosticCode (type alias for ErrorCode)
DiagnosticSeverity (type alias for ErrorSeverity)
ErrorSeverity (type alias for DiagnosticSeverity, deprecated)
ErrorType (type alias for DiagnosticType, deprecated)
package:analyzer/error/listener.dart:
AnalysisErrorListener (class extends Object):
@@ -4694,17 +4694,17 @@ package:analyzer/instrumentation/service.dart:
InstrumentationService (see above)
InstrumentationServiceAttachment (see above)
package:analyzer/source/error_processor.dart:
severityMap (static getter: Map<String, ErrorSeverity>)
severityMap (static getter: Map<String, DiagnosticSeverity>)
ErrorConfig (class extends Object):
new (constructor: ErrorConfig Function(YamlNode?))
processors (getter: List<ErrorProcessor>)
ErrorProcessor (class extends Object):
getProcessor (static method: ErrorProcessor? Function(AnalysisOptions?, Diagnostic))
ignore (constructor: ErrorProcessor Function(String))
new (constructor: ErrorProcessor Function(String, [ErrorSeverity?]))
new (constructor: ErrorProcessor Function(String, [DiagnosticSeverity?]))
code (getter: String)
description (getter: String)
severity (getter: ErrorSeverity?)
severity (getter: DiagnosticSeverity?)
appliesTo (method: bool Function(Diagnostic))
toString (method: String Function())
package:analyzer/source/file_source.dart:
+2
View File
@@ -17,6 +17,8 @@ export 'package:_fe_analyzer_shared/src/base/errors.dart'
DiagnosticSeverity,
DiagnosticType,
ErrorCode,
// Continue exporting the deleted element until it is removed.
// ignore: deprecated_member_use
ErrorSeverity,
// Continue exporting the deleted element until it is removed.
// ignore: deprecated_member_use