Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 61096ac

Browse files
danrubelcommit-bot@chromium.org
authored andcommitted
Improve analyzer/fasta error code matching
This addresses a comment in https://dart-review.googlesource.com/c/sdk/+/74600, updates the "run fasta generate first" exception message, and converts more Analyzer errors to be auto generated. Change-Id: I9c98184d1b9fe642dab4d66fbcd961dc27dfa6f9 Reviewed-on: https://dart-review.googlesource.com/74900 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Dan Rubel <[email protected]>
1 parent 1a8b1bf commit 61096ac

File tree

6 files changed

+106
-102
lines changed

6 files changed

+106
-102
lines changed

pkg/analyzer/lib/src/dart/error/syntactic_errors.dart

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -602,17 +602,14 @@ class ParserErrorCode extends ErrorCode {
602602
correction: "Try adding a library name after the 'of'.");
603603

604604
static const ParserErrorCode MISSING_PREFIX_IN_DEFERRED_IMPORT =
605-
const ParserErrorCode('MISSING_PREFIX_IN_DEFERRED_IMPORT',
606-
"Deferred imports should have a prefix.",
607-
correction: "Try adding a prefix to the import.");
605+
_MISSING_PREFIX_IN_DEFERRED_IMPORT;
608606

609607
static const ParserErrorCode MISSING_STAR_AFTER_SYNC = const ParserErrorCode(
610608
'MISSING_STAR_AFTER_SYNC',
611609
"The modifier 'sync' must be followed by a star ('*').",
612610
correction: "Try removing the modifier, or add a star.");
613611

614-
static const ParserErrorCode MISSING_STATEMENT =
615-
const ParserErrorCode('MISSING_STATEMENT', "Expected a statement.");
612+
static const ParserErrorCode MISSING_STATEMENT = _MISSING_STATEMENT;
616613

