@@ -13,81 +13,14 @@ import 'package:analyzer/dart/element/element.dart';
13
13
import 'package:analyzer/dart/element/type.dart' ;
14
14
import 'package:analyzer/error/error.dart' ;
15
15
16
+ import '../lint_codes.dart' ;
17
+
16
18
const _desc =
17
19
'Do not expose implementation details through the analyzer public API.' ;
18
20
19
21
class AnalyzerPublicApi extends MultiAnalysisRule {
20
22
static const ruleName = 'analyzer_public_api' ;
21
23
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
-
91
24
AnalyzerPublicApi ()
92
25
: super (
93
26
name: ruleName,
@@ -97,10 +30,10 @@ class AnalyzerPublicApi extends MultiAnalysisRule {
97
30
98
31
@override
99
32
List <DiagnosticCode > get diagnosticCodes => [
100
- badPartDirective ,
101
- badType ,
102
- exportsNonPublicName ,
103
- implInPublicApi ,
33
+ LinterLintCode .analyzerPublicApiBadPartDirective ,
34
+ LinterLintCode .analyzerPublicApiBadType ,
35
+ LinterLintCode .analyzerPublicApiExportsNonPublicName ,
36
+ LinterLintCode .analyzerPublicApiImplInPublicApi ,
104
37
];
105
38
106
39
@override
@@ -170,7 +103,7 @@ class _Visitor extends SimpleAstVisitor<void> {
170
103
if (badNames != null ) {
171
104
rule.reportAtNode (
172
105
node,
173
- diagnosticCode: AnalyzerPublicApi .exportsNonPublicName ,
106
+ diagnosticCode: LinterLintCode .analyzerPublicApiExportsNonPublicName ,
174
107
arguments: [badNames.join (', ' )],
175
108
);
176
109
}
@@ -187,7 +120,7 @@ class _Visitor extends SimpleAstVisitor<void> {
187
120
if (! partElement.includedFragment! .source.uri.isInAnalyzerPublicLib) {
188
121
rule.reportAtNode (
189
122
node,
190
- diagnosticCode: AnalyzerPublicApi .badPartDirective ,
123
+ diagnosticCode: LinterLintCode .analyzerPublicApiBadPartDirective ,
191
124
);
192
125
}
193
126
}
@@ -217,7 +150,7 @@ class _Visitor extends SimpleAstVisitor<void> {
217
150
rule.reportAtOffset (
218
151
fragment.nameOffset! ,
219
152
name.length,
220
- diagnosticCode: AnalyzerPublicApi .implInPublicApi ,
153
+ diagnosticCode: LinterLintCode .analyzerPublicApiImplInPublicApi ,
221
154
);
222
155
}
223
156
switch (fragment) {
@@ -302,7 +235,7 @@ class _Visitor extends SimpleAstVisitor<void> {
302
235
rule.reportAtOffset (
303
236
offset,
304
237
length,
305
- diagnosticCode: AnalyzerPublicApi .badType ,
238
+ diagnosticCode: LinterLintCode .analyzerPublicApiBadType ,
306
239
arguments: [problems.join (', ' )],
307
240
);
308
241
}
0 commit comments