Skip to content

Commit 020427a

Browse files
srawlinsCommit Queue
authored andcommitted
Remove most use cases of LintFilter.
The only time a `filter` was passed to LinterOptions was in `benchmark.dart` and in tests of the filter option. But then `benchmark.dart` just grabs it back out, so we can remove it from LinterOptions. It can then be removed from all of the reporters (DetailedReporter, ReportFormatter, SimpleFormatter). Remove unused `quiet` option from these reporters as well. I suspect more can be removed from the reporters (again they are only used in tests; no uses in the wild) but I'll save those for later. Change-Id: I784666fb9c9cb42760fabec346aa5135af15e6fb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/389403 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 434d6bc commit 020427a

File tree

7 files changed

+59
-106
lines changed

7 files changed

+59
-106
lines changed

pkg/analyzer/lib/src/lint/linter.dart

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,13 @@ final class LinterContextWithResolvedResults implements LinterContext {
187187
class LinterOptions extends DriverOptions {
188188
final Iterable<LintRule> enabledRules;
189189
final String? analysisOptions;
190-
LintFilter? filter;
191190

192191
LinterOptions({
193192
Iterable<LintRule>? enabledRules,
194193
this.analysisOptions,
195-
this.filter,
196194
}) : enabledRules = enabledRules ?? Registry.ruleRegistry;
197195
}
198196

199-
/// Filtered lints are omitted from linter output.
200-
abstract class LintFilter {
201-
bool filter(AnalysisError lint);
202-
}
203-
204197
/// Describes a lint rule.
205198
abstract class LintRule {
206199
/// Used to report lint warnings.

pkg/linter/lib/src/analyzer.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export 'package:analyzer/src/lint/linter.dart'
2222
dart2_12,
2323
dart3,
2424
dart3_3,
25-
LintFilter,
2625
LintRule,
2726
LinterContext,
2827
LinterOptions,

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

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ String _escapePipe(String input) {
5252
}
5353

5454
class DetailedReporter extends SimpleFormatter {
55-
DetailedReporter(super.errors, super.filter, super.out,
56-
{super.fileCount,
57-
super.elapsedMs,
58-
super.fileRoot,
59-
super.showStatistics,
60-
super.machineOutput,
61-
super.quiet});
55+
DetailedReporter(
56+
super.errors,
57+
super.out, {
58+
super.fileCount,
59+
super.elapsedMs,
60+
super.fileRoot,
61+
super.showStatistics,
62+
super.machineOutput,
63+
});
6264

6365
@override
6466
void writeLint(AnalysisError error, {int? offset, int? line, int? column}) {
@@ -79,20 +81,23 @@ class DetailedReporter extends SimpleFormatter {
7981

8082
abstract class ReportFormatter {
8183
factory ReportFormatter(
82-
Iterable<AnalysisErrorInfo> errors, LintFilter? filter, IOSink out,
83-
{int? fileCount,
84-
int? elapsedMs,
85-
String? fileRoot,
86-
bool showStatistics = false,
87-
bool machineOutput = false,
88-
bool quiet = false}) =>
89-
DetailedReporter(errors, filter, out,
90-
fileCount: fileCount,
91-
fileRoot: fileRoot,
92-
elapsedMs: elapsedMs,
93-
showStatistics: showStatistics,
94-
machineOutput: machineOutput,
95-
quiet: quiet);
84+
Iterable<AnalysisErrorInfo> errors,
85+
IOSink out, {
86+
int? fileCount,
87+
int? elapsedMs,
88+
String? fileRoot,
89+
bool showStatistics = false,
90+
bool machineOutput = false,
91+
}) =>
92+
DetailedReporter(
93+
errors,
94+
out,
95+
fileCount: fileCount,
96+
fileRoot: fileRoot,
97+
elapsedMs: elapsedMs,
98+
showStatistics: showStatistics,
99+
machineOutput: machineOutput,
100+
);
96101

97102
void write();
98103
}
@@ -101,29 +106,25 @@ abstract class ReportFormatter {
101106
class SimpleFormatter implements ReportFormatter {
102107
final IOSink out;
103108
final Iterable<AnalysisErrorInfo> errors;
104-
final LintFilter? filter;
105109

106110
int errorCount = 0;
107-
int filteredLintCount = 0;
108111

109112
final int? fileCount;
110113
final int? elapsedMs;
111114
final String? fileRoot;
112115
final bool showStatistics;
113116
final bool machineOutput;
114-
final bool quiet;
115117

116118
/// Cached for the purposes of statistics report formatting.
117119
int _summaryLength = 0;
118120

119121
Map<String, int> stats = <String, int>{};
120122

121-
SimpleFormatter(this.errors, this.filter, this.out,
123+
SimpleFormatter(this.errors, this.out,
122124
{this.fileCount,
123125
this.fileRoot,
124126
this.elapsedMs,
125127
this.showStatistics = false,
126-
this.quiet = false,
127128
this.machineOutput = false});
128129

129130
/// Override to influence error sorting
@@ -204,23 +205,15 @@ class SimpleFormatter implements ReportFormatter {
204205
}
205206

206207
void writeLints() {
207-
var filter = this.filter;
208208
for (var info in errors) {
209209
for (var e in (info.errors.toList()..sort(compare))) {
210-
if (filter != null && filter.filter(e)) {
211-
filteredLintCount++;
212-
} else {
213-
++errorCount;
214-
if (!quiet) {
215-
_writeLint(e, info.lineInfo);
216-
}
217-
_recordStats(e);
218-
}
210+
++errorCount;
211+
_writeLint(e, info.lineInfo);
212+
213+
_recordStats(e);
219214
}
220215
}
221-
if (!quiet) {
222-
out.writeln();
223-
}
216+
out.writeln();
224217
}
225218

226219
void writeStatistics() {
@@ -230,8 +223,7 @@ class SimpleFormatter implements ReportFormatter {
230223

231224
void writeSummary() {
232225
var summary = '${pluralize("file", fileCount)} analyzed, '
233-
'${pluralize("issue", errorCount)} found'
234-
"${filteredLintCount == 0 ? '' : ' ($filteredLintCount filtered)'}, in $elapsedMs ms.";
226+
'${pluralize("issue", errorCount)} found, in $elapsedMs ms.';
235227
out.writeln(summary);
236228
// Cache for output table sizing
237229
_summaryLength = summary.length;

pkg/linter/test/formatter_test.dart

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ void defineTests() {
4242

4343
var out = CollectingSink();
4444

45-
var reporter =
46-
SimpleFormatter([info], null, out, fileCount: 1, elapsedMs: 13)
47-
..write();
45+
var reporter = SimpleFormatter([info], out, fileCount: 1, elapsedMs: 13)
46+
..write();
4847

4948
test('count', () {
5049
expect(reporter.errorCount, 1);
@@ -58,7 +57,7 @@ void defineTests() {
5857

5958
test('stats', () {
6059
out.buffer.clear();
61-
SimpleFormatter([info], null, out,
60+
SimpleFormatter([info], out,
6261
fileCount: 1, showStatistics: true, elapsedMs: 13)
6362
.write();
6463
expect(out.buffer.toString(),
@@ -93,29 +92,10 @@ mock_code 1
9392

9493
var out = CollectingSink();
9594

96-
group('filtered', () {
97-
var reporter = SimpleFormatter([info], _RejectingFilter(), out,
98-
fileCount: 1, elapsedMs: 13)
99-
..write();
100-
101-
test('error count', () {
102-
expect(reporter.errorCount, 0);
103-
});
104-
105-
test('filter count', () {
106-
expect(reporter.filteredLintCount, 1);
107-
});
108-
109-
test('write', () {
110-
expect(out.buffer.toString().trim(),
111-
'1 file analyzed, 0 issues found (1 filtered), in 13 ms.');
112-
});
113-
});
114-
11595
group('machine-output', () {
11696
test('write', () {
11797
out.buffer.clear();
118-
SimpleFormatter([info], null, out,
98+
SimpleFormatter([info], out,
11999
fileCount: 1, machineOutput: true, elapsedMs: 13)
120100
.write();
121101

@@ -128,8 +108,3 @@ mock_code 1
128108
});
129109
});
130110
}
131-
132-
class _RejectingFilter extends LintFilter {
133-
@override
134-
bool filter(AnalysisError lint) => true;
135-
}

pkg/linter/test/rule_test.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,8 @@ void _validateExpectedLints(File file, Iterable<AnalysisErrorInfo> errorInfos,
230230
}
231231
}
232232

233-
/// A [LintFilter] that filters no lint.
234-
class NoFilter implements LintFilter {
235-
@override
236-
bool filter(AnalysisError lint) => false;
237-
}
238-
239233
/// A [DetailedReporter] that filters no lint, only used in debug mode, when
240234
/// actual lints do not match expectations.
241235
class ResultReporter extends DetailedReporter {
242-
ResultReporter(Iterable<AnalysisErrorInfo> errors)
243-
: super(errors, NoFilter(), stdout);
236+
ResultReporter(Iterable<AnalysisErrorInfo> errors) : super(errors, stdout);
244237
}

pkg/linter/tool/benchmark.dart

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Future<void> main(List<String> args) async {
2929
const unableToProcessExitCode = 64;
3030

3131
Future<Iterable<AnalysisErrorInfo>> lintFiles(
32-
TestLinter linter, List<File> filesToLint) async {
32+
TestLinter linter, List<File> filesToLint, LintFilter filter) async {
3333
// Setup an error watcher to track whether an error was logged to stderr so
3434
// we can set the exit code accordingly.
3535
var errorWatcher = _ErrorWatchingSink(errorSink);
@@ -38,7 +38,7 @@ Future<Iterable<AnalysisErrorInfo>> lintFiles(
3838
if (errorWatcher.encounteredError) {
3939
exitCode = loggedAnalyzerErrorExitCode;
4040
} else if (errors.isNotEmpty) {
41-
exitCode = _maxSeverity(errors, linter.options.filter);
41+
exitCode = _maxSeverity(errors, filter);
4242
}
4343

4444
return errors;
@@ -95,12 +95,13 @@ Future<void> runLinter(List<String> args) async {
9595
var ruleNames = options['rules'];
9696

9797
LinterOptions linterOptions;
98+
LintFilter? filter;
9899
if (configFile is String) {
99100
var config = LintConfig.parse(readFile(configFile));
100101
var enabledRules = Registry.ruleRegistry.where((LintRule rule) =>
101102
!config.ruleConfigs.any((rc) => rc.disables(rule.name)));
102-
var filter = _FileGlobFilter(config.fileIncludes, config.fileExcludes);
103-
linterOptions = LinterOptions(enabledRules: enabledRules, filter: filter);
103+
filter = _FileGlobFilter(config.fileIncludes, config.fileExcludes);
104+
linterOptions = LinterOptions(enabledRules: enabledRules);
104105
} else if (ruleNames is Iterable<String> && ruleNames.isNotEmpty) {
105106
var rules = <LintRule>[];
106107
for (var ruleName in ruleNames) {
@@ -115,6 +116,7 @@ Future<void> runLinter(List<String> args) async {
115116
} else {
116117
linterOptions = LinterOptions();
117118
}
119+
filter ??= _FileGlobFilter([], []);
118120

119121
var customSdk = options['dart-sdk'];
120122
if (customSdk is String) {
@@ -130,14 +132,14 @@ Future<void> runLinter(List<String> args) async {
130132
.map(File.new),
131133
];
132134

133-
await writeBenchmarks(outSink, filesToLint, linterOptions);
135+
await writeBenchmarks(outSink, filesToLint, linterOptions, filter);
134136
}
135137

136-
Future<void> writeBenchmarks(
137-
IOSink out, List<File> filesToLint, LinterOptions linterOptions) async {
138+
Future<void> writeBenchmarks(IOSink out, List<File> filesToLint,
139+
LinterOptions linterOptions, LintFilter filter) async {
138140
var timings = <String, int>{};
139141
for (var i = 0; i < benchmarkRuns; ++i) {
140-
await lintFiles(TestLinter(linterOptions), filesToLint);
142+
await lintFiles(TestLinter(linterOptions), filesToLint, filter);
141143
lintRuleTimers.timers.forEach((n, t) {
142144
var timing = t.elapsedMilliseconds;
143145
var previous = timings[n];
@@ -167,18 +169,19 @@ Future<void> writeBenchmarks(
167169
out.writeTimings(stats, 0);
168170
}
169171

170-
Iterable<AnalysisError> _filtered(
171-
Iterable<AnalysisError> errors, LintFilter? filter) =>
172-
(filter == null)
173-
? errors
174-
: errors.where((AnalysisError e) => !filter.filter(e));
175-
176-
int _maxSeverity(List<AnalysisErrorInfo> infos, LintFilter? filter) {
177-
var allErrors = _filtered(infos.expand((i) => i.errors), filter);
178-
return allErrors.fold(
172+
int _maxSeverity(List<AnalysisErrorInfo> infos, LintFilter filter) {
173+
var filteredErrors = infos
174+
.expand((i) => i.errors)
175+
.where((AnalysisError e) => !filter.filter(e));
176+
return filteredErrors.fold(
179177
0, (value, e) => math.max(value, e.errorCode.errorSeverity.ordinal));
180178
}
181179

180+
/// Filtered lints are omitted from linter output.
181+
abstract class LintFilter {
182+
bool filter(AnalysisError lint);
183+
}
184+
182185
class _ErrorWatchingSink implements IOSink {
183186
bool encounteredError = false;
184187

pkg/linter/tool/checks/driver.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ class Driver {
113113
}
114114
}
115115
}
116-
ReportFormatter(
117-
errors, null /*_TodoFilter()*/, silent ? MockIOSink() : io.stdout)
118-
.write();
116+
ReportFormatter(errors, silent ? MockIOSink() : io.stdout).write();
119117

120118
for (var info in errors) {
121119
failedChecks.addAll(info.errors);

0 commit comments

Comments
 (0)