Skip to content

Commit 918fb7c

Browse files
stereotype441Commit Queue
authored andcommitted
[front_end] Allow messages to have arbitrary parameter names.
Changes `ParsedPlaceholder.fromMatch` so that it no longer computes the parameter's type by looking it up in a hardcoded table. Instead, the parameter's type is instead obtained by looking it up by name in the `ErrorCodeInfo.parameters` map. This allows messages to have arbitrary parameter names. The hardcoded table, `_templateParameterNameToType`, is removed since it is no longer needed. This also enabled some clean-up of the template compiler. I've split it into three methods: - `compile`, which compiles the entire template. - `computeInterpolator`, which figures out the correct interpolation expression for a single parameter substitution. - `interpolate`, which converts either the `problemMessage` or the `correctionMessage` into an interpolation string. There is a small change to the generated code: in messages that use a `TypeLabeler`, the `TypeLabeler` is declared and initialized just before its first use, rather than at the top of the `withArguments` method. This change makes the generation of the `withArguments` method a little more straightforward, since it allows the body of `withArguments` to be computed via a single scan through all the placeholders. This change shouldn't have any user-visible effect. Change-Id: I6a6a69647a9af5f32c3102b78d54431e290e0591 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/448237 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 6f6c20d commit 918fb7c

File tree

4 files changed

+153
-190
lines changed

4 files changed

+153
-190
lines changed

pkg/analyzer_utilities/lib/messages.dart

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,6 @@ const Map<String, String> severityEnumNames = <String, String>{
2020
'INFO': 'info',
2121
};
2222

23-
/// Map assigning each possible template parameter a [ErrorCodeParameterType].
24-
///
25-
// TODO(paulberry): Change the format of `messages.yaml` so that the template
26-
// parameters, and their types, are stated explicitly, as they are in the
27-
// analyzer's `messages.yaml` file. Then this constant will not be needed.
28-
const _templateParameterNameToType = {
29-
'character': ErrorCodeParameterType.character,
30-
'unicode': ErrorCodeParameterType.unicode,
31-
'name': ErrorCodeParameterType.name,
32-
'name2': ErrorCodeParameterType.name,
33-
'name3': ErrorCodeParameterType.name,
34-
'name4': ErrorCodeParameterType.name,
35-
'nameOKEmpty': ErrorCodeParameterType.nameOKEmpty,
36-
'names': ErrorCodeParameterType.names,
37-
'lexeme': ErrorCodeParameterType.token,
38-
'lexeme2': ErrorCodeParameterType.token,
39-
'string': ErrorCodeParameterType.string,
40-
'string2': ErrorCodeParameterType.string,
41-
'string3': ErrorCodeParameterType.string,
42-
'stringOKEmpty': ErrorCodeParameterType.stringOKEmpty,
43-
'type': ErrorCodeParameterType.type,
44-
'type2': ErrorCodeParameterType.type,
45-
'type3': ErrorCodeParameterType.type,
46-
'type4': ErrorCodeParameterType.type,
47-
'uri': ErrorCodeParameterType.uri,
48-
'uri2': ErrorCodeParameterType.uri,
49-
'uri3': ErrorCodeParameterType.uri,
50-
'count': ErrorCodeParameterType.int,
51-
'count2': ErrorCodeParameterType.int,
52-
'count3': ErrorCodeParameterType.int,
53-
'count4': ErrorCodeParameterType.int,
54-
'constant': ErrorCodeParameterType.constant,
55-
'num1': ErrorCodeParameterType.num,
56-
'num2': ErrorCodeParameterType.num,
57-
'num3': ErrorCodeParameterType.num,
58-
};
59-
6023
/// Decoded messages from the front end's `messages.yaml` file.
6124
final Map<String, FrontEndErrorCodeInfo> frontEndMessages =
6225
_loadFrontEndMessages();
@@ -928,45 +891,32 @@ class ParsedPlaceholder {
928891
/// This is the identifier that immediately follows the `#`.
929892
final String name;
930893

931-
/// The type of the corresponding template parameter.
932-
final ErrorCodeParameterType templateParameterType;
933-
934-
/// The conversion that should be applied to the template parameter.
935-
final Conversion? conversion;
894+
/// The conversion specified in the placeholder, if any.
895+
///
896+
/// If `null`, the default conversion for the parameter's type will be used.
897+
final Conversion? conversionOverride;
936898

937899
/// Builds a [ParsedPlaceholder] from the given [match] of
938900
/// [placeholderPattern].
939901
factory ParsedPlaceholder.fromMatch(Match match) {
940902
String name = match[1]!;
941903

942-
var templateParameterType = _templateParameterNameToType[name];
943-
if (templateParameterType == null) {
944-
throw "Unhandled placeholder in template: '$name'";
945-
}
946-
947904
return ParsedPlaceholder._(
948905
name: name,
949-
templateParameterType: templateParameterType,
950-
conversion:
951-
NumericConversion.from(match) ?? templateParameterType.cfeConversion,
906+
conversionOverride: NumericConversion.from(match),
952907
);
953908
}
954909

955-
ParsedPlaceholder._({
956-
required this.name,
957-
required this.templateParameterType,
958-
required this.conversion,
959-
});
910+
ParsedPlaceholder._({required this.name, required this.conversionOverride});
960911

961912
@override
962-
int get hashCode => Object.hash(name, templateParameterType, conversion);
913+
int get hashCode => Object.hash(name, conversionOverride);
963914

964915
@override
965916
bool operator ==(Object other) =>
966917
other is ParsedPlaceholder &&
967918
other.name == name &&
968-
other.templateParameterType == templateParameterType &&
969-
other.conversion == conversion;
919+
other.conversionOverride == conversionOverride;
970920
}
971921

972922
/// A [Conversion] that invokes a top level function via the `conversions`

0 commit comments

Comments
 (0)