Skip to content

Commit c92417b

Browse files
srawlinsCommit Queue
authored andcommitted
linter: Remove unnecessary DiagnosticInfo class
Change-Id: Iac32c119288d2aab9549f01880d11c9e237ba9c3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/444782 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 375f03a commit c92417b

File tree

8 files changed

+69
-127
lines changed

8 files changed

+69
-127
lines changed

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

pkg/linter/test/formatter_test.dart

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/diagnostic/diagnostic.dart';
6-
import 'package:analyzer/source/line_info.dart';
7-
import 'package:linter/src/test_utilities/analysis_error_info.dart';
6+
import 'package:analyzer/file_system/physical_file_system.dart';
7+
import 'package:analyzer/source/file_source.dart';
88
import 'package:test/test.dart';
99
import 'package:test_descriptor/test_descriptor.dart' as d;
1010

@@ -16,73 +16,62 @@ void main() {
1616
}
1717

1818
void defineTests() {
19-
group('formatter', () {
20-
test('pluralize', () {
21-
expect(pluralize('issue', 0), '0 issues');
22-
expect(pluralize('issue', 1), '1 issue');
23-
expect(pluralize('issue', 2), '2 issues');
24-
});
25-
26-
group('reporter', () {
27-
late DiagnosticInfo info;
28-
late StringBuffer out;
29-
late String sourcePath;
30-
late ReportFormatter reporter;
31-
32-
setUp(() async {
33-
var lineInfo = LineInfo([3, 6, 9]);
34-
35-
var type = MockDiagnosticType()..displayName = 'test';
36-
37-
var code = TestDiagnosticCode('mock_code', 'MSG')..type = type;
38-
39-
await d.dir('project', [
40-
d.file('foo.dart', '''
19+
group(ReportFormatter, () {
20+
late Diagnostic diagnostic;
21+
late StringBuffer out;
22+
late String sourcePath;
23+
late ReportFormatter reporter;
24+
25+
setUp(() async {
26+
var type = MockDiagnosticType()..displayName = 'test';
27+
var code = TestDiagnosticCode('mock_code', 'MSG')..type = type;
28+
29+
await d.dir('project', [
30+
d.file('foo.dart', '''
4131
var x = 11;
4232
var y = 22;
4333
var z = 33;
4434
'''),
45-
]).create();
46-
sourcePath = '${d.sandbox}/project/foo.dart';
47-
var source = MockSource(sourcePath);
48-
49-
var error = Diagnostic.tmp(
50-
source: source,
51-
offset: 10,
52-
length: 3,
53-
diagnosticCode: code,
54-
);
55-
56-
info = DiagnosticInfo([error], lineInfo);
57-
out = StringBuffer();
58-
reporter = ReportFormatter([info], out)..write();
59-
});
35+
]).create();
36+
sourcePath = '${d.sandbox}/project/foo.dart';
37+
var file = PhysicalResourceProvider.INSTANCE.getFile(sourcePath);
38+
var source = FileSource(file);
39+
40+
diagnostic = Diagnostic.tmp(
41+
source: source,
42+
offset: 25,
43+
length: 3,
44+
diagnosticCode: code,
45+
);
46+
47+
out = StringBuffer();
48+
reporter = ReportFormatter([diagnostic], out)..write();
49+
});
6050

61-
test('count', () {
62-
expect(reporter.errorCount, 1);
63-
});
51+
test('count', () {
52+
expect(reporter.diagnosticCount, 1);
53+
});
6454

65-
test('write', () {
66-
expect(out.toString().trim(), '''$sourcePath 3:2 [test] MSG
55+
test('write', () {
56+
expect(out.toString().trim(), '''$sourcePath 3:2 [test] MSG
6757
var z = 33;
6858
^^^
6959
7060
files analyzed, 1 issue found.''');
71-
});
61+
});
7262

73-
test('stats', () {
74-
out.clear();
75-
ReportFormatter([info], out).write();
76-
expect(
77-
out.toString(),
78-
startsWith('''$sourcePath 3:2 [test] MSG
63+
test('stats', () {
64+
out.clear();
65+
ReportFormatter([diagnostic], out).write();
66+
expect(
67+
out.toString(),
68+
startsWith('''$sourcePath 3:2 [test] MSG
7969
var z = 33;
8070
^^^
8171
8272
files analyzed, 1 issue found.
8373
'''),
84-
);
85-
});
74+
);
8675
});
8776
});
8877
}

pkg/linter/test/mocks.dart

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import 'dart:convert';
66
import 'dart:io';
77

88
import 'package:analyzer/error/error.dart';
9-
import 'package:analyzer/src/generated/engine.dart';
10-
import 'package:analyzer/src/generated/source.dart';
119

1210
class MockDiagnosticType implements DiagnosticType {
1311
@override
@@ -64,22 +62,6 @@ class MockIOSink implements IOSink {
6462
void writeln([Object? obj = '']) {}
6563
}
6664

67-
class MockSource extends BasicSource {
68-
@override
69-
final String fullName;
70-
71-
MockSource(this.fullName) : super(Uri.file(fullName));
72-
73-
@override
74-
TimestampedData<String> get contents => TimestampedData<String>(0, '');
75-
76-
@override
77-
bool exists() => false;
78-
79-
@override
80-
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
81-
}
82-
8365
class TestDiagnosticCode extends DiagnosticCode {
8466
@override
8567
late DiagnosticSeverity severity;

pkg/linter/tool/benchmark.dart

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

99
import 'package:analyzer/analysis_rule/analysis_rule.dart';
10+
import 'package:analyzer/diagnostic/diagnostic.dart';
1011
import 'package:analyzer/src/lint/analysis_rule_timers.dart';
1112
import 'package:analyzer/src/lint/config.dart';
1213
import 'package:analyzer/src/lint/io.dart';
@@ -15,7 +16,6 @@ import 'package:analyzer/src/util/file_paths.dart' as file_paths;
1516
import 'package:args/args.dart';
1617
import 'package:linter/src/extensions.dart';
1718
import 'package:linter/src/rules.dart';
18-
import 'package:linter/src/test_utilities/analysis_error_info.dart';
1919
import 'package:path/path.dart' as path;
2020
import 'package:yaml/yaml.dart';
2121

@@ -212,13 +212,10 @@ Future<void> writeBenchmarks(
212212
out.writeTimings(stats, 0);
213213
}
214214

215-
int _maxSeverity(List<DiagnosticInfo> infos) {
216-
var filteredErrors = infos.expand((i) => i.diagnostics);
217-
return filteredErrors.fold(
218-
0,
219-
(value, e) => math.max(value, e.diagnosticCode.severity.ordinal),
220-
);
221-
}
215+
int _maxSeverity(List<Diagnostic> diagnostics) => diagnostics.fold(
216+
0,
217+
(value, e) => math.max(value, e.diagnosticCode.severity.ordinal),
218+
);
222219

223220
class Stat implements Comparable<Stat> {
224221
final String name;

pkg/linter/tool/checks/driver.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
1414
import 'package:analyzer/src/lint/registry.dart';
1515
import 'package:cli_util/cli_logging.dart';
1616
import 'package:linter/src/analyzer.dart';
17-
import 'package:linter/src/test_utilities/analysis_error_info.dart';
1817
import 'package:path/path.dart' as path;
1918

2019
import '../../test/mocks.dart';
@@ -78,7 +77,7 @@ class Driver {
7877
resourceProvider: resourceProvider,
7978
);
8079

81-
var errors = <DiagnosticInfo>[];
80+
var diagnostics = <Diagnostic>[];
8281

8382
for (var context in collection.contexts) {
8483
// Add lints.
@@ -95,7 +94,7 @@ class Driver {
9594
try {
9695
var result = await context.currentSession.getErrors(filePath);
9796
if (result is ErrorsResult) {
98-
errors.add(DiagnosticInfo(result.diagnostics, result.lineInfo));
97+
diagnostics.addAll(result.diagnostics);
9998
}
10099
} on Exception catch (e) {
101100
_print('Exception caught analyzing: $filePath');
@@ -104,11 +103,9 @@ class Driver {
104103
}
105104
}
106105
}
107-
ReportFormatter(errors, silent ? MockIOSink() : io.stdout).write();
106+
ReportFormatter(diagnostics, silent ? MockIOSink() : io.stdout).write();
108107

109-
for (var info in errors) {
110-
failedChecks.addAll(info.diagnostics);
111-
}
108+
failedChecks.addAll(diagnostics);
112109
}
113110

114111
// Unregister our checks.

pkg/linter/tool/lint_driver.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import 'dart:io' as io;
66

77
import 'package:analyzer/dart/analysis/results.dart';
8+
import 'package:analyzer/diagnostic/diagnostic.dart';
89
import 'package:analyzer/file_system/file_system.dart';
910
import 'package:analyzer/file_system/physical_file_system.dart' as file_system;
1011
import 'package:analyzer/instrumentation/instrumentation.dart';
1112
import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart';
1213
import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine;
1314
import 'package:analyzer/src/lint/io.dart';
14-
import 'package:linter/src/test_utilities/analysis_error_info.dart';
1515

1616
import 'linter_options.dart';
1717

@@ -27,7 +27,7 @@ class LintDriver {
2727
ResourceProvider get _resourceProvider =>
2828
file_system.PhysicalResourceProvider.INSTANCE;
2929

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

3333
var filesPaths =
@@ -49,14 +49,12 @@ class LintDriver {
4949

5050
_filesAnalyzed.addAll(filesPaths);
5151

52-
var result = <DiagnosticInfo>[];
52+
var result = <Diagnostic>[];
5353
for (var path in _filesAnalyzed) {
5454
var analysisSession = contextCollection.contextFor(path).currentSession;
5555
var errorsResult = await analysisSession.getErrors(path);
5656
if (errorsResult is ErrorsResult) {
57-
result.add(
58-
DiagnosticInfo(errorsResult.diagnostics, errorsResult.lineInfo),
59-
);
57+
result.addAll(errorsResult.diagnostics);
6058
}
6159
}
6260
return result;

pkg/linter/tool/test_linter.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import 'package:analyzer/source/file_source.dart';
1111
import 'package:analyzer/source/source.dart';
1212
import 'package:analyzer/src/lint/pub.dart';
1313
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
14-
import 'package:linter/src/test_utilities/analysis_error_info.dart';
1514
import 'package:path/path.dart' as path;
1615

1716
import 'lint_driver.dart';
@@ -32,7 +31,7 @@ class TestLinter implements DiagnosticListener {
3231
path.Context get _pathContext =>
3332
file_system.PhysicalResourceProvider.INSTANCE.pathContext;
3433

35-
Future<List<DiagnosticInfo>> lintFiles(List<File> files) async {
34+
Future<List<Diagnostic>> lintFiles(List<File> files) async {
3635
var lintDriver = LintDriver(options);
3736
var errors = await lintDriver.analyze(
3837
files.where((f) => f.path.endsWith('.dart')),

pkg/linter/tool/util/formatter.dart

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import 'dart:math';
77

88
import 'package:analyzer/diagnostic/diagnostic.dart';
99
import 'package:analyzer/source/line_info.dart';
10-
import 'package:linter/src/test_utilities/analysis_error_info.dart';
11-
12-
String pluralize(String word, int count) =>
13-
"$count ${count == 1 ? word : '${word}s'}";
10+
import 'package:analyzer/src/utilities/extensions/string.dart';
1411

1512
String _getLineContents(int lineNumber, Diagnostic diagnostic) {
1613
var path = diagnostic.source.fullName;
@@ -32,11 +29,11 @@ String _getLineContents(int lineNumber, Diagnostic diagnostic) {
3229

3330
class ReportFormatter {
3431
final StringSink out;
35-
final Iterable<DiagnosticInfo> errors;
32+
final Iterable<Diagnostic> diagnostics;
3633

37-
int errorCount = 0;
34+
int diagnosticCount = 0;
3835

39-
ReportFormatter(this.errors, this.out);
36+
ReportFormatter(this.diagnostics, this.out);
4037

4138
/// Override to influence diagnostic sorting.
4239
int compare(Diagnostic diagnostic1, Diagnostic diagnostic2) {
@@ -88,8 +85,9 @@ class ReportFormatter {
8885
out.writeln(result);
8986
}
9087

91-
void _writeLint(Diagnostic diagnostic, LineInfo lineInfo) {
88+
void _writeLint(Diagnostic diagnostic) {
9289
var offset = diagnostic.offset;
90+
var lineInfo = LineInfo.fromContent(diagnostic.source.contents.data);
9391
var location = lineInfo.getLocation(offset);
9492
var line = location.lineNumber;
9593
var column = location.columnNumber;
@@ -98,17 +96,18 @@ class ReportFormatter {
9896
}
9997

10098
void _writeLints() {
101-
for (var info in errors) {
102-
for (var e in (info.diagnostics.toList()..sort(compare))) {
103-
++errorCount;
104-
_writeLint(e, info.lineInfo);
105-
}
99+
for (var e in (diagnostics.toList()..sort(compare))) {
100+
++diagnosticCount;
101+
_writeLint(e);
106102
}
103+
107104
out.writeln();
108105
}
109106

110107
void _writeSummary() {
111-
var summary = 'files analyzed, ${pluralize("issue", errorCount)} found.';
108+
var summary =
109+
'files analyzed, '
110+
'$diagnosticCount ${"issue".pluralized(diagnosticCount)} found.';
112111
out.writeln(summary);
113112
}
114113
}

0 commit comments

Comments
 (0)