617614
/**
618615
* Parameters:
@@ -638,11 +635,8 @@ class ParserErrorCode extends ErrorCode {
638635
"Can't have both positional and named parameters in a single parameter list.",
639636
correction: "Try choosing a single style of optional parameters.");
640637

641-
static const ParserErrorCode MULTIPLE_EXTENDS_CLAUSES = const ParserErrorCode(
642-
'MULTIPLE_EXTENDS_CLAUSES',
643-
"Each class definition can have at most one extends clause.",
644-
correction:
645-
"Try choosing one superclass and define your class to implement (or mix in) the others.");
638+
static const ParserErrorCode MULTIPLE_EXTENDS_CLAUSES =
639+
_MULTIPLE_EXTENDS_CLAUSES;
646640

647641
static const ParserErrorCode MULTIPLE_IMPLEMENTS_CLAUSES = const ParserErrorCode(
648642
'MULTIPLE_IMPLEMENTS_CLAUSES',
@@ -651,24 +645,17 @@ class ParserErrorCode extends ErrorCode {
651645
"Try combining all of the implements clauses into a single clause.");
652646

653647
static const ParserErrorCode MULTIPLE_LIBRARY_DIRECTIVES =
654-
const ParserErrorCode('MULTIPLE_LIBRARY_DIRECTIVES',
655-
"Only one library directive may be declared in a file.",
656-
correction: "Try removing all but one of the library directives.");
648+
_MULTIPLE_LIBRARY_DIRECTIVES;
657649

658650
static const ParserErrorCode MULTIPLE_NAMED_PARAMETER_GROUPS =
659651
const ParserErrorCode('MULTIPLE_NAMED_PARAMETER_GROUPS',
660652
"Can't have multiple groups of named parameters in a single parameter list.",
661653
correction: "Try combining all of the groups into a single group.");
662654

663-
static const ParserErrorCode MULTIPLE_ON_CLAUSES = const ParserErrorCode(
664-
'MULTIPLE_ON_CLAUSES',
665-
"Each mixin definition can have at most one on clause.",
666-
correction: "Try combining all of the on clauses into a single clause.");
655+
static const ParserErrorCode MULTIPLE_ON_CLAUSES = _MULTIPLE_ON_CLAUSES;
667656

668657
static const ParserErrorCode MULTIPLE_PART_OF_DIRECTIVES =
669-
const ParserErrorCode('MULTIPLE_PART_OF_DIRECTIVES',
670-
"Only one part-of directive may be declared in a file.",
671-
correction: "Try removing all but one of the part-of directives.");
658+
_MULTIPLE_PART_OF_DIRECTIVES;
672659

673660
static const ParserErrorCode MULTIPLE_POSITIONAL_PARAMETER_GROUPS =
674661
const ParserErrorCode('MULTIPLE_POSITIONAL_PARAMETER_GROUPS',
@@ -687,11 +674,7 @@ class ParserErrorCode extends ErrorCode {
687674
correction:
688675
"Try moving all but one of the declarations inside the loop body.");
689676

690-
static const ParserErrorCode MULTIPLE_WITH_CLAUSES = const ParserErrorCode(
691-
'MULTIPLE_WITH_CLAUSES',
692-
"Each class definition can have at most one with clause.",
693-
correction:
694-
"Try combining all of the with clauses into a single clause.");
677+
static const ParserErrorCode MULTIPLE_WITH_CLAUSES = _MULTIPLE_WITH_CLAUSES;
695678

696679
static const ParserErrorCode NAMED_FUNCTION_EXPRESSION = const ParserErrorCode(
697680
'NAMED_FUNCTION_EXPRESSION', "Function expressions can't be named.",
@@ -722,10 +705,7 @@ class ParserErrorCode extends ErrorCode {
722705
correction: "Try removing the word 'native'.");
723706

724707
static const ParserErrorCode NATIVE_CLAUSE_SHOULD_BE_ANNOTATION =
725-
const ParserErrorCode('NATIVE_CLAUSE_SHOULD_BE_ANNOTATION',
726-
"Native clause in this form is deprecated.",
727-
correction: "Try removing this native clause and adding @native()"
728-
" or @native('native-name') before the declaration.");
708+
_NATIVE_CLAUSE_SHOULD_BE_ANNOTATION;
729709

730710
static const ParserErrorCode NON_CONSTRUCTOR_FACTORY = const ParserErrorCode(
731711
'NON_CONSTRUCTOR_FACTORY',
@@ -798,16 +778,10 @@ class ParserErrorCode extends ErrorCode {
798778
_PREFIX_AFTER_COMBINATOR;
799779

800780
static const ParserErrorCode REDIRECTING_CONSTRUCTOR_WITH_BODY =
801-
const ParserErrorCode('REDIRECTING_CONSTRUCTOR_WITH_BODY',
802-
"Redirecting constructors can't have a body.",
803-
correction: "Try removing the body, or "
804-
"not making this a redirecting constructor.");
781+
_REDIRECTING_CONSTRUCTOR_WITH_BODY;
805782

806783
static const ParserErrorCode REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR =
807-
const ParserErrorCode('REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR',
808-
"Only factory constructor can specify '=' redirection.",
809-
correction: "Try making this a factory constructor, or "
810-
"not making this a redirecting constructor.");
784+
_REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR;
811785

812786
static const ParserErrorCode SETTER_IN_FUNCTION = const ParserErrorCode(
813787
'SETTER_IN_FUNCTION',

pkg/analyzer/lib/src/dart/error/syntactic_errors.g.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ final fastaAnalyzerErrorCodes = <ErrorCode>[
2828
_STATIC_AFTER_VAR,
2929
_STATIC_AFTER_FINAL,
3030
_STATIC_AFTER_CONST,
31+
_REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR,
32+
_REDIRECTING_CONSTRUCTOR_WITH_BODY,
33+
_NATIVE_CLAUSE_SHOULD_BE_ANNOTATION,
34+
_MULTIPLE_WITH_CLAUSES,
35+
_MULTIPLE_PART_OF_DIRECTIVES,
36+
_MULTIPLE_ON_CLAUSES,
37+
_MULTIPLE_LIBRARY_DIRECTIVES,
38+
_MULTIPLE_EXTENDS_CLAUSES,
39+
_MISSING_STATEMENT,
40+
_MISSING_PREFIX_IN_DEFERRED_IMPORT,
3141
];
3242

3343
const ParserErrorCode _CONTINUE_OUTSIDE_OF_LOOP = const ParserErrorCode(
@@ -64,11 +74,63 @@ const ParserErrorCode _INVALID_AWAIT_IN_FOR = const ParserErrorCode(
6474
"The keyword 'await' isn't allowed for a normal 'for' statement.",
6575
correction: "Try removing the keyword, or use a for-each statement.");
6676

77+
const ParserErrorCode _MISSING_PREFIX_IN_DEFERRED_IMPORT =
78+
const ParserErrorCode('MISSING_PREFIX_IN_DEFERRED_IMPORT',
79+
"Deferred imports should have a prefix.",
80+
correction: "Try adding a prefix to the import.");
81+
82+
const ParserErrorCode _MISSING_STATEMENT =
83+
const ParserErrorCode('MISSING_STATEMENT', "Expected a statement.");
84+
85+
const ParserErrorCode _MULTIPLE_EXTENDS_CLAUSES = const ParserErrorCode(
86+
'MULTIPLE_EXTENDS_CLAUSES',
87+
"Each class definition can have at most one extends clause.",
88+
correction:
89+
"Try choosing one superclass and define your class to implement (or mix in) the others.");
90+
91+
const ParserErrorCode _MULTIPLE_LIBRARY_DIRECTIVES = const ParserErrorCode(
92+
'MULTIPLE_LIBRARY_DIRECTIVES',
93+
"Only one library directive may be declared in a file.",
94+
correction: "Try removing all but one of the library directives.");
95+
96+
const ParserErrorCode _MULTIPLE_ON_CLAUSES = const ParserErrorCode(
97+
'MULTIPLE_ON_CLAUSES',
98+
"Each mixin definition can have at most one on clause.",
99+
correction: "Try combining all of the on clauses into a single clause.");
100+
101+
const ParserErrorCode _MULTIPLE_PART_OF_DIRECTIVES = const ParserErrorCode(
102+
'MULTIPLE_PART_OF_DIRECTIVES',
103+
"Only one part-of directive may be declared in a file.",
104+
correction: "Try removing all but one of the part-of directives.");
105+
106+
const ParserErrorCode _MULTIPLE_WITH_CLAUSES = const ParserErrorCode(
107+
'MULTIPLE_WITH_CLAUSES',
108+
"Each class definition can have at most one with clause.",
109+
correction: "Try combining all of the with clauses into a single clause.");
110+
111+
const ParserErrorCode _NATIVE_CLAUSE_SHOULD_BE_ANNOTATION = const ParserErrorCode(
112+
'NATIVE_CLAUSE_SHOULD_BE_ANNOTATION',
113+
"Native clause in this form is deprecated.",
114+
correction:
115+
"Try removing this native clause and adding @native() or @native('native-name') before the declaration.");
116+
67117
const ParserErrorCode _PREFIX_AFTER_COMBINATOR = const ParserErrorCode(
68118
'PREFIX_AFTER_COMBINATOR',
69119
"The prefix ('as' clause) should come before any show/hide combinators.",
70120
correction: "Try moving the prefix before the combinators.");
71121

122+
const ParserErrorCode _REDIRECTING_CONSTRUCTOR_WITH_BODY = const ParserErrorCode(
123+
'REDIRECTING_CONSTRUCTOR_WITH_BODY',
124+
"Redirecting constructors can't have a body.",
125+
correction:
126+
"Try removing the body, or not making this a redirecting constructor.");
127+
128+
const ParserErrorCode _REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR =
129+
const ParserErrorCode('REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR',
130+
"Only factory constructor can specify '=' redirection.",
131+
correction:
132+
"Try making this a factory constructor, or remove the redirection.");
133+
72134
const ParserErrorCode _STATIC_AFTER_CONST = const ParserErrorCode(
73135
'STATIC_AFTER_CONST',
74136
"The modifier 'static' should be before the modifier 'const'.",

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

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -454,46 +454,18 @@ class FastaErrorReporter {
454454
errorReporter?.reportErrorForOffset(
455455
ParserErrorCode.MISSING_METHOD_PARAMETERS, offset, length);
456456
return;
457-
case "MISSING_PREFIX_IN_DEFERRED_IMPORT":
458-
errorReporter?.reportErrorForOffset(
459-
ParserErrorCode.MISSING_PREFIX_IN_DEFERRED_IMPORT, offset, length);
460-
return;
461457
case "MISSING_STAR_AFTER_SYNC":
462458
errorReporter?.reportErrorForOffset(
463459
ParserErrorCode.MISSING_STAR_AFTER_SYNC, offset, length);
464460
return;
465-
case "MISSING_STATEMENT":
466-
errorReporter?.reportErrorForOffset(
467-
ParserErrorCode.MISSING_STATEMENT, offset, length);
468-
return;
469461
case "MISSING_TYPEDEF_PARAMETERS":
470462
errorReporter?.reportErrorForOffset(
471463
ParserErrorCode.MISSING_TYPEDEF_PARAMETERS, offset, length);
472464
return;
473-
case "MULTIPLE_EXTENDS_CLAUSES":
474-
errorReporter?.reportErrorForOffset(
475-
ParserErrorCode.MULTIPLE_EXTENDS_CLAUSES, offset, length);
476-
return;
477465
case "MULTIPLE_IMPLEMENTS_CLAUSES":
478466
errorReporter?.reportErrorForOffset(
479467
ParserErrorCode.MULTIPLE_IMPLEMENTS_CLAUSES, offset, length);
480468
return;
481-
case "MULTIPLE_LIBRARY_DIRECTIVES":
482-
errorReporter?.reportErrorForOffset(
483-
ParserErrorCode.MULTIPLE_LIBRARY_DIRECTIVES, offset, length);
484-
return;
485-
case "MULTIPLE_ON_CLAUSES":
486-
errorReporter?.reportErrorForOffset(
487-
ParserErrorCode.MULTIPLE_ON_CLAUSES, offset, length);
488-
return;
489-
case "MULTIPLE_WITH_CLAUSES":
490-
errorReporter?.reportErrorForOffset(
491-
ParserErrorCode.MULTIPLE_WITH_CLAUSES, offset, length);
492-
return;
493-
case "MULTIPLE_PART_OF_DIRECTIVES":
494-
errorReporter?.reportErrorForOffset(
495-
ParserErrorCode.MULTIPLE_PART_OF_DIRECTIVES, offset, length);
496-
return;
497469
case "NAMED_FUNCTION_EXPRESSION":
498470
errorReporter?.reportErrorForOffset(
499471
ParserErrorCode.NAMED_FUNCTION_EXPRESSION, offset, length);
@@ -502,10 +474,6 @@ class FastaErrorReporter {
502474
errorReporter?.reportErrorForOffset(
503475
ParserErrorCode.NAMED_PARAMETER_OUTSIDE_GROUP, offset, length);
504476
return;
505-
case "NATIVE_CLAUSE_SHOULD_BE_ANNOTATION":
506-
errorReporter?.reportErrorForOffset(
507-
ParserErrorCode.NATIVE_CLAUSE_SHOULD_BE_ANNOTATION, offset, length);
508-
return;
509477
case "NON_PART_OF_DIRECTIVE_IN_PART":
510478
errorReporter?.reportErrorForOffset(
511479
ParserErrorCode.NON_PART_OF_DIRECTIVE_IN_PART, offset, length);
@@ -524,16 +492,6 @@ class FastaErrorReporter {
524492
offset,
525493
length);
526494
return;
527-
case "REDIRECTING_CONSTRUCTOR_WITH_BODY":
528-
errorReporter?.reportErrorForOffset(
529-
ParserErrorCode.REDIRECTING_CONSTRUCTOR_WITH_BODY, offset, length);
530-
return;
531-
case "REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR":
532-
errorReporter?.reportErrorForOffset(
533-
ParserErrorCode.REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR,
534-
offset,
535-
length);
536-
return;
537495
case "RETURN_IN_GENERATOR":
538496
errorReporter?.reportErrorForOffset(
539497
CompileTimeErrorCode.RETURN_IN_GENERATOR, offset, length,

pkg/analyzer/tool/messages/generate.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ part of 'syntactic_errors.dart';
184184
for (ErrorCode errorCode in errorCodeValues) {
185185
if (errorCode is ParserErrorCode) {
186186
String message =
187-
errorCode.message.replaceAll('{0}', '').replaceAll('{1}', '');
187+
errorCode.message.replaceAll(new RegExp(r'\{\d+\}'), '');
188188
messageToName[message] = errorCode.name;
189189
}
190190
}
@@ -262,6 +262,6 @@ Error: Expected the text in the 'analyzerCode:' field to contain
262262
""";
263263

264264
const shouldRunFastaGenerateMessagesFirst = """
265-
Error: Encountered an error that would be caught
266-
by first running 'pkg/front_end/tool/fasta generate-messages'.
265+
Error: After modifying message.yaml, run this first:
266+
pkg/front_end/tool/fasta generate-messages
267267
""";

pkg/front_end/lib/src/fasta/fasta_codes_generated.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,7 +2608,7 @@ const Code<Null> codeExpectedStatement = messageExpectedStatement;
26082608
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
26092609
const MessageCode messageExpectedStatement = const MessageCode(
26102610
"ExpectedStatement",
2611-
analyzerCode: "MISSING_STATEMENT",
2611+
index: 29,
26122612
message: r"""Expected a statement.""");
26132613

26142614
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
@@ -5160,7 +5160,7 @@ const Code<Null> codeMissingPrefixInDeferredImport =
51605160
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
51615161
const MessageCode messageMissingPrefixInDeferredImport = const MessageCode(
51625162
"MissingPrefixInDeferredImport",
5163-
analyzerCode: "MISSING_PREFIX_IN_DEFERRED_IMPORT",
5163+
index: 30,
51645164
message: r"""Deferred imports should have a prefix.""",
51655165
tip: r"""Try adding a prefix to the import.""");
51665166

@@ -5223,7 +5223,7 @@ const Code<Null> codeMultipleExtends = messageMultipleExtends;
52235223

52245224
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
52255225
const MessageCode messageMultipleExtends = const MessageCode("MultipleExtends",
5226-
analyzerCode: "MULTIPLE_EXTENDS_CLAUSES",
5226+
index: 28,
52275227
message: r"""Each class definition can have at most one extends clause.""",
52285228
tip:
52295229
r"""Try choosing one superclass and define your class to implement (or mix in) the others.""");
@@ -5247,7 +5247,7 @@ const Code<Null> codeMultipleLibraryDirectives =
52475247
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
52485248
const MessageCode messageMultipleLibraryDirectives = const MessageCode(
52495249
"MultipleLibraryDirectives",
5250-
analyzerCode: "MULTIPLE_LIBRARY_DIRECTIVES",
5250+
index: 27,
52515251
message: r"""Only one library directive may be declared in a file.""",
52525252
tip: r"""Try removing all but one of the library directives.""");
52535253

@@ -5257,7 +5257,7 @@ const Code<Null> codeMultipleOnClauses = messageMultipleOnClauses;
52575257
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
52585258
const MessageCode messageMultipleOnClauses = const MessageCode(
52595259
"MultipleOnClauses",
5260-
analyzerCode: "MULTIPLE_ON_CLAUSES",
5260+
index: 26,
52615261
message: r"""Each mixin definition can have at most one on clause.""",
52625262
tip: r"""Try combining all of the on clauses into a single clause.""");
52635263

@@ -5266,7 +5266,7 @@ const Code<Null> codeMultipleWith = messageMultipleWith;
52665266

52675267
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
52685268
const MessageCode messageMultipleWith = const MessageCode("MultipleWith",
5269-
analyzerCode: "MULTIPLE_WITH_CLAUSES",
5269+
index: 24,
52705270
message: r"""Each class definition can have at most one with clause.""",
52715271
tip: r"""Try combining all of the with clauses into a single clause.""");
52725272

@@ -5286,7 +5286,7 @@ const Code<Null> codeNativeClauseShouldBeAnnotation =
52865286
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
52875287
const MessageCode messageNativeClauseShouldBeAnnotation = const MessageCode(
52885288
"NativeClauseShouldBeAnnotation",
5289-
analyzerCode: "NATIVE_CLAUSE_SHOULD_BE_ANNOTATION",
5289+
index: 23,
52905290
message: r"""Native clause in this form is deprecated.""",
52915291
tip:
52925292
r"""Try removing this native clause and adding @native() or @native('native-name') before the declaration.""");
@@ -6075,7 +6075,7 @@ const Code<Null> codePartOfTwice = messagePartOfTwice;
60756075

60766076
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
60776077
const MessageCode messagePartOfTwice = const MessageCode("PartOfTwice",
6078-
analyzerCode: "MULTIPLE_PART_OF_DIRECTIVES",
6078+
index: 25,
60796079
message: r"""Only one part-of directive may be declared in a file.""",
60806080
tip: r"""Try removing all but one of the part-of directives.""");
60816081

@@ -6322,7 +6322,7 @@ const Code<Null> codeRedirectingConstructorWithBody =
63226322
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
63236323
const MessageCode messageRedirectingConstructorWithBody = const MessageCode(
63246324
"RedirectingConstructorWithBody",
6325-
analyzerCode: "REDIRECTING_CONSTRUCTOR_WITH_BODY",
6325+
index: 22,
63266326
message: r"""Redirecting constructors can't have a body.""",
63276327
tip:
63286328
r"""Try removing the body, or not making this a redirecting constructor.""");
@@ -6333,7 +6333,7 @@ const Code<Null> codeRedirectionInNonFactory = messageRedirectionInNonFactory;
63336333
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
63346334
const MessageCode messageRedirectionInNonFactory = const MessageCode(
63356335
"RedirectionInNonFactory",
6336-
analyzerCode: "REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR",
6336+
index: 21,
63376337
message: r"""Only factory constructor can specify '=' redirection.""",
63386338
tip:
63396339
r"""Try making this a factory constructor, or remove the redirection.""");

0 commit comments

Comments
 (0)