Skip to content

Commit 8aa021f

Browse files
srawlinsCommit Queue
authored andcommitted
linter: Use Diagnostic instead of deprecated AnalysisError
Work towards #60635 Change-Id: I225bcbe2c0033872b0349258f7f2135aab6a8d20 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426561 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 81a7f3e commit 8aa021f

File tree

8 files changed

+71
-67
lines changed

8 files changed

+71
-67
lines changed

pkg/linter/lib/src/test_utilities/analysis_error_info.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/error/error.dart';
5+
import 'package:analyzer/diagnostic/diagnostic.dart';
66
import 'package:analyzer/source/line_info.dart';
77

8-
/// The analysis errors and line info associated with a source.
9-
class AnalysisErrorInfo {
8+
/// The analysis diagnostics and line info associated with a source.
9+
class DiagnosticInfo {
1010
/// The analysis errors associated with a source, or `null` if there are no
1111
/// errors.
12-
final List<AnalysisError> errors;
12+
final List<Diagnostic> diagnostics;
1313

1414
/// The line information associated with the errors, or `null` if there are no
1515
/// errors.
1616
final LineInfo lineInfo;
1717

18-
AnalysisErrorInfo(this.errors, this.lineInfo);
18+
DiagnosticInfo(this.diagnostics, this.lineInfo);
1919
}

pkg/linter/lib/src/test_utilities/lint_driver.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class LintDriver {
2828

2929
LintDriver(this._options, this._resourceProvider);
3030

31-
Future<List<AnalysisErrorInfo>> analyze(Iterable<io.File> files) async {
31+
Future<List<DiagnosticInfo>> analyze(Iterable<io.File> files) async {
3232
AnalysisEngine.instance.instrumentationService = _StdInstrumentation();
3333

3434
var filesPaths =
@@ -50,14 +50,12 @@ class LintDriver {
5050

5151
_filesAnalyzed.addAll(filesPaths);
5252

53-
var result = <AnalysisErrorInfo>[];
53+
var result = <DiagnosticInfo>[];
5454
for (var path in _filesAnalyzed) {
5555
var analysisSession = contextCollection.contextFor(path).currentSession;
5656
var errorsResult = await analysisSession.getErrors(path);
5757
if (errorsResult is ErrorsResult) {
58-
result.add(
59-
AnalysisErrorInfo(errorsResult.errors, errorsResult.lineInfo),
60-
);
58+
result.add(DiagnosticInfo(errorsResult.errors, errorsResult.lineInfo));
6159
}
6260
}
6361
return result;

pkg/linter/lib/src/test_utilities/test_linter.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'dart:io';
66

7-
import 'package:analyzer/error/error.dart';
7+
import 'package:analyzer/diagnostic/diagnostic.dart';
88
import 'package:analyzer/error/listener.dart';
99
import 'package:analyzer/file_system/file_system.dart' as file_system;
1010
import 'package:analyzer/file_system/physical_file_system.dart' as file_system;
@@ -29,7 +29,7 @@ Source _createSource(Uri uri) {
2929

3030
/// Dart source linter, only for package:linter's tools and tests.
3131
class TestLinter implements AnalysisErrorListener {
32-
final errors = <AnalysisError>[];
32+
final errors = <Diagnostic>[];
3333

3434
final LinterOptions options;
3535
final file_system.ResourceProvider _resourceProvider;
@@ -38,8 +38,8 @@ class TestLinter implements AnalysisErrorListener {
3838
: _resourceProvider =
3939
resourceProvider ?? file_system.PhysicalResourceProvider.INSTANCE;
4040

41-
Future<List<AnalysisErrorInfo>> lintFiles(List<File> files) async {
42-
var errors = <AnalysisErrorInfo>[];
41+
Future<List<DiagnosticInfo>> lintFiles(List<File> files) async {
42+
var errors = <DiagnosticInfo>[];
4343
var lintDriver = LintDriver(options, _resourceProvider);
4444
errors.addAll(await lintDriver.analyze(files.where(isDartFile)));
4545
for (var file in files.where(isPubspecFile)) {
@@ -76,5 +76,5 @@ class TestLinter implements AnalysisErrorListener {
7676
}
7777

7878
@override
79-
void onError(AnalysisError error) => errors.add(error);
79+
void onError(Diagnostic error) => errors.add(error);
8080
}

pkg/linter/test/formatter_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/error/error.dart';
5+
import 'package:analyzer/diagnostic/diagnostic.dart';
66
import 'package:linter/src/analyzer.dart';
77
import 'package:linter/src/test_utilities/analysis_error_info.dart';
88
import 'package:test/test.dart';
@@ -24,7 +24,7 @@ void defineTests() {
2424
});
2525

2626
group('reporter', () {
27-
late AnalysisErrorInfo info;
27+
late DiagnosticInfo info;
2828
late StringBuffer out;
2929
late String sourcePath;
3030
late ReportFormatter reporter;
@@ -46,14 +46,14 @@ var z = 33;
4646
sourcePath = '${d.sandbox}/project/foo.dart';
4747
var source = MockSource(sourcePath);
4848

49-
var error = AnalysisError.tmp(
49+
var error = Diagnostic.tmp(
5050
source: source,
5151
offset: 10,
5252
length: 3,
5353
errorCode: code,
5454
);
5555

56-
info = AnalysisErrorInfo([error], lineInfo);
56+
info = DiagnosticInfo([error], lineInfo);
5757
out = StringBuffer();
5858
reporter = ReportFormatter([info], out)..write();
5959
});

pkg/linter/test/rule_test_support.dart

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:convert' show json;
66

77
import 'package:analyzer/dart/analysis/results.dart';
8+
import 'package:analyzer/diagnostic/diagnostic.dart';
89
import 'package:analyzer/error/error.dart';
910
import 'package:analyzer/error/listener.dart';
1011
import 'package:analyzer/file_system/file_system.dart';
@@ -41,7 +42,7 @@ ExpectedDiagnostic error(
4142
Pattern? messageContains,
4243
}) => _ExpectedError(code, offset, length, messageContains: messageContains);
4344

44-
typedef DiagnosticMatcher = bool Function(AnalysisError error);
45+
typedef DiagnosticMatcher = bool Function(Diagnostic diagnostic);
4546

4647
/// A description of a diagnostic that is expected to be reported.
4748
class ExpectedDiagnostic {
@@ -70,16 +71,17 @@ class ExpectedDiagnostic {
7071
}) : _messageContains = messageContains,
7172
_correctionContains = correctionContains;
7273

73-
/// Whether the [error] matches this description of what it's expected to be.
74-
bool matches(AnalysisError error) {
75-
if (!_diagnosticMatcher(error)) return false;
76-
if (error.offset != _offset) return false;
77-
if (error.length != _length) return false;
78-
if (_messageContains != null && !error.message.contains(_messageContains)) {
74+
/// Whether the [diagnostic] matches this description of what it's expected to be.
75+
bool matches(Diagnostic diagnostic) {
76+
if (!_diagnosticMatcher(diagnostic)) return false;
77+
if (diagnostic.offset != _offset) return false;
78+
if (diagnostic.length != _length) return false;
79+
if (_messageContains != null &&
80+
!diagnostic.message.contains(_messageContains)) {
7981
return false;
8082
}
8183
if (_correctionContains != null) {
82-
var correctionMessage = error.correctionMessage;
84+
var correctionMessage = diagnostic.correctionMessage;
8385
if (correctionMessage == null ||
8486
!correctionMessage.contains(_correctionContains)) {
8587
return false;
@@ -202,7 +204,7 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
202204
) async {
203205
addTestFile(content);
204206
await resolveTestFile();
205-
await _assertDiagnosticsIn(_errors, expectedDiagnostics);
207+
await _assertDiagnosticsIn(_diagnostics, expectedDiagnostics);
206208
}
207209

208210
/// Asserts that the number of diagnostics that have been gathered at [path]
@@ -215,7 +217,7 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
215217
List<ExpectedDiagnostic> expectedDiagnostics,
216218
) async {
217219
await _resolveFile(path);
218-
await _assertDiagnosticsIn(_errors, expectedDiagnostics);
220+
await _assertDiagnosticsIn(_diagnostics, expectedDiagnostics);
219221
}
220222

221223
/// Asserts that the diagnostics for each `path` match those in the paired
@@ -334,15 +336,15 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
334336
writePackageConfig(path, configCopy);
335337
}
336338

337-
/// Asserts that the diagnostics in [errors] match [expectedDiagnostics].
339+
/// Asserts that the diagnostics in [diagnostics] match [expectedDiagnostics].
338340
Future<void> _assertDiagnosticsIn(
339-
List<AnalysisError> errors,
341+
List<Diagnostic> diagnostics,
340342
List<ExpectedDiagnostic> expectedDiagnostics,
341343
) async {
342344
//
343345
// Match actual diagnostics to expected diagnostics.
344346
//
345-
var unmatchedActual = errors.toList();
347+
var unmatchedActual = diagnostics.toList();
346348
var unmatchedExpected = expectedDiagnostics.toList();
347349
var actualIndex = 0;
348350
while (actualIndex < unmatchedActual.length) {
@@ -414,10 +416,12 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
414416
}
415417
}
416418
if (buffer.isNotEmpty) {
417-
errors.sort((first, second) => first.offset.compareTo(second.offset));
419+
diagnostics.sort(
420+
(first, second) => first.offset.compareTo(second.offset),
421+
);
418422
buffer.writeln();
419423
buffer.writeln('To accept the current state, expect:');
420-
for (var actual in errors) {
424+
for (var actual in diagnostics) {
421425
late String diagnosticKind;
422426
Object? description;
423427
if (actual.errorCode is LintCode) {
@@ -461,7 +465,7 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
461465
}
462466
}
463467

464-
Future<List<AnalysisError>> _resolvePubspecFile(String content) async {
468+
Future<List<Diagnostic>> _resolvePubspecFile(String content) async {
465469
var path = convertPath(testPackagePubspecPath);
466470
var pubspecRules = <LintRule, PubspecVisitor<Object?>>{};
467471
for (var rule in Registry.ruleRegistry.where(
@@ -530,8 +534,8 @@ abstract class _ContextResolutionTest
530534

531535
List<String> get _collectionIncludedPaths;
532536

533-
/// The analysis errors that were computed during analysis.
534-
List<AnalysisError> get _errors =>
537+
/// The diagnostics that were computed during analysis.
538+
List<Diagnostic> get _diagnostics =>
535539
result.errors
536540
.whereNot((e) => ignoredErrorCodes.any((c) => e.errorCode == c))
537541
.toList();

pkg/linter/tool/benchmark.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ Future<void> writeBenchmarks(
214214
out.writeTimings(stats, 0);
215215
}
216216

217-
int _maxSeverity(List<AnalysisErrorInfo> infos) {
218-
var filteredErrors = infos.expand((i) => i.errors);
217+
int _maxSeverity(List<DiagnosticInfo> infos) {
218+
var filteredErrors = infos.expand((i) => i.diagnostics);
219219
return filteredErrors.fold(
220220
0,
221221
(value, e) => math.max(value, e.errorCode.errorSeverity.ordinal),

pkg/linter/tool/checks/driver.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'dart:io' as io;
66

77
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
88
import 'package:analyzer/dart/analysis/results.dart';
9-
import 'package:analyzer/error/error.dart';
9+
import 'package:analyzer/diagnostic/diagnostic.dart';
1010
import 'package:analyzer/file_system/file_system.dart';
1111
import 'package:analyzer/file_system/physical_file_system.dart';
1212
import 'package:analyzer/src/dart/analysis/analysis_options.dart';
@@ -32,7 +32,7 @@ Future<void> main() async {
3232

3333
var customChecks = [VisitRegisteredNodes(), NoSoloTests(), NoTrailingSpaces()];
3434

35-
Future<List<AnalysisError>> runChecks() async {
35+
Future<List<Diagnostic>> runChecks() async {
3636
var rules = path.normalize(
3737
io.File(path.join('lib', 'src', 'rules')).absolute.path,
3838
);
@@ -49,7 +49,7 @@ class Driver {
4949

5050
Driver(this.lints, {this.silent = true});
5151

52-
Future<List<AnalysisError>> analyze(List<String> sources) async {
52+
Future<List<Diagnostic>> analyze(List<String> sources) async {
5353
if (sources.isEmpty) {
5454
_print('Specify one or more files and directories.');
5555
return [];
@@ -60,7 +60,7 @@ class Driver {
6060
return failedChecks;
6161
}
6262

63-
Future<List<AnalysisError>> _analyzeFiles(
63+
Future<List<Diagnostic>> _analyzeFiles(
6464
ResourceProvider resourceProvider,
6565
List<String> analysisRoots,
6666
) async {
@@ -70,15 +70,15 @@ class Driver {
7070
lints.forEach(Registry.ruleRegistry.registerLintRule);
7171

7272
// Track failures.
73-
var failedChecks = <AnalysisError>{};
73+
var failedChecks = <Diagnostic>{};
7474

7575
for (var root in analysisRoots) {
7676
var collection = AnalysisContextCollection(
7777
includedPaths: [root],
7878
resourceProvider: resourceProvider,
7979
);
8080

81-
var errors = <AnalysisErrorInfo>[];
81+
var errors = <DiagnosticInfo>[];
8282

8383
for (var context in collection.contexts) {
8484
// Add lints.
@@ -100,7 +100,7 @@ class Driver {
100100
.where((e) => e.errorCode.name != 'TODO')
101101
.toList();
102102
if (filtered.isNotEmpty) {
103-
errors.add(AnalysisErrorInfo(filtered, result.lineInfo));
103+
errors.add(DiagnosticInfo(filtered, result.lineInfo));
104104
}
105105
}
106106
} on Exception catch (e) {
@@ -113,7 +113,7 @@ class Driver {
113113
ReportFormatter(errors, silent ? MockIOSink() : io.stdout).write();
114114

115115
for (var info in errors) {
116-
failedChecks.addAll(info.errors);
116+
failedChecks.addAll(info.diagnostics);
117117
}
118118
}
119119

0 commit comments

Comments
 (0)