Skip to content

Commit 9e86286

Browse files
srawlinsCommit Queue
authored andcommitted
Tidy up ErrorProcessor and AnalyzerConverter
Some of the parameters noted can be required and/or non-nullable as per our internal usage, but this is all public API, so I'm just leaving a note. Mainly just simplify the implementation of ErrorProcessor.getProcessor. Change-Id: Idebcf4a2485029c914d495cabfa3a1e98824c72c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396360 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 6ae5ede commit 9e86286

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

pkg/analyzer/lib/source/error_processor.dart

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import 'package:analyzer/error/error.dart';
66
import 'package:analyzer/src/generated/engine.dart';
77
import 'package:analyzer/src/task/options.dart';
8+
import 'package:collection/collection.dart';
9+
import 'package:meta/meta.dart';
810
import 'package:yaml/yaml.dart';
911

1012
/// String identifiers mapped to associated severities.
@@ -77,31 +79,23 @@ class ErrorProcessor {
7779
///
7880
/// Note: [code] is normalized to uppercase; `errorCode.name` for regular
7981
/// analysis issues uses uppercase; `errorCode.name` for lints uses lowercase.
82+
@visibleForTesting
8083
bool appliesTo(AnalysisError error) =>
8184
code == error.errorCode.name ||
8285
code == error.errorCode.name.toUpperCase();
8386

8487
@override
8588
String toString() => "ErrorProcessor[code='$code', severity=$severity]";
8689

87-
/// Return an error processor associated in the [analysisOptions] for the
90+
/// Returns an error processor associated in the [analysisOptions] for the
8891
/// given [error], or `null` if none is found.
8992
static ErrorProcessor? getProcessor(
90-
AnalysisOptions? analysisOptions, AnalysisError error) {
91-
if (analysisOptions == null) {
92-
return null;
93-
}
94-
95-
// Let the user configure how specific errors are processed.
96-
List<ErrorProcessor> processors = analysisOptions.errorProcessors;
97-
98-
// Add the strong mode processor.
99-
processors = processors.toList();
100-
for (var processor in processors) {
101-
if (processor.appliesTo(error)) {
102-
return processor;
103-
}
104-
}
105-
return null;
93+
// TODO(srawlins): Make `analysisOptions` non-nullable, in a breaking
94+
// change release.
95+
AnalysisOptions? analysisOptions,
96+
AnalysisError error,
97+
) {
98+
return analysisOptions?.errorProcessors
99+
.firstWhereOrNull((processor) => processor.appliesTo(error));
106100
}
107101
}

pkg/analyzer_plugin/lib/utilities/analyzer_converter.dart

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ import 'package:analyzer_plugin/protocol/protocol_common.dart' as plugin;
1717
///
1818
/// Clients may not extend, implement or mix-in this class.
1919
class AnalyzerConverter {
20-
/// Convert the analysis [error] from the 'analyzer' package to an analysis
21-
/// error defined by the plugin API. If a [lineInfo] is provided then the
22-
/// error's location will have a start line and start column. If a [severity]
23-
/// is provided, then it will override the severity defined by the error.
24-
plugin.AnalysisError convertAnalysisError(analyzer.AnalysisError error,
25-
{analyzer.LineInfo? lineInfo, analyzer.ErrorSeverity? severity}) {
20+
/// Converts the analysis [error] from the 'analyzer' package to an analysis
21+
/// error defined by the plugin API.
22+
///
23+
/// If a [lineInfo] is provided then the error's location will have a start
24+
/// line and start column. If a [severity] is provided, then it will override
25+
/// the severity defined by the error.
26+
plugin.AnalysisError convertAnalysisError(
27+
analyzer.AnalysisError error, {
28+
analyzer.LineInfo? lineInfo,
29+
analyzer.ErrorSeverity? severity,
30+
}) {
2631
var errorCode = error.errorCode;
2732
severity ??= errorCode.errorSeverity;
2833
var offset = error.offset;
@@ -58,15 +63,19 @@ class AnalyzerConverter {
5863
hasFix: true);
5964
}
6065

61-
/// Convert the list of analysis [errors] from the 'analyzer' package to a
62-
/// list of analysis errors defined by the plugin API. If a [lineInfo] is
63-
/// provided then the resulting errors locations will have a start line and
64-
/// start column. If an analysis [options] is provided then the severities of
65-
/// the errors will be altered based on those options.
66+
/// Converts the list of analysis [errors] from the 'analyzer' package to a
67+
/// list of analysis errors defined by the plugin API.
68+
///
69+
/// The severities of the errors are altered based on [options].
6670
List<plugin.AnalysisError> convertAnalysisErrors(
67-
List<analyzer.AnalysisError> errors,
68-
{analyzer.LineInfo? lineInfo,
69-
analyzer.AnalysisOptions? options}) {
71+
List<analyzer.AnalysisError> errors, {
72+
// TODO(srawlins): Make `lineInfo` required and non-nullable, in a breaking
73+
// change release.
74+
analyzer.LineInfo? lineInfo,
75+
// TODO(srawlins): Make `options` required and non-nullable, in a breaking
76+
// change release.
77+
analyzer.AnalysisOptions? options,
78+
}) {
7079
var serverErrors = <plugin.AnalysisError>[];
7180
for (var error in errors) {
7281
var processor = analyzer.ErrorProcessor.getProcessor(options, error);

pkg/analyzer_plugin/test/utilities/analyzer_converter_test.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,11 @@ class AnalyzerConverterTest extends AbstractSingleUnitTest {
141141
];
142142
var lineInfo = analyzer.LineInfo([0, 10, 20]);
143143

144-
var pluginErrors =
145-
converter.convertAnalysisErrors(analyzerErrors, lineInfo: lineInfo);
144+
var pluginErrors = converter.convertAnalysisErrors(
145+
analyzerErrors,
146+
lineInfo: lineInfo,
147+
options: analyzer.AnalysisOptionsImpl(),
148+
);
146149
expect(pluginErrors, hasLength(analyzerErrors.length));
147150
assertError(pluginErrors[0], analyzerErrors[0],
148151
startColumn: 4, startLine: 2);
@@ -177,7 +180,10 @@ class AnalyzerConverterTest extends AbstractSingleUnitTest {
177180
await createError(25),
178181
];
179182

180-
var pluginErrors = converter.convertAnalysisErrors(analyzerErrors);
183+
var pluginErrors = converter.convertAnalysisErrors(
184+
analyzerErrors,
185+
options: analyzer.AnalysisOptionsImpl(),
186+
);
181187
expect(pluginErrors, hasLength(analyzerErrors.length));
182188
assertError(pluginErrors[0], analyzerErrors[0]);
183189
assertError(pluginErrors[1], analyzerErrors[1]);

0 commit comments

Comments
 (0)