Skip to content

Commit 3d215a3

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Add some more diagnostic code generation features.
The following features are added: - The ability to exclude a generated error class from the generated `diagnosticCodeValues` list. - The ability for the code generator to add a documentation comment at the top of the generated error class. These features are not used yet. In follow-up CLs, I will be switching some error classes over to code generation, and that will require these features. Change-Id: I6a6a696408ac8498ef6cd9a053b3a82005378789 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/445280 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent c1caa5c commit 3d215a3

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

pkg/analyzer/tool/messages/error_code_info.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,15 @@ class ErrorClassInfo {
507507
/// notice) after migration to camel case error codes.
508508
final Set<String> deprecatedSnakeCaseNames;
509509

510+
/// If `true` (the default), error codes of this class will be included in the
511+
/// automatically-generated `diagnosticCodeValues` list.
512+
final bool includeInDiagnosticCodeValues;
513+
514+
/// Documentation comment to generate for the error class.
515+
///
516+
/// If no documentation comment is needed, this should be the empty string.
517+
final String comment;
518+
510519
const ErrorClassInfo({
511520
this.extraImports = const [],
512521
required this.file,
@@ -516,6 +525,8 @@ class ErrorClassInfo {
516525
this.superclass = 'DiagnosticCode',
517526
required this.type,
518527
this.deprecatedSnakeCaseNames = const {},
528+
this.includeInDiagnosticCodeValues = true,
529+
this.comment = '',
519530
});
520531

521532
/// Generates the code to compute the severity of errors of this class.
@@ -868,9 +879,12 @@ class GeneratedErrorCodeFile {
868879
/// constructor invocations.
869880
final bool shouldUseExplicitConst;
870881

882+
final bool shouldIgnorePreferSingleQuotes;
883+
871884
const GeneratedErrorCodeFile({
872885
required this.path,
873886
required this.preferredImportUri,
874887
this.shouldUseExplicitConst = false,
888+
this.shouldIgnorePreferSingleQuotes = false,
875889
});
876890
}

pkg/analyzer/tool/messages/generate.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,20 @@ class _AnalyzerErrorGenerator {
8787
8888
// While transitioning `HintCodes` to `WarningCodes`, we refer to deprecated
8989
// codes here.
90-
// ignore_for_file: deprecated_member_use_from_same_package
91-
//
92-
// Generated comments don't quite align with flutter style.
93-
// ignore_for_file: flutter_style_todos
9490
''');
9591

9692
_AnalyzerErrorGenerator(this.file, this.errorClasses, this.generatedCodes);
9793

9894
void generate() {
95+
out.writeln('// ignore_for_file: deprecated_member_use_from_same_package');
96+
if (file.shouldIgnorePreferSingleQuotes) {
97+
out.writeln('// ignore_for_file: prefer_single_quotes');
98+
}
99+
out.write('''
100+
//
101+
// Generated comments don't quite align with flutter style.
102+
// ignore_for_file: flutter_style_todos
103+
''');
99104
out.writeln();
100105
out.write('''
101106
/// @docImport 'package:analyzer/src/dart/error/syntactic_errors.g.dart';
@@ -132,6 +137,11 @@ library;
132137
for (var errorClass
133138
in errorClasses.toList()..sort((a, b) => a.name.compareTo(b.name))) {
134139
out.writeln();
140+
if (errorClass.comment.isNotEmpty) {
141+
errorClass.comment.trimRight().split('\n').forEach((line) {
142+
out.writeln('/// $line');
143+
});
144+
}
135145
out.write('class ${errorClass.name} extends ${errorClass.superclass} {');
136146
var entries = [
137147
...analyzerMessages[errorClass.name]!.entries,
@@ -155,7 +165,9 @@ library;
155165
);
156166
out.writeln('${errorCodeInfo.aliasFor};');
157167
} else {
158-
generatedCodes.add((errorClass.name, errorName));
168+
if (errorClass.includeInDiagnosticCodeValues) {
169+
generatedCodes.add((errorClass.name, errorName));
170+
}
159171
var constantName = errorName.toCamelCase();
160172
out.writeln(' static const ${errorClass.name} $constantName =');
161173
out.writeln(

0 commit comments

Comments
 (0)