Skip to content

Commit aa4d235

Browse files
stereotype441Commit Queue
authored andcommitted
[linter] Move some stray LintCode constants to code generation.
Moving these diagnostic constants to code generation will make it easier to make further improvements to the analyzer diagnostic representation, because I'll be able to make a single adjustment to the code generator and then re-generate all the diagnostics. Change-Id: I6a6a6964228cb6df26798411f16524d15c6f8098 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/445283 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 912090b commit aa4d235

File tree

8 files changed

+267
-111
lines changed

8 files changed

+267
-111
lines changed

pkg/linter/lib/src/lint_codes.g.dart

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,85 @@ class LinterLintCode extends LintCode {
103103
hasPublishedDocs: true,
104104
);
105105

106+
/// Lint issued if a file in the analyzer public API contains a `part`
107+
/// directive that points to a file that's not in the analyzer public API.
108+
///
109+
/// The rationale for this lint is that if such a `part` directive were to
110+
/// exist, it would cause all the members of the part file to become part of
111+
/// the analyzer's public API, even though they don't appear to be public API.
112+
///
113+
/// Note that the analyzer doesn't make very much use of `part` directives,
114+
/// but it may do so in the future once augmentations and enhanced parts are
115+
/// supported.
116+
///
117+
/// No parameters.
118+
static const LintCode analyzerPublicApiBadPartDirective = LinterLintCode(
119+
LintNames.analyzer_public_api_bad_part_directive,
120+
"Part directives in the analyzer public API should point to files in the "
121+
"analyzer public API.",
122+
);
123+
124+
/// Lint issued if a method, function, getter, or setter in the analyzer
125+
/// public API makes use of a type that's not part of the analyzer public API,
126+
/// or if a non-public type appears in an `extends`, `implements`, `with`, or
127+
/// `on` clause.
128+
///
129+
/// The reason this is a problem is that it makes it possible for analyzer
130+
/// clients to implicitly reference analyzer internal types. This can happen
131+
/// in many ways; here are some examples:
132+
///
133+
/// - If `C` is a public API class that implements `B`, and `B` is a private
134+
/// class with a getter called `x`, then a client can access `B.x` via `C`.
135+
///
136+
/// - If `f` has return type `T`, and `T` is a private class with a getter
137+
/// called `x`, then a client can access `T.x` via `f().x`.
138+
///
139+
/// - If `f` has type `void Function(T)`, and `T` is a private class with a
140+
/// getter called `x`, then a client can access `T.x` via
141+
/// `var g = f; g = (t) { print(t.x); }`.
142+
///
143+
/// This lint can be suppressed either with an `ignore` comment, or by marking
144+
/// the referenced type with `@AnalyzerPublicApi(...)`. The advantage of
145+
/// marking the referenced type with `@AnalyzerPublicApi(...)` is that it
146+
/// causes the members of referenced type to be checked by this lint.
147+
///
148+
/// Parameters:
149+
/// String types: list of types, separated by `, `
150+
static const LintCode analyzerPublicApiBadType = LinterLintCode(
151+
LintNames.analyzer_public_api_bad_type,
152+
"Element makes use of type(s) which is not part of the analyzer public "
153+
"API: {0}.",
154+
);
155+
156+
/// Lint issued if a file in the analyzer public API contains an `export`
157+
/// directive that exports a name that's not part of the analyzer public API.
158+
///
159+
/// This lint can be suppressed either with an `ignore` comment, or by marking
160+
/// the exported declaration with `@AnalyzerPublicApi(...)`. The advantage of
161+
/// marking the exported declaration with `@AnalyzerPublicApi(...)` is that it
162+
/// causes the members of the exported declaration to be checked by this lint.
163+
///
164+
/// Parameters:
165+
/// String elements: List of elements, separated by `, `
166+
static const LintCode analyzerPublicApiExportsNonPublicName = LinterLintCode(
167+
LintNames.analyzer_public_api_exports_non_public_name,
168+
"Export directive exports element(s) that are not part of the analyzer "
169+
"public API: {0}.",
170+
);
171+
172+
/// Lint issued if a top level declaration in the analyzer public API has a
173+
/// name ending in `Impl`.
174+
///
175+
/// Such declarations are not meant to be members of the analyzer public API,
176+
/// so if they are either declared outside of `package:analyzer/src`, or
177+
/// marked with `@AnalyzerPublicApi(...)`, that is almost certainly a mistake.
178+
///
179+
/// No parameters.
180+
static const LintCode analyzerPublicApiImplInPublicApi = LinterLintCode(
181+
LintNames.analyzer_public_api_impl_in_public_api,
182+
"Declarations in the analyzer public API should not end in \"Impl\".",
183+
);
184+
106185
/// Parameters:
107186
/// Object p0: undocumented
108187
static const LintCode annotateOverrides = LinterLintCode(
@@ -1129,6 +1208,23 @@ class LinterLintCode extends LintCode {
11291208
correctionMessage: "Try removing the assignment that has no direct effect.",
11301209
);
11311210

1211+
/// No parameters.
1212+
static const LintCode noSoloTests = LinterLintCode(
1213+
LintNames.no_solo_tests,
1214+
"Don't commit soloed tests.",
1215+
correctionMessage:
1216+
"Try removing the 'soloTest' annotation or 'solo_' prefix.",
1217+
hasPublishedDocs: true,
1218+
);
1219+
1220+
/// No parameters.
1221+
static const LintCode noTrailingSpaces = LinterLintCode(
1222+
LintNames.no_trailing_spaces,
1223+
"Don't create string literals with trailing spaces in tests.",
1224+
correctionMessage: "Try removing the trailing spaces.",
1225+
hasPublishedDocs: true,
1226+
);
1227+
11321228
/// No parameters.
11331229
static const LintCode noWildcardVariableUses = LinterLintCode(
11341230
LintNames.no_wildcard_variable_uses,
@@ -2412,6 +2508,15 @@ class LinterLintCode extends LintCode {
24122508
hasPublishedDocs: true,
24132509
);
24142510

2511+
/// No parameters.
2512+
static const LintCode visitRegisteredNodes = LinterLintCode(
2513+
LintNames.visit_registered_nodes,
2514+
"Declare 'visit' methods for all registered node types.",
2515+
correctionMessage:
2516+
"Try declaring a 'visit' method for all registered node types.",
2517+
hasPublishedDocs: true,
2518+
);
2519+
24152520
/// No parameters.
24162521
static const LintCode voidChecks = LinterLintCode(
24172522
LintNames.void_checks,

pkg/linter/lib/src/lint_names.g.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ abstract final class LintNames {
2929

3030
static const String always_use_package_imports = 'always_use_package_imports';
3131

32+
static const String analyzer_public_api_bad_part_directive =
33+
'analyzer_public_api_bad_part_directive';
34+
35+
static const String analyzer_public_api_bad_type =
36+
'analyzer_public_api_bad_type';
37+
38+
static const String analyzer_public_api_exports_non_public_name =
39+
'analyzer_public_api_exports_non_public_name';
40+
41+
static const String analyzer_public_api_impl_in_public_api =
42+
'analyzer_public_api_impl_in_public_api';
43+
3244
static const String annotate_overrides = 'annotate_overrides';
3345

3446
static const String annotate_redeclares = 'annotate_redeclares';
@@ -290,6 +302,10 @@ abstract final class LintNames {
290302

291303
static const String no_self_assignments = 'no_self_assignments';
292304

305+
static const String no_solo_tests = 'no_solo_tests';
306+
307+
static const String no_trailing_spaces = 'no_trailing_spaces';
308+
293309
static const String no_wildcard_variable_uses = 'no_wildcard_variable_uses';
294310

295311
static const String non_constant_identifier_names =
@@ -614,5 +630,7 @@ abstract final class LintNames {
614630

615631
static const String valid_regexps = 'valid_regexps';
616632

633+
static const String visit_registered_nodes = 'visit_registered_nodes';
634+
617635
static const String void_checks = 'void_checks';
618636
}

pkg/linter/lib/src/rules/analyzer_public_api.dart

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,81 +13,14 @@ import 'package:analyzer/dart/element/element.dart';
1313
import 'package:analyzer/dart/element/type.dart';
1414
import 'package:analyzer/error/error.dart';
1515

16+
import '../lint_codes.dart';
17+
1618
const _desc =
1719
'Do not expose implementation details through the analyzer public API.';
1820

1921
class AnalyzerPublicApi extends MultiAnalysisRule {
2022
static const ruleName = 'analyzer_public_api';
2123

22-
/// Lint issued if a file in the analyzer public API contains a `part`
23-
/// directive that points to a file that's not in the analyzer public API.
24-
///
25-
/// The rationale for this lint is that if such a `part` directive were to
26-
/// exist, it would cause all the members of the part file to become part of
27-
/// the analyzer's public API, even though they don't appear to be public API.
28-
///
29-
/// Note that the analyzer doesn't make very much use of `part` directives,
30-
/// but it may do so in the future once augmentations and enhanced parts are
31-
/// supported.
32-
static const LintCode badPartDirective = LintCode(
33-
'analyzer_public_api_bad_part_directive',
34-
'Part directives in the analyzer public API should point to files in the '
35-
'analyzer public API.',
36-
);
37-
38-
/// Lint issued if a method, function, getter, or setter in the analyzer
39-
/// public API makes use of a type that's not part of the analyzer public API,
40-
/// or if a non-public type appears in an `extends`, `implements`, `with`, or
41-
/// `on` clause.
42-
///
43-
/// The reason this is a problem is that it makes it possible for analyzer
44-
/// clients to implicitly reference analyzer internal types. This can happen
45-
/// in many ways; here are some examples:
46-
///
47-
/// - If `C` is a public API class that implements `B`, and `B` is a private
48-
/// class with a getter called `x`, then a client can access `B.x` via `C`.
49-
///
50-
/// - If `f` has return type `T`, and `T` is a private class with a getter
51-
/// called `x`, then a client can access `T.x` via `f().x`.
52-
///
53-
/// - If `f` has type `void Function(T)`, and `T` is a private class with a
54-
/// getter called `x`, then a client can access `T.x` via
55-
/// `var g = f; g = (t) { print(t.x); }`.
56-
///
57-
/// This lint can be suppressed either with an `ignore` comment, or by marking
58-
/// the referenced type with `@AnalyzerPublicApi(...)`. The advantage of
59-
/// marking the referenced type with `@AnalyzerPublicApi(...)` is that it
60-
/// causes the members of referenced type to be checked by this lint.
61-
static const LintCode badType = LintCode(
62-
'analyzer_public_api_bad_type',
63-
'Element makes use of type(s) which is not part of the analyzer public '
64-
'API: {0}.',
65-
);
66-
67-
/// Lint issued if a file in the analyzer public API contains an `export`
68-
/// directive that exports a name that's not part of the analyzer public API.
69-
///
70-
/// This lint can be suppressed either with an `ignore` comment, or by marking
71-
/// the exported declaration with `@AnalyzerPublicApi(...)`. The advantage of
72-
/// marking the exported declaration with `@AnalyzerPublicApi(...)` is that it
73-
/// causes the members of the exported declaration to be checked by this lint.
74-
static const LintCode exportsNonPublicName = LintCode(
75-
'analyzer_public_api_exports_non_public_name',
76-
'Export directive exports element(s) that are not part of the analyzer '
77-
'public API: {0}.',
78-
);
79-
80-
/// Lint issued if a top level declaration in the analyzer public API has a
81-
/// name ending in `Impl`.
82-
///
83-
/// Such declarations are not meant to be members of the analyzer public API,
84-
/// so if they are either declared outside of `package:analyzer/src`, or
85-
/// marked with `@AnalyzerPublicApi(...)`, that is almost certainly a mistake.
86-
static const LintCode implInPublicApi = LintCode(
87-
'analyzer_public_api_impl_in_public_api',
88-
'Declarations in the analyzer public API should not end in "Impl".',
89-
);
90-
9124
AnalyzerPublicApi()
9225
: super(
9326
name: ruleName,
@@ -97,10 +30,10 @@ class AnalyzerPublicApi extends MultiAnalysisRule {
9730

9831
@override
9932
List<DiagnosticCode> get diagnosticCodes => [
100-
badPartDirective,
101-
badType,
102-
exportsNonPublicName,
103-
implInPublicApi,
33+
LinterLintCode.analyzerPublicApiBadPartDirective,
34+
LinterLintCode.analyzerPublicApiBadType,
35+
LinterLintCode.analyzerPublicApiExportsNonPublicName,
36+
LinterLintCode.analyzerPublicApiImplInPublicApi,
10437
];
10538

10639
@override
@@ -170,7 +103,7 @@ class _Visitor extends SimpleAstVisitor<void> {
170103
if (badNames != null) {
171104
rule.reportAtNode(
172105
node,
173-
diagnosticCode: AnalyzerPublicApi.exportsNonPublicName,
106+
diagnosticCode: LinterLintCode.analyzerPublicApiExportsNonPublicName,
174107
arguments: [badNames.join(', ')],
175108
);
176109
}
@@ -187,7 +120,7 @@ class _Visitor extends SimpleAstVisitor<void> {
187120
if (!partElement.includedFragment!.source.uri.isInAnalyzerPublicLib) {
188121
rule.reportAtNode(
189122
node,
190-
diagnosticCode: AnalyzerPublicApi.badPartDirective,
123+
diagnosticCode: LinterLintCode.analyzerPublicApiBadPartDirective,
191124
);
192125
}
193126
}
@@ -217,7 +150,7 @@ class _Visitor extends SimpleAstVisitor<void> {
217150
rule.reportAtOffset(
218151
fragment.nameOffset!,
219152
name.length,
220-
diagnosticCode: AnalyzerPublicApi.implInPublicApi,
153+
diagnosticCode: LinterLintCode.analyzerPublicApiImplInPublicApi,
221154
);
222155
}
223156
switch (fragment) {
@@ -302,7 +235,7 @@ class _Visitor extends SimpleAstVisitor<void> {
302235
rule.reportAtOffset(
303236
offset,
304237
length,
305-
diagnosticCode: AnalyzerPublicApi.badType,
238+
diagnosticCode: LinterLintCode.analyzerPublicApiBadType,
306239
arguments: [problems.join(', ')],
307240
);
308241
}

0 commit comments

Comments
 (0)