Skip to content

Commit 8999635

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Add sanity check for sharedName.
Adds logic to the message code generators to verify that there are no unnecessary analyzer `sharedName` code mappings. An unnecessary `sharedName` code mapping is an analyzer message whose shared name is unique, but different from the analyzer code. For example, if there were a `ParserErrorCode.FOO` message with a shared name of `BAR`, but no other message had the shared name of `BAR`, that would be unnecessary, because the message's analyzer code could simply be changed to `ParserErrorCode.BAR`. Note, however, that if there were a `ParserErrorCode.FOO` message with a shared name of `BAR` and a `ParserErrorCode.BAR` message with no explicitly declared `sharedName`, that would be ok, because `ParserErrorCode.BAR` would implicitly have a shared code of `BAR`, meaning that `BAR` would not be unique. Change-Id: I6a6a6964da34e1950df292419231ef4db096c121 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/457541 Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 0cf9748 commit 8999635

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pkg/analyzer_utilities/lib/messages.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ class DiagnosticTables {
480480
var analyzerCodeCamelCaseNameDuplicateChecker = _DuplicateChecker<String>(
481481
kind: 'Analyzer code camelCase name',
482482
);
483+
var analyzerSharedNameToMessages =
484+
<String, List<MessageWithAnalyzerCode>>{};
483485
for (var message in messages) {
484486
if (message is CfeStyleMessage) {
485487
var frontEndCode = message.frontEndCode;
@@ -495,15 +497,39 @@ class DiagnosticTables {
495497
analyzerCodeDuplicateChecker[analyzerCode] = message;
496498
analyzerCodeCamelCaseNameDuplicateChecker[analyzerCode.camelCaseName] =
497499
message;
500+
(analyzerSharedNameToMessages[message.sharedName ??
501+
analyzerCode.snakeCaseName] ??=
502+
[])
503+
.add(message);
498504
}
499505
}
500506

501507
analyzerCodeDuplicateChecker.check();
502508
analyzerCodeCamelCaseNameDuplicateChecker.check();
503509
frontEndCodeDuplicateChecker.check();
510+
_checkSharedNames(analyzerSharedNameToMessages);
511+
504512
sortedSharedDiagnostics.sortBy((e) => e.analyzerCode.camelCaseName);
505513
sortedFrontEndDiagnostics.sortBy((e) => e.frontEndCode);
506514
}
515+
516+
static void _checkSharedNames(
517+
Map<String, List<MessageWithAnalyzerCode>> analyzerSharedNameToMessages,
518+
) {
519+
for (var MapEntry(key: sharedName, value: messages)
520+
in analyzerSharedNameToMessages.entries) {
521+
if (messages case [
522+
var message,
523+
] when sharedName != message.analyzerCode.snakeCaseName) {
524+
var sharedNameJson = json.encode(sharedName);
525+
throw LocatedError(
526+
'This is the only message that uses shared name '
527+
'$sharedNameJson. The message should be renamed to $sharedNameJson.',
528+
node: message.keyNode,
529+
);
530+
}
531+
}
532+
}
507533
}
508534

509535
/// In-memory representation of diagnostic information obtained from the file

0 commit comments

Comments
 (0)