[messages] Use lower case diagnostic names in analyzer.

Changes the logic in `pkg/analyzer` to use
`DiagnosticCode.lowerCaseName` instead of `DiagnosticCode.name`, and
`DiagnosticCode.lowerCaseUniqueName` instead of
`DiagnosticCode.uniqueName`. This ensures that diagnostic codes are
matched in a case-insensitive fashion.

This paves the way for deprecating (and eventually removing) the
`DiagnosticCode.name` and `DiagnosticCode.uniqueName` getters.

Change-Id: I6a6a6964bae7f2d423e44211d2ad73202da65727
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/466281
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
This commit is contained in:
Paul Berry
2025-12-09 08:04:44 -08:00
committed by Commit Queue
parent 33f4b692ae
commit 22013528d6
21 changed files with 46 additions and 41 deletions
@@ -113,7 +113,7 @@ ParseStringResult parseString({
for (var error in result.errors) {
var location = lineInfo.getLocation(error.offset);
buffer.writeln(
' ${error.diagnosticCode.name}: ${error.message} - '
' ${error.diagnosticCode.lowerCaseName}: ${error.message} - '
'${location.lineNumber}:${location.columnNumber}',
);
}
+4 -4
View File
@@ -16,7 +16,7 @@ export 'package:analyzer/src/dart/error/lint_codes.dart' show LintCode;
export 'package:analyzer/src/diagnostic/diagnostic_code_values.dart'
show diagnosticCodeValues, errorCodeValues;
/// The lazy initialized map from [DiagnosticCode.uniqueName] to the
/// The lazy initialized map from [DiagnosticCode.lowerCaseUniqueName] to the
/// [DiagnosticCode] instance.
final HashMap<String, DiagnosticCode> _uniqueNameToCodeMap =
_computeUniqueNameToCodeMap();
@@ -27,12 +27,12 @@ DiagnosticCode? errorCodeByUniqueName(String uniqueName) {
return _uniqueNameToCodeMap[uniqueName];
}
/// The map from [DiagnosticCode.uniqueName] to the [DiagnosticCode] instance
/// for all [diagnosticCodeValues].
/// The map from [DiagnosticCode.lowerCaseUniqueName] to the [DiagnosticCode]
/// instance for all [diagnosticCodeValues].
HashMap<String, DiagnosticCode> _computeUniqueNameToCodeMap() {
var result = HashMap<String, DiagnosticCode>();
for (DiagnosticCode diagnosticCode in diagnosticCodeValues) {
var uniqueName = diagnosticCode.uniqueName;
var uniqueName = diagnosticCode.lowerCaseUniqueName;
assert(() {
if (result.containsKey(uniqueName)) {
throw StateError('Not unique: $uniqueName');
+1 -1
View File
@@ -79,7 +79,7 @@ class ErrorProcessor {
/// Check if this processor applies to the given [diagnostic].
@visibleForTesting
bool appliesTo(Diagnostic diagnostic) =>
code == diagnostic.diagnosticCode.name.toLowerCase();
code == diagnostic.diagnosticCode.lowerCaseName;
@override
String toString() => "ErrorProcessor[code='$code', severity=$severity]";
@@ -346,7 +346,7 @@ class _AnalyzerTopLevelOptionsValidator extends _TopLevelOptionValidator {
class _CannotIgnoreOptionValidator extends OptionsValidator {
/// Lazily populated set of diagnostic code names.
static final Set<String> _diagnosticCodes = diagnosticCodeValues
.map((DiagnosticCode code) => code.name.toUpperCase())
.map((DiagnosticCode code) => code.lowerCaseName.toUpperCase())
.toSet();
/// The diagnostic code names that existed, but were removed.
@@ -591,7 +591,7 @@ class _ErrorFilterOptionValidator extends OptionsValidator {
/// Lazily populated set of diagnostic code names.
static final Set<String> _diagnosticCodes = diagnosticCodeValues
.map((DiagnosticCode code) => code.name.toUpperCase())
.map((DiagnosticCode code) => code.lowerCaseName.toUpperCase())
.toSet();
/// The diagnostic code names that existed, but were removed.
@@ -289,13 +289,13 @@ final class AnalysisOptionsBuilder {
// If the severity of [error] is also changed in this options file,
// use the changed severity.
var processors = errorProcessors.where(
(processor) => processor.code == diagnostic.name.toLowerCase(),
(processor) => processor.code == diagnostic.lowerCaseName,
);
DiagnosticSeverity? diagnosticSeverity = processors.isNotEmpty
? processors.first.severity
: diagnostic.severity;
if (diagnosticSeverity == severity) {
unignorableDiagnosticCodeNames.add(diagnostic.name.toLowerCase());
unignorableDiagnosticCodeNames.add(diagnostic.lowerCaseName);
}
}
} else {
@@ -2907,7 +2907,7 @@ class ErrorEncoding {
return AnalysisDriverUnitErrorBuilder(
offset: diagnostic.offset,
length: diagnostic.length,
uniqueName: diagnostic.diagnosticCode.uniqueName,
uniqueName: diagnostic.diagnosticCode.lowerCaseUniqueName,
message: diagnostic.message,
correction: diagnostic.correctionMessage ?? '',
contextMessages: contextMessages,
@@ -588,7 +588,7 @@ class LibraryAnalyzer {
bool isIgnored(Diagnostic diagnostic) {
var code = diagnostic.diagnosticCode;
// Don't allow un-ignorable codes to be ignored.
if (unignorableCodes.contains(code.name.toLowerCase())) {
if (unignorableCodes.contains(code.lowerCaseName)) {
return false;
}
return ignoreInfo.ignored(diagnostic);
@@ -65,7 +65,7 @@ class LintCode extends DiagnosticCode {
);
@override
int get hashCode => uniqueName.hashCode;
int get hashCode => lowerCaseUniqueName.hashCode;
@override
DiagnosticType get type => DiagnosticType.LINT;
@@ -75,7 +75,7 @@ class LintCode extends DiagnosticCode {
@override
bool operator ==(Object other) =>
other is LintCode && uniqueName == other.uniqueName;
other is LintCode && lowerCaseUniqueName == other.lowerCaseUniqueName;
}
/// Private subtype of [LintCode] that supports runtime checking of parameter
@@ -92,14 +92,14 @@ abstract class LintCodeWithExpectedTypes extends DiagnosticCodeWithExpectedTypes
}) : super(type: DiagnosticType.LINT);
@override
int get hashCode => uniqueName.hashCode;
int get hashCode => lowerCaseUniqueName.hashCode;
@override
String? get url => null;
@override
bool operator ==(Object other) =>
other is LintCode && uniqueName == other.uniqueName;
other is LintCode && lowerCaseUniqueName == other.lowerCaseUniqueName;
}
/// Defines security-related best practice recommendations.
@@ -15,7 +15,7 @@ class IgnoreValidator {
/// A list of known diagnostic codes used to ensure we don't over-report
/// `unnecessary_ignore`s on error codes that may be contributed by a plugin.
static final Set<String> _validDiagnosticCodeNames = diagnosticCodeValues
.map((d) => d.name.toLowerCase())
.map((d) => d.lowerCaseName)
.toSet();
/// Diagnostic codes used to report `unnecessary_ignore`s.
@@ -284,7 +284,7 @@ class IgnoreValidator {
}
extension on Diagnostic {
String get ignoreName => diagnosticCode.name.toLowerCase();
String get ignoreName => diagnosticCode.lowerCaseName;
}
extension on List<IgnoredElement> {
@@ -39,10 +39,10 @@ class IgnoredDiagnosticName implements IgnoredElement {
if (this.pluginName != pluginName) {
return false;
}
if (name == diagnosticCode.name.toLowerCase()) {
if (name == diagnosticCode.lowerCaseName) {
return true;
}
var uniqueName = diagnosticCode.uniqueName;
var uniqueName = diagnosticCode.lowerCaseUniqueName;
var period = uniqueName.indexOf('.');
if (period >= 0) {
uniqueName = uniqueName.substring(period + 1);
+4 -4
View File
@@ -81,7 +81,7 @@ class Registry with IterableMixin<AbstractAnalysisRule> {
void registerLintRule(AbstractAnalysisRule rule) {
_lintRules[rule.name.toLowerCase()] = rule;
for (var code in rule.diagnosticCodes) {
_codeMap[code.uniqueName.toLowerCase()] = code;
_codeMap[code.lowerCaseUniqueName] = code;
}
}
@@ -89,7 +89,7 @@ class Registry with IterableMixin<AbstractAnalysisRule> {
void registerWarningRule(AbstractAnalysisRule rule) {
_warningRules[rule.name.toLowerCase()] = rule;
for (var code in rule.diagnosticCodes) {
_codeMap[code.uniqueName.toLowerCase()] = code;
_codeMap[code.lowerCaseUniqueName] = code;
}
}
@@ -97,7 +97,7 @@ class Registry with IterableMixin<AbstractAnalysisRule> {
void unregisterLintRule(AbstractAnalysisRule rule) {
_lintRules.remove(rule.name.toLowerCase());
for (var code in rule.diagnosticCodes) {
_codeMap.remove(code.uniqueName.toLowerCase());
_codeMap.remove(code.lowerCaseUniqueName);
}
}
@@ -105,7 +105,7 @@ class Registry with IterableMixin<AbstractAnalysisRule> {
void unregisterWarningRule(AbstractAnalysisRule rule) {
_warningRules.remove(rule.name.toLowerCase());
for (var code in rule.diagnosticCodes) {
_codeMap.remove(code.uniqueName.toLowerCase());
_codeMap.remove(code.lowerCaseUniqueName);
}
}
}
@@ -293,7 +293,7 @@ class GatheringDiagnosticListener implements DiagnosticListener {
}
buffer.write(expectedCount);
buffer.write(" errors of type ");
buffer.write(code.uniqueName);
buffer.write(code.lowerCaseUniqueName);
buffer.write(", found ");
buffer.write(actualCount);
}
@@ -313,7 +313,7 @@ class GatheringDiagnosticListener implements DiagnosticListener {
buffer.write("; ");
}
buffer.write("0 errors of type ");
buffer.write(code.uniqueName);
buffer.write(code.lowerCaseUniqueName);
buffer.write(", found ");
buffer.write(actualCount);
buffer.write(" (");
@@ -500,5 +500,5 @@ extension on DiagnosticCode {
///
/// For example, if the unique name is `TestClass.MY_ERROR`, this method will
/// return `diag.myError`.
String get constantName => 'diag.${uniqueName.toCamelCase()}';
String get constantName => 'diag.${lowerCaseUniqueName.toCamelCase()}';
}
@@ -61,7 +61,9 @@ class ConstantsDataComputer extends DataComputer<String> {
.map((e) => e.diagnosticCode)
.where((c) => c != diag.constInitializedWithNonConstantValue);
return diagnosticCodes.isNotEmpty
? diagnosticCodes.map((c) => c.uniqueName.toUpperCase()).join(',')
? diagnosticCodes
.map((c) => c.lowerCaseUniqueName.toUpperCase())
.join(',')
: null;
}
@@ -66,7 +66,7 @@ class _InheritanceDataComputer extends DataComputer<String> {
List<Diagnostic> diagnostics,
) {
return diagnostics
.map((e) => e.diagnosticCode.uniqueName.toUpperCase())
.map((e) => e.diagnosticCode.lowerCaseUniqueName.toUpperCase())
.join(',');
}
@@ -348,7 +348,7 @@ void f() {
void _assertHasLintReported(List<Diagnostic> diagnostics, String name) {
var matching = diagnostics.where((element) {
var diagnosticCode = element.diagnosticCode;
return diagnosticCode is LintCode && diagnosticCode.name == name;
return diagnosticCode is LintCode && diagnosticCode.lowerCaseName == name;
}).toList();
expect(matching, hasLength(1));
}
@@ -108,7 +108,7 @@ class AnalysisDriver_LintTest extends PubPackageResolutionTest
useEmptyByteStore();
registerLintRule(_AlwaysReportedLint.instance);
writeTestPackageAnalysisOptionsFile(
analysisOptionsContent(rules: [_AlwaysReportedLint.code.name]),
analysisOptionsContent(rules: [_AlwaysReportedLint.code.lowerCaseName]),
);
}
@@ -123,7 +123,10 @@ class AnalysisDriver_LintTest extends PubPackageResolutionTest
await resolveTestFile();
// Existing/empty file triggers the lint.
_assertHasLintReported(result.diagnostics, _AlwaysReportedLint.code.name);
_assertHasLintReported(
result.diagnostics,
_AlwaysReportedLint.code.lowerCaseName,
);
}
test_getResolvedUnit_lint_notExistingFile() async {
@@ -136,7 +139,7 @@ class AnalysisDriver_LintTest extends PubPackageResolutionTest
void _assertHasLintReported(List<Diagnostic> diagnostics, String name) {
var matching = diagnostics.where((element) {
var diagnosticCode = element.diagnosticCode;
return diagnosticCode is LintCode && diagnosticCode.name == name;
return diagnosticCode is LintCode && diagnosticCode.lowerCaseName == name;
}).toList();
expect(matching, hasLength(1));
}
@@ -415,7 +415,7 @@ class DriverEventsPrinter {
void _writeDiagnostic(Diagnostic d) {
sink.writelnWithIndent(
'${d.offset} +${d.length} ${d.diagnosticCode.name.toUpperCase()}',
'${d.offset} +${d.length} ${d.diagnosticCode.lowerCaseName.toUpperCase()}',
);
}
@@ -1983,7 +1983,7 @@ class ResolvedUnitResultPrinter {
void _writeDiagnostic(Diagnostic d) {
sink.writelnWithIndent(
'${d.offset} +${d.length} ${d.diagnosticCode.name.toUpperCase()}',
'${d.offset} +${d.length} ${d.diagnosticCode.lowerCaseName.toUpperCase()}',
);
}
@@ -43,7 +43,7 @@ class ErrorCodeValuesTest {
StringBuffer missingCodes = StringBuffer();
errorTypeMap.forEach((Type errorType, List<DiagnosticCode> codes) {
var listedNames = codes
.map((DiagnosticCode code) => code.uniqueName)
.map((DiagnosticCode code) => code.lowerCaseUniqueName)
.toSet();
var declaredNames = reflectClass(errorType).declarations.values
@@ -445,7 +445,7 @@ class DocumentationValidator {
_reportProblem('Expected one error but found none ($section $index).');
} else if (errorCount == 1) {
Diagnostic diagnostic = diagnostics[0];
if (diagnostic.diagnosticCode.name.toLowerCase() != codeName) {
if (diagnostic.diagnosticCode.lowerCaseName != codeName) {
_reportProblem(
'Expected an error with code $codeName, '
'found ${diagnostic.diagnosticCode} ($section $index).',
@@ -492,7 +492,7 @@ class VerifyDiagnosticsTest {
var nameToCodeMap = <String, List<DiagnosticCode>>{};
var nameToPublishedMap = <String, bool>{};
for (var code in diagnosticCodeValues) {
var name = code.name;
var name = code.lowerCaseName;
nameToCodeMap.putIfAbsent(name, () => []).add(code);
nameToPublishedMap[name] =
(nameToPublishedMap[name] ?? false) || code.hasPublishedDocs;
@@ -516,7 +516,7 @@ class VerifyDiagnosticsTest {
);
for (var code in unpublished) {
buffer.writeln();
buffer.write('- ${code.runtimeType}.${code.uniqueName}');
buffer.write('- ${code.runtimeType}.${code.lowerCaseUniqueName}');
}
fail(buffer.toString());
}
+1 -1
View File
@@ -60,7 +60,7 @@ class SnippetTester {
bool isAllowedLint(Diagnostic diagnostic) {
var errorCode = diagnostic.diagnosticCode;
return errorCode is LintCode &&
errorCode.name == 'non_constant_identifier_names' &&
errorCode.lowerCaseName == 'non_constant_identifier_names' &&
diagnostic.message.contains("'test_");
}
+1 -1
View File
@@ -87,7 +87,7 @@ class ABEngine {
out.add(
HarnessDiagnostic(
path: file,
code: diagnostic.diagnosticCode.name,
code: diagnostic.diagnosticCode.lowerCaseName,
severity: severityName,
offset: diagnostic.offset,
length: diagnostic.length,