Skip to content

Commit 47cc314

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Clean up representation of analyzer code.
Changes the code generation logic for messages so that there is no longer an `analyzerCodes` getter in the `CfeStyleErrorCodeInfo` base class, because only the derived class `SharedErrorCodeInfo` can have an analyzer code. Also, removes the logic that used to exist for allowing a front-end error code to have multiple analyzer codes. It no longer makes sense to support this, since instances of `SharedErrorCodeInfo` must always have exactly one analyzer code. Finally, removes the unused `analyzerCodes` field from the `_TemplateCompiler` class. These clean-ups simplisy the logic for supporting `SharedErrorCodeInfo.analyzerCode`, so they should make that support easier to maintain in the future. Change-Id: I6a6a6964a800f3c01d7585cf2129b5af736b4ca2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/452848 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 602bd27 commit 47cc314

File tree

3 files changed

+40
-57
lines changed

3 files changed

+40
-57
lines changed

pkg/analyzer_utilities/lib/messages.dart

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,6 @@ class AnalyzerCode implements Comparable<AnalyzerCode> {
233233
/// In-memory representation of error code information obtained from a
234234
/// `messages.yaml` file in `pkg/front_end` or `pkg/_fe_analyzer_shared`.
235235
abstract class CfeStyleErrorCodeInfo extends ErrorCodeInfo {
236-
/// The set of analyzer error codes that corresponds to this error code, if
237-
/// any.
238-
final List<AnalyzerCode> analyzerCodes;
239-
240236
/// The index of the error in the analyzer's `fastaAnalyzerErrorCodes` table.
241237
final int? index;
242238

@@ -245,45 +241,14 @@ abstract class CfeStyleErrorCodeInfo extends ErrorCodeInfo {
245241
final String? cfeSeverity;
246242

247243
CfeStyleErrorCodeInfo.fromYaml(YamlMap yaml)
248-
: analyzerCodes = _decodeAnalyzerCodes(yaml['analyzerCode']),
249-
index = _decodeIndex(yaml['index']),
244+
: index = _decodeIndex(yaml['index']),
250245
cfeSeverity = _decodeSeverity(yaml['severity']),
251246
super.fromYaml(yaml) {
252247
if (yaml['problemMessage'] == null) {
253248
throw 'Missing problemMessage';
254249
}
255250
}
256251

257-
static AnalyzerCode _decodeAnalyzerCode(String s) {
258-
switch (s.split('.')) {
259-
case [var errorName] when errorName == errorName.toUpperCase():
260-
return AnalyzerCode(className: null, snakeCaseErrorName: errorName);
261-
case [var className, var errorName]
262-
when errorName == errorName.toUpperCase():
263-
return AnalyzerCode(
264-
className: className,
265-
snakeCaseErrorName: errorName,
266-
);
267-
default:
268-
throw StateError(
269-
'Analyzer codes must take the form DIAGNOSTIC_NAME or '
270-
'ClassName.DIAGNOSTIC_NAME. Found ${json.encode(s)} instead.',
271-
);
272-
}
273-
}
274-
275-
static List<AnalyzerCode> _decodeAnalyzerCodes(Object? value) {
276-
if (value == null) {
277-
return const [];
278-
} else if (value is String) {
279-
return [_decodeAnalyzerCode(value)];
280-
} else if (value is List) {
281-
return [for (var s in value) _decodeAnalyzerCode(s as String)];
282-
} else {
283-
throw 'Unrecognized analyzer code: $value';
284-
}
285-
}
286-
287252
static int? _decodeIndex(Object? value) {
288253
switch (value) {
289254
case null:
@@ -870,11 +835,11 @@ class FrontEndErrorCodeInfo extends CfeStyleErrorCodeInfo {
870835
// codes.
871836
final String? pseudoSharedCode;
872837

873-
FrontEndErrorCodeInfo.fromYaml(super.yaml)
838+
FrontEndErrorCodeInfo.fromYaml(YamlMap yaml)
874839
: pseudoSharedCode = yaml['pseudoSharedCode'] as String?,
875-
super.fromYaml() {
876-
if (analyzerCodes.isNotEmpty) {
877-
throw StateError('Only shared messages can have analyzer codes');
840+
super.fromYaml(yaml) {
841+
if (yaml['analyzerCode'] != null) {
842+
throw StateError('Only shared messages can have an analyzer code');
878843
}
879844
if (index != null) {
880845
throw StateError('Non-shared messages must not have an index');
@@ -1039,26 +1004,43 @@ class ParsedPlaceholder {
10391004
/// In-memory representation of error code information obtained from the file
10401005
/// `pkg/_fe_analyzer_shared/messages.yaml`.
10411006
class SharedErrorCodeInfo extends CfeStyleErrorCodeInfo {
1042-
SharedErrorCodeInfo.fromYaml(super.yaml) : super.fromYaml() {
1043-
if (analyzerCodes.length != 1) {
1044-
throw StateError('Shared messages must have exactly one analyzerCode');
1045-
}
1046-
if (super.index == null) {
1047-
throw StateError('Shared messages must have an index');
1048-
}
1049-
}
1050-
10511007
/// The analyzer error code that corresponds to this shared error code.
10521008
///
10531009
/// Shared error codes are required to have exactly one analyzer error code
10541010
/// associated with them.
1055-
AnalyzerCode get analyzerCode => analyzerCodes.single;
1011+
final AnalyzerCode analyzerCode;
1012+
1013+
SharedErrorCodeInfo.fromYaml(super.yaml)
1014+
: analyzerCode = _decodeAnalyzerCode(yaml['analyzerCode'] as String),
1015+
super.fromYaml() {
1016+
if (super.index == null) {
1017+
throw StateError('Shared messages must have an index');
1018+
}
1019+
}
10561020

10571021
/// The index of the error in the analyzer's `fastaAnalyzerErrorCodes` table.
10581022
///
10591023
/// Shared error codes are required to have a non-null index.
10601024
@override
10611025
int get index => super.index!;
1026+
1027+
static AnalyzerCode _decodeAnalyzerCode(String s) {
1028+
switch (s.split('.')) {
1029+
case [var errorName] when errorName == errorName.toUpperCase():
1030+
return AnalyzerCode(className: null, snakeCaseErrorName: errorName);
1031+
case [var className, var errorName]
1032+
when errorName == errorName.toUpperCase():
1033+
return AnalyzerCode(
1034+
className: className,
1035+
snakeCaseErrorName: errorName,
1036+
);
1037+
default:
1038+
throw StateError(
1039+
'Analyzer codes must take the form DIAGNOSTIC_NAME or '
1040+
'ClassName.DIAGNOSTIC_NAME. Found ${json.encode(s)} instead.',
1041+
);
1042+
}
1043+
}
10621044
}
10631045

10641046
/// Data tables mapping between shared errors and their corresponding

pkg/front_end/test/messages_suite.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class MessageTestSuite extends ChainContext {
198198
bool includeErrorContext = false;
199199
List<Example> examples = <Example>[];
200200
String? externalTest;
201-
List<String>? analyzerCodes;
201+
String? analyzerCode;
202202
CfeSeverity? severity;
203203
YamlNode? badSeverity;
204204
YamlNode? unnecessarySeverity;
@@ -325,9 +325,12 @@ class MessageTestSuite extends ChainContext {
325325
break;
326326

327327
case "analyzerCode":
328-
analyzerCodes = value is String
329-
? <String>[value]
330-
: new List<String>.from(value);
328+
if (value is! String) {
329+
throw new ArgumentError(
330+
'analyzerCode should be a string: $value',
331+
);
332+
}
333+
analyzerCode = value;
331334
if (!analyzerCodeRequired) {
332335
throw new ArgumentError(
333336
'analyzerCode not allowed in package ${json.encode(package)}',
@@ -706,7 +709,7 @@ class MessageTestSuite extends ChainContext {
706709
createDescription(
707710
"analyzerCode",
708711
null,
709-
analyzerCodeRequired && analyzerCodes == null
712+
analyzerCodeRequired && analyzerCode == null
710713
? (
711714
expectation: KnownExpectation.missingAnalyzerCode,
712715
message:

pkg/front_end/tool/generate_messages_lib.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ class _TemplateCompiler {
168168
final int? index;
169169
final String problemMessage;
170170
final String? correctionMessage;
171-
final List<AnalyzerCode> analyzerCodes;
172171
final String? severity;
173172
final Map<String, ErrorCodeParameter> parameters;
174173
final String? pseudoSharedCode;
@@ -199,7 +198,6 @@ class _TemplateCompiler {
199198
required this.pseudoSharedCodeValues,
200199
}) : problemMessage = errorCodeInfo.problemMessage,
201200
correctionMessage = errorCodeInfo.correctionMessage,
202-
analyzerCodes = errorCodeInfo.analyzerCodes,
203201
severity = errorCodeInfo.cfeSeverity,
204202
parameters = errorCodeInfo.parameters,
205203
pseudoSharedCode = errorCodeInfo is FrontEndErrorCodeInfo

0 commit comments

Comments
 (0)