Skip to content

Commit 6445304

Browse files
stereotype441Commit Queue
authored andcommitted
[linter] Prepare to rename lint codes to camelCase.
In https://dart-review.googlesource.com/c/sdk/+/444921, which renames analyzer diagnostics from `SCREAMING_CAPS` to `camelCase`, I failed to notice that the lint codes use `lower_snake_case`, so they were not renamed. This change updates the `rename_error_constants.dart` script so that it will also rename diagnostics from `lower_snake_case` form. It also adds a `_useLowerCamelCaseNames` boolean to the linter's error code generator, so after the updated script is run, the generated code will match up with the renamed lint error codes. Change-Id: I6a6a6964d32a17a605947feb4dc07a0677b77191 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/445111 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 6d725e2 commit 6445304

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

pkg/analyzer/tool/messages/rename_error_constants.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
/// camelCase.
77
///
88
/// This script analyzes `pkg/analyzer` (and related packages), and renames
9-
/// every static const error code declaration from SCREAMING_CAPS format to
10-
/// camelCase format. It also flips the constant `_useLowerCamelCaseNames`
11-
/// (in `messages/generate.dart`) from `false` to `true`, so that error message
12-
/// code generation will start generating error codes in camelCase format.
9+
/// every static const error code declaration from SCREAMING_CAPS or
10+
/// lower_snake_case format to camelCase format. It also flips the constant
11+
/// `_useLowerCamelCaseNames` (in `messages/generate.dart` and
12+
/// `generate_lints.dart`) from `false` to `true`, so that error message code
13+
/// generation will start generating errorcodes in camelCase format.
1314
///
1415
/// This script will be run once to change the format of all the analyzer error
1516
/// codes, and then it will be removed from the codebase.
@@ -86,6 +87,8 @@ void main() async {
8687
}
8788
}
8889

90+
final _lowerSnakeCaseRegExp = RegExp(r'^[a-z0-9][A-Za-z0-9]*_[A-Za-z0-9_]+$');
91+
8992
final _screamingCapsRegExp = RegExp(r'^[A-Z0-9_]+$');
9093

9194
class _Elements {
@@ -104,8 +107,7 @@ class _Visitor extends RecursiveAstVisitor {
104107

105108
@override
106109
visitSimpleIdentifier(SimpleIdentifier node) {
107-
if (_isScreamingCaps(node.name) &&
108-
_isDiagnosticCodeConstant(node.element)) {
110+
if (_isSnakeCase(node.name) && _isDiagnosticCodeConstant(node.element)) {
109111
_edits.add(SourceEdit(node.offset, node.length, node.name.toCamelCase()));
110112
}
111113
return super.visitSimpleIdentifier(node);
@@ -114,11 +116,14 @@ class _Visitor extends RecursiveAstVisitor {
114116
@override
115117
visitVariableDeclaration(VariableDeclaration node) {
116118
if (node.name.lexeme == '_useLowerCamelCaseNames' &&
117-
path.basename(_file) == 'generate.dart') {
119+
const {
120+
'generate.dart',
121+
'generate_lints.dart',
122+
}.contains(path.basename(_file))) {
118123
_edits.add(
119124
SourceEdit(node.initializer!.offset, node.initializer!.length, 'true'),
120125
);
121-
} else if (_isScreamingCaps(node.name.lexeme) &&
126+
} else if (_isSnakeCase(node.name.lexeme) &&
122127
_isDiagnosticCodeConstant(node.declaredFragment?.element)) {
123128
_edits.add(
124129
SourceEdit(
@@ -148,5 +153,6 @@ class _Visitor extends RecursiveAstVisitor {
148153
);
149154
}
150155

151-
bool _isScreamingCaps(String s) => _screamingCapsRegExp.hasMatch(s);
156+
bool _isSnakeCase(String s) =>
157+
_screamingCapsRegExp.hasMatch(s) || _lowerSnakeCaseRegExp.hasMatch(s);
152158
}

pkg/linter/tool/generate_lints.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/// the entries in `pkg/linter/messages.yaml`.
77
library;
88

9+
import 'package:analyzer/src/utilities/extensions/string.dart';
910
import 'package:analyzer_testing/package_root.dart' as pkg_root;
1011
import 'package:analyzer_utilities/tools.dart';
1112

@@ -34,6 +35,14 @@ const linterLintCodeInfo = ErrorClassInfo(
3435
type: 'LINT',
3536
);
3637

38+
/// Whether the error constants that should be generated should use camel-case
39+
/// names.
40+
///
41+
/// This is a temporary flag to allow the codebase to be transitioned to using
42+
/// camel-case error constants. TODO(paulberry): once the transition is
43+
/// complete, remove this constant.
44+
const _useLowerCamelCaseNames = false;
45+
3746
GeneratedFile get generatedCodesFile =>
3847
GeneratedFile(generatedCodesPath, (pkgRoot) async {
3948
var out = StringBuffer('''
@@ -69,7 +78,9 @@ class LinterLintCode extends LintCode {
6978
if (codeInfo.deprecatedMessage case var deprecatedMessage?) {
7079
out.writeln(' @Deprecated("$deprecatedMessage")');
7180
}
72-
out.writeln(' static const LintCode $errorName =');
81+
var constantName =
82+
_useLowerCamelCaseNames ? errorName.toCamelCase() : errorName;
83+
out.writeln(' static const LintCode $constantName =');
7384
out.writeln(
7485
codeInfo.toAnalyzerCode(
7586
linterLintCodeInfo,
@@ -81,11 +92,13 @@ class LinterLintCode extends LintCode {
8192
out.writeln();
8293
}
8394

95+
var removedLintName =
96+
_useLowerCamelCaseNames ? 'removedLint' : 'removed_lint';
8497
out.writeln('''
8598
/// A lint code that removed lints can specify as their `lintCode`.
8699
///
87100
/// Avoid other usages as it should be made unnecessary and removed.
88-
static const LintCode removed_lint = LinterLintCode(
101+
static const LintCode $removedLintName = LinterLintCode(
89102
'removed_lint',
90103
'Removed lint.',
91104
);

0 commit comments

Comments
 (0)