Skip to content

Commit 1bcb9fa

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer/linter] Prevent backsliding to the old messages.yaml format.
Error checking is added to: - `class ErrorCodeInfo`, to ensure that old-style place holders (`{N}`) are no longer used. - `class AnalyzerErrorCodeInfo`, to ensure that all error codes contain a `parameters` entry. (This check can't be in `ErrorCodeInfo`, because the CFE's `messages.yaml` doesn't contain `parameters` entries yet). This will prevent adding new error messages that use the old format. Change-Id: If37b912d4dfbfd74339c813673571092f4471bda Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/444364 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 07834e8 commit 1bcb9fa

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

pkg/analyzer/messages.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@
4545
# strings take the form `#NAME`.
4646
#
4747
# If a diagnostic takes no parameters, it should have an entry of the
48-
# form `parameters: none`. (If there is no `parameters:` entry, then
49-
# the error hasn't been translated from the old placeholder format
50-
# yet).
48+
# form `parameters: none`.
5149
#
5250
# At the time the analyzer reports the error, it supplies
5351
# substitutions to take the place of the `#NAME` placeholders in the

pkg/analyzer/tool/messages/error_code_info.dart

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ final String linterPkgPath = normalize(join(pkg_root.packageRoot, 'linter'));
146146
final Map<String, Map<String, AnalyzerErrorCodeInfo>> lintMessages =
147147
_loadLintMessages();
148148

149+
/// Pattern formerly used by the analyzer to identify placeholders in error
150+
/// message strings.
151+
///
152+
/// (This pattern is still used internally by the analyzer implementation, but
153+
/// it is no longer supported in `messages.yaml`.)
154+
final RegExp oldPlaceholderPattern = RegExp(r'\{\d+\}');
155+
149156
/// Pattern for placeholders in error message strings.
150157
// TODO(paulberry): share this regexp (and the code for interpreting
151158
// it) between the CFE and analyzer.
@@ -357,7 +364,9 @@ class AnalyzerErrorCodeInfo extends ErrorCodeInfo {
357364
required super.problemMessage,
358365
super.removedIn,
359366
super.sharedName,
360-
});
367+
}) {
368+
_check();
369+
}
361370

362371
factory AnalyzerErrorCodeInfo.fromYaml(Map<Object?, Object?> yaml) {
363372
if (yaml['aliasFor'] case var aliasFor?) {
@@ -367,7 +376,13 @@ class AnalyzerErrorCodeInfo extends ErrorCodeInfo {
367376
}
368377
}
369378

370-
AnalyzerErrorCodeInfo._fromYaml(super.yaml) : super.fromYaml();
379+
AnalyzerErrorCodeInfo._fromYaml(super.yaml) : super.fromYaml() {
380+
_check();
381+
}
382+
383+
void _check() {
384+
if (parameters == null) throw StateError('Missing `parameters` entry.');
385+
}
371386
}
372387

373388
/// Data tables mapping between CFE errors and their corresponding automatically
@@ -563,7 +578,21 @@ abstract class ErrorCodeInfo {
563578
this.previousName,
564579
this.removedIn,
565580
this.parameters,
566-
});
581+
}) {
582+
for (var MapEntry(:key, :value)
583+
in {
584+
'problemMessage': problemMessage,
585+
'correctionMessage': correctionMessage,
586+
}.entries) {
587+
if (value == null) continue;
588+
if (value.contains(oldPlaceholderPattern)) {
589+
throw StateError(
590+
'$key is ${json.encode(value)}, which contains an old-style analyzer '
591+
'placeholder pattern. Please convert to #NAME format.',
592+
);
593+
}
594+
}
595+
}
567596

568597
/// Decodes an [ErrorCodeInfo] object from its YAML representation.
569598
ErrorCodeInfo.fromYaml(Map<Object?, Object?> yaml)

pkg/analyzer/tool/messages/generate.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,10 @@ class _SyntacticErrorGenerator {
339339
// Build a map of error message to ParserErrorCode
340340
var messageToName = <String, String>{};
341341
for (var entry in analyzerMessages['ParserErrorCode']!.entries) {
342-
String message = entry.value.problemMessage
343-
.replaceAll(RegExp(r'\{\d+\}'), '')
344-
.replaceAll(placeholderPattern, '');
342+
String message = entry.value.problemMessage.replaceAll(
343+
placeholderPattern,
344+
'',
345+
);
345346
messageToName[message] = entry.key;
346347
}
347348

0 commit comments

Comments
 (0)