Skip to content

Commit 2c63b81

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Change analyzerCodes into a list of enums.
Changes the representation of analyzer codes in the `Codes` class from a `List<String>` to a `List<AnalyzerCode>`, where `AnalyzerCode` is a generated enum. This has two major benefits: - It allows switch statements acting on analyzer codes to be more efficient, since they don't have to do string comparisons. - It allows unnecessary cases to be safely removed from switch statements acting on analyzer codes, since it is now clear from the type system what possible values an AnalyzerCode can take on. Several unnecessary cases are removed in this CL. Change-Id: I6a6a696463848ece8db2a34fa65ddd5cf0dc8f0c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/449920 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent be2d42a commit 2c63b81

File tree

6 files changed

+221
-303
lines changed

6 files changed

+221
-303
lines changed

pkg/_fe_analyzer_shared/lib/src/messages/codes.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Code {
2828
/// this error to its corresponding Analyzer error.
2929
final int index;
3030

31-
final List<String>? analyzerCodes;
31+
final List<AnalyzerCode>? analyzerCodes;
3232

3333
final CfeSeverity severity;
3434

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 119 additions & 61 deletions
Large diffs are not rendered by default.

pkg/_fe_analyzer_shared/lib/src/scanner/errors.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void translateErrorToken(ErrorToken token, ReportError reportError) {
3131

3232
Code errorCode = token.errorCode;
3333
switch (errorCode.analyzerCodes?.first) {
34-
case "UNTERMINATED_STRING_LITERAL":
34+
case AnalyzerCode.unterminatedStringLiteral:
3535
// TODO(paulberry,ahe): Fasta reports the error location as the entire
3636
// string; analyzer expects the end of the string.
3737
reportError(
@@ -41,7 +41,7 @@ void translateErrorToken(ErrorToken token, ReportError reportError) {
4141
);
4242
return;
4343

44-
case "UNTERMINATED_MULTI_LINE_COMMENT":
44+
case AnalyzerCode.unterminatedMultiLineComment:
4545
// TODO(paulberry,ahe): Fasta reports the error location as the entire
4646
// comment; analyzer expects the end of the comment.
4747
reportError(
@@ -51,27 +51,27 @@ void translateErrorToken(ErrorToken token, ReportError reportError) {
5151
);
5252
return;
5353

54-
case "MISSING_DIGIT":
54+
case AnalyzerCode.missingDigit:
5555
// TODO(paulberry,ahe): Fasta reports the error location as the entire
5656
// number; analyzer expects the end of the number.
5757
charOffset = endOffset - 1;
5858
return _makeError(ScannerErrorCode.missingDigit, null);
5959

60-
case "MISSING_HEX_DIGIT":
60+
case AnalyzerCode.missingHexDigit:
6161
// TODO(paulberry,ahe): Fasta reports the error location as the entire
6262
// number; analyzer expects the end of the number.
6363
charOffset = endOffset - 1;
6464
return _makeError(ScannerErrorCode.missingHexDigit, null);
6565

66-
case "ILLEGAL_CHARACTER":
66+
case AnalyzerCode.illegalCharacter:
6767
// We can safely assume `token.character` is non-`null` because this error
6868
// is only reported when there is a character associated with the token.
6969
return _makeError(ScannerErrorCode.illegalCharacter, [token.character!]);
7070

71-
case "UNEXPECTED_SEPARATOR_IN_NUMBER":
71+
case AnalyzerCode.unexpectedSeparatorInNumber:
7272
return _makeError(ScannerErrorCode.unexpectedSeparatorInNumber, null);
7373

74-
case "UNSUPPORTED_OPERATOR":
74+
case AnalyzerCode.unsupportedOperator:
7575
return _makeError(ScannerErrorCode.unsupportedOperator, [
7676
(token as UnsupportedOperator).token.lexeme,
7777
]);

pkg/analyzer/lib/src/fasta/ast_builder.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import 'package:_fe_analyzer_shared/src/messages/codes.dart'
3030
codeExpectedIdentifier,
3131
codeExperimentNotEnabled,
3232
codeExtraneousModifier,
33-
codeInternalProblemUnhandled;
33+
codeInternalProblemUnhandled,
34+
AnalyzerCode;
3435
import 'package:_fe_analyzer_shared/src/parser/parser.dart'
3536
show
3637
Assert,
@@ -223,7 +224,7 @@ class AstBuilder extends StackListener {
223224
}) {
224225
if (directives.isEmpty &&
225226
(message.code.analyzerCodes?.contains(
226-
'NON_PART_OF_DIRECTIVE_IN_PART',
227+
AnalyzerCode.nonPartOfDirectiveInPart,
227228
) ??
228229
false)) {
229230
message = codeDirectiveAfterDeclaration;

0 commit comments

Comments
 (0)