|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
5 | | -import 'package:analyzer/dart/ast/ast.dart' |
6 | | - show AstNode, ConstructorDeclaration; |
7 | | -import 'package:analyzer/dart/ast/syntactic_entity.dart'; |
8 | | -import 'package:analyzer/dart/ast/token.dart'; |
9 | | -import 'package:analyzer/dart/element/element.dart'; |
10 | | -import 'package:analyzer/dart/element/type.dart'; |
11 | 5 | import 'package:analyzer/diagnostic/diagnostic.dart'; |
12 | | -import 'package:analyzer/error/error.dart'; |
13 | 6 | import 'package:analyzer/source/source.dart'; |
14 | 7 | import 'package:analyzer/src/error/listener.dart'; |
15 | | -import 'package:analyzer/src/utilities/extensions/collection.dart'; |
16 | | -import 'package:meta/meta.dart'; |
17 | | -import 'package:source_span/source_span.dart'; |
| 8 | + |
| 9 | +export 'package:analyzer/src/error/listener.dart' show DiagnosticReporter; |
18 | 10 |
|
19 | 11 | @Deprecated("Use 'BooleanDiagnosticListener' instead") |
20 | 12 | typedef BooleanErrorListener = BooleanDiagnosticListener; |
@@ -69,248 +61,6 @@ abstract class DiagnosticListener implements DiagnosticOrErrorListener { |
69 | 61 |
|
70 | 62 | sealed class DiagnosticOrErrorListener {} |
71 | 63 |
|
72 | | -/// An object used to create diagnostics and report them to a diagnostic |
73 | | -/// listener. |
74 | | -class DiagnosticReporter { |
75 | | - /// The diagnostic listener to which diagnostics are reported. |
76 | | - final DiagnosticOrErrorListener _diagnosticListener; |
77 | | - |
78 | | - /// The source to be used when reporting diagnostics. |
79 | | - final Source _source; |
80 | | - |
81 | | - /// The lock level; if greater than zero, no diagnostic will be reported. |
82 | | - /// |
83 | | - /// This is used to prevent reporting diagnostics inside comments. |
84 | | - @internal |
85 | | - int lockLevel = 0; |
86 | | - |
87 | | - /// Initializes a newly created error reporter that will report diagnostics to the |
88 | | - /// given [_diagnosticListener]. |
89 | | - /// |
90 | | - /// Diagnostics are reported against the [_source] unless another source is |
91 | | - /// provided later. |
92 | | - DiagnosticReporter(this._diagnosticListener, this._source); |
93 | | - |
94 | | - Source get source => _source; |
95 | | - |
96 | | - /// Reports a diagnostic with the given [diagnosticCode] and [arguments]. |
97 | | - /// |
98 | | - /// The location of the diagnostic will be the name of the [node]. |
99 | | - /// |
100 | | - /// The reported [Diagnostic] is returned so that the caller may attach |
101 | | - /// additional information to it (for example, using an expando). |
102 | | - Diagnostic atConstructorDeclaration( |
103 | | - ConstructorDeclaration node, |
104 | | - DiagnosticCode diagnosticCode, { |
105 | | - List<Object>? arguments, |
106 | | - List<DiagnosticMessage>? contextMessages, |
107 | | - @Deprecated('Use an expando instead') Object? data, |
108 | | - }) { |
109 | | - // TODO(brianwilkerson): Consider extending this method to take any |
110 | | - // declaration and compute the correct range for the name of that |
111 | | - // declaration. This might make it easier to be consistent. |
112 | | - if (node.name case var nameToken?) { |
113 | | - var offset = node.returnType.offset; |
114 | | - return atOffset( |
115 | | - offset: offset, |
116 | | - length: nameToken.end - offset, |
117 | | - diagnosticCode: diagnosticCode, |
118 | | - arguments: arguments, |
119 | | - ); |
120 | | - } else { |
121 | | - return atNode(node.returnType, diagnosticCode, arguments: arguments); |
122 | | - } |
123 | | - } |
124 | | - |
125 | | - /// Reports a diagnostic with the given [diagnosticCode] and [arguments]. |
126 | | - /// |
127 | | - /// The [element] is used to compute the location of the diagnostic. |
128 | | - /// |
129 | | - /// The reported [Diagnostic] is returned so that the caller may attach |
130 | | - /// additional information to it (for example, using an expando). |
131 | | - @experimental |
132 | | - Diagnostic atElement2( |
133 | | - Element element, |
134 | | - DiagnosticCode diagnosticCode, { |
135 | | - List<Object>? arguments, |
136 | | - List<DiagnosticMessage>? contextMessages, |
137 | | - @Deprecated('Use an expando instead') Object? data, |
138 | | - }) { |
139 | | - var nonSynthetic = element.nonSynthetic; |
140 | | - return atOffset( |
141 | | - diagnosticCode: diagnosticCode, |
142 | | - offset: nonSynthetic.firstFragment.nameOffset ?? -1, |
143 | | - length: nonSynthetic.name?.length ?? 0, |
144 | | - arguments: arguments, |
145 | | - contextMessages: contextMessages, |
146 | | - // ignore: deprecated_member_use_from_same_package |
147 | | - data: data, |
148 | | - ); |
149 | | - } |
150 | | - |
151 | | - /// Reports a diagnostic with the given [diagnosticCode] and [arguments]. |
152 | | - /// |
153 | | - /// The [entity] is used to compute the location of the diagnostic. |
154 | | - /// |
155 | | - /// The reported [Diagnostic] is returned so that the caller may attach |
156 | | - /// additional information to it (for example, using an expando). |
157 | | - Diagnostic atEntity( |
158 | | - SyntacticEntity entity, |
159 | | - DiagnosticCode diagnosticCode, { |
160 | | - List<Object>? arguments, |
161 | | - List<DiagnosticMessage>? contextMessages, |
162 | | - @Deprecated('Use an expando instead') Object? data, |
163 | | - }) { |
164 | | - return atOffset( |
165 | | - diagnosticCode: diagnosticCode, |
166 | | - offset: entity.offset, |
167 | | - length: entity.length, |
168 | | - arguments: arguments, |
169 | | - contextMessages: contextMessages, |
170 | | - // ignore: deprecated_member_use_from_same_package |
171 | | - data: data, |
172 | | - ); |
173 | | - } |
174 | | - |
175 | | - /// Reports a diagnostic with the given [diagnosticCode] and [arguments]. |
176 | | - /// |
177 | | - /// The [node] is used to compute the location of the diagnostic. |
178 | | - /// |
179 | | - /// The reported [Diagnostic] is returned so that the caller may attach |
180 | | - /// additional information to it (for example, using an expando). |
181 | | - Diagnostic atNode( |
182 | | - AstNode node, |
183 | | - DiagnosticCode diagnosticCode, { |
184 | | - List<Object>? arguments, |
185 | | - List<DiagnosticMessage>? contextMessages, |
186 | | - @Deprecated('Use an expando instead') Object? data, |
187 | | - }) { |
188 | | - return atOffset( |
189 | | - diagnosticCode: diagnosticCode, |
190 | | - offset: node.offset, |
191 | | - length: node.length, |
192 | | - arguments: arguments, |
193 | | - contextMessages: contextMessages, |
194 | | - // ignore: deprecated_member_use_from_same_package |
195 | | - data: data, |
196 | | - ); |
197 | | - } |
198 | | - |
199 | | - /// Reports a diagnostic with the given [diagnosticCode] (or [errorCode], |
200 | | - /// deprecated) and [arguments]. |
201 | | - /// |
202 | | - /// The location of the diagnostic is specified by the given [offset] and |
203 | | - /// [length]. |
204 | | - /// |
205 | | - /// The reported [Diagnostic] is returned so that the caller may attach |
206 | | - /// additional information to it (for example, using an expando). |
207 | | - Diagnostic atOffset({ |
208 | | - required int offset, |
209 | | - required int length, |
210 | | - @Deprecated("Use 'diagnosticCode' instead") DiagnosticCode? errorCode, |
211 | | - DiagnosticCode? diagnosticCode, |
212 | | - List<Object>? arguments, |
213 | | - List<DiagnosticMessage>? contextMessages, |
214 | | - @Deprecated('Use an expando instead') Object? data, |
215 | | - }) { |
216 | | - if ((errorCode == null && diagnosticCode == null) || |
217 | | - (errorCode != null && diagnosticCode != null)) { |
218 | | - throw ArgumentError( |
219 | | - "Exactly one of 'errorCode' (deprecated) and 'diagnosticCode' should be given", |
220 | | - ); |
221 | | - } |
222 | | - |
223 | | - diagnosticCode ??= errorCode!; |
224 | | - |
225 | | - if (arguments != null) { |
226 | | - var invalid = |
227 | | - arguments |
228 | | - .whereNotType<String>() |
229 | | - .whereNotType<DartType>() |
230 | | - .whereNotType<Element>() |
231 | | - .whereNotType<int>() |
232 | | - .whereNotType<Uri>(); |
233 | | - if (invalid.isNotEmpty) { |
234 | | - throw ArgumentError( |
235 | | - 'Tried to format a diagnostic using ' |
236 | | - '${invalid.map((e) => e.runtimeType).join(', ')}', |
237 | | - ); |
238 | | - } |
239 | | - } |
240 | | - |
241 | | - contextMessages ??= []; |
242 | | - contextMessages.addAll(convertTypeNames(arguments)); |
243 | | - var diagnostic = Diagnostic.tmp( |
244 | | - source: _source, |
245 | | - offset: offset, |
246 | | - length: length, |
247 | | - diagnosticCode: diagnosticCode, |
248 | | - arguments: arguments ?? const [], |
249 | | - contextMessages: contextMessages, |
250 | | - // ignore: deprecated_member_use |
251 | | - data: data, |
252 | | - ); |
253 | | - reportError(diagnostic); |
254 | | - return diagnostic; |
255 | | - } |
256 | | - |
257 | | - /// Reports a diagnostic with the given [diagnosticCode] and [arguments]. |
258 | | - /// |
259 | | - /// The [span] is used to compute the location of the diagnostic. |
260 | | - /// |
261 | | - /// The reported [Diagnostic] is returned so that the caller may attach |
262 | | - /// additional information to it (for example, using an expando). |
263 | | - Diagnostic atSourceSpan( |
264 | | - SourceSpan span, |
265 | | - DiagnosticCode diagnosticCode, { |
266 | | - List<Object>? arguments, |
267 | | - List<DiagnosticMessage>? contextMessages, |
268 | | - @Deprecated('Use an expando instead') Object? data, |
269 | | - }) { |
270 | | - return atOffset( |
271 | | - diagnosticCode: diagnosticCode, |
272 | | - offset: span.start.offset, |
273 | | - length: span.length, |
274 | | - arguments: arguments, |
275 | | - contextMessages: contextMessages, |
276 | | - // ignore: deprecated_member_use_from_same_package |
277 | | - data: data, |
278 | | - ); |
279 | | - } |
280 | | - |
281 | | - /// Reports a diagnostic with the given [diagnosticCode] and [arguments]. |
282 | | - /// |
283 | | - /// The [token] is used to compute the location of the diagnostic. |
284 | | - /// |
285 | | - /// The reported [Diagnostic] is returned so that the caller may attach |
286 | | - /// additional information to it (for example, using an expando). |
287 | | - Diagnostic atToken( |
288 | | - Token token, |
289 | | - DiagnosticCode diagnosticCode, { |
290 | | - List<Object>? arguments, |
291 | | - List<DiagnosticMessage>? contextMessages, |
292 | | - @Deprecated('Use an expando instead') Object? data, |
293 | | - }) { |
294 | | - return atOffset( |
295 | | - diagnosticCode: diagnosticCode, |
296 | | - offset: token.offset, |
297 | | - length: token.length, |
298 | | - arguments: arguments, |
299 | | - contextMessages: contextMessages, |
300 | | - // ignore: deprecated_member_use_from_same_package |
301 | | - data: data, |
302 | | - ); |
303 | | - } |
304 | | - |
305 | | - /// Report the given [diagnostic]. |
306 | | - void reportError(Diagnostic diagnostic) { |
307 | | - if (lockLevel != 0) { |
308 | | - return; |
309 | | - } |
310 | | - _diagnosticListener.onDiagnostic(diagnostic); |
311 | | - } |
312 | | -} |
313 | | - |
314 | 64 | /// A diagnostic listener that records the diagnostics that are reported to it |
315 | 65 | /// in a way that is appropriate for caching those diagnostic within an |
316 | 66 | /// analysis context. |
|
0 commit comments