Skip to content

Commit 6f6c20d

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Make ErrorCodeInfo.parameters a map.
Changes `ErrorCodeInfo.parameters` from a list of `ErrorCodeParameter` to a map from parameter name to `ErrorCodeParameter`. The `name` field is removed from `ErrorCodeParameter` to avoid redundancy with the map key. This change will allow the CFE diagnostic message generation code to easily look up error code parameters by their name. Change-Id: I6a6a6964297684b27faade4d1014b6355e59ae5b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/448253 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 4a0a36e commit 6f6c20d

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

pkg/analyzer_utilities/lib/messages.dart

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,11 @@ abstract class ErrorCodeInfo {
297297
/// [previousName] to its current name (or [sharedName]).
298298
final String? previousName;
299299

300-
/// A list of [ErrorCodeParameter] objects describing the parameters for this
301-
/// error code, obtained from the `parameters` entry in the yaml file.
302-
final List<ErrorCodeParameter> parameters;
300+
/// Map describing the parameters for this error code, obtained from the
301+
/// `parameters` entry in the yaml file.
302+
///
303+
/// Map keys are parameter names. Map values are [ErrorCodeParameter] objects.
304+
final Map<String, ErrorCodeParameter> parameters;
303305

304306
/// The raw YAML node that this `ErrorCodeInfo` was parsed from, or `null` if
305307
/// this `ErrorCodeInfo` was created without reference to a raw YAML node.
@@ -363,10 +365,7 @@ abstract class ErrorCodeInfo {
363365
Map<String, int> computePlaceholderToIndexMap() {
364366
// Parameters are always explicitly specified, so the mapping is determined
365367
// by the order in which they were specified.
366-
return {
367-
for (var (index, parameter) in parameters.indexed)
368-
'#${parameter.name}': index,
369-
};
368+
return {for (var (index, name) in parameters.keys.indexed) '#$name': index};
370369
}
371370

372371
void outputConstantHeader(StringSink out) {
@@ -401,17 +400,17 @@ abstract class ErrorCodeInfo {
401400
'Error code declares parameters using a `parameters` entry, but '
402401
"doesn't use them",
403402
);
404-
} else if (parameters.any((p) => !p.type.isSupportedByAnalyzer)) {
403+
} else if (parameters.values.any((p) => !p.type.isSupportedByAnalyzer)) {
405404
// Do not generate literate API yet.
406405
className = errorClassInfo.name;
407406
} else if (parameters.isNotEmpty) {
408407
// Parameters are present so generate a diagnostic template (with
409408
// `.withArguments` support).
410409
className = errorClassInfo.templateName;
411-
var withArgumentsParams = parameters
412-
.map((p) => 'required ${p.type.analyzerName} ${p.name}')
410+
var withArgumentsParams = parameters.entries
411+
.map((p) => 'required ${p.value.type.analyzerName} ${p.key}')
413412
.join(', ');
414-
var argumentNames = parameters.map((p) => p.name).join(', ');
413+
var argumentNames = parameters.keys.join(', ');
415414
var pascalCaseName = diagnosticCode.toPascalCase();
416415
withArgumentsName = '_withArguments$pascalCaseName';
417416
templateParameters =
@@ -492,14 +491,14 @@ static LocatableDiagnostic $withArgumentsName({$withArgumentsParams}) {
492491

493492
// Add a `Parameters:` section to the bottom of the comment if appropriate.
494493
switch (parameters) {
495-
case []:
494+
case Map(isEmpty: true):
496495
if (commentLines.isNotEmpty) commentLines.add('');
497496
commentLines.add('No parameters.');
498497
default:
499498
if (commentLines.isNotEmpty) commentLines.add('');
500499
commentLines.add('Parameters:');
501-
for (var p in parameters) {
502-
var prefix = '${p.type.messagesYamlName} ${p.name}: ';
500+
for (var MapEntry(key: name, value: p) in parameters.entries) {
501+
var prefix = '${p.type.messagesYamlName} $name: ';
503502
var extraIndent = ' ' * prefix.length;
504503
var firstLineWidth = 80 - 4 - indent.length;
505504
var lines = _splitText(
@@ -535,7 +534,8 @@ static LocatableDiagnostic $withArgumentsName({$withArgumentsParams}) {
535534

536535
String _computeExpectedTypes() {
537536
var expectedTypes = [
538-
for (var parameter in parameters) 'ExpectedType.${parameter.type.name}',
537+
for (var parameter in parameters.values)
538+
'ExpectedType.${parameter.type.name}',
539539
];
540540
return '[${expectedTypes.join(', ')}]';
541541
}
@@ -561,22 +561,22 @@ static LocatableDiagnostic $withArgumentsName({$withArgumentsParams}) {
561561
}
562562
}
563563

564-
static List<ErrorCodeParameter> _decodeParameters(Object? yaml) {
564+
static Map<String, ErrorCodeParameter> _decodeParameters(Object? yaml) {
565565
if (yaml == null) {
566566
throw StateError('Missing parameters section');
567567
}
568-
if (yaml == 'none') return const [];
568+
if (yaml == 'none') return const {};
569569
yaml as Map<Object?, Object?>;
570-
var result = <ErrorCodeParameter>[];
570+
var result = <String, ErrorCodeParameter>{};
571571
for (var MapEntry(:key, :value) in yaml.entries) {
572572
switch ((key as String).split(' ')) {
573573
case [var type, var name]:
574-
result.add(
575-
ErrorCodeParameter(
576-
type: ErrorCodeParameterType.fromMessagesYamlName(type),
577-
name: name,
578-
comment: value as String,
579-
),
574+
if (result.containsKey(name)) {
575+
throw StateError('Duplicate parameter name: $name');
576+
}
577+
result[name] = ErrorCodeParameter(
578+
type: ErrorCodeParameterType.fromMessagesYamlName(type),
579+
comment: value as String,
580580
);
581581
default:
582582
throw StateError(
@@ -591,16 +591,14 @@ static LocatableDiagnostic $withArgumentsName({$withArgumentsParams}) {
591591

592592
/// In-memory representation of a single key/value pair from the `parameters`
593593
/// map for an error code.
594+
///
595+
/// The name of the parameter is not included, since parameters are stored in a
596+
/// map from name to [ErrorCodeParameter].
594597
class ErrorCodeParameter {
595598
final ErrorCodeParameterType type;
596-
final String name;
597599
final String comment;
598600

599-
ErrorCodeParameter({
600-
required this.type,
601-
required this.name,
602-
required this.comment,
603-
});
601+
ErrorCodeParameter({required this.type, required this.comment});
604602
}
605603

606604
/// In-memory representation of the type of a single diagnostic code's

0 commit comments

Comments
 (0)